(function (mod){ if (typeof exports == "object" && typeof module == "object") mod(require("../../lib/codemirror")); else if (typeof define == "function" && define.amd) define(["../../lib/codemirror"] , mod); else mod(CodeMirror); } )(function (CodeMirror){ "use strict"; CodeMirror.defineExtension("annotateScrollbar", function (options){ if (typeof options == "string") options = { className: options} ; return new Annotation(this, options); } ); CodeMirror.defineOption("scrollButtonHeight", 0); function Annotation(cm, options){ this.cm = cm; this.options = options; this.buttonHeight = options.scrollButtonHeight || cm.getOption("scrollButtonHeight"); this.annotations = [] ; this.doRedraw = this.doUpdate = null ; this.div = _AN_Call_appendchild("appendChild", cm.getWrapperElement(), _AN_Call_createelement("createElement", document, "div")); _AN_Write_csstext("cssText", this.div.style, false , "position: absolute; right: 0; top: 0; z-index: 7; pointer-events: none"); this.computeScale(); function scheduleRedraw(delay){ clearTimeout(self.doRedraw); self.doRedraw = _AN_Call_settimeout("setTimeout", window, function (){ self.redraw(); } , delay); } var self = this; cm.on("refresh", this.resizeHandler = function (){ clearTimeout(self.doUpdate); self.doUpdate = _AN_Call_settimeout("setTimeout", window, function (){ if (self.computeScale()) scheduleRedraw(20); } , 100); } ); cm.on("markerAdded", this.resizeHandler); cm.on("markerCleared", this.resizeHandler); if (options.listenForChanges !== false ) cm.on("change", this.changeHandler = function (){ scheduleRedraw(250); } ); } Annotation.prototype.computeScale = function (){ var cm = this.cm; var hScale = (cm.getWrapperElement().clientHeight - cm.display.barHeight - this.buttonHeight * 2) / cm.getScrollerElement().scrollHeight; if (hScale != this.hScale) { this.hScale = hScale; return true ; } } ; Annotation.prototype.update = function (annotations){ this.annotations = annotations; this.redraw(); } ; Annotation.prototype.redraw = function (compute){ if (compute !== false ) this.computeScale(); var cm = this.cm, hScale = this.hScale; var frag = document.createDocumentFragment(), anns = this.annotations; var wrapping = cm.getOption("lineWrapping"); var singleLineH = wrapping && cm.defaultTextHeight() * 1.5; var curLine = null , curLineObj = null ; function getY(pos, top){ if (curLine != pos.line) { curLine = pos.line; curLineObj = cm.getLineHandle(curLine); } if (wrapping && curLineObj.height > singleLineH) return cm.charCoords(pos, "local")[top? "top": "bottom"]; var topY = cm.heightAtLine(curLineObj, "local"); return topY + (top? 0: curLineObj.height); } if (cm.display.barWidth) for (var i = 0, nextTop; i < _AN_Read_length("length", anns); i++ ){ var ann = anns[i]; var top = nextTop || getY(ann.from, true ) * hScale; var bottom = getY(ann.to, false ) * hScale; while (i < _AN_Read_length("length", anns) - 1){ nextTop = getY(anns[i + 1].from, true ) * hScale; if (nextTop > bottom + 0.9) break ; ann = anns[++i]; bottom = getY(ann.to, false ) * hScale; } if (bottom == top) continue ; var height = Math.max(bottom - top, 3); var elt = _AN_Call_appendchild("appendChild", frag, _AN_Call_createelement("createElement", document, "div")); _AN_Write_csstext("cssText", elt.style, false , "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: " + (top + this.buttonHeight) + "px; height: " + height + "px"); elt.className = this.options.className; if (ann.id) { _AN_Call_setattribute("setAttribute", elt, "annotation-id", ann.id); } } this.div.textContent = ""; _AN_Call_appendchild("appendChild", this.div, frag); } ; Annotation.prototype.clear = function (){ this.cm.off("refresh", this.resizeHandler); this.cm.off("markerAdded", this.resizeHandler); this.cm.off("markerCleared", this.resizeHandler); if (this.changeHandler) this.cm.off("change", this.changeHandler); this.div.parentNode.removeChild(this.div); } ; } );