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