Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: http://codemirror.net/LICENSE
3
4
(function(mod) {
5
if (typeof exports == "object" && typeof module == "object") // CommonJS
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd) // AMD
8
define(["../../lib/codemirror"], mod);
9
else // Plain browser env
10
mod(CodeMirror);
11
})(function(CodeMirror) {
12
"use strict";
13
14
function Bar(cls, orientation, scroll) {
15
this.orientation = orientation;
16
this.scroll = scroll;
17
this.screen = this.total = this.size = 1;
18
this.pos = 0;
19
20
this.node = document.createElement("div");
21
this.node.className = cls + "-" + orientation;
22
this.inner = this.node.appendChild(document.createElement("div"));
23
24
var self = this;
25
CodeMirror.on(this.inner, "mousedown", function(e) {
26
if (e.which != 1) return;
27
CodeMirror.e_preventDefault(e);
28
var axis = self.orientation == "horizontal" ? "pageX" : "pageY";
29
var start = e[axis], startpos = self.pos;
30
function move(e) {
31
if (e.which != 1) {
32
CodeMirror.off(document, "mousemove", move);
33
return;
34
}
35
self.moveTo(startpos + (e[axis] - start) * (self.total / self.size));
36
}
37
CodeMirror.on(document, "mousemove", move);
38
});
39
40
CodeMirror.on(this.node, "click", function(e) {
41
CodeMirror.e_preventDefault(e);
42
var innerBox = self.inner.getBoundingClientRect(), where;
43
if (self.orientation == "horizontal")
44
where = e.clientX < innerBox.left ? -1 : e.clientX > innerBox.right ? 1 : 0;
45
else
46
where = e.clientY < innerBox.top ? -1 : e.clientY > innerBox.bottom ? 1 : 0;
47
self.moveTo(self.pos + where * self.screen);
48
});
49
50
function onWheel(e) {
51
var moved = CodeMirror.wheelEventPixels(e)[self.orientation == "horizontal" ? "x" : "y"];
52
var oldPos = self.pos;
53
self.moveTo(self.pos + moved);
54
if (self.pos != oldPos) CodeMirror.e_preventDefault(e);
55
}
56
CodeMirror.on(this.node, "mousewheel", onWheel);
57
CodeMirror.on(this.node, "DOMMouseScroll", onWheel);
58
}
59
60
Bar.prototype.moveTo = function(pos, update) {
61
if (pos < 0) pos = 0;
62
if (pos > this.total - this.screen) pos = this.total - this.screen;
63
if (pos == this.pos) return;
64
this.pos = pos;
65
this.inner.style[this.orientation == "horizontal" ? "left" : "top"] =
66
(pos * (this.size / this.total)) + "px";
67
if (update !== false) this.scroll(pos, this.orientation);
68
};
69
70
Bar.prototype.update = function(scrollSize, clientSize, barSize) {
71
this.screen = clientSize;
72
this.total = scrollSize;
73
this.size = barSize;
74
75
// FIXME clip to min size?
76
this.inner.style[this.orientation == "horizontal" ? "width" : "height"] =
77
this.screen * (this.size / this.total) + "px";
78
this.inner.style[this.orientation == "horizontal" ? "left" : "top"] =
79
this.pos * (this.size / this.total) + "px";
80
};
81
82
function SimpleScrollbars(cls, place, scroll) {
83
this.addClass = cls;
84
this.horiz = new Bar(cls, "horizontal", scroll);
85
place(this.horiz.node);
86
this.vert = new Bar(cls, "vertical", scroll);
87
place(this.vert.node);
88
this.width = null;
89
}
90
91
SimpleScrollbars.prototype.update = function(measure) {
92
if (this.width == null) {
93
var style = window.getComputedStyle ? window.getComputedStyle(this.horiz.node) : this.horiz.node.currentStyle;
94
if (style) this.width = parseInt(style.height);
95
}
96
var width = this.width || 0;
97
98
var needsH = measure.scrollWidth > measure.clientWidth + 1;
99
var needsV = measure.scrollHeight > measure.clientHeight + 1;
100
this.vert.node.style.display = needsV ? "block" : "none";
101
this.horiz.node.style.display = needsH ? "block" : "none";
102
103
if (needsV) {
104
this.vert.update(measure.scrollHeight, measure.clientHeight,
105
measure.viewHeight - (needsH ? width : 0));
106
this.vert.node.style.display = "block";
107
this.vert.node.style.bottom = needsH ? width + "px" : "0";
108
}
109
if (needsH) {
110
this.horiz.update(measure.scrollWidth, measure.clientWidth,
111
measure.viewWidth - (needsV ? width : 0) - measure.barLeft);
112
this.horiz.node.style.right = needsV ? width + "px" : "0";
113
this.horiz.node.style.left = measure.barLeft + "px";
114
}
115
116
return {right: needsV ? width : 0, bottom: needsH ? width : 0};
117
};
118
119
SimpleScrollbars.prototype.setScrollTop = function(pos) {
120
this.vert.moveTo(pos, false);
121
};
122
123
SimpleScrollbars.prototype.setScrollLeft = function(pos) {
124
this.horiz.moveTo(pos, false);
125
};
126
127
SimpleScrollbars.prototype.clear = function() {
128
var parent = this.horiz.node.parentNode;
129
parent.removeChild(this.horiz.node);
130
parent.removeChild(this.vert.node);
131
};
132
133
CodeMirror.scrollbarModel.simple = function(place, scroll) {
134
return new SimpleScrollbars("CodeMirror-simplescroll", place, scroll);
135
};
136
CodeMirror.scrollbarModel.overlay = function(place, scroll) {
137
return new SimpleScrollbars("CodeMirror-overlayscroll", place, scroll);
138
};
139
});
140
141