SchemaLayoutView.js 19.8 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
                addTerm: '[data-id="addTerm"]',
48
                showMoreLess: '[data-id="showMoreLess"]',
49
                showMoreLessTerm: '[data-id="showMoreLessTerm"]',
50
                addAssignTag: "[data-id='addAssignTag']",
51
                checkDeletedEntity: "[data-id='checkDeletedEntity']"
52 53 54 55
            },
            /** ui events hash */
            events: function() {
                var events = {};
56
                events["click " + this.ui.addTag] = 'checkedValue';
57
                events["click " + this.ui.addTerm] = 'checkedValue';
58
                events["click " + this.ui.addAssignTag] = 'checkedValue';
59 60 61 62 63 64 65 66 67 68 69 70
                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
                        });
                    }
                };
71
                events["click " + this.ui.showMoreLess] = function(e) {
72 73 74 75 76 77
                    this.$('.popover.popoverTag').hide();
                    $(e.currentTarget).parent().find("div.popover").show();
                    var positionContent = $(e.currentTarget).position();
                    positionContent.top = positionContent.top + 26;
                    positionContent.left = positionContent.left - 41;
                    $(e.currentTarget).parent().find("div.popover").css(positionContent);
78
                };
79 80 81 82 83 84 85 86 87
                events["click " + this.ui.showMoreLessTerm] = function(e) {
                    $(e.currentTarget).find('i').toggleClass('fa fa-angle-right fa fa-angle-up');
                    $(e.currentTarget).parents('.searchTerm').find('div.termTableBreadcrumb>div.showHideDiv').toggleClass('hide');
                    if ($(e.currentTarget).find('i').hasClass('fa-angle-right')) {
                        $(e.currentTarget).find('span').text('Show More');
                    } else {
                        $(e.currentTarget).find('span').text('Show less');
                    }
                };
88 89
                events["click " + this.ui.checkDeletedEntity] = 'onCheckDeletedEntity';

