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