scripts/simplescrollbars.js (view raw)
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://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 done() {
31 CodeMirror.off(document, "mousemove", move);
32 CodeMirror.off(document, "mouseup", done);
33 }
34 function move(e) {
35 if (e.which != 1) return done();
36 self.moveTo(startpos + (e[axis] - start) * (self.total / self.size));
37 }
38 CodeMirror.on(document, "mousemove", move);
39 CodeMirror.on(document, "mouseup", done);
40 });
41
42 CodeMirror.on(this.node, "click", function(e) {
43 CodeMirror.e_preventDefault(e);
44 var innerBox = self.inner.getBoundingClientRect(), where;
45 if (self.orientation == "horizontal")
46 where = e.clientX < innerBox.left ? -1 : e.clientX > innerBox.right ? 1 : 0;
47 else
48 where = e.clientY < innerBox.top ? -1 : e.clientY > innerBox.bottom ? 1 : 0;
49 self.moveTo(self.pos + where * self.screen);
50 });
51
52 function onWheel(e) {
53 var moved = CodeMirror.wheelEventPixels(e)[self.orientation == "horizontal" ? "x" : "y"];
54 var oldPos = self.pos;
55 self.moveTo(self.pos + moved);
56 if (self.pos != oldPos) CodeMirror.e_preventDefault(e);
57 }
58 CodeMirror.on(this.node, "mousewheel", onWheel);
59 CodeMirror.on(this.node, "DOMMouseScroll", onWheel);
60 }
61
62 Bar.prototype.setPos = function(pos, force) {
63 if (pos < 0) pos = 0;
64 if (pos > this.total - this.screen) pos = this.total - this.screen;
65 if (!force && pos == this.pos) return false;
66 this.pos = pos;
67 this.inner.style[this.orientation == "horizontal" ? "left" : "top"] =
68 (pos * (this.size / this.total)) + "px";
69 return true
70 };
71
72 Bar.prototype.moveTo = function(pos) {
73 if (this.setPos(pos)) this.scroll(pos, this.orientation);
74 }
75
76 var minButtonSize = 10;
77
78 Bar.prototype.update = function(scrollSize, clientSize, barSize) {
79 var sizeChanged = this.screen != clientSize || this.total != scrollSize || this.size != barSize
80 if (sizeChanged) {
81 this.screen = clientSize;
82 this.total = scrollSize;
83 this.size = barSize;
84 }
85
86 var buttonSize = this.screen * (this.size / this.total);
87 if (buttonSize < minButtonSize) {
88 this.size -= minButtonSize - buttonSize;
89 buttonSize = minButtonSize;
90 }
91 this.inner.style[this.orientation == "horizontal" ? "width" : "height"] =
92 buttonSize + "px";
93 this.setPos(this.pos, sizeChanged);
94 };
95
96 function SimpleScrollbars(cls, place, scroll) {
97 this.addClass = cls;
98 this.horiz = new Bar(cls, "horizontal", scroll);
99 place(this.horiz.node);
100 this.vert = new Bar(cls, "vertical", scroll);
101 place(this.vert.node);
102 this.width = null;
103 }
104
105 SimpleScrollbars.prototype.update = function(measure) {
106 if (this.width == null) {
107 var style = window.getComputedStyle ? window.getComputedStyle(this.horiz.node) : this.horiz.node.currentStyle;
108 if (style) this.width = parseInt(style.height);
109 }
110 var width = this.width || 0;
111
112 var needsH = measure.scrollWidth > measure.clientWidth + 1;
113 var needsV = measure.scrollHeight > measure.clientHeight + 1;
114 this.vert.node.style.display = needsV ? "block" : "none";
115 this.horiz.node.style.display = needsH ? "block" : "none";
116
117 if (needsV) {
118 this.vert.update(measure.scrollHeight, measure.clientHeight,
119 measure.viewHeight - (needsH ? width : 0));
120 this.vert.node.style.bottom = needsH ? width + "px" : "0";
121 }
122 if (needsH) {
123 this.horiz.update(measure.scrollWidth, measure.clientWidth,
124 measure.viewWidth - (needsV ? width : 0) - measure.barLeft);
125 this.horiz.node.style.right = needsV ? width + "px" : "0";
126 this.horiz.node.style.left = measure.barLeft + "px";
127 }
128
129 return {right: needsV ? width : 0, bottom: needsH ? width : 0};
130 };
131
132 SimpleScrollbars.prototype.setScrollTop = function(pos) {
133 this.vert.setPos(pos);
134 };
135
136 SimpleScrollbars.prototype.setScrollLeft = function(pos) {
137 this.horiz.setPos(pos);
138 };
139
140 SimpleScrollbars.prototype.clear = function() {
141 var parent = this.horiz.node.parentNode;
142 parent.removeChild(this.horiz.node);
143 parent.removeChild(this.vert.node);
144 };
145
146 CodeMirror.scrollbarModel.simple = function(place, scroll) {
147 return new SimpleScrollbars("CodeMirror-simplescroll", place, scroll);
148 };
149 CodeMirror.scrollbarModel.overlay = function(place, scroll) {
150 return new SimpleScrollbars("CodeMirror-overlayscroll", place, scroll);
151 };
152});