1/**
2 * Delete Page Button plugin scripts
3 *
4 * @copyright (c) 2020 Damien Regad
5 * @license GPLv2 or later (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
6 * @author  Damien Regad
7 */
8jQuery(function() {
9    // Get current template name, see action_plugin_deletepagebutton::addJsInfo()
10    // noinspection JSUnresolvedVariable
11    let template = JSINFO.deletepagebutton_template;
12
13    // jQuery selector for the Delete Page button
14    let selector;
15    switch (template) {
16        case 'bootstrap3':
17            selector = 'li.action a.deletepagebutton';
18            break;
19        // Default selector (from DokuWiki default template)
20        case 'dokuwiki':
21        default:
22            selector = '.deletepagebutton a';
23    }
24
25    let $button = jQuery(selector);
26    if ($button.length === 0) {
27        const urlGitHubNewIssue =
28            'https://github.com/dregad/dokuwiki-plugin-deletepagebutton/issues/new'
29            + "?labels=bug"
30            + "&title=Confirmation+dialog+not+working+with+" + template + "+template"
31            + "&body=Please+provide+sample+HTML+for+the+*Delete+Page*+button";
32        console.warn(
33            "DokuWiki DeletePageButton plugin: Template '" + template + "' "
34            + "is not fully supported (the Confirmation dialog will not work). "
35            + "Please report the problem by clicking the following URL "
36            + urlGitHubNewIssue
37        );
38        return;
39    }
40
41    $button.on('click', function(e) {
42        e.preventDefault();
43        let submit_url = this.href;
44        let $dialog = jQuery(
45            '<div><span>'
46            + LANG.plugins.deletepagebutton.confirm
47            + '</span></div>'
48        );
49        $dialog.dialog({
50            title: LANG.plugins.deletepagebutton.title,
51            resizable: true,
52            width: "auto",
53            height: "auto",
54            modal: true,
55            buttons: [
56                {
57                    text: LANG.plugins.deletepagebutton.btn_ok,
58                    click: function () {
59                        $dialog.dialog("close");
60                        console.log(submit_url);
61                        window.location.href = submit_url
62                    }
63                },
64                {
65                    text: LANG.plugins.deletepagebutton.btn_cancel,
66                    click: function () {
67                        $dialog.dialog("close");
68                    }
69                }
70            ],
71            close: function () {
72                // remove the dialog's HTML
73                jQuery(this).remove();
74                // Due to the preventDefault() call, the "Delete page" span
75                // remains active when the dialog is closed, so we need to
76                // manually remove focus from it.
77                document.activeElement.blur();
78            }
79        });
80    });
81});
82