You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.1 KiB

3 years ago
  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 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.moveTo = function(pos, update) {
  56. if (pos < 0) pos = 0;
  57. if (pos > this.total - this.screen) pos = this.total - this.screen;
  58. if (pos == this.pos) return;
  59. this.pos = pos;
  60. this.inner.style[this.orientation == "horizontal" ? "left" : "top"] =
  61. (pos * (this.size / this.total)) + "px";
  62. if (update !== false) this.scroll(pos, this.orientation);
  63. };
  64. Bar.prototype.update = function(scrollSize, clientSize, barSize) {
  65. this.screen = clientSize;
  66. this.total = scrollSize;
  67. this.size = barSize;
  68. // FIXME clip to min size?
  69. this.inner.style[this.orientation == "horizontal" ? "width" : "height"] =
  70. this.screen * (this.size / this.total) + "px";
  71. this.inner.style[this.orientation == "horizontal" ? "left" : "top"] =
  72. this.pos * (this.size / this.total) + "px";
  73. };
  74. function SimpleScrollbars(cls, place, scroll) {
  75. this.addClass = cls;
  76. this.horiz = new Bar(cls, "horizontal", scroll);
  77. place(this.horiz.node);
  78. this.vert = new Bar(cls, "vertical", scroll);
  79. place(this.vert.node);
  80. this.width = null;
  81. }
  82. SimpleScrollbars.prototype.update = function(measure) {
  83. if (this.width == null) {
  84. var style = window.getComputedStyle ? window.getComputedStyle(this.horiz.node) : this.horiz.node.currentStyle;
  85. if (style) this.width = parseInt(style.height);
  86. }
  87. var width = this.width || 0;
  88. var needsH = measure.scrollWidth > measure.clientWidth + 1;
  89. var needsV = measure.scrollHeight > measure.clientHeight + 1;
  90. this.vert.node.style.display = needsV ? "block" : "none";
  91. this.horiz.node.style.display = needsH ? "block" : "none";
  92. if (needsV) {
  93. this.vert.update(measure.scrollHeight, measure.clientHeight,
  94. measure.viewHeight - (needsH ? width : 0));
  95. this.vert.node.style.display = "block";
  96. this.vert.node.style.bottom = needsH ? width + "px" : "0";
  97. }
  98. if (needsH) {
  99. this.horiz.update(measure.scrollWidth, measure.clientWidth,
  100. measure.viewWidth - (needsV ? width : 0) - measure.barLeft);
  101. this.horiz.node.style.right = needsV ? width + "px" : "0";
  102. this.horiz.node.style.left = measure.barLeft + "px";
  103. }
  104. return {right: needsV ? width : 0, bottom: needsH ? width : 0};
  105. };
  106. SimpleScrollbars.prototype.setScrollTop = function(pos) {
  107. this.vert.moveTo(pos, false);
  108. };
  109. SimpleScrollbars.prototype.setScrollLeft = function(pos) {
  110. this.horiz.moveTo(pos, false);
  111. };
  112. SimpleScrollbars.prototype.clear = function() {
  113. var parent = this.horiz.node.parentNode;
  114. parent.removeChild(this.horiz.node);
  115. parent.removeChild(this.vert.node);
  116. };
  117. CodeMirror.scrollbarModel.simple = function(place, scroll) {
  118. return new SimpleScrollbars("CodeMirror-simplescroll", place, scroll);
  119. };
  120. CodeMirror.scrollbarModel.overlay = function(place, scroll) {
  121. return new SimpleScrollbars("CodeMirror-overlayscroll", place, scroll);
  122. };
  123. });