chimeric / dokuwiki-plugin-lastfm

dokuwiki-plugin-lastfm / script.js
100644 75 lines (62 sloc) 1.872 kb
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
/**
* javascript functionality for the lastfm plugin
*
* @author Michael Klier <chi@chimeric.de>
*/
 
function lastfm_ajax(chart, opts) {
    if(!document.getElementById) return;
    if(!chart) return;
    if(!opts) return;
 
    var ajax = new sack(DOKU_BASE+'lib/exe/ajax.php');
    ajax.AjaxFailedAlert = '';
    ajax.encodeURIString = false;
 
    ajax.setVar('call', 'plugin_lastfm');
    ajax.setVar('plugin_lastfm_chart', chart.id);
 
    for(var i = 0; i < opts.length; i++) {
        ajax.setVar(opts[i].firstChild.className, opts[i].firstChild.innerHTML);
    }
 
    // show loader
    lastfm_loader(chart);
 
    // define callback
    ajax.onCompletion = function(){
        var data = this.response;
        if(data === ''){ return; }
        chart.style.visibility = 'hidden';
        chart.innerHTML = data;
        chart.style.visibility = 'visible';
    };
 
    ajax.runAJAX();
}
 
/**
* Calls the ajax function for each requested chart
*
* @author Michael Klier <chi@chimeric.de>
*/
function lastfm_get_charts(charts, opts){
    if(!document.getElementById) return;
    if(!charts) return;
    if(!opts) return;
 
    for(var i = 0; i < charts.length; i++) {
        lastfm_ajax(charts[i], opts);
    }
}
 
/**
* shows the loading image
*
* @author Michael KLier <chi@chimeric.de>
*/
function lastfm_loader(obj) {
    if(!obj) return;
    obj.innerHTML = '<img src="'+DOKU_BASE+'lib/plugins/lastfm/images/loader.gif" />';
}
 
// add the init event
addInitEvent(function() {
    var objs = getElementsByClass('plugin_lastfm', document, 'div');
    if(!objs) return;
    for(var i = 0; i < objs.length; i++) {
        var opts = getElementsByClass('plugin_lastfm_opt', objs[i], 'li');
        var charts = getElementsByClass('plugin_lastfm_chart', objs[i], 'div');
        lastfm_get_charts(charts, opts);
    }
});
 
// vim:ts=4:sw=4:et:enc=utf-8: