{#
/*
 * Spring Signage Ltd - http://www.springsignage.com
 * Copyright (C) 2015-17 Spring Signage Ltd
 * (ticker-form-edit.twig)
 */
#}

{% extends "form-base.twig" %}
{% import "forms.twig" as forms %}

{% block formTitle %}
    {% if module.getOption("sourceId") == 2 %}
        {% set dataSetName = module.getExtra().dataSet.dataSet %}
        {% trans %}Edit Ticker: DataSet is {{ dataSetName }}{% endtrans %}
    {% else %}
        {% trans "Edit Ticker" %}
    {% endif %}
{% endblock %}

{% block formButtons %}
    {% trans "Cancel" %}, XiboDialogClose()
    {% trans "Apply" %}, XiboDialogApply("#tickerEditForm")
    {% trans "Save" %}, $("#tickerEditForm").submit()
{% endblock %}

{% block callBack %}dataSetTickerFormSetup{% endblock %}

{% block extra %}{{ module.getExtra()|json_encode|raw }}{% endblock %}

{% block formFieldActions %}
    [{
    "field": "useDuration",
    "trigger": "init",
    "value": false,
    "operation": "is:checked",
    "actions": {
    ".duration-fields": { "display": "none" }
    }
    },{
    "field": "useDuration",
    "trigger": "change",
    "value": false,
    "operation": "is:checked",
    "actions": {
    ".duration-fields": { "display": "none" }
    }
    },{
    "field": "useDuration",
    "trigger": "init",
    "value": true,
    "operation": "is:checked",
    "actions": {
    ".duration-fields": { "display": "block" }
    }
    },{
    "field": "useDuration",
    "trigger": "change",
    "value": true,
    "operation": "is:checked",
    "actions": {
    ".duration-fields": { "display": "block" }
    }
    }
    {% if module.getOption("sourceId") == 1 %}
    ,{
    "field": "overrideTemplate",
    "trigger": "init",
    "value": false,
    "operation": "is:checked",
    "actions": {
    ".template-override-controls": { "display": "none" },
    ".template-selector-control": { "display": "block" }
    }
    },{
    "field": "overrideTemplate",
    "trigger": "change",
    "value": false,
    "operation": "is:checked",
    "actions": {
    ".template-override-controls": { "display": "none" },
    ".template-selector-control": { "display": "block" }
    }
    },{
    "field": "overrideTemplate",
    "trigger": "init",
    "value": true,
    "operation": "is:checked",
    "actions": {
    ".template-override-controls": { "display": "block" },
    ".template-selector-control": { "display": "none" }
    }
    },{
    "field": "overrideTemplate",
    "trigger": "change",
    "value": true,
    "operation": "is:checked",
    "actions": {
    ".template-override-controls": { "display": "block" },
    ".template-selector-control": { "display": "none" }
    }
    }
    {% else %}
    ,{
        "field": "useOrderingClause",
        "trigger": "init",
        "value": false,
        "operation": "is:checked",
        "actions": {
        ".order-clause-field": { "display": "none" },
        "#orderClause": { "display": "block" }
        }
        },{
        "field": "useOrderingClause",
        "trigger": "change",
        "value": false,
        "operation": "is:checked",
        "actions": {
        ".order-clause-field": { "display": "none" },
        "#orderClause": { "display": "block" }
        }
        },{
        "field": "useOrderingClause",
        "trigger": "init",
        "value": true,
        "operation": "is:checked",
        "actions": {
        ".order-clause-field": { "display": "block" },
        "#orderClause": { "display": "none" }
        }
        },{
        "field": "useOrderingClause",
        "trigger": "change",
        "value": true,
        "operation": "is:checked",
        "actions": {
        ".order-clause-field": { "display": "block" },
        "#orderClause": { "display": "none" }
        }
        },{
        "field": "useFilteringClause",
        "trigger": "init",
        "value": false,
        "operation": "is:checked",
        "actions": {
        ".filter-clause-field": { "display": "none" },
        "#filterClause": { "display": "block" }
        }
        },{
        "field": "useFilteringClause",
        "trigger": "change",
        "value": false,
        "operation": "is:checked",
        "actions": {
        ".filter-clause-field": { "display": "none" },
        "#filterClause": { "display": "block" }
        }
        },{
        "field": "useFilteringClause",
        "trigger": "init",
        "value": true,
        "operation": "is:checked",
        "actions": {
        ".filter-clause-field": { "display": "block" },
        "#filterClause": { "display": "none" }
        }
        },{
        "field": "useFilteringClause",
        "trigger": "change",
        "value": true,
        "operation": "is:checked",
        "actions": {
        ".filter-clause-field": { "display": "block" },
        "#filterClause": { "display": "none" }
        }
        }
    {% endif %}
    ]
{% endblock %}

{% block formHtml %}
    <div class="row">
        <div class="col-md-12">
            <ul class="nav nav-tabs" role="tablist">
                <li class="active"><a href="#general" role="tab" data-toggle="tab"><span>{% trans "General" %}</span></a></li>
                <li><a href="#template" role="tab" data-toggle="tab"><span>{% trans "Apperance" %}</span></a></li>
                {% if module.getOption("sourceId") == 2 %}
                <li><a href="#orderCriteria" role="tab" data-toggle="tab"><span>{% trans "Order" %}</span></a></li>
                <li><a href="#filterCriteria" role="tab" data-toggle="tab"><span>{% trans "Filter" %}</span></a></li>
                {% endif %}
                <li><a href="#format" role="tab" data-toggle="tab"><span>{% trans "Format" %}</span></a></li>
                <li><a href="#advanced" role="tab" data-toggle="tab"><span>{% trans "Advanced" %}</span></a></li>
            </ul>
            <form id="tickerEditForm" class="XiboForm form-horizontal" method="put" action="{{ urlFor("module.widget.edit", {id: module.widget.widgetId}) }}">
                <div class="tab-content">

                {# Common Fields #}
                {% set title %}{% trans "Name" %}{% endset %}
                {% set helpText %}{% trans "An optional name for this widget" %}{% endset %}
                {% set fieldName %}{{ forms.input("name", title, module.getOption("name"), helpText) }}{% endset %}

                {% set title %}{% trans "Set a duration?" %}{% endset %}
                {% set helpText %}{% trans "Select to provide a specific duration for this Widget" %}{% endset %}
                {% set fieldUseDuration %}{{ forms.checkbox("useDuration", title, module.getUseDuration(), helpText) }}{% endset %}

                {% set title %}{% trans "Duration" %}{% endset %}
                {% set helpText %}{% trans "The duration in seconds this item should be displayed" %}{% endset %}
                {% set fieldDuration %}{{ forms.number("duration", title, module.getDuration(), helpText, "duration-fields", "required") }}{% endset %}

                {% set title %}{% trans "Effect" %}{% endset %}
                {% set helpText %}{% trans "Please select the effect that will be used to transition between items. Marquee effects are CPU intensive and may not be suitable for lower power displays." %}{% endset %}

                {% set showAll %}{% trans "Show all items" %}{% endset %}
                {% set showPaged %}{% trans "Paged" %}{% endset %}
                {% set marqueeLeft %}{% trans "Marquee Left" %}{% endset %}
                {% set marqueeRight %}{% trans "Marquee Right" %}{% endset %}
                {% set marqueeUp %}{% trans "Marquee Up" %}{% endset %}
                {% set marqueeDown %}{% trans "Marquee Down" %}{% endset %}
                {% set optionGroups = [
                    {id: "showAll", label: showAll},
                    {id: "showPaged", label: showPaged}
                ] %}

                {% set noneOption %}{% trans "No effect - all items are shown" %}{% endset %}
                {% set showAllOptions = [
                    { effectid: "none", effect: noneOption },
                    { effectid: "marqueeLeft", effect: marqueeLeft},
                    { effectid: "marqueeRight", effect: marqueeRight},
                    { effectid: "marqueeUp", effect: marqueeUp},
                    { effectid: "marqueeDown", effect: marqueeDown}
                ] %}

                {% set noTransitionOption %}{% trans "No transition" %}{% endset %}
                {% set fade %}{% trans "Fade" %}{% endset %}
                {% set fadeout %}{% trans "Fade Out" %}{% endset %}
                {% set scrollHorz %}{% trans "Scroll Horizontal" %}{% endset %}
                {% set scrollVert %}{% trans "Scroll Vertical" %}{% endset %}
                {% set flipHorz %}{% trans "Flip Horizontal" %}{% endset %}
                {% set flipVert %}{% trans "Flip Vertical" %}{% endset %}
                {% set shuffle %}{% trans "Shuffle" %}{% endset %}
                {% set tileSlide %}{% trans "Tile Slide" %}{% endset %}
                {% set tileBlind %}{% trans "Tile Blind" %}{% endset %}
                {% set showPagedOptions = [
                { effectid: "noTransition", effect: noTransitionOption },
                { effectid: "fade", effect: fade },
                { effectid: "fadeout", effect: fadeout},
                { effectid: "scrollHorz", effect: scrollHorz},
                { effectid: "scrollVert", effect: scrollVert},
                { effectid: "flipHorz", effect: flipHorz},
                { effectid: "flipVert", effect: flipVert},
                { effectid: "shuffle", effect: shuffle},
                { effectid: "tileSlide", effect: tileSlide},
                { effectid: "tileBlind", effect: tileBlind}
                ] %}
                {% set fieldEffect %}{{ forms.dropdown("effect", "single", title, module.getOption("effect"), {showAll: showAllOptions, showPaged: showPagedOptions}, "effectid", "effect", helpText, "l", "", "", "", "", optionGroups) }}{% endset %}

                {% set title %}{% trans "Speed" %}{% endset %}
                {% set helpText %}{% trans "The transition speed of the selected effect in milliseconds (normal = 1000) or the Marquee Speed in a low to high scale (normal = 1)." %}{% endset %}
                {% set fieldSpeed %}{{ forms.number("speed", title, module.getOption("speed", module.getOption("scrollSpeed")), helpText, "effect-controls") }}{% endset %}

                {% set title %}{% trans "Background Colour" %}{% endset %}
                {% set helpText %}{% trans "The selected effect works best with a background colour. Optionally add one here." %}{% endset %}
                {% set fieldBackgroundColor %}{{ forms.input("backgroundColor", title, module.getOption("backgroundColor"), helpText, "background-color-group") }}{% endset %}

                {% set title %}{% trans "Items per page" %}{% endset %}
                {% set helpText %}{% trans "If an effect has been selected from the General tab, how many pages should we split the items across? If you don't enter anything here 1 item will be put on each page." %}{% endset %}
                {% set fieldItemsPerPage %}{{ forms.number("itemsPerPage", title, module.getOption("itemsPerPage"), helpText, "effect-controls") }}{% endset %}

                {% set title %}{% trans "Update Interval (mins)" %}{% endset %}
                {% set helpText %}{% trans "Please enter the update interval in minutes. This should be kept as high as possible. For example, if the data will only change once per hour this could be set to 60." %}{% endset %}
                {% set fieldUpdateInterval %}{{ forms.number("updateInterval", title, module.getOption("updateInterval", 5), helpText, "", "required") }}{% endset %}

                {% set title %}{% trans "Duration is per item" %}{% endset %}
                {% set helpText %}{% trans "The duration specified is per item otherwise it is per feed." %}{% endset %}
                {% set fieldDurationIsPerItem %}{{ forms.checkbox("durationIsPerItem", title, module.getOption("durationIsPerItem"), helpText) }}{% endset %}

                {% set title %}{% trans "Show items side by side?" %}{% endset %}
                {% set helpText %}{% trans "Should items be shown side by side?" %}{% endset %}
                {% set fieldItemsSideBySide %}{{ forms.checkbox("itemsSideBySide", title, module.getOption("itemsSideBySide"), helpText) }}{% endset %}

                {% set helpText %}
                    {% trans "Enter the template. Please note that the background colour has automatically coloured to your layout background colour." %}
                    {% trans "The red rectangle reflects the size of the region you are editing." %}
                    {% trans "Shift+Enter will drop a single line. Enter alone starts a new paragraph." %}
                {% endset %}
                {% set fieldTemplate %}{{ forms.textarea("ta_text", "", module.getRawNode("template"), helpText, "template-override-controls", "", 10) }}{% endset %}

                {% set title %}{% trans "Optional Stylesheet" %}{% endset %}
                {% set fieldStyleSheet %}{{ forms.textarea("ta_css", "", module.getRawNode("css"), title, "template-override-controls", "", 10) }}{% endset %}

                {% set title %}{% trans "Optional JavaScript" %}{% endset %}
                {% set fieldJavaScript %}{{ forms.textarea("javaScript", "", module.getRawNode("javaScript"), title, "", "", 10) }}{% endset %}

                {% set helpText %}{% trans "A message to display when no data is returned from the source" %}{% endset %}
                {% set fieldNoDataMessage %}
                    <div class="pull-right">
                        <label class="col-sm-2 control-label" for="ckeditor_library_select">{% trans "Library" %}</label>
                        <div class="col-sm-10">
                            <select id="ckeditor_library_select"
                                    class="ckeditor_library_select form-control"
                                    data-linked-to="noDataMessage"
                                    data-search-url="{{ urlFor("library.search") }}"
                                    data-image-url="{{ urlFor("library.download") }}?preview=1">
                            </select>
                        </div>
                    </div>
                    {{ forms.textarea("noDataMessage", "", module.getRawNode("noDataMessage"), helpText, "", "", 5) }}
                {% endset %}

                {# Different fields for each type of Ticker #}
                {% if module.getOption("sourceId") == 1 %}
                    {# Source = Feed #}
                    <div class="tab-pane active" id="general">
                        {% set title %}{% trans "Feed URL" %}{% endset %}
                        {% set helpText %}{% trans "The Link for the RSS feed" %}{% endset %}
                        {{ forms.input("uri", title, module.getOption("uri")|url_decode, helpText, "feed-fields") }}

                        {{ fieldName }}
                        {{ fieldUseDuration }}
                        {{ fieldDuration }}
                        {{ fieldEffect }}
                    </div>
                    <div class="tab-pane" id="template">

                        {% set title %}{% trans "Template" %}{% endset %}
                        {% set helpText %}{% trans "Select the template you would like to apply. This can be overridden using the check box below." %}{% endset %}
                        {{ forms.dropdown("templateId", "single", title, module.getOption("templateId", "title-only"), module.templatesAvailable(), "id", "value", helptext, "template-selector-control selectPicker selectPickerWithImage", null, null, null, [{ name: "data-minimum-results-for-search", value: "Infinity" },{ name: "data-width", value: "100%" }], null, "image") }}

                        {% set title %}{% trans "Override the template?" %}{% endset %}
                        {% set helpText %}{% trans "Tick if you would like to override the template." %}{% endset %}
                        {{ forms.checkbox("overrideTemplate", title, module.getOption("overrideTemplate", 0), helpText) }}

                        <div class="template-override-controls">
                            <div class="col-sm-6">
                                <div class="well">
                                    <div class="text-center text-info">{% trans "Available Substitutions" %}</div>
                                    <ul id="TickerDataSetColumns">
                                        <li class="ckeditor_snippits" linkedto="ta_text">Name</li>
                                        <li class="ckeditor_snippits" linkedto="ta_text">Title</li>
                                        <li class="ckeditor_snippits" linkedto="ta_text">Description</li>
                                        <li class="ckeditor_snippits" linkedto="ta_text">Date</li>
                                        <li class="ckeditor_snippits" linkedto="ta_text">Content</li>
                                        <li class="ckeditor_snippits" linkedto="ta_text">Copyright</li>
                                        <li class="ckeditor_snippits" linkedto="ta_text">Link</li>
                                        <li class="ckeditor_snippits" linkedto="ta_text">PermaLink</li>
                                        <li class="ckeditor_snippits" linkedto="ta_text">Tag|Namespace</li>
                                    </ul>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group well">
                                    <label class="col-sm-2 control-label" for="ckeditor_library_select2">{% trans "Library" %}</label>
                                    <div class="col-sm-10">
                                        <select id="ckeditor_library_select2"
                                                class="ckeditor_library_select form-control"
                                                data-linked-to="ta_text"
                                                data-search-url="{{ urlFor("library.search") }}"
                                                data-image-url="{{ urlFor("library.download") }}?preview=1">
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>

                        {{ fieldTemplate }}
                        {{ fieldStyleSheet }}
                    </div>
                    <div class="tab-pane" id="format">
                        {{ fieldSpeed }}

                        {% set title %}{% trans "Text direction" %}{% endset %}
                        {% set helpText %}{% trans "Which direction does the text in the feed use?" %}{% endset %}
                        {% set leftToRight %}{% trans "Left to Right (LTR)" %}{% endset %}
                        {% set rightToLeft %}{% trans "Right to Left (RTL)" %}{% endset %}
                        {% set options = [
                        { textdirectionid: "ltr", textdirection: leftToRight },
                        { textdirectionid: "rtl", textdirection: rightToLeft }
                        ] %}
                        {{ forms.dropdown("textDirection", "single", title, module.getOption("textDirection", "ltr"), options, "textdirectionid", "textdirection", helpText) }}

                        {% set title %}{% trans "Number of Items" %}{% endset %}
                        {% set helpText %}{% trans "The Number of RSS items you want to display" %}{% endset %}
                        {{ forms.number("numItems", title, module.getOption("numItems"), helpText) }}

                        {{ fieldItemsPerPage }}

                        {% set title %}{% trans "Take items from the" %}{% endset %}
                        {% set helpText %}{% trans "Take the items from the beginning or the end of the list" %}{% endset %}
                        {% set startOfTheFeed %}{% trans "Start of the Feed" %}{% endset %}
                        {% set endOfTheFeed %}{% trans "End of the Feed" %}{% endset %}
                        {% set options = [
                        { takeitemsfromid: "start", takeitemsfrom: startOfTheFeed },
                        { takeitemsfromid: "end", takeitemsfrom: endOfTheFeed }
                        ] %}
                        {{ forms.dropdown("takeItemsFrom", "single", title, module.getOption("takeItemsFrom", "start"), options, "takeitemsfromid", "takeitemsfrom", helpText) }}

                        {{ fieldDurationIsPerItem }}

                        {% set title %}{% trans "Randomise?" %}{% endset %}
                        {% set helpText %}{% trans "Should the order of the feed be randomised? When enabled each time the Widget is shown the items will be randomly shuffled and displayed in a random order." %}{% endset %}
                        {{ forms.checkbox("randomiseItems", title, module.getOption("randomiseItems"), helpText) }}

                    </div>
                    <div class="tab-pane" id="advanced">
                        {{ fieldBackgroundColor }}
                        {{ fieldUpdateInterval }}

                        {% set title %}{% trans "Copyright" %}{% endset %}
                        {% set helpText %}{% trans "Copyright information to display as the last item in this feed. This can be styled with the #copyright CSS selector." %}{% endset %}
                        {{ forms.input("copyright", title, module.getOption("copyright"), helpText) }}

                        {{ fieldItemsSideBySide }}

                        {% set title %}{% trans "Date Format" %}{% endset %}
                        {% set manualUrl %}{{ helpService.address("advanced_dateFormat.html") }}{% endset %}
                        {% set helpText %}{% trans %}The format to apply to all dates returned by the ticker. See the <a href="{{ manualUrl }}">Manual</a> for allowed formats.{% endtrans %}{% endset %}
                        {{ forms.input("dateFormat", title, module.getOption("dateFormat"), helpText) }}

                        {% set title %}{% trans "Allowable Attributes" %}{% endset %}
                        {% set helpText %}{% trans "A comma separated list of attributes that should not be stripped from the incoming feed." %}{% endset %}
                        {{ forms.input("allowedAttributes", title, module.getOption("allowedAttributes"), helpText) }}

                        {% set title %}{% trans "Strip Tags" %}{% endset %}
                        {% set helpText %}{% trans "A comma separated list of HTML tags that should be stripped from the feed in addition to the default ones." %}{% endset %}
                        {{ forms.input("stripTags", title, module.getOption("stripTags"), helpText) }}

                        {% set title %}{% trans "Disable Date Sort" %}{% endset %}
                        {% set helpText %}{% trans "Should the date sort applied to the feed be disabled?" %}{% endset %}
                        {{ forms.checkbox("disableDateSort", title, module.getOption("disableDateSort"), helpText) }}

                        {{ fieldNoDataMessage }}

                        {{ fieldJavaScript }}
                    </div>
                {% else %}
                    {# Source = Dataset #}
                    <div class="tab-pane active" id="general">
                        {{ fieldName }}
                        {{ fieldUseDuration }}
                        {{ fieldDuration }}
                        {{ fieldEffect }}
                        {{ fieldSpeed }}
                    </div>
                    <div class="tab-pane" id="template">
                        <div class="col-sm-6">
                            <div class="well">
                                <div class="text-center text-info">{% trans "Available Substitutions" %}</div>
                                <ul id="TickerDataSetColumns">
                                    {% for column in module.dataSetColumns() %}
                                    <li class="ckeditor_snippits" linkedto="ta_text" datasetcolumnid="{{ column.dataSetColumnId }}">{{ column.heading }}</li>
                                    {% endfor %}
                                </ul>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group well">
                                <label class="col-sm-2 control-label" for="ckeditor_library_select3">{% trans "Library" %}</label>
                                <div class="col-sm-10">
                                    <select id="ckeditor_library_select3"
                                            class="ckeditor_library_select form-control"
                                            data-linked-to="ta_text"
                                            data-search-url="{{ urlFor("library.search") }}"
                                            data-image-url="{{ urlFor("library.download") }}?preview=1">
                                    </select>
                                </div>
                            </div>
                        </div>
                        {{ fieldTemplate }}
                        {{ fieldStyleSheet }}
                    </div>
                    <div class="tab-pane" id="orderCriteria">

                        {% set message %}{% trans "The DataSet results can be ordered by any column and set below. New fields can be added by selecting the plus icon at the end of the current row. Should a more complicated order be required the advanced checkbox can be selected to provide custom SQL syntax." %}{% endset %}
                        {{ forms.message(message) }}

                        <div id="orderClause">

                        </div>

                        {% set title %}{% trans "Use advanced order clause?" %}{% endset %}
                        {% set helpText %}{% trans "Provide a custom clause instead of using the clause builder above." %}{% endset %}
                        {{ forms.checkbox("useOrderingClause", title, module.getOption("useOrderingClause", 0), helpText) }}

                        {% set title %}{% trans "Order" %}{% endset %}
                        {% set helpText %}{% trans "Please enter a SQL clause for how this dataset should be ordered" %}{% endset %}
                        {{ forms.input("ordering", title, module.getOption("ordering"), helpText, "order-clause-field") }}

                    </div>
                    <div class="tab-pane" id="filterCriteria">
                        {% set message %}{% trans "The DataSet results can be filtered by any column and set below. New fields can be added by selecting the plus icon at the end of the current row. Should a more complicated filter be required the advanced checkbox can be selected to provide custom SQL syntax." %}{% endset %}
                        {{ forms.message(message) }}

                        <div id="filterClause">

                        </div>

                        {% set title %}{% trans "Use advanced filter clause?" %}{% endset %}
                        {% set helpText %}{% trans "Provide a custom clause instead of using the clause builder above." %}{% endset %}
                        {{ forms.checkbox("useFilteringClause", title, module.getOption("useFilteringClause", 0), helpText) }}

                        {% set title %}{% trans "Filter" %}{% endset %}
                        {% set helpText %}{% trans "Please enter a SQL clause to filter this DataSet." %}{% endset %}
                        {{ forms.input("filter", title, module.getOption("filter"), helpText, "filter-clause-field") }}
                    </div>
                    <div class="tab-pane" id="format">
                        {{ fieldItemsPerPage }}
                        {{ fieldItemsSideBySide }}
                    </div>
                    <div class="tab-pane" id="advanced">
                        {{ fieldBackgroundColor }}
                        {{ fieldDurationIsPerItem }}
                        {{ fieldUpdateInterval }}

                        {% set title %}{% trans "Lower Row Limit" %}{% endset %}
                        {% set helpText %}{% trans "Please enter the Lower Row Limit for this DataSet (enter 0 for no limit)" %}{% endset %}
                        {{ forms.number("lowerLimit", title, module.getOption("lowerLimit"), helpText) }}

                        {% set title %}{% trans "Upper Row Limit" %}{% endset %}
                        {% set helpText %}{% trans "Please enter the Upper Row Limit for this DataSet (enter 0 for no limit)" %}{% endset %}
                        {{ forms.number("upperLimit", title, module.getOption("upperLimit"), helpText) }}

                        {{ fieldNoDataMessage }}

                        {{ fieldJavaScript }}
                    </div>
                {% endif %}
                </div>
            </form>
        </div>
    </div>
{% endblock %}