-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
356 lines (294 loc) · 9.31 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
* Page scripts for Ad Hominem Info Template
*
* @author Sascha Leib <sascha@leib.be>
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/
/* everything is contained in the $p namespace: */
$p = {
/* called to initialize the entire script */
init: function() {
$p.cookie_banner.init();
$p.linkinfo.init();
$p.search.init();
$p.togglers.init();
$p.langMenu.init();
},
/* link information */
linkinfo: {
init: function() {
/* find all links in the main section */
var main = document.getElementById("main-layout");
var al = main.getElementsByTagName("a");
Array.prototype.forEach.call(al, function (a) {
Object.entries($p.linkinfo._restURLs).forEach((c) => {
var cls = c[0];
if (a.classList.contains(cls)) {
a.addEventListener('mouseover', $p.linkinfo._linkHoverCallback);
}
});
});
},
/* pre-defined REST API URLs for different sites. */
/* variables are enclosed in %, allowed vars are: */
/* - basedir = this site's basedir (e.g. "/"), */
/* - id = the data id of the link (internal only) */
/* - ln = the link name (e.g. for Wikipedia links) */
/* types can be 'internal', 'wikimedia', or 'ahtpl' */
/* for other sites using this template. */
_restURLs : {
'wikilink1' : {
url: '%basedir%lib/tpl/ad-hominem/rest/pageinfo.php?id=%id%&v=preview',
type:'internal'
},
'iw_wp' : {
url:'https://en.wikipedia.org/api/rest_v1/page/summary/%id%',
type:'wikimedia'
},
'iw_wpfr' : {
url:'https://fr.wikipedia.org/api/rest_v1/page/summary/%id%',
type:'wikimedia'
},
'iw_wpde' : {
url:'https://de.wikipedia.org/api/rest_v1/page/summary/%id%',
type:'wikimedia'
},
'iw_wpes' : {
url:'https://es.wikipedia.org/api/rest_v1/page/summary/%id%',
type:'wikimedia'
},
'iw_wppl' : {
url:'https://pl.wikipedia.org/api/rest_v1/page/summary/%id%',
type:'wikimedia'
},
'iw_wpja' : {
url:'https://it.wikipedia.org/api/rest_v1/page/summary/%id%',
type:'wikimedia'
},
'iw_wpru' : {
url:'https://ru.wikipedia.org/api/rest_v1/page/summary/%id%',
type:'wikimedia'
},
'iw_meta' : {
url:'https://meta.wikipedia.org/api/rest_v1/page/summary/%id%',
type:'wikimedia'
},
'iw_fo' : {
url:'https://fallacies.online/wiki/lib/tpl/ad-hominem/rest/pageinfo.php?id=%id%&v=preview',
base:'https://fallacies.online/wiki/',
type:'ahtpl'
},
'iw_dfo' : {
url:'https://denkfehler.online/wiki/lib/tpl/ad-hominem/rest/pageinfo.php?id=%id%&v=preview',
base:'https://denkfehler.online/wiki/',
type:'ahtpl'
}
},
/* note: this covers the internal links and the most common
wikipedia lang versions. If you know about any other
relevant sites to be added here, let the author of this
template know (ad@hominem.info) */
/* TODO: mechanism to dynamically add sites by site admin */
/* callback for the onhover event of links: */
_linkHoverCallback: function() {
var a = jQuery(this);
var hi = jQuery.data(this, 'has-info');
var href = jQuery(this).attr('href');
var wid = null;
var url = null;
var type = '';
/* only if the info hasn't been set yet: */
if (hi == undefined || hi == '') {
// remember that we are now working on the link:
jQuery.data(this, 'has-info', '0');
// find the URL to query:
try {
for (var cls in $p.linkinfo._restURLs) {
if (a.hasClass(cls)) {
url = $p.linkinfo._restURLs[cls].url;
type = $p.linkinfo._restURLs[cls].type;
break;
}
};
} catch (e) {}
/* get the ID to request: */
switch(type) {
case 'internal': // internal links
url = url.replace('%basedir%', (typeof BASEDIR!=='undefined'?BASEDIR:'/'));
wid = jQuery(this).data('wiki-id');
break;
case 'wikimedia': // wikipedia sites
wid = href.substring(href.lastIndexOf('/')+1);
break;
case 'ahtpl': // Other sites with this template
wid = href.substring($p.linkinfo._restURLs[cls].base.length).replaceAll('/', ':');
break;
default: // unknown -> skip
return;
}
// URL & ID found?
if (url !== null && typeof wid !== 'undefined') {
/* load the page info */
jQuery.ajax({
url: url.replace('%id%', encodeURIComponent(wid)),
context: a,
dataType: 'json',
crossDomain: true,
error: function(xhr, msg, e) {
console.error(msg);
},
success: function(data, msg, xhr) {
// build the new title for the element:
if (typeof data.title !== 'undefined') {
jQuery(this).attr('title', data.title + "\n" + data.extract);
jQuery.data(this, 'has-info', '1');
}
},
complete: function() {
if (jQuery.data(this, 'has-info') == '0') {
jQuery.removeData(this, 'has-info');
}
}
});
}
}
}
},
/* anything related to the search */
search: {
/* initializer */
init: function() {
$p.search.gui.init();
},
/* the search gui */
gui: {
_container: null,
_elements: { field: null, clear: null, search: null },
/* init the gui */
init: function() {
try {
/* find all the search elements: */
var form = document.getElementById('dw__search');
var div = form.getElementsByClassName('search-field')[0];
$p.search.gui._container = div;
var field = div.getElementsByTagName('input')[0];
$p.search.gui._elements.field = field;
field.addEventListener('focus', $p.search.gui.__elementFocus);
field.addEventListener('blur', $p.search.gui.__elementBlur);
var buttons = div.getElementsByTagName('button');
Array.prototype.forEach.call(buttons, function(b) {
var type = b.getAttribute('type');
if (type == 'reset') {
$p.search.gui._elements.clear = b;
} else if (type == 'submit') {
$p.search.gui._elements.search = b;
}
b.addEventListener('focus', $p.search.gui.__elementFocus);
b.addEventListener('blur', $p.search.gui.__elementBlur);
});
} catch (e) {
console.warn("Can’t initialize search form.");
console.error(e);
}
},
/* call back for fields */
__elementFocus: function() {
$p.search.gui._container.classList.add("focus");
},
__elementBlur: function() {
$p.search.gui._container.classList.remove("focus");
}
}
},
/* expaning sections, for menus, etc. */
togglers: {
/* initialize togglers */
init: function() {
const togglers = document.getElementsByClassName("toggle");
Array.prototype.forEach.call(togglers, function(t) {
/* add default state */
if (!(t.classList.contains('show') || (t.classList.contains('hide')))) {
t.classList.add('auto');
}
/* add a callback to the toggler buttons */
var btn = t.getElementsByClassName('tg_button');
Array.prototype.forEach.call(btn, function(b) {
b.addEventListener('click', $p.togglers._buttonCallback);
b.classList.add('active');
});
});
},
/* callback for the toggler button click */
_buttonCallback: function() {
var t = this.parentNode;
/* current state of the toggler: */
var state = 'auto';
if (t.classList.contains('show')) state = 'show';
if (t.classList.contains('hide')) state = 'hide';
if (t.classList.contains('alt')) state = 'alt';
/* set new state: */
var newState = 'alt';
if (state == 'show') { newState = 'hide' }
else if (state == 'hide') { newState = 'show' }
else if (state == 'alt') { newState = 'auto' }
t.classList.remove(state);
t.classList.add(newState);
}
},
/* the language menu, if present */
langMenu: {
/* initialize lang menu */
init: function() {
const langMenu = document.getElementById('langButton');
if (langMenu) {
jQuery(langMenu).click($p.langMenu._btnCallback);
}
},
_btnCallback: function(e) {
const btn = e.currentTarget;
if (btn) {
const menuId = btn.getAttribute('aria-controls');
const expanded = (btn.getAttribute('aria-expanded') == 'true');
if (menuId) {
const menu = document.getElementById(menuId);
if (menu) {
if (expanded) {
jQuery(menu).hide();
btn.setAttribute('aria-expanded', 'false')
} else {
jQuery(menu).show();
btn.setAttribute('aria-expanded', 'true')
}
}
}
}
}
},
/* Cookies info banner */
cookie_banner: {
/* initialize Cookies info banner */
init: function() {
// find the cookiebanner elements:
var btn = jQuery('#cookiebanner button');
var cookie = jQuery.cookie('cookielaw');
if ( (cookie !== '1') && (btn.length >= 1) ) { // if found only
// assign callback:
jQuery(btn).click($p.cookie_banner._buttonCallback);
// show the banner
jQuery('#cookiebanner').show();
// set focus:
jQuery(btn).first().focus();
}
},
/* callback for the "OK" button */
_buttonCallback: function() {
const date = new Date();
date.setFullYear(date.getFullYear() + 1);
var path = ( typeof BASEDIR !== 'undefined' ? BASEDIR : '/');
document.cookie = 'cookielaw=1; path=' + path + '; expires=' + date.toUTCString() + '; SameSite=Lax';
jQuery('#cookiebanner').remove();
}
}
};
/* load the script when the DOM is ready */
window.addEventListener("DOMContentLoaded", $p.init);