forked from kazmiya/dokuwiki-plugin-editsections2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
307 lines (261 loc) · 8.92 KB
/
script.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
* EditSections2 Plugin for DokuWiki / script.js
*
* Replaces section edit highlighting events
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Kazutaka Miyasaka <kazmiya@gmail.com>
*/
(function() {
/**
* Lookup table for section edit id (startPos => secedit_id)
*/
var editIdLookup = {};
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
// compatibility check
if (
typeof DEPRECATED === 'function' ||
typeof addInitEvent === 'undefined'
) {
// for DokuWiki Angua or later
jQuery(replaceSectionEditButtonEvents);
} else if (typeof JSINFO === 'object') {
if (JSINFO.plugin_editsections2) {
// for DokuWiki Anteater and Rincewind
addInitEvent(replaceSectionEditButtonEvents_Anteater);
} else {
// for DokuWiki Lemming
addInitEvent(replaceSectionEditButtonEvents_Lemming);
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* Replaces mouseover events on section edit buttons
*/
function replaceSectionEditButtonEvents() {
jQuery('form.btn_secedit')
.each(function() {
addEditIdLookupTable(this);
})
.unbind('mouseover')
.bind('mouseover', function(event) {
highlightSections(event);
})
// FIXME: a huge change has happened... will make a real fix later
// https://github.com/splitbrain/dokuwiki/commit/870c8a4b77dd7c2cfdc14045f8604b5bbf34c01e
.unbind('mouseout')
.bind('mouseout', function(event) {
jQuery('.section_highlight').removeClass('section_highlight');
});
}
/**
* Returns start/end value of the section edit range
*/
function getRangeValue(range, startOrEnd) {
var matched,
ret;
if (!range || !(matched = /^(\d+)-(\d*)$/.exec(range.value))) {
ret = false;
} else if (startOrEnd === 'start') {
ret = Number(matched[1]);
} else if (matched[2].length) {
ret = Number(matched[2]);
} else {
ret = 'last';
}
return ret;
}
/**
* Scans and adds section edit id lookup table
*/
function addEditIdLookupTable(sectionEditForm) {
var parent,
idMatched,
startPos;
parent = sectionEditForm.parentNode;
if (
parent &&
parent.tagName &&
parent.tagName.toLowerCase() === 'div' &&
parent.className &&
(idMatched = /\b(?:editbutton_(\d+))\b/.exec(parent.className)) &&
(startPos = getRangeValue(sectionEditForm.range, 'start'))
) {
editIdLookup[startPos] = idMatched[1];
}
}
/**
* Checks if an element is heading
*/
function isHeading(element) {
return element.tagName && /^H[1-6]/i.test(element.tagName);
}
/**
* Highlights sections in the edit range
*/
function highlightSections(event) {
var sectionEditForm,
endPos,
stopClassRegExp,
doNotHighlightHeadings,
cursor;
sectionEditForm = event.target.form;
if (!sectionEditForm) {
return;
}
endPos = getRangeValue(sectionEditForm.range, 'end');
// set stopClass regexp
if (endPos === false) {
return;
} else if (endPos === 'last') {
stopClassRegExp = /\b(?:footnotes)\b/;
} else if (editIdLookup[endPos + 1]) {
stopClassRegExp = new RegExp(
'\\b(?:footnotes|sectionedit' +
String(editIdLookup[endPos + 1]).replace(/(\W)/g, '\\$1') +
')\\b'
);
} else {
// edittable plugin etc.
return;
}
doNotHighlightHeadings =
JSINFO.plugin_editsections2.highlight_target === 'exclude_headings';
cursor = sectionEditForm.parentNode;
// highlight until the stopClass appeared
while (cursor = cursor.nextSibling) {
if (!cursor.className) {
continue;
}
if (stopClassRegExp.test(cursor.className)) {
break;
}
if (
!(doNotHighlightHeadings && isHeading(cursor)) &&
!/\b(?:editbutton_section)\b/.test(cursor.className)
) {
cursor.className += ' section_highlight';
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* Replaces mouseover events on section edit buttons
* (for DokuWiki Anteater and Rincewind)
*/
function replaceSectionEditButtonEvents_Anteater() {
var i,
iMax,
parent,
events,
guid,
buttonForms,
sectionEditForms = [];
buttonForms = getElementsByClass('btn_secedit', document, 'form');
// extract section edit forms
for (i = 0, iMax = buttonForms.length; i < iMax; i++) {
parent = buttonForms[i].parentNode;
// parent element must be 'div.editbutton_section'
if (
parent &&
parent.tagName &&
parent.tagName.toLowerCase() === 'div' &&
parent.className &&
/\b(?:editbutton_section)\b/.test(parent.className)
) {
sectionEditForms[sectionEditForms.length] = buttonForms[i];
}
}
// remove events and collect section info
for (i = 0, iMax = sectionEditForms.length; i < iMax; i++) {
events = sectionEditForms[i].events;
// remove all of the previously-set mouseover events from the button
if (events && events.mouseover) {
for (guid in events.mouseover) {
removeEvent(
sectionEditForms[i], 'mouseover', events.mouseover[guid]
);
}
}
addEditIdLookupTable(sectionEditForms[i]);
}
// add new event to highlight sections to be edited
for (i = 0, iMax = sectionEditForms.length; i < iMax; i++) {
addEvent(
sectionEditForms[i],
'mouseover',
highlightSections
);
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/**
* Replaces mouseover events on section edit buttons
* (for DokuWiki Lemming)
*/
function replaceSectionEditButtonEvents_Lemming() {
var i,
iMax,
events,
guid,
buttonForms;
buttonForms = getElementsByClass('btn_secedit', document, 'form');
for (i = 0, iMax = buttonForms.length; i < iMax; i++) {
events = buttonForms[i].events;
// remove all of the previously-set mouseover events from the button
if (events && events.mouseover) {
for (guid in events.mouseover) {
removeEvent(
buttonForms[i], 'mouseover', events.mouseover[guid]
);
}
}
// add new mouseover event to highlight sections to be edited
addEvent(buttonForms[i], 'mouseover', highlightSections_Lemming);
}
}
/**
* Highlights sections in the edit range
* (for DokuWiki Lemming or earlier)
*/
function highlightSections_Lemming(event) {
var buttonForm,
sectionEditForm,
cursor,
startPos,
endPos;
buttonForm = event.target.form;
// get the end position of the section
endPos = getRangeValue(buttonForm.lines, 'end');
if (endPos === false) {
return;
}
cursor = buttonForm.parentNode;
if (!cursor.tagName || cursor.tagName.toLowerCase() !== 'div') {
return;
}
// add "section_highlight" class to DIV elements in the edit range
while (cursor = cursor.nextSibling) {
if (
!cursor.tagName ||
cursor.tagName.toLowerCase() !== 'div' ||
!cursor.className
) {
continue;
}
if (
/\b(?:secedit)\b/.test(cursor.className) &&
endPos !== 'last' &&
(sectionEditForm = cursor.getElementsByTagName('form').item(0)) &&
(startPos = getRangeValue(sectionEditForm.lines, 'start')) !== false &&
endPos < startPos
) {
// out of the edit range
break;
}
if (/\b(?:level\d)\b/.test(cursor.className)) {
cursor.className += ' section_highlight';
}
}
}
})();