EntityLabelDefineView.js 8.05 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
    'backbone',
    'hbs!tmpl/entity/EntityLabelDefineView_tmpl',
    'models/VEntity',
    'utils/Utils',
    'utils/Messages',
    'utils/Enums',
26
    'utils/UrlLinks',
27
    'utils/CommonViewFunction',
28
], function(require, Backbone, EntityLabelDefineView_tmpl, VEntity, Utils, Messages, Enums, UrlLinks, CommonViewFunction) {
29
    'use strict';
30 31 32 33 34 35 36 37 38

    return Backbone.Marionette.LayoutView.extend({
        _viewName: 'REntityLabelDefineView',
        template: EntityLabelDefineView_tmpl,
        templateHelpers: function() {
            return {
                swapItem: this.swapItem,
                labels: this.labels,
                saveLabels: this.saveLabels,
39 40 41
                readOnlyEntity: this.readOnlyEntity,
                div_1: this.dynamicId_1,
                div_2: this.dynamicId_2
42 43 44 45 46
            };
        },
        ui: {
            addLabelOptions: "[data-id='addLabelOptions']",
            addLabels: "[data-id='addLabels']",
47 48
            saveLabels: "[data-id='saveLabels']",
            cancel: "[data-id='cancel']"
49 50 51 52 53 54
        },
        events: function() {
            var events = {};
            events["change " + this.ui.addLabelOptions] = 'onChangeLabelChange';
            events["click " + this.ui.addLabels] = 'handleBtnClick';
            events["click " + this.ui.saveLabels] = 'saveUserDefinedLabels';
55
            events["click " + this.ui.cancel] = 'onCancelClick';
56 57 58 59
            return events;
        },
        initialize: function(options) {
            var self = this;
60
            _.extend(this, _.pick(options, 'entity', 'customFilter', 'renderAuditTableLayoutView'));
61
            this.swapItem = false, this.saveLabels = false;
62
            this.readOnlyEntity = this.customFilter === undefined ? Enums.entityStateReadOnly[this.entity.status] : this.customFilter;
63 64
            this.entityModel = new VEntity(this.entity);
            this.labels = this.entity.labels || [];
65 66
            this.dynamicId_1 = CommonViewFunction.getRandomIdAndAnchor();
            this.dynamicId_2 = CommonViewFunction.getRandomIdAndAnchor();
67 68 69 70
        },
        onRender: function() {
            this.populateLabelOptions();
        },
71
        bindEvents: function() {},
72 73
        populateLabelOptions: function() {
            var that = this,
74
                str = this.labels.map(function(label) {
75
                    return "<option selected > " + _.escape(label) + " </option>";
76
                });
77
            this.ui.addLabelOptions.html(str);
78
            var getLabelData = function(data, selectedData) {
79
                if (data.suggestions.length) {
80 81 82 83 84 85 86 87 88 89 90 91
                    return _.map(data.suggestions, function(name, index) {
                        var findValue = _.find(selectedData, { id: name })
                        if (findValue) {
                            return findValue;
                        } else {
                            return {
                                id: name,
                                text: name
                            }
                        }
                    });
                } else {
92 93
                    var findValue = _.find(selectedData, { id: data.prefixString })
                    return findValue ? [findValue] : [];
94 95
                }
            };
96 97
            this.ui.addLabelOptions.select2({
                placeholder: "Select Label",
98
                allowClear: false,
99
                tags: true,
100
                multiple: true,
101 102 103 104 105 106
                ajax: {
                    url: UrlLinks.searchApiUrl('suggestions'),
                    dataType: 'json',
                    delay: 250,
                    data: function(params) {
                        return {
107
                            prefixString: params.term, // search term
108 109 110 111 112 113 114
                            fieldName: '__labels'
                        };
                    },
                    processResults: function(data, params) {
                        return { results: getLabelData(data, this.$element.select2("data")) };
                    },
                    cache: true
115
                },
116 117 118 119 120 121
                createTag: function(data) {
                    var found = _.find(this.$element.select2("data"), { id: data.term });
                    if (!found) {
                        return { id: data.term, text: data.term };
                    }
                },
122
                templateResult: this.formatResultSearch
123 124
            });
        },
125 126 127 128 129
        formatResultSearch: function(state) {
            if (!state.id) {
                return state.text;
            }
            if (!state.element) {
130
                return $("<span>Add<strong> '" + _.escape(state.text) + "'</strong></span>");
131
            }
132
        },
133
        onChangeLabelChange: function() {
134
            this.labels = this.ui.addLabelOptions.val();
135 136
        },
        handleBtnClick: function() {
137
            this.swapItem = !this.swapItem;
138 139 140 141 142
            if (this.customFilter === undefined) {
                this.saveLabels = this.swapItem === true ? true : false;
            } else {
                this.saveLabels = false;
            }
143 144
            this.render();
        },
145 146 147 148 149 150
        onCancelClick: function() {
            this.labels = this.entityModel.get("labels") || [];
            this.swapItem = false;
            this.saveLabels = false;
            this.render();
        },
151 152 153
        saveUserDefinedLabels: function() {
            var that = this;
            var entityJson = that.entityModel.toJSON();
154 155
            if (entityJson.labels !== undefined || this.labels.length !== 0) {
                var payload = this.labels;
156
                that.entityModel.saveEntityLabels(entityJson.guid, {
157 158 159
                    data: JSON.stringify(payload),
                    type: 'POST',
                    success: function() {
160
                        var msg = entityJson.labels === undefined ? 'addSuccessMessage' : 'editSuccessMessage',
161
                            caption = "One or more label";
162
                        if (payload.length === 0) {
163 164
                            msg = 'removeSuccessMessage';
                            caption = "One or more existing label";
165 166 167 168 169
                            that.entityModel.unset('labels');
                        } else {
                            that.entityModel.set('labels', payload);
                        }
                        Utils.notifySuccess({
170
                            content: caption + Messages.getAbbreviationMsg(true, msg)
171 172 173 174
                        });
                        that.swapItem = false;
                        that.saveLabels = false;
                        that.render();
175 176 177
                        if (that.renderAuditTableLayoutView) {
                            that.renderAuditTableLayoutView();
                        }
178
                    },
179 180
                    error: function(e) {
                        that.ui.saveLabels && that.ui.saveLabels.length > 0 && that.ui.saveLabels[0].setAttribute("disabled", false);
181 182 183 184
                        Utils.notifySuccess({
                            content: e.message
                        });
                    },
185 186
                    complete: function() {
                        that.ui.saveLabels && that.ui.saveLabels.length > 0 && that.ui.saveLabels[0].setAttribute("disabled", false);
187
                        that.render();
188
                    }
189 190
                });
            }
191 192
        }
    });
193
});