xref: /template/ad-hominem/script.js (revision 439bc143cbfcecd06a51eb963d76f061852d764e)
1/**
2 *  Site scripts for Ad Hominem Info
3 *
4 * @author     Sascha Leib <sascha@leib.be>
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 */
7
8/* list of REST APIs for different link types: */
9var kRestURLs = {
10	'wikilink1'	: '%basedir%lib/tpl/ad-hominem/json/pi.php?id=%id%&v=preview',
11	'iw_wp'		: 'https://en.wikipedia.org/api/rest_v1/page/summary/%ln%',
12	'iw_wpde' 	: 'https://de.wikipedia.org/api/rest_v1/page/summary/%ln%'
13};
14
15/**
16 * Loads title info for internal and Wikipedia links
17 *
18**/
19
20function loadWikiPageInfo() {
21	var a = jQuery(this);
22	var hi = jQuery.data(this, 'has-info');
23	var url = null;
24
25	/* only if the info hasn't been set yet: */
26	if (hi == undefined || hi == '') {
27
28		// remember that we are now working on it:
29		jQuery.data(this, 'has-info', '0');
30
31		for (var cls in kRestURLs) {
32			if (a.hasClass(cls)) {
33				url = kRestURLs[cls];
34				break;
35			}
36		};
37
38		if (url !== null) {
39
40			/* modify the URLs: */
41			var href = jQuery(this).attr('href');
42
43			var rp = {
44				'basedir': BASEDIR,
45				'id': jQuery(this).data('wiki-id'),
46				'ln': href.substring(href.lastIndexOf('/')+1)
47			};
48
49			for (var p in rp) {
50				url = url.replace('%'+p+'%', rp[p]);
51			}
52
53			/* load the page info */
54			jQuery.ajax({
55				url:		url,
56				context:	a,
57				dataType:	'json',
58				error:		function(xhr, msg, e) {
59								console.error(msg);
60							},
61				success:	function(data, msg, xhr) {
62								// build the new title for the element:
63								jQuery(this).attr('title', data.title + "\n" + data.extract);
64								jQuery.data(this, 'has-info', '1')
65							},
66				complete:	function() {
67								if (jQuery.data(this, 'has-info') == '0') {
68									jQuery.removeData(this, 'has-info');
69								}
70							}
71			});
72		}
73	}
74}
75
76jQuery(function(){
77
78	/* lazy-load link information to mouse-overs: */
79	jQuery('main a:link').hover(loadWikiPageInfo);
80});