Blame view

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