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