SchemaLayoutView.js 21.3 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', 'referredEntities', '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 169 170 171 172 173 174 175 176 177
                        if (firstColumn) {
                            try {
                                var mapObj = JSON.parse(defObj.get('options').schemaAttributes);
                                that.schemaTableAttribute = _.pick(firstColumn.attributes, mapObj);
                            } catch (e) {}
                        }
                    }
                }
                _.each(this.attribute, function(obj) {
                    newModel = that.referredEntities[obj.guid];
                    if (newModel.attributes['position']) {
                        newModel['position'] = newModel.attributes['position'];
                    }
                    if (!Enums.entityStateReadOnly[newModel.status]) {
                        that.activeObj.push(newModel);
                        that.schemaCollection.push(newModel);
                    } else if (Enums.entityStateReadOnly[newModel.status]) {
                        that.deleteObj.push(newModel);
178 179
                    }
                });
180 181 182 183 184 185 186 187 188
                $('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();
                    }
                });
189 190 191 192 193 194 195
                if (this.schemaCollection.length === 0 && this.deleteObj.length) {
                    this.ui.checkDeletedEntity.find("input").prop('checked', true);
                    this.schemaCollection.reset(this.deleteObj, { silent: true });
                }
                if (this.activeObj.length === 0 && this.deleteObj.length === 0) {
                    this.ui.checkDeletedEntity.hide();
                }
196
                this.renderTableLayoutView();
197
            },
198
            showLoader: function() {
199
                this.$('.fontLoader').show();
200
                this.$('.tableOverlay').show()
201
            },
202 203
            hideLoader: function(argument) {
                this.$('.fontLoader').hide();
204
                this.$('.tableOverlay').hide();
205
            },
206
            renderTableLayoutView: function() {
207
                var that = this;
208
                require(['utils/TableLayout'], function(TableLayout) {
209
                    var columnCollection = Backgrid.Columns.extend({
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
                        // sortKey: "position",
                        // comparator: function(item) {
                        //     return item.get(this.sortKey) || 999;
                        // },
                        // setPositions: function() {
                        //     _.each(this.models, function(model, index) {
                        //         if (model.get('name') == "name") {
                        //             model.set("position", 2, { silent: true });
                        //             model.set("label", "Name");
                        //         } else if (model.get('name') == "description") {
                        //             model.set("position", 3, { silent: true });
                        //             model.set("label", "Description");
                        //         } else if (model.get('name') == "owner") {
                        //             model.set("position", 4, { silent: true });
                        //             model.set("label", "Owner");
                        //         }
                        //     });
                        //     return this;
                        // }
229
                    });
230
                    var columns = new columnCollection(that.getSchemaTableColumns());
231
                    //columns.setPositions().sort();
232
                    that.RSchemaTableLayoutView.show(new TableLayout(_.extend({}, that.commonTableOptions, {
233
                        columns: columns
234
                    })));
235 236
                    that.$('.multiSelectTerm').hide();
                    that.$('.multiSelectTag').hide();
237 238 239 240 241 242
                    that.renderBreadcrumb();
                });
            },
            renderBreadcrumb: function() {
                var that = this;
                _.each(this.bradCrumbList, function(object) {
243 244 245 246
                    _.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 });
                    });
247 248
                });
            },
249
            getSchemaTableColumns: function() {
250
                var that = this,
251 252 253 254 255 256 257
                    col = {
                        Check: {
                            name: "selected",
                            label: "",
                            cell: "select-row",
                            headerCell: "select-all"
                        }
258
                    }
259 260
                if (this.schemaTableAttribute) {
                    _.each(_.keys(this.schemaTableAttribute), function(key) {
261
                        if (key !== "position") {
262
                            col[key] = {
263 264
                                label: key,
                                cell: "html",
265 266
                                editable: false,
                                sortable: false,
267
                                className: "searchTableName",
268 269
                                formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                                    fromRaw: function(rawValue, model) {
270
                                        var value = model.get('attributes')[key];
271 272
                                        if (key === "name" && model.get('guid')) {
                                            var nameHtml = '<a href="#!/detailPage/' + model.get('guid') + '">' + value + '</a>';
273
                                            if (model.get('status') && Enums.entityStateReadOnly[model.get('status')]) {
274 275
                                                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>';
276
                                            } else {
277
                                                return nameHtml;
278
                                            }
279
                                        } else {
280
                                            return value
281 282
                                        }
                                    }
283 284 285
                                })
                            };
                        }
286 287 288
                    });
                    col['tag'] = {
                        label: "Tags",
289 290 291
                        cell: "Html",
                        editable: false,
                        sortable: false,
292
                        className: 'searchTag',
293 294
                        formatter: _.extend({}, Backgrid.CellFormatter.prototype, {
                            fromRaw: function(rawValue, model) {
295 296 297 298 299
                                var obj = model.toJSON();
                                if (obj.status && Enums.entityStateReadOnly[obj.status]) {
                                    return '<div class="readOnly">' + CommonViewFunction.tagForTable(obj); + '</div>';
                                } else {
                                    return CommonViewFunction.tagForTable(obj);
300
                                }
301 302
                            }
                        })
303
                    };
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
                    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);
