Merge pull request #5987 from WoltLab/acp-dahsboard-box-hight
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / selection / active-line.js
CommitLineData
44f4c339 1// CodeMirror, copyright (c) by Marijn Haverbeke and others
373d1232 2// Distributed under an MIT license: https://codemirror.net/LICENSE
77b7b761 3
837afb80
TD
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) {
77b7b761
TD
12 "use strict";
13 var WRAP_CLASS = "CodeMirror-activeline";
14 var BACK_CLASS = "CodeMirror-activeline-background";
44f4c339 15 var GUTT_CLASS = "CodeMirror-activeline-gutter";
77b7b761
TD
16
17 CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) {
373d1232
TD
18 var prev = old == CodeMirror.Init ? false : old;
19 if (val == prev) return
20 if (prev) {
837afb80
TD
21 cm.off("beforeSelectionChange", selectionChange);
22 clearActiveLines(cm);
23 delete cm.state.activeLines;
77b7b761 24 }
373d1232
TD
25 if (val) {
26 cm.state.activeLines = [];
27 updateActiveLines(cm, cm.listSelections());
28 cm.on("beforeSelectionChange", selectionChange);
29 }
77b7b761
TD
30 });
31
837afb80
TD
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);
44f4c339 36 cm.removeLineClass(cm.state.activeLines[i], "gutter", GUTT_CLASS);
77b7b761
TD
37 }
38 }
39
837afb80
TD
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;
44 return true;
77b7b761 45 }
837afb80
TD
46
47 function updateActiveLines(cm, ranges) {
48 var active = [];
49 for (var i = 0; i < ranges.length; i++) {
44f4c339 50 var range = ranges[i];
373d1232
TD
51 var option = cm.getOption("styleActiveLine");
52 if (typeof option == "object" && option.nonEmpty ? range.anchor.line != range.head.line : !range.empty())
53 continue
44f4c339 54 var line = cm.getLineHandleVisualStart(range.head.line);
837afb80
TD
55 if (active[active.length - 1] != line) active.push(line);
56 }
57 if (sameArray(cm.state.activeLines, active)) return;
58 cm.operation(function() {
59 clearActiveLines(cm);
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);
44f4c339 63 cm.addLineClass(active[i], "gutter", GUTT_CLASS);
837afb80
TD
64 }
65 cm.state.activeLines = active;
66 });
67 }
68
69 function selectionChange(cm, sel) {
70 updateActiveLines(cm, sel.ranges);
71 }
72});