xref: /plugin/addnewpage/script.js (revision bd4513feecdef79f35b3c5d7d75f185ae8cedbef)
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            if (page_id.indexOf(PLACEHOLDER) !== -1) {
28                // Process the placeholder
29                page_id = page_id.replace(PLACEHOLDER, $title.val());
30            } else {
31                // There is no placeholder, just append the user's input
32                page_id += ":" + $title.val();
33            }
34
35            // Save the new page ID in the hidden form field
36            $id.val(page_id);
37
38            // Clean up the form vars, just to make the resultant URL a bit nicer
39            $ns.prop("disabled", true);
40            $title.prop("disabled", true);
41
42            return true;
43        });
44
45    });
46});
47