SchemaLayoutView.js 14.1 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
/**
 * 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/schema/SchemaTableLayoutView_tmpl',
    'collection/VSchemaList',
    'utils/Utils',
24
    'utils/CommonViewFunction',
25
    'utils/Messages',
26 27 28 29
    'utils/Globals',
    'utils/Enums',
    'utils/UrlLinks'
], function(require, Backbone, SchemaTableLayoutViewTmpl, VSchemaList, Utils, CommonViewFunction, Messages, Globals, Enums, UrlLinks) {
30 31 32 33 34 35 36 37 38 39 40
    'use strict';

    var SchemaTableLayoutView = Backbone.Marionette.LayoutView.extend(
        /** @lends SchemaTableLayoutView */
        {
            _viewName: 'SchemaTableLayoutView',

            template: SchemaTableLayoutViewTmpl,

            /** Layout sub regions */
            regions: {
41
                RSchemaTableLayoutView: "#r_schemaTableLayoutView",
42 43 44 45 46
            },
            /** ui selector cache */
            ui: {
                tagClick: '[data-id="tagClick"]',
                addTag: "[data-id='addTag']",
47
                addAssignTag: "[data-id='addAssignTag']",
48
                checkDeletedEntity: "[data-id='checkDeletedEntity']"
49 50 51 52
            },
            /** ui events hash */
            events: function() {
                var events = {};
53 54
                events["click " + this.ui.addTag] = 'checkedValue';
                events["click " + this.ui.addAssignTag] = 'checkedValue';
55 56 57 58 59 60 61 62 63 64 65 66
                events["click " + this.ui.tagClick] = function(e) {
                    if (e.target.nodeName.toLocaleLowerCase() == "i") {
                        this.onClickTagCross(e);
                    } else {
                        var value = e.currentTarget.text;
                        Utils.setUrl({
                            url: '#!/tag/tagAttribute/' + value,
                            mergeBrowserUrl: false,
                            trigger: true
                        });
                    }
                };
67 68
                events["click " + this.ui.checkDeletedEntity] = 'onCheckDeletedEntity';

69 70 71 72 73 74 75
                return events;
            },
            /**
             * intialize a new SchemaTableLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
76
                _.extend(this, _.pick(options, 'guid', 'classificationDefCollection', 'entityDefCollection', 'attribute', 'fetchCollection', 'enumDefCollection'));
77 78 79 80
                this.schemaCollection = new VSchemaList([], {});
                this.commonTableOptions = {
                    collection: this.schemaCollection,
                    includeFilter: false,
81
                    includePagination: true,
82
                    includePageSize: false,
83
                    includeFooterRecords: true,
84
                    includeOrderAbleColumns: false,
85
                    gridOpts: {
86
                        className: "table table-hover backgrid table-quickMenu",
87 88 89 90 91 92
                        emptyText: 'No records found!'
                    },
                    filterOpts: {},
                    paginatorOpts: {}
                };
                this.bindEvents();
93
                this.bradCrumbList = [];
94 95
            },
            bindEvents: function() {
96
                var that = this;
97
                this.listenTo(this.schemaCollection, 'backgrid:selected', function(model, checked) {
98
                    this.arr = [];
99 100 101 102 103 104
                    if (checked === true) {
                        model.set("isEnable", true);
                    } else {
                        model.set("isEnable", false);
                    }
                    this.schemaCollection.find(function(item) {
105
                        var obj = item.toJSON();
106 107
                        if (item.get('isEnable')) {
                            that.arr.push({
108
                                id: obj.guid,
109
                                model: obj
110 111 112
                            });
                        }
                    });
113 114 115 116 117
                    if (this.arr.length > 0) {
                        this.$('.multiSelectTag').show();
                    } else {
                        this.$('.multiSelectTag').hide();
                    }
118
                });
119 120
            },
            onRender: function() {
121 122 123 124 125 126 127 128 129 130 131
                this.generateTableData();
            },
            generateTableData: function(checkedDelete) {
                var that = this,
                    newModel;
                this.activeObj = [];
                this.deleteObj = [];
                this.schemaTableAttribute = null;
                if (this.attribute && this.attribute[0]) {
                    var firstColumn = this.attribute[0],
                        defObj = that.entityDefCollection.fullCollection.find({ name: firstColumn.typeName });
132
                    if (defObj && defObj.get('options') && defObj.get('options').schemaAttributes) {
133 134 135 136 137 138 139 140 141
                        if (firstColumn) {
                            try {
                                var mapObj = JSON.parse(defObj.get('options').schemaAttributes);
                                that.schemaTableAttribute = _.pick(firstColumn.attributes, mapObj);
                            } catch (e) {}
                        }
                    }
                }
                _.each(this.attribute, function(obj) {
142 143 144 145 146
                    if (!Enums.entityStateReadOnly[obj.status]) {
                        that.activeObj.push(obj);
                        that.schemaCollection.push(obj);
                    } else if (Enums.entityStateReadOnly[obj.status]) {
                        that.deleteObj.push(obj);
147 148
                    }
                });
149 150
                if (this.schemaCollection.length === 0 && this.deleteObj.length) {
                    this.ui.checkDeletedEntity.find("input").prop('checked', true);
151
                    this.schemaCollection.fullCollection.reset(this.deleteObj);
152 153 154 155
                }
                if (this.activeObj.length === 0 && this.deleteObj.length === 0) {
                    this.ui.checkDeletedEntity.hide();
                }
156
                this.renderTableLayoutView();
157
            },
158
            showLoader: function() {
159
                this.$('.fontLoader').show();
160
                this.$('.tableOverlay').show()
161
            },
162 163
            hideLoader: function(argument) {
                this.$('.fontLoader').hide();
164
                this.$('.tableOverlay').hide();
165
            },
166
            renderTableLayoutView: function() {
167
                var that = this;
168
                require(['utils/TableLayout'], function(TableLayout) {
169 170
                    var columnCollection = Backgrid.Columns.extend({}),
                        columns = new columnCollection(that.getSchemaTableColumns());
171
                    that.RSchemaTableLayoutView.show(new TableLayout(_.extend({}, that.commonTableOptions, {
172
                        columns: columns
173
                    })));
174
                    that.$('.multiSelectTag').hide();
175 176
                    Utils.generatePopover({
                        el: that.$('[data-id="showMoreLess"]'),
177
                        contentClass: 'popover-tag-term',
178
                        viewFixedPopover: true,
179
                        popoverOptions: {
180
                            container: null,
181
                            content: function() {
182
                                return $(this).find('.popup-tag-term').children().clone();
183 184 185
                            }
                        }
                    });
186 187
                });
            },
188
            getSchemaTableColumns: function() {
189
                var that = this,
190 191 192 193 194 195 196
                    col = {
                        Check: {
                            name: "selected",
                            label: "",
                            cell: "select-row",
                            headerCell: "select-all"
                        }
197
                    }
198 199
                if (this.schemaTableAttribute) {
                    _.each(_.keys(this.schemaTableAttribute), function(key) {
200
                        if (key !== "position") {
201
                            col[key] = {
202 203
                                label: key,
                                cell: "html",
204 205
                                editable: false,
                                sortable: false,
206
                                className: "searchTableName",
207 208
                                formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                                    fromRaw: function(rawValue, model) {
209
                                        var value = model.get('attributes')[key];
210 211
                                        if (key === "name" && model.get('guid')) {
                                            var nameHtml = '<a href="#!/detailPage/' + model.get('guid') + '">' + value + '</a>';
212
                                            if (model.get('status') && Enums.entityStateReadOnly[model.get('status')]) {
213
                                                nameHtml += '<button type="button" title="Deleted" class="btn btn-action btn-md deleteBtn"><i class="fa fa-trash"></i></button>';
214
                                                return '<div class="readOnly readOnlyLink">' + nameHtml + '</div>';
215
                                            } else {
216
                                                return nameHtml;
217
                                            }
218
                                        } else {
219
                                            return value
220 221
                                        }
                                    }
222 223 224
                                })
                            };
                        }
225 226 227
                    });
                    col['tag'] = {
                        label: "Tags",
228 229 230
                        cell: "Html",
                        editable: false,
                        sortable: false,
231
                        className: 'searchTag',
232 233
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
234 235 236 237 238
                                var obj = model.toJSON();
                                if (obj.status && Enums.entityStateReadOnly[obj.status]) {
                                    return '<div class="readOnly">' + CommonViewFunction.tagForTable(obj); + '</div>';
                                } else {
                                    return CommonViewFunction.tagForTable(obj);
239
                                }
240 241
                            }
                        })
242
                    };
243
                    return this.schemaCollection.constructor.getTableCols(col, this.schemaCollection);
244
                }
245

246
            },
247 248 249 250 251
            checkedValue: function(e) {
                if (e) {
                    e.stopPropagation();
                }
                var guid = "",
252
                    that = this,
253 254 255
                    isTagMultiSelect = $(e.currentTarget).hasClass('multiSelectTag');
                if (isTagMultiSelect && this.arr && this.arr.length) {
                    that.addTagModalView(guid, this.arr);
256
                } else {
257 258
                    guid = that.$(e.currentTarget).data("guid");
                    that.addTagModalView(guid);
259 260
                }
            },
261
            addTagModalView: function(guid, multiple) {
262
                var that = this;
263
                var tagList = that.schemaCollection.find({ 'guid': guid });
264
                require(['views/tag/AddTagModalView'], function(AddTagModalView) {
265
                    var view = new AddTagModalView({
266 267
                        guid: guid,
                        multiple: multiple,
268 269 270
                        tagList: _.map((tagList ? tagList.get('classifications') : []), function(obj) {
                            return obj.typeName;
                        }),
271 272
                        callback: function() {
                            that.fetchCollection();
273 274
                            that.arr = [];
                        },
275
                        hideLoader: that.hideLoader.bind(that),
276
                        showLoader: that.showLoader.bind(that),
277
                        collection: that.classificationDefCollection,
278
                        enumDefCollection: that.enumDefCollection
279
                    });
280 281
                });
            },
282
            onClickTagCross: function(e) {
283 284
                var that = this,
                    tagName = $(e.target).data("name"),
285
                    guid = $(e.target).data("guid"),
286 287 288 289 290 291 292
                    assetName = $(e.target).data("assetname");
                CommonViewFunction.deleteTag({
                    tagName: tagName,
                    guid: guid,
                    msg: "<div class='ellipsis'>Remove: " + "<b>" + _.escape(tagName) + "</b> assignment from" + " " + "<b>" + assetName + " ?</b></div>",
                    titleMessage: Messages.removeTag,
                    okText: "Remove",
293 294
                    showLoader: that.showLoader.bind(that),
                    hideLoader: that.hideLoader.bind(that),
295 296 297
                    callback: function() {
                        that.fetchCollection();
                    }
298
                });
299 300 301 302
            },
            onCheckDeletedEntity: function(e) {
                if (e.target.checked) {
                    if (this.deleteObj.length) {
303
                        this.schemaCollection.fullCollection.reset(this.activeObj.concat(this.deleteObj));
304 305
                    }
                } else {
306
                    this.schemaCollection.fullCollection.reset(this.activeObj)
307
                }
308 309 310
            }
        });
    return SchemaTableLayoutView;
311
});