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