5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineExtension("annotateScrollbar", function(options) {
15
if (typeof options == "string") options = {className: options};
16
return new Annotation(this, options);
19
CodeMirror.defineOption("scrollButtonHeight", 0);
21
function Annotation(cm, options) {
23
this.options = options;
24
this.buttonHeight = options.scrollButtonHeight || cm.getOption("scrollButtonHeight");
25
this.annotations = [];
26
this.doRedraw = this.doUpdate = null;
27
this.div = cm.getWrapperElement().appendChild(document.createElement("div"));
28
this.div.style.cssText = "position: absolute; right: 0; top: 0; z-index: 7; pointer-events: none";
31
function scheduleRedraw(delay) {
32
clearTimeout(self.doRedraw);
33
self.doRedraw = setTimeout(function() { self.redraw(); }, delay);
37
cm.on("refresh", this.resizeHandler = function() {
38
clearTimeout(self.doUpdate);
39
self.doUpdate = setTimeout(function() {
40
if (self.computeScale()) scheduleRedraw(20);
43
cm.on("markerAdded", this.resizeHandler);
44
cm.on("markerCleared", this.resizeHandler);
45
if (options.listenForChanges !== false)
46
cm.on("changes", this.changeHandler = function() {
51
Annotation.prototype.computeScale = function() {
53
var hScale = (cm.getWrapperElement().clientHeight - cm.display.barHeight - this.buttonHeight * 2) /
54
cm.getScrollerElement().scrollHeight
55
if (hScale != this.hScale) {
61
Annotation.prototype.update = function(annotations) {
62
this.annotations = annotations;
66
Annotation.prototype.redraw = function(compute) {
67
if (compute !== false) this.computeScale();
68
var cm = this.cm, hScale = this.hScale;
70
var frag = document.createDocumentFragment(), anns = this.annotations;
72
var wrapping = cm.getOption("lineWrapping");
73
var singleLineH = wrapping && cm.defaultTextHeight() * 1.5;
74
var curLine = null, curLineObj = null;
76
function getY(pos, top) {
77
if (curLine != pos.line) {
79
curLineObj = cm.getLineHandle(pos.line)
80
var visual = cm.getLineHandleVisualStart(curLineObj)
81
if (visual != curLineObj) {
82
curLine = cm.getLineNumber(visual)
86
if ((curLineObj.widgets && curLineObj.widgets.length) ||
87
(wrapping && curLineObj.height > singleLineH))
88
return cm.charCoords(pos, "local")[top ? "top" : "bottom"];
89
var topY = cm.heightAtLine(curLineObj, "local");
90
return topY + (top ? 0 : curLineObj.height);
93
var lastLine = cm.lastLine()
94
if (cm.display.barWidth) for (var i = 0, nextTop; i < anns.length; i++) {
96
if (ann.to.line > lastLine) continue;
97
var top = nextTop || getY(ann.from, true) * hScale;
98
var bottom = getY(ann.to, false) * hScale;
99
while (i < anns.length - 1) {
100
if (anns[i + 1].to.line > lastLine) break;
101
nextTop = getY(anns[i + 1].from, true) * hScale;
102
if (nextTop > bottom + .9) break;
104
bottom = getY(ann.to, false) * hScale;
106
if (bottom == top) continue;
107
var height = Math.max(bottom - top, 3);
109
var elt = frag.appendChild(document.createElement("div"));
110
elt.style.cssText = "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: "
111
+ (top + this.buttonHeight) + "px; height: " + height + "px";
112
elt.className = this.options.className;
114
elt.setAttribute("annotation-id", ann.id);
117
this.div.textContent = "";
118
this.div.appendChild(frag);
121
Annotation.prototype.clear = function() {
122
this.cm.off("refresh", this.resizeHandler);
123
this.cm.off("markerAdded", this.resizeHandler);
124
this.cm.off("markerCleared", this.resizeHandler);
125
if (this.changeHandler) this.cm.off("changes", this.changeHandler);
126
this.div.parentNode.removeChild(this.div);