xref: /plugin/quicksubscribe/script.js (revision 529dbfdbe83b711e08a2e02e9b8539bc2b920656)
1addInitEvent(function () {
2    var links = getElementsByClass('qsub__link', document, 'img');
3    if (links.length === 0) return;
4
5    function prettyid(ns) {
6        return ns ? (ns + ':*') : '*';
7    }
8
9    function onclick() {
10        var overlay = $('plugin_qsub_popup');
11        if (overlay) overlay.parentNode.removeChild(overlay);
12
13        var content = document.createElement('div');
14        var s = this.className.match(/qsub__notsubscribed/);
15        content.className = 'content';
16        if (s) {
17            content.innerHTML = '<p>' + LANG.plugins.quicksubscribe.subscr_in_progress + '</p>';
18            var ajax = new doku_ajax('plugin_quicksubscribe_subscribe', {ns: NS + ':'});
19        } else {
20            content.innerHTML = '<p>' + LANG.plugins.quicksubscribe.is_subscr.replace(/%s/, this.title) +
21                                '<br/>' + LANG.plugins.quicksubscribe.del_subscr +
22                                ' <button class="button">' + LANG.plugins.quicksubscribe.del_subscr_button +
23                                '</button></p>';
24            var ajax = new doku_ajax('plugin_quicksubscribe_unsubscribe', {ns: this.ns});
25            addEvent(content.lastChild.lastChild, 'click', function () {
26                // late bind!
27                ajax.runAJAX();
28            });
29        }
30
31        var _this = this;
32        var tgt = content.lastChild;
33        ajax.onCompletion = function () {
34            tgt.innerHTML = LANG.plugins.quicksubscribe[(s ? 'sub' : 'unsub') +
35                                                        '_' + (this.responseStatus[0] == 200 ?
36                                                        'succ' : 'fail')].replace(/%s/, prettyid(this.ns));
37            if (this.responseStatus[0] !== 200) {
38                return;
39            }
40            _this.className = _this.className.replace(/qsub__(not)?subscribed/g, '') +
41                              (s ? 'qsub__subscribed' : 'qsub__notsubscribed');
42            if (s) {
43                _this.ns = NS + ':';
44            }
45            _this.title     = s ? prettyid(NS) : LANG.plugins.quicksubscribe.subscribe;
46        };
47        if (s) ajax.runAJAX();
48
49        plugin_qsub__createOverlay(LANG.plugins.quicksubscribe.title, content, this);
50        return false;
51    }
52
53    for (var i = 0 ; i < links.length ; ++i) {
54        var link = links[i].parentNode;
55        link.className += ' ' + links[i].className;
56        link.ns = link.className.match(/qsubns__([^ ]+)/);
57        link.ns = link.ns ? link.ns[1] : (NS + ':');
58        link.title = links[i].title;
59        link.innerHTML = ' ';
60        addEvent(link, 'click', onclick);
61    }
62});
63
64function plugin_qsub__createOverlay(title, content, button) {
65    var div = document.createElement('div');
66    div.innerHTML = '<div class="title">' +
67                    '<img src="' + DOKU_BASE + 'lib/images/close.png">' +
68                    title + '</div>';
69
70    content.appendChild(document.createElement('hr'));
71    var more = document.createElement('p');
72    more.innerHTML = LANG.plugins.quicksubscribe.edit_subscr +
73                     ' <button class="button">' + LANG.plugins.quicksubscribe.edit_subscr_button +
74                     '</button>';
75
76    addEvent(more.lastChild, 'click', function () {document.location = button.href});
77    content.appendChild(more);
78    div.appendChild(content);
79
80    div.id = 'plugin_qsub_popup';
81
82    div.__close = function(event) {
83        div.style.display = 'none';
84    };
85
86    addEvent(div.firstChild.firstChild,'click',div.__close);
87
88    drag.attach(div, div.firstChild);
89    var dw = getElementsByClass('dokuwiki', document.body, 'div')[0];
90    dw.appendChild(div);
91    // FIXME
92    div.style.top  = '300px';
93    div.style.left = '500px';
94    return div;
95}
96