1window.tablelayout = window.tablelayout || {};
2
3jQuery(function () {
4    'use strict';
5
6    /**
7     * Ensure that the current values are valid and trigger a preview
8     *
9     * @param {Event} event the submit form event
10     *
11     * @return {void}
12     */
13    function handleLayoutFormSubmit(event) {
14        event.preventDefault();
15        var $layoutcontainer = jQuery('#layoutcontainer');
16        var $layoutfield = jQuery('#dw__editform').find('input[name=tablelayout]');
17        var layout = window.tablelayout.initLayout($layoutfield.val());
18
19        // validation
20        var rowsHeaderSource = $layoutcontainer.find('select[name="rowsHeaderSource"] :selected').val();
21        var rowsVisible = parseInt($layoutcontainer.find('input[name="rowsVisible"]').val());
22        var float = $layoutcontainer.find('select[name="float"]').val();
23        var MAX_HEADER_ROWS = 10;
24        if (0 <= parseInt(rowsHeaderSource) && parseInt(rowsHeaderSource) <= MAX_HEADER_ROWS) {
25            layout.rowsHeaderSource = rowsHeaderSource;
26        } else {
27            layout.rowsHeaderSource = 'Auto';
28        }
29        if (!(rowsVisible && rowsVisible > 0)) {
30            delete layout.rowsVisible;
31        } else {
32            layout.rowsVisible = rowsVisible;
33        }
34        if (float && (float === 'left' || float === 'right' || float === 'center')) {
35            layout.float = float;
36        } else {
37            delete layout.float;
38        }
39        var tableSort = $layoutcontainer.find('input[name="tableSort"]').is(':checked');
40        layout.tableSort = tableSort;
41        var tableSearch = $layoutcontainer.find('input[name="tableSearch"]').is(':checked');
42        layout.tableSearch = tableSearch;
43        var tablePrint = $layoutcontainer.find('input[name="tablePrint"]').is(':checked');
44        layout.tablePrint = tablePrint;
45
46        $layoutfield.val(JSON.stringify(layout));
47        jQuery('#dw__editform').find('button[name="do[preview]"]').click();
48    }
49
50    /**
51     *
52     * @param {string} staticFormHTML the basic form html as returned by the server
53     *
54     * @return {void}
55     */
56    function initializeLayoutForm(staticFormHTML) {
57        var $layoutcontainer = jQuery('#layoutcontainer');
58        $layoutcontainer.html(staticFormHTML);
59        $layoutcontainer.find('fieldset legend').click(function () {
60            $layoutcontainer.find('fieldset').toggleClass('borderless');
61            $layoutcontainer.find('fieldset > div').slideToggle();
62        });
63        var $layoutfield = jQuery('#dw__editform').find('input[name=tablelayout]');
64        var layout = window.tablelayout.initLayout($layoutfield.val());
65        $layoutcontainer.find('select[name="rowsHeaderSource"]').val(layout.rowsHeaderSource);
66        if (layout.rowsHeaderSource && layout.rowsVisible) {
67            $layoutcontainer.find('input[name="rowsVisible"]').val(layout.rowsVisible);
68        }
69        if (layout.float) {
70            $layoutcontainer.find('select[name="float"]').val(layout.float);
71        }
72        if (typeof layout.tableSort !== 'undefined' && layout.tableSort === true) {
73            $layoutcontainer.find('input[name="tableSort"]').attr('checked', true);
74        }
75        if (typeof layout.tableSearch !== 'undefined' && layout.tableSearch === true) {
76            $layoutcontainer.find('input[name="tableSearch"]').attr('checked', true);
77        }
78        if (typeof layout.tablePrint !== 'undefined' && layout.tablePrint === true) {
79            $layoutcontainer.find('input[name="tablePrint"]').attr('checked', true);
80        }
81        $layoutcontainer.find('form').submit(handleLayoutFormSubmit);
82    }
83
84    if (!jQuery('#edittable__editor').length) {
85        return;
86    }
87    jQuery('#dw__editform').before('<div id="layoutcontainer">' + window.LANG.plugins.tablelayout.loading + '</div>');
88    jQuery.get(
89        window.DOKU_BASE + 'lib/exe/ajax.php',
90        {
91            call: 'plugin_tablelayout_form'
92        }
93    ).done(initializeLayoutForm).fail(function (jqXhr) {
94        var $layoutcontainer = jQuery('#layoutcontainer');
95        $layoutcontainer.html(jqXhr.responseText);
96    });
97});
98
99