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) {
13
var WRAP_CLASS = "CodeMirror-activeline";
14
var BACK_CLASS = "CodeMirror-activeline-background";
15
var GUTT_CLASS = "CodeMirror-activeline-gutter";
17
CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
18
var prev = old == CodeMirror.Init ? false : old;
19
if (val == prev) return
21
cm.off("beforeSelectionChange", selectionChange);
23
delete cm.state.activeLines;
26
cm.state.activeLines = [];
27
updateActiveLines(cm, cm.listSelections());
28
cm.on("beforeSelectionChange", selectionChange);
32
function clearActiveLines(cm) {
33
for (var i = 0; i < cm.state.activeLines.length; i++) {
34
cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS);
35
cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS);
36
cm.removeLineClass(cm.state.activeLines[i], "gutter", GUTT_CLASS);
40
function sameArray(a, b) {
41
if (a.length != b.length) return false;
42
for (var i = 0; i < a.length; i++)
43
if (a[i] != b[i]) return false;
47
function updateActiveLines(cm, ranges) {
49
for (var i = 0; i < ranges.length; i++) {
50
var range = ranges[i];
51
var option = cm.getOption("styleActiveLine");
52
if (typeof option == "object" && option.nonEmpty ? range.anchor.line != range.head.line : !range.empty())
54
var line = cm.getLineHandleVisualStart(range.head.line);
55
if (active[active.length - 1] != line) active.push(line);
57
if (sameArray(cm.state.activeLines, active)) return;
58
cm.operation(function() {
60
for (var i = 0; i < active.length; i++) {
61
cm.addLineClass(active[i], "wrap", WRAP_CLASS);
62
cm.addLineClass(active[i], "background", BACK_CLASS);
63
cm.addLineClass(active[i], "gutter", GUTT_CLASS);
65
cm.state.activeLines = active;
69
function selectionChange(cm, sel) {
70
updateActiveLines(cm, sel.ranges);