1/**
2 * Rename dialog for end users
3 *
4 * @author Andreas Gohr <gohr@cosmocode.de>
5 */
6(function () {
7    if (!JSINFO || !JSINFO.move_renameokay) return;
8
9
10    // basic dialog template
11    const $dialog = jQuery(
12        '<div>' +
13        '<form>' +
14        '<label>' + LANG.plugins.move.newname + '<br>' +
15        '<input type="text" name="id" style="width:100%">' +
16        '</label>' +
17        '</form>' +
18        '</div>'
19    );
20
21    /**
22     * Executes the renaming based on the form contents
23     * @return {boolean}
24     */
25    const renameFN = function () {
26        const newid = $dialog.find('input[name=id]').val();
27        if (!newid) return false;
28
29        // remove buttons and show throbber
30        $dialog.html(
31            '<img src="' + DOKU_BASE + 'lib/images/throbber.gif" /> ' +
32            LANG.plugins.move.inprogress
33        );
34        $dialog.dialog('option', 'buttons', []);
35
36        // post the data
37        jQuery.post(
38            DOKU_BASE + 'lib/exe/ajax.php',
39            {
40                call: 'plugin_move_rename',
41                id: JSINFO.id,
42                newid: newid
43            },
44            // redirect or display error
45            function (result) {
46                if (result.error) {
47                    $dialog.html(result.error.msg);
48                } else {
49                    window.location.href = result.redirect_url;
50                }
51            }
52        );
53
54        return false;
55    };
56
57    /**
58     * Create the actual dialog modal and show it
59     */
60    const showDialog = function () {
61        $dialog.dialog({
62            title: LANG.plugins.move.rename + ' ' + JSINFO.id,
63            width: 800,
64            height: 200,
65            dialogClass: 'plugin_move_dialog',
66            modal: true,
67            buttons: [
68                {
69                    text: LANG.plugins.move.cancel,
70                    click: function () {
71                        $dialog.dialog("close");
72                    }
73                },
74                {
75                    text: LANG.plugins.move.rename,
76                    click: renameFN
77                }
78            ],
79            // remove HTML from DOM again
80            close: function () {
81                jQuery(this).remove();
82            }
83        });
84        $dialog.find('input[name=id]').val(JSINFO.id);
85        $dialog.find('form').submit(renameFN);
86    };
87
88    /**
89     * Bind an event handler as the first handler
90     *
91     * @param {jQuery} $owner
92     * @param {string} event
93     * @param {function} handler
94     * @link https://stackoverflow.com/a/4700103
95     */
96    const bindFirst = function ($owner, event, handler) {
97        $owner.unbind(event, handler);
98        $owner.bind(event, handler);
99
100        const events = jQuery._data($owner[0])['events'][event];
101        events.unshift(events.pop());
102
103        jQuery._data($owner[0])['events'][event] = events;
104    };
105
106
107    // attach handler to menu item
108    jQuery('.plugin_move_page')
109        .show()
110        .click(function (e) {
111            e.preventDefault();
112            showDialog();
113        });
114
115    // attach handler to mobile menu entry
116    const $mobileMenuOption = jQuery('form select[name=do] option[value=plugin_move]');
117    if ($mobileMenuOption.length === 1) {
118        bindFirst($mobileMenuOption.closest('select[name=do]'), 'change', function (e) {
119            const $select = jQuery(this);
120            if ($select.val() !== 'plugin_move') return;
121            e.preventDefault();
122            e.stopPropagation();
123            e.stopImmediatePropagation();
124            $select.val('');
125            showDialog();
126        });
127    }
128
129})();
130