330
                }
331

332
            },
333 334 335 336 337 338
            checkedValue: function(e) {
                if (e) {
                    e.stopPropagation();
                }
                var guid = "",
                    that = this;
339 340 341 342 343 344 345 346
                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);
                    }
347
                } else {
348 349 350 351 352 353
                    if (this.arr && this.arr.length) {
                        that.addTermModalView(guid, this.arr);
                    } else {
                        guid = that.$(e.currentTarget).data("guid");
                        that.addTermModalView(guid);
                    }
354 355
                }
            },
356
            addTagModalView: function(guid, multiple) {
357
                var that = this;
358
                var tagList = that.schemaCollection.find({ 'guid': guid });
359 360
                require(['views/tag/addTagModalView'], function(AddTagModalView) {
                    var view = new AddTagModalView({
361 362
                        guid: guid,
                        multiple: multiple,
363 364 365
                        tagList: _.map((tagList ? tagList.get('classifications') : []), function(obj) {
                            return obj.typeName;
                        }),
366 367
                        callback: function() {
                            that.fetchCollection();
368 369
                            that.arr = [];
                        },
370
                        hideLoader: that.hideLoader.bind(that),
371 372
                        showLoader: that.showLoader.bind(that),
                        enumDefCollection: that.enumDefCollection
373
                    });
374 375 376
                    // view.saveTagData = function() {
                    //override saveTagData function 
                    // }
377 378
                });
            },
379 380
            addTermModalView: function(guid, multiple) {

381 382 383 384 385
                var that = this;
                require([
                    'views/business_catalog/AddTermToEntityLayoutView',
                ], function(AddTermToEntityLayoutView) {
                    var view = new AddTermToEntityLayoutView({
386 387 388
                        guid: guid,
                        multiple: multiple,
                        callback: function(termName) {
389
                            that.fetchCollection();
390 391
                            that.arr = [];
                        },
392 393
                        hideLoader: that.hideLoader.bind(that),
                        showLoader: that.showLoader.bind(that)
394 395 396
                    });
                });
            },
397
            onClickTagCross: function(e) {
398
                var tagName = $(e.target).data("name"),
399
                    guid = $(e.target).data("guid"),
400
                    assetName = $(e.target).data("assetname"),
401
                    tagOrTerm = $(e.target).data("type"),
402
                    that = this;
403
                if (tagOrTerm === "term") {
404
                    var modal = CommonViewFunction.deleteTagModel({
405
                        msg: "<div class='ellipsis'>Remove: " + "<b>" + _.escape(tagName) + "</b> assignment from" + " " + "<b>" + assetName + " ?</b></div>",
406 407 408
                        titleMessage: Messages.removeTerm,
                        buttonText: "Remove"
                    });
409
                } else if (tagOrTerm === "tag") {
410
                    var modal = CommonViewFunction.deleteTagModel({
411
                        msg: "<div class='ellipsis'>Remove: " + "<b>" + _.escape(tagName) + "</b> assignment from" + " " + "<b>" + assetName + " ?</b></div>",
412 413 414 415
                        titleMessage: Messages.removeTag,
                        buttonText: "Remove"
                    });
                }
416 417 418 419 420 421 422 423
                if (modal) {
                    modal.on('ok', function() {
                        that.deleteTagData(e, tagOrTerm);
                    });
                    modal.on('closeModal', function() {
                        modal.trigger('cancel');
                    });
                }
424
            },
425
            deleteTagData: function(e, tagOrTerm) {
426
                var that = this,
427 428 429 430 431
                    tagName = $(e.target).data("name"),
                    guid = $(e.target).data("guid");
                CommonViewFunction.deleteTag({
                    'tagName': tagName,
                    'guid': guid,
432
                    'tagOrTerm': tagOrTerm,
433 434
                    showLoader: that.showLoader.bind(that),
                    hideLoader: that.hideLoader.bind(that),
435 436 437
                    callback: function() {
                        that.fetchCollection();
                    }
438
                });
439 440 441 442 443 444 445 446 447
            },
            onCheckDeletedEntity: function(e) {
                if (e.target.checked) {
                    if (this.deleteObj.length) {
                        this.schemaCollection.reset(this.activeObj.concat(this.deleteObj));
                    }
                } else {
                    this.schemaCollection.reset(this.activeObj);
                }
448 449 450 451
            }
        });
    return SchemaTableLayoutView;
});