xref: /plugin/struct/script/EntryEditor.js (revision 6822dd25a0794a243305ee8fe6a558b1bdb56211)
1/**
2 * Attaches all the special handlers to the entry edit form
3 *
4 * @param {jQuery} $form The form where all the handlers are attached
5 */
6var EntryEditor = function($form) {
7
8    /** counter for copied multi templates */
9    var copycount = 0;
10
11    /**
12     * hints
13     */
14    $form.find('.struct .hashint').tooltip();
15
16    /**
17     * Attach datepicker to date types, if lacking HTML5 support.
18     */
19    var ftypetext = function() { return this.type === 'text'; };
20    $form.find('input.struct_date').filter(ftypetext).datepicker({
21        dateFormat: 'yy-mm-dd',
22        changeYear: true,
23    });
24
25    /**
26     * Attach datepicker to datetype types, keeps time part.
27     * Only if browser does not support HTML5 datetime-local input.
28     */
29
30    $form.find('input.struct_datetime').filter(ftypetext).each(function (index, element) {
31        const $dtInput = jQuery(element);
32        $dtInput.val($dtInput.val().replace('T', ' '));
33        $dtInput.datepicker({
34            dateFormat: 'yy-mm-dd',
35            changeYear: true,
36            onSelect: function (date, inst) {
37                var $input = jQuery(this);
38                var both = inst.lastVal.split(' ', 2);
39                if (both.length === 2) {
40                    date += ' ' + both[1];
41                } else {
42                    date += ' 00:00';
43                }
44                $input.val(date);
45            }
46        });
47    });
48
49    /**
50     * Attach image dialog to image types
51     */
52    $form.find('button.struct_media').click(function () {
53        var input_id = jQuery(this).siblings('input').attr('id');
54        window.open(
55            DOKU_BASE + 'lib/exe/mediamanager.php' +
56            '?ns=' + encodeURIComponent(JSINFO['namespace']) +
57            '&edid=' + encodeURIComponent(input_id) +
58            '&onselect=insertStructMedia',
59            'mediaselect',
60            'width=750,height=500,left=20,top=20,scrollbars=yes,resizable=yes'); //
61    });
62
63    /**
64     * Autocomplete for single type
65     */
66    $form.find('input.struct_autocomplete').autocomplete({
67        ismulti: false,
68        source: function (request, cb) {
69            var name = jQuery(this.element[0]).closest('div.field').find('label').first().data('column');
70            var term = request.term;
71            if (this.options.ismulti) {
72                term = extractLast(term);
73            }
74            struct_ajax(name, cb, {search: term});
75        }
76    });
77
78    /**
79     * Autocomplete for multi type
80     */
81    $form.find('.multiwrap input.struct_autocomplete').autocomplete('option', {
82        ismulti: true,
83        focus: function () {
84            // prevent value inserted on focus
85            return false;
86        },
87        select: function (event, ui) {
88            var terms = split(this.value);
89            // remove the current input
90            terms.pop();
91            // add the selected item
92            terms.push(ui.item.value);
93            // add placeholder to get the comma-and-space at the end
94            terms.push("");
95            this.value = terms.join(", ");
96            return false;
97        }
98    });
99
100    /**
101     * Duplicate the elements in .newtemplate whenever any input in it changes
102     */
103    $form.find('.newtemplate').each(function () {
104        var $tplwrapper = jQuery(this);
105        var $tpl = $tplwrapper.children().clone(true, true);
106
107        $tplwrapper.on('change', 'input,textarea,select', function () {
108            if (jQuery(this).val() == '') return;
109
110            // prepare a new template and make sure all the IDs in it are unique
111            var $copy = $tpl.clone(true, true);
112            copycount++;
113            $copy.find('*[id]').each(function () {
114                this.id = this.id + '_' + copycount;
115            });
116
117            // move edited .multiwrap out of .newtemplate container
118            $tplwrapper.before(jQuery(this).closest('.multiwrap').detach());
119
120            // append the template
121            $tplwrapper.append($copy);
122        });
123    });
124
125    /**
126     * Toggle fieldsets in edit form and remeber in cookie
127     */
128    $form.find('.struct_entry_form fieldset legend').each(function () {
129        var $legend = jQuery(this);
130        var $fset = $legend.parent();
131
132        // reinit saved state from cookie
133        if (DokuCookie.getValue($fset.data('schema'))) {
134            $fset.toggleClass('closed');
135        }
136
137        // attach click handler
138
139        $legend.click(function () {
140            $fset.toggleClass('closed');
141            // remember setting in preference cookie
142            if ($fset.hasClass('closed')) {
143                DokuCookie.setValue($fset.data('schema'), 1);
144            } else {
145                DokuCookie.setValue($fset.data('schema'), '');
146            }
147        });
148    });
149
150};
151