xref: /plugin/tagging/script/admin.js (revision 398bea75be4a398796c675b8bb46758fc5c018f8)
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
11    /**
12     * Trigger a backend action via AJAX and refresh page on success
13     *
14     * @param params
15     * @returns {*}
16     */
17    const callBackend = function(params) {
18        const mergedParams = jQuery.extend(
19            {},
20            requestParams,
21            params
22        );
23
24        return jQuery.ajax({
25            url     : url,
26            data    : mergedParams,
27            type    : 'POST'
28        })
29            .done(jQuery.proxy(function(response) {
30                location.reload();
31            }, this))
32            .fail(jQuery.proxy(function(xhr) {
33                var msg = typeof xhr === 'string' ? xhr : xhr.responseText || xhr.statusText || 'Unknown error';
34                alert(msg);
35            }, this));
36    };
37
38    /**
39     * Translated modal buttons
40     *
41     * @type {*[]}
42     */
43    const dialogButtons = [
44        {
45            text: LANG.plugins.tagging.admin_confirm,
46            click: function () {
47                const actionData = dialog.dialog('option', 'data');
48                if (actionData.action === 'delete') {
49                    callBackend({call: 'plugin_tagging_delete', tagging: {tid: [actionData.tid]}});
50                } else if (actionData.action === 'rename') {
51                    const $renameForm = jQuery('#tagging__rename');
52                    $renameForm.submit();
53                }
54            }
55        },
56        {
57            text: LANG.plugins.tagging.admin_cancel,
58            click: function () {
59                dialog.dialog('close');
60            }
61        },
62    ];
63
64    /**
65     * Modal for further user interaction
66     *
67     * @type {jQuery}
68     */
69    const dialog = jQuery("#tagging__action-dialog").dialog({
70        autoOpen: false,
71        height: 400,
72        width: 300,
73        modal: true,
74        buttons: dialogButtons,
75        close: function () {
76            dialog.html('');
77        },
78        open: function( event, ui ) {
79            dialogHtml();
80        }
81    });
82
83    /**
84     * Injects dialog contents that match the triggered action
85     */
86    const dialogHtml = function() {
87
88        const actionData = dialog.dialog('option', 'data');
89        let $renameForm;
90
91        if (actionData.action === 'delete') {
92            dialog.append('<h1>' + LANG.plugins.tagging.admin_delete + ' ' + actionData.tid + '</h1>');
93            dialog.append('<p>' + LANG.plugins.tagging.admin_sure + '</p>');
94        } else if (actionData.action === 'rename') {
95            dialog.append('<h1>' + LANG.plugins.tagging.admin_rename + ' ' + actionData.tid + '</h1>');
96            dialog.append('<p>' + LANG.plugins.tagging.admin_newtags + ' </p>');
97            dialog.append('<form id="tagging__rename"><input type="text" name="newtags" id="tagging__newtags"></form>');
98
99            $renameForm = jQuery('#tagging__rename');
100            $renameForm.on('submit', function( event ) {
101                event.preventDefault();
102                const newValue = jQuery(this).find('#tagging__newtags').val();
103                callBackend({call: 'plugin_tagging_rename', tagging: {oldValue: actionData.tid, newValue: newValue} });
104            });
105        }
106        dialog.append('<p class="warning">' + LANG.plugins.tagging.admin_warning_all + '</p>');
107    };
108
109    /**
110     * Action buttons open a dialog window
111     */
112    $actionButtons.click(function (e) {
113        e.preventDefault();
114        e.stopPropagation();
115
116        dialog.dialog('option', { data: jQuery(this).data() });
117        dialog.dialog('open');
118    });
119});
120