1
0

rulers.js 2.1 KB

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