EntityUserDefineView.js 6.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**
 * 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',
20 21 22 23 24 25 26
    'backbone',
    'hbs!tmpl/entity/EntityUserDefineView_tmpl',
    'models/VEntity',
    'utils/Utils',
    'utils/Enums',
    'utils/Messages',
    'utils/CommonViewFunction',
27
], function(require, Backbone, EntityUserDefineView_tmpl, VEntity, Utils, Enums, Messages, CommonViewFunction) {
28
    'use strict';
29 30 31 32 33 34 35

    return Backbone.Marionette.LayoutView.extend({
        _viewName: 'EntityUserDefineView',
        template: EntityUserDefineView_tmpl,
        templateHelpers: function() {
            return {
                customAttibutes: this.customAttibutes,
36
                readOnlyEntity: this.readOnlyEntity,
37
                swapItem: this.swapItem,
38 39 40
                saveAttrItems: this.saveAttrItems,
                divId_1: this.dynamicId_1,
                divId_2: this.dynamicId_2
41 42 43 44 45
            };
        },
        ui: {
            addAttr: "[data-id='addAttr']",
            editAttr: "[data-id='editAttr']",
46
            saveAttrItems: "[data-id='saveAttrItems']"
47 48 49
        },
        events: function() {
            var events = {};
50
            events["click " + this.ui.addAttr] = 'onAddAttrClick';
51
            events["click " + this.ui.editAttr] = 'onEditAttrClick';
52
            events["click " + this.ui.saveAttrItems] = 'onEditAttrClick';
53 54 55
            return events;
        },
        initialize: function(options) {
56
            _.extend(this, _.pick(options, 'entity', 'customFilter'));
57
            this.userDefineAttr = this.entity && this.entity.customAttributes || [];
58 59
            this.initialCall = false;
            this.swapItem = false, this.saveAttrItems = false;
60
            this.readOnlyEntity = this.customFilter === undefined ? Enums.entityStateReadOnly[this.entity.status] : this.customFilter;
61
            this.entityModel = new VEntity(this.entity);
62 63
            this.dynamicId_1 = CommonViewFunction.getRandomIdAndAnchor();
            this.dynamicId_2 = CommonViewFunction.getRandomIdAndAnchor();
64 65
            this.generateTableFields();
        },
66
        onRender: function() {},
67 68
        renderEntityUserDefinedItems: function() {
            var that = this;
69

70
            require(['views/entity/EntityUserDefineItemView'], function(EntityUserDefineItemView) {
71
                that.itemView = new EntityUserDefineItemView({ items: that.customAttibutes });
72 73 74 75 76 77 78 79 80 81 82 83
                that.REntityUserDefinedItemView.show(that.itemView);
            });
        },
        bindEvents: {},
        addChildRegion: function() {
            this.addRegions({
                REntityUserDefinedItemView: "#r_entityUserDefinedItemView"
            });
            this.renderEntityUserDefinedItems();
        },
        onAddAttrClick: function() {
            this.swapItem = !this.swapItem;
84 85 86 87 88
            if (this.customFilter === undefined) {
                this.saveAttrItems = this.swapItem === true ? true : false;
            } else {
                this.saveAttrItems = false;
            }
89 90 91 92 93
            this.initialCall = true;
            this.render();
            if (this.swapItem === true) {
                this.addChildRegion();
            }
94 95 96 97 98 99 100 101 102 103 104
        },
        generateTableFields: function() {
            var that = this;
            this.customAttibutes = [];
            _.each(Object.keys(that.userDefineAttr), function(key, i) {
                that.customAttibutes.push({
                    key: key,
                    value: that.userDefineAttr[key]
                });
            });
        },
105
        onEditAttrClick: function() {
106
            this.initialCall = this.customAttibutes.length > 0 ? false : true;
107
            this.setAttributeModal(this.itemView);
108
        },
109 110 111
        structureAttributes: function(list) {
            var obj = {}
            list.map(function(o) {
112 113 114 115
                obj[o.key] = o.value;
            });
            return obj;
        },
116
        saveAttributes: function(list) {
117 118 119 120
            var that = this;
            var entityJson = that.entityModel.toJSON();
            var properties = that.structureAttributes(list);
            entityJson.customAttributes = properties;
121
            var payload = { entity: entityJson };
122 123 124 125
            that.entityModel.createOreditEntity({
                data: JSON.stringify(payload),
                type: 'POST',
                success: function() {
126
                    var msg = that.initialCall ? 'addSuccessMessage' : 'editSuccessMessage',
127
                        caption = "One or more user-defined propertie"; // 's' will be added in abbreviation function
128
                    that.customAttibutes = list;
129 130 131 132
                    if (list.length === 0) {
                        msg = 'removeSuccessMessage';
                        caption = "One or more existing user-defined propertie";
                    }
133
                    Utils.notifySuccess({
134
                        content: caption + Messages.getAbbreviationMsg(true, msg)
135
                    });
136 137
                    that.swapItem = false;
                    that.saveAttrItems = false;
138 139
                    that.render();
                },
140
                error: function(e) {
141
                    that.initialCall = false;
142 143 144
                    Utils.notifySuccess({
                        content: e.message
                    });
145
                    that.ui.saveAttrItems.attr("disabled", false);
146
                },
147
                complete: function() {
148 149
                    that.ui.saveAttrItems.attr("disabled", false);
                    that.initialCall = false;
150 151 152
                }
            });
        },
153
        setAttributeModal: function(itemView) {
154
            var self = this;
155 156 157 158
            this.ui.saveAttrItems.attr("disabled", true);
            var list = itemView.$el.find("[data-type]"),
                dataList = [];
            Array.prototype.push.apply(dataList, itemView.items);
159 160
            var field = CommonViewFunction.CheckDuplicateAndEmptyInput(list, dataList);
            if (field.validation && !field.hasDuplicate) {
161 162 163 164
                self.saveAttributes(itemView.items);
            } else {
                this.ui.saveAttrItems.attr("disabled", false);
            }
165 166
        }
    });
167
});