Fixed time zone calculation issue
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / 3rdParty / codemirror / addon / lint / lint.js
1 CodeMirror.validate = (function() {
2 var GUTTER_ID = "CodeMirror-lint-markers";
3 var SEVERITIES = /^(?:error|warning)$/;
4
5 function showTooltip(e, content) {
6 var tt = document.createElement("div");
7 tt.className = "CodeMirror-lint-tooltip";
8 tt.appendChild(content.cloneNode(true));
9 document.body.appendChild(tt);
10
11 function position(e) {
12 if (!tt.parentNode) return CodeMirror.off(document, "mousemove", position);
13 tt.style.top = Math.max(0, e.clientY - tt.offsetHeight - 5) + "px";
14 tt.style.left = (e.clientX + 5) + "px";
15 }
16 CodeMirror.on(document, "mousemove", position);
17 position(e);
18 if (tt.style.opacity != null) tt.style.opacity = 1;
19 return tt;
20 }
21 function rm(elt) {
22 if (elt.parentNode) elt.parentNode.removeChild(elt);
23 }
24 function hideTooltip(tt) {
25 if (!tt.parentNode) return;
26 if (tt.style.opacity == null) rm(tt);
27 tt.style.opacity = 0;
28 setTimeout(function() { rm(tt); }, 600);
29 }
30
31 function showTooltipFor(e, content, node) {
32 var tooltip = showTooltip(e, content);
33 function hide() {
34 CodeMirror.off(node, "mouseout", hide);
35 if (tooltip) { hideTooltip(tooltip); tooltip = null; }
36 }
37 var poll = setInterval(function() {
38 if (tooltip) for (var n = node;; n = n.parentNode) {
39 if (n == document.body) return;
40 if (!n) { hide(); break; }
41 }
42 if (!tooltip) return clearInterval(poll);
43 }, 400);
44 CodeMirror.on(node, "mouseout", hide);
45 }
46
47 function LintState(cm, options, hasGutter) {
48 this.marked = [];
49 this.options = options;
50 this.timeout = null;
51 this.hasGutter = hasGutter;
52 this.onMouseOver = function(e) { onMouseOver(cm, e); };
53 }
54
55 function parseOptions(options) {
56 if (options instanceof Function) return {getAnnotations: options};
57 else if (!options || !options.getAnnotations) throw new Error("Required option 'getAnnotations' missing (lint addon)");
58 return options;
59 }
60
61 function clearMarks(cm) {
62 var state = cm.state.lint;
63 if (state.hasGutter) cm.clearGutter(GUTTER_ID);
64 for (var i = 0; i < state.marked.length; ++i)
65 state.marked[i].clear();
66 state.marked.length = 0;
67 }
68
69 function makeMarker(labels, severity, multiple, tooltips) {
70 var marker = document.createElement("div"), inner = marker;
71 marker.className = "CodeMirror-lint-marker-" + severity;
72 if (multiple) {
73 inner = marker.appendChild(document.createElement("div"));
74 inner.className = "CodeMirror-lint-marker-multiple";
75 }
76
77 if (tooltips != false) CodeMirror.on(inner, "mouseover", function(e) {
78 showTooltipFor(e, labels, inner);
79 });
80
81 return marker;
82 }
83
84 function getMaxSeverity(a, b) {
85 if (a == "error") return a;
86 else return b;
87 }
88
89 function groupByLine(annotations) {
90 var lines = [];
91 for (var i = 0; i < annotations.length; ++i) {
92 var ann = annotations[i], line = ann.from.line;
93 (lines[line] || (lines[line] = [])).push(ann);
94 }
95 return lines;
96 }
97
98 function annotationTooltip(ann) {
99 var severity = ann.severity;
100 if (!SEVERITIES.test(severity)) severity = "error";
101 var tip = document.createElement("div");
102 tip.className = "CodeMirror-lint-message-" + severity;
103 tip.appendChild(document.createTextNode(ann.message));
104 return tip;
105 }
106
107 function startLinting(cm) {
108 var state = cm.state.lint, options = state.options;
109 if (options.async)
110 options.getAnnotations(cm, updateLinting, options);
111 else
112 updateLinting(cm, options.getAnnotations(cm.getValue()));
113 }
114
115 function updateLinting(cm, annotationsNotSorted) {
116 clearMarks(cm);
117 var state = cm.state.lint, options = state.options;
118
119 var annotations = groupByLine(annotationsNotSorted);
120
121 for (var line = 0; line < annotations.length; ++line) {
122 var anns = annotations[line];
123 if (!anns) continue;
124
125 var maxSeverity = null;
126 var tipLabel = state.hasGutter && document.createDocumentFragment();
127
128 for (var i = 0; i < anns.length; ++i) {
129 var ann = anns[i];
130 var severity = ann.severity;
131 if (!SEVERITIES.test(severity)) severity = "error";
132 maxSeverity = getMaxSeverity(maxSeverity, severity);
133
134 if (options.formatAnnotation) ann = options.formatAnnotation(ann);
135 if (state.hasGutter) tipLabel.appendChild(annotationTooltip(ann));
136
137 if (ann.to) state.marked.push(cm.markText(ann.from, ann.to, {
138 className: "CodeMirror-lint-mark-" + severity,
139 __annotation: ann
140 }));
141 }
142
143 if (state.hasGutter)
144 cm.setGutterMarker(line, GUTTER_ID, makeMarker(tipLabel, maxSeverity, anns.length > 1,
145 state.options.tooltips));
146 }
147 if (options.onUpdateLinting) options.onUpdateLinting(annotationsNotSorted, annotations, cm);
148 }
149
150 function onChange(cm) {
151 var state = cm.state.lint;
152 clearTimeout(state.timeout);
153 state.timeout = setTimeout(function(){startLinting(cm);}, state.options.delay || 500);
154 }
155
156 function popupSpanTooltip(ann, e) {
157 var target = e.target || e.srcElement;
158 showTooltipFor(e, annotationTooltip(ann), target);
159 }
160
161 // When the mouseover fires, the cursor might not actually be over
162 // the character itself yet. These pairs of x,y offsets are used to
163 // probe a few nearby points when no suitable marked range is found.
164 var nearby = [0, 0, 0, 5, 0, -5, 5, 0, -5, 0];
165
166 function onMouseOver(cm, e) {
167 if (!/\bCodeMirror-lint-mark-/.test((e.target || e.srcElement).className)) return;
168 for (var i = 0; i < nearby.length; i += 2) {
169 var spans = cm.findMarksAt(cm.coordsChar({left: e.clientX + nearby[i],
170 top: e.clientY + nearby[i + 1]}));
171 for (var j = 0; j < spans.length; ++j) {
172 var span = spans[j], ann = span.__annotation;
173 if (ann) return popupSpanTooltip(ann, e);
174 }
175 }
176 }
177
178 CodeMirror.defineOption("lintWith", false, function(cm, val, old) {
179 if (old && old != CodeMirror.Init) {
180 clearMarks(cm);
181 cm.off("change", onChange);
182 CodeMirror.off(cm.getWrapperElement(), "mouseover", cm.state.lint.onMouseOver);
183 delete cm.state.lint;
184 }
185
186 if (val) {
187 var gutters = cm.getOption("gutters"), hasLintGutter = false;
188 for (var i = 0; i < gutters.length; ++i) if (gutters[i] == GUTTER_ID) hasLintGutter = true;
189 var state = cm.state.lint = new LintState(cm, parseOptions(val), hasLintGutter);
190 cm.on("change", onChange);
191 if (state.options.tooltips != false)
192 CodeMirror.on(cm.getWrapperElement(), "mouseover", state.onMouseOver);
193
194 startLinting(cm);
195 }
196 });
197 })();