annotatescrollbar.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. CodeMirror.defineExtension("annotateScrollbar", function(options) {
  13. if (typeof options == "string") options = {className: options};
  14. return new Annotation(this, options);
  15. });
  16. CodeMirror.defineOption("scrollButtonHeight", 0);
  17. function Annotation(cm, options) {
  18. this.cm = cm;
  19. this.options = options;
  20. this.buttonHeight = options.scrollButtonHeight || cm.getOption("scrollButtonHeight");
  21. this.annotations = [];
  22. this.doRedraw = this.doUpdate = null;
  23. this.div = cm.getWrapperElement().appendChild(document.createElement("div"));
  24. this.div.style.cssText = "position: absolute; right: 0; top: 0; z-index: 7; pointer-events: none";
  25. this.computeScale();
  26. function scheduleRedraw(delay) {
  27. clearTimeout(self.doRedraw);
  28. self.doRedraw = setTimeout(function() { self.redraw(); }, delay);
  29. }
  30. var self = this;
  31. cm.on("refresh", this.resizeHandler = function() {
  32. clearTimeout(self.doUpdate);
  33. self.doUpdate = setTimeout(function() {
  34. if (self.computeScale()) scheduleRedraw(20);
  35. }, 100);
  36. });
  37. cm.on("markerAdded", this.resizeHandler);
  38. cm.on("markerCleared", this.resizeHandler);
  39. if (options.listenForChanges !== false)
  40. cm.on("change", this.changeHandler = function() {
  41. scheduleRedraw(250);
  42. });
  43. }
  44. Annotation.prototype.computeScale = function() {
  45. var cm = this.cm;
  46. var hScale = (cm.getWrapperElement().clientHeight - cm.display.barHeight - this.buttonHeight * 2) /
  47. cm.getScrollerElement().scrollHeight
  48. if (hScale != this.hScale) {
  49. this.hScale = hScale;
  50. return true;
  51. }
  52. };
  53. Annotation.prototype.update = function(annotations) {
  54. this.annotations = annotations;
  55. this.redraw();
  56. };
  57. Annotation.prototype.redraw = function(compute) {
  58. if (compute !== false) this.computeScale();
  59. var cm = this.cm, hScale = this.hScale;
  60. var frag = document.createDocumentFragment(), anns = this.annotations;
  61. var wrapping = cm.getOption("lineWrapping");
  62. var singleLineH = wrapping && cm.defaultTextHeight() * 1.5;
  63. var curLine = null, curLineObj = null;
  64. function getY(pos, top) {
  65. if (curLine != pos.line) {
  66. curLine = pos.line;
  67. curLineObj = cm.getLineHandle(curLine);
  68. }
  69. if (wrapping && curLineObj.height > singleLineH)
  70. return cm.charCoords(pos, "local")[top ? "top" : "bottom"];
  71. var topY = cm.heightAtLine(curLineObj, "local");
  72. return topY + (top ? 0 : curLineObj.height);
  73. }
  74. if (cm.display.barWidth) for (var i = 0, nextTop; i < anns.length; i++) {
  75. var ann = anns[i];
  76. var top = nextTop || getY(ann.from, true) * hScale;
  77. var bottom = getY(ann.to, false) * hScale;
  78. while (i < anns.length - 1) {
  79. nextTop = getY(anns[i + 1].from, true) * hScale;
  80. if (nextTop > bottom + .9) break;
  81. ann = anns[++i];
  82. bottom = getY(ann.to, false) * hScale;
  83. }
  84. if (bottom == top) continue;
  85. var height = Math.max(bottom - top, 3);
  86. var elt = frag.appendChild(document.createElement("div"));
  87. elt.style.cssText = "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: "
  88. + (top + this.buttonHeight) + "px; height: " + height + "px";
  89. elt.className = this.options.className;
  90. if (ann.id) {
  91. elt.setAttribute("annotation-id", ann.id);
  92. }
  93. }
  94. this.div.textContent = "";
  95. this.div.appendChild(frag);
  96. };
  97. Annotation.prototype.clear = function() {
  98. this.cm.off("refresh", this.resizeHandler);
  99. this.cm.off("markerAdded", this.resizeHandler);
  100. this.cm.off("markerCleared", this.resizeHandler);
  101. if (this.changeHandler) this.cm.off("change", this.changeHandler);
  102. this.div.parentNode.removeChild(this.div);
  103. };
  104. });