xref: /plugin/structsection/script/main.js (revision 6a5d40b1bde36bd9bd330ee7361c92af8079e66a)
1jQuery(() => {
2    /**
3     * This removes unintended elements created by other plugins and broken for structsection
4     *
5     * Currently it removes the edit-button shown by edittable, which cannot work inside this plugin
6     *
7     * @returns {void}
8     */
9    function cleanOtherPlugins() {
10        jQuery('#plugin__structsection_output').find('.editbutton_table').remove();
11    }
12    cleanOtherPlugins();
13
14    jQuery('#plugin__structsection_output').on('submit', 'form.btn_secedit', function handleEdit(e) {
15        e.preventDefault();
16        e.stopPropagation();
17
18        const $self = jQuery(this).parent().prev().find('div.level2'); // fixme: too fragile!
19        const pid = JSINFO.id;
20        const field = $self.data('struct');
21
22        if (!pid) return;
23        if (!field) return;
24
25
26        // prepare the edit overlay
27        const $div = jQuery('<div class="struct_inlineditor"><form></form><div class="err"></div></div>');
28        const $form = $div.find('form');
29        const $errors = $div.find('div.err').hide();
30        const $save = jQuery('<button type="submit">Save</button>');
31        const $cancel = jQuery('<button>Cancel</button>');
32        $form.append(jQuery('<input type="hidden" name="pid">').val(pid));
33        $form.append(jQuery('<input type="hidden" name="field">').val(field));
34        $form.append('<input type="hidden" name="call" value="plugin_struct_inline_save">');
35        $form.append(jQuery('<div class="ctl">').append($save).append($cancel));
36
37        /**
38         * load the editor
39         */
40        jQuery.post(
41            `${DOKU_BASE}lib/exe/ajax.php`,
42            {
43                call: 'plugin_struct_inline_editor',
44                pid,
45                field,
46            },
47            (data) => {
48                if (!data) return; // we're done
49
50                $form.prepend(data);
51
52                // show
53                $self.closest('.dokuwiki').append($div);
54                $div.position({
55                    my: 'left top',
56                    at: 'left top',
57                    of: $self,
58                });
59
60                // attach entry handlers to the inline form
61                // EntryEditor($form);
62
63                // focus first input
64                $form.find('input, textarea').first().focus();
65            },
66        );
67
68        /**
69         * Save the data, then close the form
70         */
71        $form.submit((submitEvent) => {
72            submitEvent.preventDefault();
73            jQuery.post(
74                `${DOKU_BASE}lib/exe/ajax.php`,
75                $form.serialize(),
76            )
77                .done((data) => {
78                    // save succeeded display new value and close editor
79                    $self.html(data);
80                    $div.remove();
81                })
82                .fail((data) => {
83                    // something went wrong, display error
84                    $errors.text(data.responseText).show();
85                })
86            ;
87        });
88
89        /**
90         * Close the editor without saving
91         */
92        $cancel.click((clickEvent) => {
93            // unlock page
94            jQuery.post(
95                `${DOKU_BASE}lib/exe/ajax.php`,
96                {
97                    call: 'plugin_struct_inline_cancel',
98                    pid,
99                },
100            );
101
102            clickEvent.preventDefault();
103            $div.remove();
104        });
105    });
106});
107