simplescrollbars.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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 move(e) {
  27. if (e.which != 1) {
  28. CodeMirror.off(document, "mousemove", move);
  29. return;
  30. }
  31. self.moveTo(startpos + (e[axis] - start) * (self.total / self.size));
  32. }
  33. CodeMirror.on(document, "mousemove", move);
  34. });
  35. CodeMirror.on(this.node, "click", function(e) {
  36. CodeMirror.e_preventDefault(e);
  37. var innerBox = self.inner.getBoundingClientRect(), where;
  38. if (self.orientation == "horizontal")
  39. where = e.clientX < innerBox.left ? -1 : e.clientX > innerBox.right ? 1 : 0;
  40. else
  41. where = e.clientY < innerBox.top ? -1 : e.clientY > innerBox.bottom ? 1 : 0;
  42. self.moveTo(self.pos + where * self.screen);
  43. });
  44. function onWheel(e) {
  45. var moved = CodeMirror.wheelEventPixels(e)[self.orientation == "horizontal" ? "x" : "y"];
  46. var oldPos = self.pos;
  47. self.moveTo(self.pos + moved);
  48. if (self.pos != oldPos) CodeMirror.e_preventDefault(e);
  49. }
  50. CodeMirror.on(this.node, "mousewheel", onWheel);
  51. CodeMirror.on(this.node, "DOMMouseScroll", onWheel);
  52. }
  53. Bar.prototype.moveTo = function(pos, update) {
  54. if (pos < 0) pos = 0;
  55. if (pos > this.total - this.screen) pos = this.total - this.screen;
  56. if (pos == this.pos) return;
  57. this.pos = pos;
  58. this.inner.style[this.orientation == "horizontal" ? "left" : "top"] =
  59. (pos * (this.size / this.total)) + "px";
  60. if (update !== false) this.scroll(pos, this.orientation);
  61. };
  62. Bar.prototype.update = function(scrollSize, clientSize, barSize) {
  63. this.screen = clientSize;
  64. this.total = scrollSize;
  65. this.size = barSize;
  66. // FIXME clip to min size?
  67. this.inner.style[this.orientation == "horizontal" ? "width" : "height"] =
  68. this.screen * (this.size / this.total) + "px";
  69. this.inner.style[this.orientation == "horizontal" ? "left" : "top"] =
  70. this.pos * (this.size / this.total) + "px";
  71. };
  72. function SimpleScrollbars(cls, place, scroll) {
  73. this.addClass = cls;
  74. this.horiz = new Bar(cls, "horizontal", scroll);
  75. place(this.horiz.node);
  76. this.vert = new Bar(cls, "vertical", scroll);
  77. place(this.vert.node);
  78. this.width = null;
  79. }
  80. SimpleScrollbars.prototype.update = function(measure) {
  81. if (this.width == null) {
  82. var style = window.getComputedStyle ? window.getComputedStyle(this.horiz.node) : this.horiz.node.currentStyle;
  83. if (style) this.width = parseInt(style.height);
  84. }
  85. var width = this.width || 0;
  86. var needsH = measure.scrollWidth > measure.clientWidth + 1;
  87. var needsV = measure.scrollHeight > measure.clientHeight + 1;
  88. this.vert.node.style.display = needsV ? "block" : "none";
  89. this.horiz.node.style.display = needsH ? "block" : "none";
  90. if (needsV) {
  91. this.vert.update(measure.scrollHeight, measure.clientHeight,
  92. measure.viewHeight - (needsH ? width : 0));
  93. this.vert.node.style.display = "block";
  94. this.vert.node.style.bottom = needsH ? width + "px" : "0";
  95. }
  96. if (needsH) {
  97. this.horiz.update(measure.scrollWidth, measure.clientWidth,
  98. measure.viewWidth - (needsV ? width : 0) - measure.barLeft);
  99. this.horiz.node.style.right = needsV ? width + "px" : "0";
  100. this.horiz.node.style.left = measure.barLeft + "px";
  101. }
  102. return {right: needsV ? width : 0, bottom: needsH ? width : 0};
  103. };
  104. SimpleScrollbars.prototype.setScrollTop = function(pos) {
  105. this.vert.moveTo(pos, false);
  106. };
  107. SimpleScrollbars.prototype.setScrollLeft = function(pos) {
  108. this.horiz.moveTo(pos, false);
  109. };
  110. SimpleScrollbars.prototype.clear = function() {
  111. var parent = this.horiz.node.parentNode;
  112. parent.removeChild(this.horiz.node);
  113. parent.removeChild(this.vert.node);
  114. };
  115. CodeMirror.scrollbarModel.simple = function(place, scroll) {
  116. return new SimpleScrollbars("CodeMirror-simplescroll", place, scroll);
  117. };
  118. CodeMirror.scrollbarModel.overlay = function(place, scroll) {
  119. return new SimpleScrollbars("CodeMirror-overlayscroll", place, scroll);
  120. };
  121. });