| 5 |
lars |
1 |
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
2 |
// Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
|
3 |
|
|
|
4 |
(function(mod) {
|
|
|
5 |
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
6 |
mod(require("../../lib/codemirror"));
|
|
|
7 |
else if (typeof define == "function" && define.amd) // AMD
|
|
|
8 |
define(["../../lib/codemirror"], mod);
|
|
|
9 |
else // Plain browser env
|
|
|
10 |
mod(CodeMirror);
|
|
|
11 |
})(function(CodeMirror) {
|
|
|
12 |
"use strict";
|
|
|
13 |
|
|
|
14 |
CodeMirror.defineOption("rulers", false, function(cm, val, old) {
|
|
|
15 |
if (old && old != CodeMirror.Init) {
|
|
|
16 |
clearRulers(cm);
|
|
|
17 |
cm.off("refresh", refreshRulers);
|
|
|
18 |
}
|
|
|
19 |
if (val && val.length) {
|
|
|
20 |
setRulers(cm);
|
|
|
21 |
cm.on("refresh", refreshRulers);
|
|
|
22 |
}
|
|
|
23 |
});
|
|
|
24 |
|
|
|
25 |
function clearRulers(cm) {
|
|
|
26 |
for (var i = cm.display.lineSpace.childNodes.length - 1; i >= 0; i--) {
|
|
|
27 |
var node = cm.display.lineSpace.childNodes[i];
|
|
|
28 |
if (/(^|\s)CodeMirror-ruler($|\s)/.test(node.className))
|
|
|
29 |
node.parentNode.removeChild(node);
|
|
|
30 |
}
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
function setRulers(cm) {
|
|
|
34 |
var val = cm.getOption("rulers");
|
|
|
35 |
var cw = cm.defaultCharWidth();
|
|
|
36 |
var left = cm.charCoords(CodeMirror.Pos(cm.firstLine(), 0), "div").left;
|
|
|
37 |
var minH = cm.display.scroller.offsetHeight + 30;
|
|
|
38 |
for (var i = 0; i < val.length; i++) {
|
|
|
39 |
var elt = document.createElement("div");
|
|
|
40 |
elt.className = "CodeMirror-ruler";
|
|
|
41 |
var col, conf = val[i];
|
|
|
42 |
if (typeof conf == "number") {
|
|
|
43 |
col = conf;
|
|
|
44 |
} else {
|
|
|
45 |
col = conf.column;
|
|
|
46 |
if (conf.className) elt.className += " " + conf.className;
|
|
|
47 |
if (conf.color) elt.style.borderColor = conf.color;
|
|
|
48 |
if (conf.lineStyle) elt.style.borderLeftStyle = conf.lineStyle;
|
|
|
49 |
if (conf.width) elt.style.borderLeftWidth = conf.width;
|
|
|
50 |
}
|
|
|
51 |
elt.style.left = (left + col * cw) + "px";
|
|
|
52 |
elt.style.top = "-50px";
|
|
|
53 |
elt.style.bottom = "-20px";
|
|
|
54 |
elt.style.minHeight = minH + "px";
|
|
|
55 |
cm.display.lineSpace.insertBefore(elt, cm.display.cursorDiv);
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
function refreshRulers(cm) {
|
|
|
60 |
clearRulers(cm);
|
|
|
61 |
setRulers(cm);
|
|
|
62 |
}
|
|
|
63 |
});
|