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

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