simplescrollbars.js 5.3 KB

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