EntityUserDefineItemView.js 3.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
define(['require',
    'backbone',
    'hbs!tmpl/entity/EntityUserDefineItemView_tmpl'

], function(require, Backbone, EntityUserDefineItemView_tmpl) {
    'use strict';

    return Backbone.Marionette.ItemView.extend({
        _viewName: 'EntityUserDefineItemView',

        template: EntityUserDefineItemView_tmpl,

        templateHelpers: function() {
            return {
                items: this.items
            };
        },

        /** Layout sub regions */
        regions: {},

        /** ui selector cache */
        ui: {
            itemKey: "[data-type='key']",
            itemValue: "[data-type='value']",
            addItem: "[data-id='addItem']",
44 45
            deleteItem: "[data-id='deleteItem']",
            charSupportMsg: "[data-id='charSupportMsg']"
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        },
        /** ui events hash */
        events: function() {
            var events = {};
            events['input ' + this.ui.itemKey] = 'onItemKeyChange';
            events['input ' + this.ui.itemValue] = 'onItemValueChange';
            events['click ' + this.ui.addItem] = 'onAddItemClick';
            events['click ' + this.ui.deleteItem] = 'onDeleteItemClick';
            return events;
        },

        /**
         * intialize a new GlobalExclusionComponentView Layout
         * @constructs
         */
        initialize: function(options) {
            if (options.items.length === 0) {
63
                this.items = [{ key: "", value: "" }];
64
            } else {
65
                this.items = options.items;
66 67
            }
        },
68
        onRender: function() {},
69 70
        onAddItemClick: function(e) {
            var el = e.currentTarget;
71
            this.items.splice(parseInt(el.dataset.index) + 1, 0, { key: "", value: "" });
72 73 74 75 76 77 78
            this.render();
        },
        onDeleteItemClick: function(e) {
            var el = e.currentTarget;
            this.items.splice(el.dataset.index, 1);
            this.render();
        },
79
        onItemKeyChange: function(e) {
80
            var el = e.currentTarget;
81 82
            this.handleCharSupport(el);
            if (!el.value.trim().includes(':')) {
83
                this.items[el.dataset.index].key = _.escape(el.value.trim());
84
            }
85
        },
86
        onItemValueChange: function(e) {
87
            var el = e.currentTarget;
88 89
            this.handleCharSupport(el);
            if (!el.value.trim().includes(':')) {
90
                this.items[el.dataset.index].value = _.escape(el.value.trim());
91 92 93 94 95 96 97 98 99 100
            }
        },
        handleCharSupport: function(el) {
            if (el.value.trim().includes(':')) {
                el.setAttribute('class', 'form-control errorClass');
                this.ui.charSupportMsg.html("These special character '(:)' are not supported.");
            } else {
                el.setAttribute('class', 'form-control');
                this.ui.charSupportMsg.html("");
            }
101 102 103
        }
    });

104
});