1/**
2 * Page Buttons plugin script
3 *
4 * @copyright (c) 2020 Cody Ernesti
5 * @license GPLv2 or later (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
6 * @author  Cody Ernesti
7 *
8 *  Modified from: https://github.com/dregad/dokuwiki-plugin-deletepagebutton
9 *
10 *   Original license info:
11 *
12 * @copyright (c) 2020 Damien Regad
13 * @license GPLv2 or later (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14 * @author  Damien Regad
15 */
16jQuery(function() {
17    var usePrompt = 0;
18    if(JSINFO && JSINFO['plugin_pagebuttons']){
19        var usePrompt = JSINFO['plugin_pagebuttons']['usePrompt'];
20        var start = JSINFO['plugin_pagebuttons']['start'];
21        var useSlash = JSINFO['plugin_pagebuttons']['useslash'];
22        var urlSeparator = useSlash ? "/" : ":";
23    }
24
25    jQuery('.plugin_pagebuttons_deletepage').click(function(d) {
26        d.preventDefault();
27
28        var submit_url = this.href;
29        if(usePrompt){
30            var page = window.confirm(LANG.plugins.pagebuttons.delete_confirm);
31            if(page == null || page == ''){}
32            else{
33                window.location.href = submit_url;
34            }
35        }else{
36            var $dialog = jQuery(
37                '<div><span>'
38                + LANG.plugins.pagebuttons.delete_confirm
39                + '</span></div>'
40            );
41            $dialog.dialog({
42                title: LANG.plugins.pagebuttons.delete_title,
43                resizable: true,
44                width: "auto",
45                height: "auto",
46                modal: true,
47                buttons: [
48                    {
49                        text: LANG.plugins.pagebuttons.btn_ok,
50                        click: function () {
51                            $dialog.dialog("close");
52                            window.location.href = submit_url
53                        }
54                    },
55                    {
56                        text: LANG.plugins.pagebuttons.btn_cancel,
57                        click: function () {
58                            $dialog.dialog("close");
59                        }
60                    }
61                ],
62                close: function () {
63                    // remove the dialog's HTML
64                    jQuery(this).remove();
65                    // Due to the preventDefault() call, the "Delete page" span
66                    // remains active when the dialog is closed, so we need to
67                    // manually remove focus from it.
68                    document.activeElement.blur();
69                }
70            });
71        }
72    });
73
74    jQuery('.plugin_pagebuttons_newfolder').click(function(f) {
75        f.preventDefault();
76
77        var pre_url = useSlash
78            ? window.location.href.substring(0, window.location.href.indexOf(JSINFO['id'].replace(/:/g, '/'))) + JSINFO['namespace'].replace(/:/g, '/')
79            : window.location.href.substring(0, window.location.href.indexOf(JSINFO['id'])) + JSINFO['namespace'];
80
81        if(usePrompt){
82            var page = window.prompt(LANG.plugins.pagebuttons.newfolder_prompt);
83            if(page == null || page == ''){}
84            else{
85                var submit_url = pre_url + urlSeparator + page + urlSeparator + start + "&do=edit";
86                window.location.href = submit_url;
87            }
88        }else{
89            var $dialog = jQuery(
90                '<div><span>'
91                + LANG.plugins.pagebuttons.newfolder_prompt
92                + '<br /><input type="text" style="z-index:10000" name="new_folder_name"><br />'
93                + '</span></div>'
94            );
95            $dialog.dialog({
96                title: LANG.plugins.pagebuttons.newfolder_title,
97                resizable: true,
98                width: "auto",
99                height: "auto",
100                modal: true,
101                buttons: [
102                    {
103                        text: LANG.plugins.pagebuttons.btn_ok,
104                        click: function () {
105                            var folder = document.getElementsByName("new_folder_name")[0].value;
106                            $dialog.dialog("close");
107                            var submit_url = pre_url + urlSeparator + folder + urlSeparator + start + "&do=edit";
108                            window.location.href = submit_url
109                        }
110                    },
111                    {
112                        text: LANG.plugins.pagebuttons.btn_cancel,
113                        click: function () {
114                            $dialog.dialog("close");
115                        }
116                    }
117                ],
118                close: function () {
119                    // remove the dialog's HTML
120                    jQuery(this).remove();
121                    // Due to the preventDefault() call, the "Delete page" span
122                    // remains active when the dialog is closed, so we need to
123                    // manually remove focus from it.
124                    document.activeElement.blur();
125                }
126            });
127        }
128    });
129
130    jQuery('.plugin_pagebuttons_newpage').click(function(p) {
131        p.preventDefault();
132
133        var pre_url = useSlash
134            ? window.location.href.substring(0, window.location.href.indexOf(JSINFO['id'].replace(/:/g, '/'))) + JSINFO['namespace'].replace(/:/g, '/')
135            : window.location.href.substring(0, window.location.href.indexOf(JSINFO['id'])) + JSINFO['namespace'];
136
137        if(usePrompt){
138            var page = window.prompt(LANG.plugins.pagebuttons.newpage_prompt);
139            if(page == null || page == ''){}
140            else{
141                var submit_url = pre_url + urlSeparator + page + "&do=edit";
142                window.location.href = submit_url;
143            }
144        }else{
145            var $dialog = jQuery(
146                '<div><span>'
147                + LANG.plugins.pagebuttons.newpage_prompt
148                + '<br /><input type="text" style="z-index:10000" name="new_page_name"><br />'
149                + '</span></div>'
150            );
151            $dialog.dialog({
152                title: LANG.plugins.pagebuttons.newpage_title,
153                resizable: true,
154                width: "auto",
155                height: "auto",
156                modal: true,
157                buttons: [
158                    {
159                        text: LANG.plugins.pagebuttons.btn_ok,
160                        click: function () {
161                            var newpage = document.getElementsByName("new_page_name")[0].value;
162                            $dialog.dialog("close");
163                            var submit_url = pre_url + urlSeparator + newpage + "&do=edit";
164                            window.location.href = submit_url
165                        }
166                    },
167                    {
168                        text: LANG.plugins.pagebuttons.btn_cancel,
169                        click: function () {
170                            $dialog.dialog("close");
171                        }
172                    }
173                ],
174                close: function () {
175                    // remove the dialog's HTML
176                    jQuery(this).remove();
177                    // Due to the preventDefault() call, the "Delete page" span
178                    // remains active when the dialog is closed, so we need to
179                    // manually remove focus from it.
180                    document.activeElement.blur();
181                }
182            });
183        }
184    });
185});
186