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