{#
/**
 * Copyright (C) 2020 Xibo Signage Ltd
 *
 * Xibo - Digital Signage - http://www.xibo.org.uk
 *
 * This file is part of Xibo.
 *
 * Xibo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * Xibo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
 */
#}

<script type="text/javascript">

    // Runs after form opens
    function datasetticker_form_edit_open() {

        if($(this).find('form').data('formStep') === 1) {
            // If data set exists, set the drop-down to that value
            var dataSetId = $(this).find('form').data().elementOptions.dataSetId;

            if( dataSetId !== undefined) {
                $(this).find('#dataSetId').val(dataSetId);
            }
        } else if($(this).find('form').data('formStep') === 2) {
            formHelpers.setupCheckboxInputFields($(this).find('form'), '#useDuration', '.duration-fields');

            // Set clock type input field, using the helper ()
            formHelpers.setupObjectValueInputFields($(this), '#effect', ['.effect-controls'], ['none'], true);

            // Set order and filtering clauses fields, using the helper ()
            formHelpers.setupCheckboxInputFields($(this).find('form'), '#useOrderingClause', '.order-clause-field', '#orderClause');
            formHelpers.setupCheckboxInputFields($(this).find('form'), '#useFilteringClause', '.filter-clause-field', '#filterClause');

            // Configure the query for sorting and filtering
            formHelpers.configureQueryBuilder(this, datasetQueryBuilderTranslations);

            // Setup editors
            formHelpers.setupDualTypeTextArea(this, 'ta_text', !$(this).data('formEditorOnly'));
            
            // Initialise CKEditor for the noDataMessage
            formHelpers.setupDualTypeTextArea(this, 'noDataMessage');
        }
    }

    // Runs before form submit
    function datasetticker_form_edit_submit() {

        if($(this).find('form').data('formStep') === 2) {
            var form = $("#dataSetTickerForm2");

            // Clean the previous generated hidden dataSetColumns from the form
            form.find('input[name="dataSetColumnId[]"]').remove();

            // Add all the selected columns to the form as hidden input fields
            $($("#columnsIn").sortable('toArray')).each(function() {
                form.append('<input type="hidden" name="dataSetColumnId[]" value="' + this + '" />');
            });

            // Submit the form
            form.data("apply", true);
            
            formHelpers.updateCKEditor();
        }
    }

</script>