xref: /plugin/addnewpage/script.js (revision 5187ea72224ec8e745b3f4d95a74aeb31375b6f3)
1jQuery(function () {
2    jQuery(".addnewpage form").each(function () {
3        let $form = jQuery(this);
4        let $ns = $form.find("[name='np_cat']");
5        let $title = $form.find("input[name='title']");
6        let $id = $form.find("input[name='id']");
7        let $submit = $form.find(':submit');
8
9        // disable submit unless something is in input or input is disabled
10        if ($title.attr('type') === 'text') {
11            $submit.attr('disabled', 'disabled');
12            $title.on('input', function () {
13                if ($title.val().length > 0) {
14                    $submit.removeAttr('disabled');
15                } else {
16                    $submit.attr('disabled', 'disabled');
17                }
18            });
19        }
20
21        // Change the form's page-ID field on submit
22        $form.on("submit", function () {
23            const PLACEHOLDER = "@INPUT@";
24
25            // Build the new page ID
26            let page_id = $ns.val();
27            let page_title = $title.val();
28
29            // Prevent subnamespace creation
30            if (!$title.data('createns')) {
31                page_title = page_title.replaceAll(':', '_');
32            }
33
34            if (page_id.indexOf(PLACEHOLDER) !== -1) {
35                // Process the placeholder
36                page_id = page_id.replaceAll(PLACEHOLDER, page_title);
37            } else if (page_title) {
38                // There is no placeholder, just append the user's input (if any)
39                page_id += ":" + page_title;
40            }
41
42            // Save the new page ID in the hidden form field
43            $id.val(page_id);
44
45            // Clean up the form vars, just to make the resultant URL a bit nicer
46            $ns.prop("disabled", true);
47            $title.prop("disabled", true);
48
49            return true;
50        });
51
52    });
53});
54