annotatescrollbar.js 2.62 KB
Newer Older
jose's avatar
jose committed
1
(function(a){if(typeof exports=="object"&&typeof module=="object"){a(require("../../lib/codemirror"))}else{if(typeof define=="function"&&define.amd){define(["../../lib/codemirror"],a)}else{a(CodeMirror)}}})(function(a){a.defineExtension("annotateScrollbar",function(c){if(typeof c=="string"){c={className:c}}return new b(this,c)});a.defineOption("scrollButtonHeight",0);function b(c,e){this.cm=c;this.options=e;this.buttonHeight=e.scrollButtonHeight||c.getOption("scrollButtonHeight");this.annotations=[];this.doRedraw=this.doUpdate=null;this.div=c.getWrapperElement().appendChild(document.createElement("div"));this.div.style.cssText="position: absolute; right: 0; top: 0; z-index: 7; pointer-events: none";this.computeScale();function f(g){clearTimeout(d.doRedraw);d.doRedraw=setTimeout(function(){d.redraw()},g)}var d=this;c.on("refresh",this.resizeHandler=function(){clearTimeout(d.doUpdate);d.doUpdate=setTimeout(function(){if(d.computeScale()){f(20)}},100)});c.on("markerAdded",this.resizeHandler);c.on("markerCleared",this.resizeHandler);if(e.listenForChanges!==false){c.on("change",this.changeHandler=function(){f(250)})}}b.prototype.computeScale=function(){var c=this.cm;var d=(c.getWrapperElement().clientHeight-c.display.barHeight-this.buttonHeight*2)/c.getScrollerElement().scrollHeight;if(d!=this.hScale){this.hScale=d;return true}};b.prototype.update=function(c){this.annotations=c;this.redraw()};b.prototype.redraw=function(o){if(o!==false){this.computeScale()}var p=this.cm,t=this.hScale;var s=document.createDocumentFragment(),g=this.annotations;var k=p.getOption("lineWrapping");var j=k&&p.defaultTextHeight()*1.5;var e=null,n=null;function q(v,u){if(e!=v.line){e=v.line;n=p.getLineHandle(e)}if(k&&n.height>j){return p.charCoords(v,"local")[u?"top":"bottom"]}var i=p.heightAtLine(n,"local");return i+(u?0:n.height)}if(p.display.barWidth){for(var h=0,l;h<g.length;h++){var d=g[h];var m=l||q(d.from,true)*t;var c=q(d.to,false)*t;while(h<g.length-1){l=q(g[h+1].from,true)*t;if(l>c+0.9){break}d=g[++h];c=q(d.to,false)*t}if(c==m){continue}var r=Math.max(c-m,3);var f=s.appendChild(document.createElement("div"));f.style.cssText="position: absolute; right: 0px; width: "+Math.max(p.display.barWidth-1,2)+"px; top: "+(m+this.buttonHeight)+"px; height: "+r+"px";f.className=this.options.className;if(d.id){f.setAttribute("annotation-id",d.id)}}}this.div.textContent="";this.div.appendChild(s)};b.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)}});