90 91 92 93 94 95 96
                return events;
            },
            /**
             * intialize a new SchemaTableLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
97
                _.extend(this, _.pick(options, 'guid', 'entityDefCollection', 'attribute', 'fetchCollection', 'enumDefCollection'));
98 99 100 101
                this.schemaCollection = new VSchemaList([], {});
                this.commonTableOptions = {
                    collection: this.schemaCollection,
                    includeFilter: false,
102
                    includePagination: true,
103
                    includePageSize: false,
104
                    includeFooterRecords: true,
105
                    includeOrderAbleColumns: false,
106
                    gridOpts: {
107
                        className: "table table-hover backgrid table-quickMenu",
108 109 110 111 112 113
                        emptyText: 'No records found!'
                    },
                    filterOpts: {},
                    paginatorOpts: {}
                };
                this.bindEvents();
114
                this.bradCrumbList = [];
115 116
            },
            bindEvents: function() {
117 118 119 120 121 122 123 124 125
                this.listenTo(this.schemaCollection, 'backgrid:selected', function(model, checked) {
                    if (checked === true) {
                        model.set("isEnable", true);
                    } else {
                        model.set("isEnable", false);
                    }
                    this.arr = [];
                    var that = this;
                    this.schemaCollection.find(function(item) {
126
                        var obj = item.toJSON();
127 128
                        if (item.get('isEnable')) {
                            that.arr.push({
129
                                id: obj.guid,
130
                                model: obj
131 132 133
                            });
                        }
                    });
134 135 136 137 138 139 140 141 142 143 144
                    if (this.arr.length > 0) {
                        if (Globals.taxonomy) {
                            this.$('.multiSelectTerm').show();
                        }
                        this.$('.multiSelectTag').show();
                    } else {
                        if (Globals.taxonomy) {
                            this.$('.multiSelectTerm').hide();
                        }
                        this.$('.multiSelectTag').hide();
                    }
145
                });
146 147
            },
            onRender: function() {
148 149 150 151 152 153 154 155 156 157 158
                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 });
159
                    if (defObj && defObj.get('options') && defObj.get('options').schemaAttributes) {
160 161 162 163 164 165 166 167 168
                        if (firstColumn) {
                            try {
                                var mapObj = JSON.parse(defObj.get('options').schemaAttributes);
                                that.schemaTableAttribute = _.pick(firstColumn.attributes, mapObj);
                            } catch (e) {}
                        }
                    }
                }
                _.each(this.attribute, function(obj) {
169 170 171 172 173
                    if (!Enums.entityStateReadOnly[obj.status]) {
                        that.activeObj.push(obj);
                        that.schemaCollection.push(obj);
                    } else if (Enums.entityStateReadOnly[obj.status]) {
                        that.deleteObj.push(obj);
174 175
                    }
                });
176 177 178 179 180 181 182 183 184
                $('body').click(function(e) {
                    var iconEvnt = e.target.nodeName;
                    if (that.$('.popoverContainer').length) {
                        if ($(e.target).hasClass('tagDetailPopover') || iconEvnt == "I") {
                            return;
                        }
                        that.$('.popover.popoverTag').hide();
                    }
                });
185 186
                if (this.schemaCollection.length === 0 && this.deleteObj.length) {
                    this.ui.checkDeletedEntity.find("input").prop('checked', true);
187
                    this.schemaCollection.fullCollection.reset(this.deleteObj, { silent: true });
188 189 190 191
                }
                if (this.activeObj.length === 0 && this.deleteObj.length === 0) {
                    this.ui.checkDeletedEntity.hide();
                }
192
                this.renderTableLayoutView();
193
            },
194
            showLoader: function() {
195
                this.$('.fontLoader').show();
196
                this.$('.tableOverlay').show()
197
            },
198 199
            hideLoader: function(argument) {
                this.$('.fontLoader').hide();
200
                this.$('.tableOverlay').hide();
201
            },
202
            renderTableLayoutView: function() {
203
                var that = this;
204
                require(['utils/TableLayout'], function(TableLayout) {
205 206
                    var columnCollection = Backgrid.Columns.extend({}),
                        columns = new columnCollection(that.getSchemaTableColumns());
207
                    that.RSchemaTableLayoutView.show(new TableLayout(_.extend({}, that.commonTableOptions, {
208
                        columns: columns
209
                    })));
210 211
                    that.$('.multiSelectTerm').hide();
                    that.$('.multiSelectTag').hide();
212 213 214 215 216 217
                    that.renderBreadcrumb();
                });
            },
            renderBreadcrumb: function() {
                var that = this;
                _.each(this.bradCrumbList, function(object) {
218 219 220 221
                    _.each(object.value, function(subObj) {
                        var scopeObject = that.$('[dataterm-id="' + object.scopeId + '"]').find('[dataterm-name="' + subObj.name + '"] .liContent');
                        CommonViewFunction.breadcrumbMaker({ urlList: subObj.valueUrl, scope: scopeObject });
                    });
222 223
                });
            },
224
            getSchemaTableColumns: function() {
225
                var that = this,
226 227 228 229 230 231 232
                    col = {
                        Check: {
                            name: "selected",
                            label: "",
                            cell: "select-row",
                            headerCell: "select-all"
                        }
233
                    }
234 235
                if (this.schemaTableAttribute) {
                    _.each(_.keys(this.schemaTableAttribute), function(key) {
236
                        if (key !== "position") {
237
                            col[key] = {
238 239
                                label: key,
                                cell: "html",
240 241
                                editable: false,
                                sortable: false,
242
                                className: "searchTableName",
243 244
                                formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                                    fromRaw: function(rawValue, model) {
245
                                        var value = model.get('attributes')[key];
246 247
                                        if (key === "name" && model.get('guid')) {
                                            var nameHtml = '<a href="#!/detailPage/' + model.get('guid') + '">' + value + '</a>';
248
                                            if (model.get('status') && Enums.entityStateReadOnly[model.get('status')]) {
249 250
                                                nameHtml += '<button type="button" title="Deleted" class="btn btn-atlasAction btn-atlas deleteBtn"><i class="fa fa-trash"></i></button>';
                                                return '<div class="readOnly readOnlyLink">' + nameHtml + '</div>';
251
                                            } else {
252
                                                return nameHtml;
253
                                            }
254
                                        } else {
255
                                            return value
256 257
                                        }
                                    }
258 259 260
                                })
                            };
                        }
261 262 263
                    });
                    col['tag'] = {
                        label: "Tags",
264 265 266
                        cell: "Html",
                        editable: false,
                        sortable: false,
267
                        className: 'searchTag',
268 269
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
270 271 272 273 274
                                var obj = model.toJSON();
                                if (obj.status && Enums.entityStateReadOnly[obj.status]) {
                                    return '<div class="readOnly">' + CommonViewFunction.tagForTable(obj); + '</div>';
                                } else {
                                    return CommonViewFunction.tagForTable(obj);
275
                                }
276 277
                            }
                        })
278
                    };
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
                    if (Globals.taxonomy) {
                        col['terms'] = {
                            label: "Terms",
                            cell: "Html",
                            editable: false,
                            sortable: false,
                            orderable: true,
                            className: 'searchTerm',
                            formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                                fromRaw: function(rawValue, model) {
                                    var obj = model.toJSON();
                                    var returnObject = CommonViewFunction.termTableBreadcrumbMaker(obj);
                                    if (returnObject.object) {
                                        that.bradCrumbList.push(returnObject.object);
                                    }
                                    if (obj.status && Enums.entityStateReadOnly[obj.status]) {
                                        return '<div class="readOnly">' + returnObject.html + '</div>';
                                    } else {
                                        return returnObject.html;
                                    }

                                }
                            })
                        };
                    }
                    return this.schemaCollection.constructor.getTableCols(col, this.schemaCollection);
305
                }
306

307
            },
308 309 310 311 312 313
            checkedValue: function(e) {
                if (e) {
                    e.stopPropagation();
                }
                var guid = "",
                    that = this;
314 315 316 317 318 319 320 321
                var multiSelectTag = $(e.currentTarget).hasClass('assignTag');
                if (multiSelectTag) {
                    if (this.arr && this.arr.length && multiSelectTag) {
                        that.addTagModalView(guid, this.arr);
                    } else {
                        guid = that.$(e.currentTarget).data("guid");
                        that.addTagModalView(guid);
                    }
322
                } else {
323 324 325 326 327 328
                    if (this.arr && this.arr.length) {
                        that.addTermModalView(guid, this.arr);
                    } else {
                        guid = that.$(e.currentTarget).data("guid");
                        that.addTermModalView(guid);
                    }
329 330
                }
            },
331
            addTagModalView: function(guid, multiple) {
332
                var that = this;
333
                var tagList = that.schemaCollection.find({ 'guid': guid });
334 335
                require(['views/tag/addTagModalView'], function(AddTagModalView) {
                    var view = new AddTagModalView({
336 337
                        guid: guid,
                        multiple: multiple,
338 339 340
                        tagList: _.map((tagList ? tagList.get('classifications') : []), function(obj) {
                            return obj.typeName;
                        }),
341 342
                        callback: function() {
                            that.fetchCollection();
343 344
                            that.arr = [];
                        },
345
                        hideLoader: that.hideLoader.bind(that),
346 347
                        showLoader: that.showLoader.bind(that),
                        enumDefCollection: that.enumDefCollection
348
                    });
349 350 351
                    // view.saveTagData = function() {
                    //override saveTagData function 
                    // }
352 353
                });
            },
354 355
            addTermModalView: function(guid, multiple) {

356 357 358 359 360
                var that = this;
                require([
                    'views/business_catalog/AddTermToEntityLayoutView',
                ], function(AddTermToEntityLayoutView) {
                    var view = new AddTermToEntityLayoutView({
361 362 363
                        guid: guid,
                        multiple: multiple,
                        callback: function(termName) {
364
                            that.fetchCollection();
365 366
                            that.arr = [];
                        },
367 368
                        hideLoader: that.hideLoader.bind(that),
                        showLoader: that.showLoader.bind(that)
369 370 371
                    });
                });
            },
372
            onClickTagCross: function(e) {
373
                var tagName = $(e.target).data("name"),
374
                    guid = $(e.target).data("guid"),
375
                    assetName = $(e.target).data("assetname"),
376
                    tagOrTerm = $(e.target).data("type"),
377
                    that = this;
378
                if (tagOrTerm === "term") {
379
                    var modal = CommonViewFunction.deleteTagModel({
380
                        msg: "<div class='ellipsis'>Remove: " + "<b>" + _.escape(tagName) + "</b> assignment from" + " " + "<b>" + assetName + " ?</b></div>",
381 382 383
                        titleMessage: Messages.removeTerm,
                        buttonText: "Remove"
                    });
384
                } else if (tagOrTerm === "tag") {
385
                    var modal = CommonViewFunction.deleteTagModel({
386
                        msg: "<div class='ellipsis'>Remove: " + "<b>" + _.escape(tagName) + "</b> assignment from" + " " + "<b>" + assetName + " ?</b></div>",
387 388 389 390
                        titleMessage: Messages.removeTag,
                        buttonText: "Remove"
                    });
                }
391 392 393 394 395 396 397 398
                if (modal) {
                    modal.on('ok', function() {
                        that.deleteTagData(e, tagOrTerm);
                    });
                    modal.on('closeModal', function() {
                        modal.trigger('cancel');
                    });
                }
399
            },
400
            deleteTagData: function(e, tagOrTerm) {
401
                var that = this,
402 403 404 405 406
                    tagName = $(e.target).data("name"),
                    guid = $(e.target).data("guid");
                CommonViewFunction.deleteTag({
                    'tagName': tagName,
                    'guid': guid,
407
                    'tagOrTerm': tagOrTerm,
408 409
                    showLoader: that.showLoader.bind(that),
                    hideLoader: that.hideLoader.bind(that),
410 411 412
                    callback: function() {
                        that.fetchCollection();
                    }
413
                });
414 415 416 417
            },
            onCheckDeletedEntity: function(e) {
                if (e.target.checked) {
                    if (this.deleteObj.length) {
418
                        this.schemaCollection.fullCollection.reset(this.activeObj.concat(this.deleteObj));
419 420
                    }
                } else {
421
                    this.schemaCollection.fullCollection.reset(this.activeObj)
422
                }
423 424 425
            }
        });
    return SchemaTableLayoutView;
426
});