1/** 2 * Page scripts for Ad Hominem Info Template 3 * 4 * @author Sascha Leib <sascha@leib.be> 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 */ 7'use strict'; 8 9/* everything is contained in the $p namespace: */ 10$p = { 11 12 /* called to initialize the entire script */ 13 init: function() { 14 15 $p.togglers.init(); 16 }, 17 18 19 togglers: { 20 21 /* initialize togglers */ 22 init: function() { 23 24 const togglers = document.getElementsByClassName("toggle"); 25 26 Array.prototype.forEach.call(togglers, function (t) { 27 28 /* mark it as open, unless it was already marked otherwise */ 29 if (!t.classList.contains('closed')) { 30 t.classList.add('open'); 31 } 32 if (!t.classList.contains('mclosed')) { 33 t.classList.add('mopen'); 34 } 35 36 /* add a callback to the toggler buttons */ 37 var btn = t.getElementsByClassName('tg_button'); 38 Array.prototype.forEach.call(btn, function (b) { 39 b.addEventListener('click', $p.togglers._buttonCallback); 40 b.classList.add('active'); 41 }); 42 43 }); 44 }, 45 46 /* callback for the toggler button click */ 47 _buttonCallback: function() { 48 console.log("Button clicked: " + Date.now()); 49 50 var t = this.parentNode; 51 var op = t.classList.contains('open'); 52 t.classList.toggle('closed', op); t.classList.toggle('mclosed', op); 53 t.classList.toggle('open', !op); t.classList.toggle('mopen', !op); 54 55 } 56 } 57} 58 59/* load the script when the DOM is ready */ 60 61window.addEventListener("DOMContentLoaded", $p.init); 62 63/* list of REST APIs for different link types: */ 64var kRestURLs = { 65 'wikilink1' : '%basedir%lib/tpl/ad-hominem/rest/pageinfo.php?id=%id%&v=preview', 66 'iw_wp' : 'https://en.wikipedia.org/api/rest_v1/page/summary/%ln%', 67 'iw_wpde' : 'https://de.wikipedia.org/api/rest_v1/page/summary/%ln%' 68}; 69 70/** 71 * Loads title info for internal and Wikipedia links 72 * 73**/ 74 75function loadWikiPageInfo() { 76 var a = jQuery(this); 77 var hi = jQuery.data(this, 'has-info'); 78 var url = null; 79 80 /* only if the info hasn't been set yet: */ 81 if (hi == undefined || hi == '') { 82 83 // remember that we are now working on it: 84 jQuery.data(this, 'has-info', '0'); 85 86 for (var cls in kRestURLs) { 87 if (a.hasClass(cls)) { 88 url = kRestURLs[cls]; 89 break; 90 } 91 }; 92 93 if (url !== null) { 94 95 /* modify the URLs: */ 96 var href = jQuery(this).attr('href'); 97 98 var rp = { 99 'basedir': BASEDIR, 100 'id': jQuery(this).data('wiki-id'), 101 'ln': href.substring(href.lastIndexOf('/')+1) 102 }; 103 104 for (var p in rp) { 105 url = url.replace('%'+p+'%', rp[p]); 106 } 107 108 /* load the page info */ 109 jQuery.ajax({ 110 url: url, 111 context: a, 112 dataType: 'json', 113 error: function(xhr, msg, e) { 114 console.error(msg); 115 }, 116 success: function(data, msg, xhr) { 117 // build the new title for the element: 118 jQuery(this).attr('title', data.title + "\n" + data.extract); 119 jQuery.data(this, 'has-info', '1') 120 }, 121 complete: function() { 122 if (jQuery.data(this, 'has-info') == '0') { 123 jQuery.removeData(this, 'has-info'); 124 } 125 } 126 }); 127 } 128 } 129} 130 131jQuery(function(){ 132 133 /* lazy-load link information to mouse-overs: */ 134 jQuery('main a:link').hover(loadWikiPageInfo); 135});