github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/app/public/codemirror/addon/scroll/simplescrollbars.js (about) 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 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.moveTo = function(pos, update) { 63 if (pos < 0) pos = 0; 64 if (pos > this.total - this.screen) pos = this.total - this.screen; 65 if (pos == this.pos) return; 66 this.pos = pos; 67 this.inner.style[this.orientation == "horizontal" ? "left" : "top"] = 68 (pos * (this.size / this.total)) + "px"; 69 if (update !== false) this.scroll(pos, this.orientation); 70 }; 71 72 var minButtonSize = 10; 73 74 Bar.prototype.update = function(scrollSize, clientSize, barSize) { 75 this.screen = clientSize; 76 this.total = scrollSize; 77 this.size = barSize; 78 79 var buttonSize = this.screen * (this.size / this.total); 80 if (buttonSize < minButtonSize) { 81 this.size -= minButtonSize - buttonSize; 82 buttonSize = minButtonSize; 83 } 84 this.inner.style[this.orientation == "horizontal" ? "width" : "height"] = 85 buttonSize + "px"; 86 this.inner.style[this.orientation == "horizontal" ? "left" : "top"] = 87 this.pos * (this.size / this.total) + "px"; 88 }; 89 90 function SimpleScrollbars(cls, place, scroll) { 91 this.addClass = cls; 92 this.horiz = new Bar(cls, "horizontal", scroll); 93 place(this.horiz.node); 94 this.vert = new Bar(cls, "vertical", scroll); 95 place(this.vert.node); 96 this.width = null; 97 } 98 99 SimpleScrollbars.prototype.update = function(measure) { 100 if (this.width == null) { 101 var style = window.getComputedStyle ? window.getComputedStyle(this.horiz.node) : this.horiz.node.currentStyle; 102 if (style) this.width = parseInt(style.height); 103 } 104 var width = this.width || 0; 105 106 var needsH = measure.scrollWidth > measure.clientWidth + 1; 107 var needsV = measure.scrollHeight > measure.clientHeight + 1; 108 this.vert.node.style.display = needsV ? "block" : "none"; 109 this.horiz.node.style.display = needsH ? "block" : "none"; 110 111 if (needsV) { 112 this.vert.update(measure.scrollHeight, measure.clientHeight, 113 measure.viewHeight - (needsH ? width : 0)); 114 this.vert.node.style.display = "block"; 115 this.vert.node.style.bottom = needsH ? width + "px" : "0"; 116 } 117 if (needsH) { 118 this.horiz.update(measure.scrollWidth, measure.clientWidth, 119 measure.viewWidth - (needsV ? width : 0) - measure.barLeft); 120 this.horiz.node.style.right = needsV ? width + "px" : "0"; 121 this.horiz.node.style.left = measure.barLeft + "px"; 122 } 123 124 return {right: needsV ? width : 0, bottom: needsH ? width : 0}; 125 }; 126 127 SimpleScrollbars.prototype.setScrollTop = function(pos) { 128 this.vert.moveTo(pos, false); 129 }; 130 131 SimpleScrollbars.prototype.setScrollLeft = function(pos) { 132 this.horiz.moveTo(pos, false); 133 }; 134 135 SimpleScrollbars.prototype.clear = function() { 136 var parent = this.horiz.node.parentNode; 137 parent.removeChild(this.horiz.node); 138 parent.removeChild(this.vert.node); 139 }; 140 141 CodeMirror.scrollbarModel.simple = function(place, scroll) { 142 return new SimpleScrollbars("CodeMirror-simplescroll", place, scroll); 143 }; 144 CodeMirror.scrollbarModel.overlay = function(place, scroll) { 145 return new SimpleScrollbars("CodeMirror-overlayscroll", place, scroll); 146 }; 147 });