xref: /plugin/quicksubscribe/script.js (revision 346619c6effe61f90c30cedb8882d7f23d5f929b)
1jQuery(function () {
2    var $links = jQuery('img.qsub__link');
3    if (!$links.length) return;
4
5    /**
6     * Add additional stuff to the dialog
7     *
8     * @param $to {object} jQuery dialog object
9     * @param href {string}
10     */
11    function addmore($to, href) {
12        var $more = jQuery(document.createElement('div'));
13        $more.addClass('more');
14        $more.html(
15                '<p>' + LANG.plugins.quicksubscribe.edit_subscr +
16                    '<button class="button">' + LANG.plugins.quicksubscribe.edit_subscr_button +
17                    '</button></p>'
18            ).find('button').click(function () {
19                document.location = href;
20            });
21
22        $to.append($more);
23    }
24
25    /**
26     * Handles the whole click processing
27     *
28     * @param e
29     * @returns {boolean}
30     */
31    function clickhandler(e) {
32        // remove any existing popup
33        jQuery('#plugin_qsub__popup').remove();
34
35        // create a new popup
36        var $overlay = jQuery(document.createElement('div'));
37        $overlay.attr({
38            id: 'plugin_qsub__popup',
39            title: LANG.plugins.quicksubscribe.title
40        });
41
42        var $link = jQuery(this);
43
44        if (jQuery(this).hasClass('qsub__notsubscribed')) {
45            // Handle Subscriptions
46
47            $overlay.html(
48                    '<p>' + LANG.plugins.quicksubscribe.subscr_in_progress + '</p>'
49                ).load(
50                DOKU_BASE + 'lib/exe/ajax.php',
51                {
52                    call: 'plugin_quicksubscribe',
53                    ns: JSINFO.namespace + ':',
54                    do: 'subscribe'
55                },
56                function (text, status) {
57                    if (status == 'success') {
58                        $link.addClass('qsub__subscribed');
59                        $link.removeClass('qsub__notsubscribed');
60                    }
61                    addmore($overlay, $link.attr('href'));
62                }
63            );
64        } else {
65            // Handle unsubscriptions
66
67            $overlay.html(
68                    '<p>' + LANG.plugins.quicksubscribe.is_subscr.replace(/%s/, this.title) +
69                        ' ' + LANG.plugins.quicksubscribe.del_subscr +
70                        '<button class="button">' +
71                        LANG.plugins.quicksubscribe.del_subscr_button +
72                        '</button>' + '</p>'
73                ).find('button').click(function () {
74                    $overlay.load(
75                        DOKU_BASE + 'lib/exe/ajax.php',
76                        {
77                            call: 'plugin_quicksubscribe',
78                            ns: $link.attr('data-ns'),
79                            do: 'unsubscribe'
80                        },
81                        function (text, status) {
82                            if (status == 'success') {
83                                $link.removeClass('qsub__subscribed');
84                                $link.addClass('qsub__notsubscribed');
85                            }
86                            addmore($overlay, $link.attr('href'));
87                        }
88                    );
89                });
90            addmore($overlay, $link.attr('href'));
91        }
92
93        // show the dialog
94        $overlay.dialog();
95        e.preventDefault();
96        e.stopPropagation();
97        return false;
98    }
99
100    // attach dialog creation to any quicksubscribe link
101    $links.each(function () {
102        var $img = jQuery(this);
103        var $link = $img.parent();
104
105        // copy attributes to surrounding link, then remove the inner image
106        $link.addClass($img.attr('class'));
107        $link.attr('title', $img.attr('title'));
108        $img.remove();
109
110        // attach namespace info to link
111        var ns = $link.attr('class').match(/qsubns__([^ ]+)/);
112        if (ns){
113            ns = ns[1];
114        }else{
115            ns = JSINFO.namespace + ':';
116        }
117        $link.attr('data-ns', ns);
118
119        // attach click handler
120        $link.click(clickhandler);
121    });
122});
123