1jQuery(function () {
2
3    const url = DOKU_BASE + 'lib/exe/ajax.php';
4    const requestParams = {
5        'id':     JSINFO.id,
6        'sectok': JSINFO.sectok
7    };
8
9    const $actionButtons = jQuery('button.action_button');
10    const $taggedPages = jQuery('a.tagslist');
11
12    /**
13     * Trigger a backend action via AJAX and refresh page on success
14     *
15     * @param {object} params Required "call" is the DokuWiki event name, plus optional data object(s)
16     * @param callback Function to call on success, reload page by default
17     * @returns {*}
18     */
19    const callBackend = function(params, callback) {
20        callback = callback || function () {
21            location.reload();
22        };
23
24        const mergedParams = jQuery.extend(
25            {},
26            requestParams,
27            params
28        );
29
30        return jQuery.ajax({
31            url     : url,
32            data    : mergedParams,
33            type    : 'POST'
34        })
35            .done(jQuery.proxy(function(response) {
36                callback(response);
37            }, this))
38            .fail(jQuery.proxy(function(xhr) {
39                var msg = typeof xhr === 'string' ? xhr : xhr.responseText || xhr.statusText || 'Unknown error';
40                alert(msg);
41            }, this));
42    };
43
44    /**
45     * Translated modal buttons
46     *
47     * @type {*[]}
48     */
49    const actionDialogButtons = [
50        {
51            text: LANG.plugins.tagging.admin_confirm,
52            click: function () {
53                const actionData = actionDialog.dialog('option', 'data');
54                if (actionData.action === 'delete') {
55                    callBackend({call: 'plugin_tagging_delete', tagging: {tid: [actionData.tid]}});
56                } else if (actionData.action === 'rename') {
57                    const $renameForm = jQuery('#tagging__rename');
58                    $renameForm.submit();
59                }
60            }
61        },
62        {
63            text: LANG.plugins.tagging.admin_cancel,
64            click: function () {
65                actionDialog.dialog('close');
66            }
67        },
68    ];
69
70    /**
71     * Modal for further user interaction
72     *
73     * @type {jQuery}
74     */
75    const actionDialog = jQuery("#tagging__action-dialog").dialog({
76        autoOpen: false,
77        height: 400,
78        width: 500,
79        modal: true,
80        title: LANG.plugins.tagging.admin_change_tag,
81        buttons: actionDialogButtons,
82        close: function () {
83            actionDialog.html('');
84        },
85        open: function( event, ui ) {
86            actionDialogHtml();
87        }
88    });
89
90    /**
91     * Modal listing pages with a given tag
92     *
93     * @type {jQuery}
94     */
95    const taggedPagesDialog = jQuery("#tagging__taggedpages-dialog").dialog({
96        autoOpen: false,
97        height: 400,
98        width: 600,
99        modal: true,
100        title: LANG.plugins.tagging.admin_tagged_pages,
101        close: function () {
102            taggedPagesDialog.html('');
103        },
104        open: function( event, ui ) {
105            taggedPagesHtml();
106        }
107    });
108
109    /**
110     * Injects dialog contents that match the triggered action
111     */
112    const actionDialogHtml = function() {
113
114        const actionData = actionDialog.dialog('option', 'data');
115        let $renameForm;
116
117        if (actionData.action === 'delete') {
118            actionDialog.append('<h2>' + LANG.plugins.tagging.admin_delete + ' ' + actionData.tid + '</h2>');
119            actionDialog.append('<p>' + LANG.plugins.tagging.admin_sure + '</p>');
120        } else if (actionData.action === 'rename') {
121            actionDialog.append('<h2>' + LANG.plugins.tagging.admin_rename + ' ' + actionData.tid + '</h2>');
122            actionDialog.append('<p>' + LANG.plugins.tagging.admin_newtags + ' </p>');
123            actionDialog.append(
124                '<form id="tagging__rename">'
125                + '<input type="text" name="newtags" id="tagging__newtags" value="' + actionData.tid + '">'
126                + '</form>'
127            );
128
129            $renameForm = jQuery('#tagging__rename');
130            $renameForm.on('submit', function( event ) {
131                event.preventDefault();
132                const newValue = jQuery(this).find('#tagging__newtags').val();
133                callBackend({call: 'plugin_tagging_rename', tagging: {oldValue: actionData.tid, newValue: newValue} });
134            });
135        }
136        actionDialog.append('<p class="warning">' + LANG.plugins.tagging.admin_warning_all + '</p>');
137    };
138
139    /**
140     * Displays tagged pages
141     */
142    const taggedPagesHtml = function() {
143
144        const $data = taggedPagesDialog.dialog('option', 'data');
145        const callback = function(response) {
146            taggedPagesDialog.html(response)
147        };
148
149        callBackend({call: 'plugin_tagging_html_pages', tagging: {tid: $data['tid']}}, callback);
150    };
151
152    /**
153     * Action buttons open a dialog window
154     */
155    $actionButtons.click(function (e) {
156        e.preventDefault();
157        e.stopPropagation();
158
159        actionDialog.dialog('option', { data: jQuery(this).data() });
160        actionDialog.dialog('open');
161    });
162
163    /**
164     * Tag links open a dialog window
165     */
166    $taggedPages.click(function (e) {
167        e.preventDefault();
168        e.stopPropagation();
169
170        taggedPagesDialog.dialog('option', { data: jQuery(this).data() });
171        taggedPagesDialog.dialog('open');
172    });
173});
174