xref: /plugin/tagging/script/admin.js (revision 7c96ae8703e61e9d1ecf0723b4774a33517f8261)
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 params
16     * @returns {*}
17     */
18    const callBackend = function(params) {
19        const mergedParams = jQuery.extend(
20            {},
21            requestParams,
22            params
23        );
24
25        return jQuery.ajax({
26            url     : url,
27            data    : mergedParams,
28            type    : 'POST'
29        })
30            .done(jQuery.proxy(function(response) {
31                location.reload();
32            }, this))
33            .fail(jQuery.proxy(function(xhr) {
34                var msg = typeof xhr === 'string' ? xhr : xhr.responseText || xhr.statusText || 'Unknown error';
35                alert(msg);
36            }, this));
37    };
38
39    /**
40     * Translated modal buttons
41     *
42     * @type {*[]}
43     */
44    const actionDialogButtons = [
45        {
46            text: LANG.plugins.tagging.admin_confirm,
47            click: function () {
48                const actionData = actionDialog.dialog('option', 'data');
49                if (actionData.action === 'delete') {
50                    callBackend({call: 'plugin_tagging_delete', tagging: {tid: [actionData.tid]}});
51                } else if (actionData.action === 'rename') {
52                    const $renameForm = jQuery('#tagging__rename');
53                    $renameForm.submit();
54                }
55            }
56        },
57        {
58            text: LANG.plugins.tagging.admin_cancel,
59            click: function () {
60                actionDialog.dialog('close');
61            }
62        },
63    ];
64
65    /**
66     * Modal for further user interaction
67     *
68     * @type {jQuery}
69     */
70    const actionDialog = jQuery("#tagging__action-dialog").dialog({
71        autoOpen: false,
72        height: 400,
73        width: 300,
74        modal: true,
75        buttons: actionDialogButtons,
76        close: function () {
77            actionDialog.html('');
78        },
79        open: function( event, ui ) {
80            actionDialogHtml();
81        }
82    });
83
84    /**
85     * Modal listing pages with a given tag
86     *
87     * @type {jQuery}
88     */
89    const taggedPagesDialog = jQuery("#tagging__taggedpages-dialog").dialog({
90        autoOpen: false,
91        height: 400,
92        width: 400,
93        modal: true,
94        close: function () {
95            taggedPagesDialog.html('');
96        },
97        open: function( event, ui ) {
98            taggedPagesgHtml();
99        }
100    });
101
102    /**
103     * Injects dialog contents that match the triggered action
104     */
105    const actionDialogHtml = function() {
106
107        const actionData = actionDialog.dialog('option', 'data');
108        let $renameForm;
109
110        if (actionData.action === 'delete') {
111            actionDialog.append('<h1>' + LANG.plugins.tagging.admin_delete + ' ' + actionData.tid + '</h1>');
112            actionDialog.append('<p>' + LANG.plugins.tagging.admin_sure + '</p>');
113        } else if (actionData.action === 'rename') {
114            actionDialog.append('<h1>' + LANG.plugins.tagging.admin_rename + ' ' + actionData.tid + '</h1>');
115            actionDialog.append('<p>' + LANG.plugins.tagging.admin_newtags + ' </p>');
116            actionDialog.append('<form id="tagging__rename"><input type="text" name="newtags" id="tagging__newtags"></form>');
117
118            $renameForm = jQuery('#tagging__rename');
119            $renameForm.on('submit', function( event ) {
120                event.preventDefault();
121                const newValue = jQuery(this).find('#tagging__newtags').val();
122                callBackend({call: 'plugin_tagging_rename', tagging: {oldValue: actionData.tid, newValue: newValue} });
123            });
124        }
125        actionDialog.append('<p class="warning">' + LANG.plugins.tagging.admin_warning_all + '</p>');
126    };
127
128    /**
129     * Displays tagged pages
130     */
131    const taggedPagesgHtml = function() {
132
133        const data = taggedPagesDialog.dialog('option', 'data');
134        const pids = data.pids.split(/,\s*/);
135
136        taggedPagesDialog.append('<h1>Tagged pages</h1>');
137        taggedPagesDialog.append('<ul>');
138        pids.forEach(function (pid) {
139            taggedPagesDialog.append('<li>' + pid + '</li>');
140        });
141        taggedPagesDialog.append('</ul>');
142    };
143
144    /**
145     * Action buttons open a dialog window
146     */
147    $actionButtons.click(function (e) {
148        e.preventDefault();
149        e.stopPropagation();
150
151        actionDialog.dialog('option', { data: jQuery(this).data() });
152        actionDialog.dialog('open');
153    });
154
155    /**
156     * Tag links open a dialog window
157     */
158    $taggedPages.click(function (e) {
159        e.preventDefault();
160        e.stopPropagation();
161
162        taggedPagesDialog.dialog('option', { data: jQuery(this).data() });
163        taggedPagesDialog.dialog('open');
164    });
165});
166