GlossaryDetailLayoutView.js 23.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 24
/**
 * 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/glossary/GlossaryDetailLayoutView_tmpl',
    'utils/Utils',
    'utils/Messages',
    'utils/Globals',
25 26 27
    'utils/CommonViewFunction',
    'collection/VGlossaryList'
], function(require, Backbone, GlossaryDetailLayoutViewTmpl, Utils, Messages, Globals, CommonViewFunction, VGlossaryList) {
28 29 30 31 32 33 34 35 36 37 38 39
    'use strict';

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

            template: GlossaryDetailLayoutViewTmpl,

            /** Layout sub regions */
            regions: {
                RSearchResultLayoutView: "#r_searchResultLayoutView",
40 41
                RTagTableLayoutView: "#r_tagTableLayoutView",
                RRelationLayoutView: "#r_relationLayoutView"
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
            },
            templateHelpers: function() {
                return {
                    isTermView: this.isTermView,
                    isCategoryView: this.isCategoryView
                };
            },

            /** ui selector cache */
            ui: {
                details: "[data-id='details']",
                editButton: "[data-id='editButton']",
                title: "[data-id='title']",
                shortDescription: "[data-id='shortDescription']",
                longDescription: "[data-id='longDescription']",
57

58 59 60 61
                categoryList: "[data-id='categoryList']",
                removeCategory: "[data-id='removeCategory']",
                categoryClick: "[data-id='categoryClick']",
                addCategory: "[data-id='addCategory']",
62

63 64 65
                termList: "[data-id='termList']",
                removeTerm: "[data-id='removeTerm']",
                termClick: "[data-id='termClick']",
66 67
                addTerm: "[data-id='addTerm']",

68 69 70 71
                tagList: "[data-id='tagListTerm']",
                removeTag: '[data-id="removeTagTerm"]',
                tagClick: '[data-id="tagClickTerm"]',
                addTag: '[data-id="addTagTerm"]',
72 73 74 75 76 77 78 79 80 81 82 83 84 85
            },
            /** ui events hash */
            events: function() {
                var events = {};
                events["click " + this.ui.categoryClick] = function(e) {
                    if (e.target.nodeName.toLocaleLowerCase() == "i") {
                        this.onClickRemoveAssociationBtn(e);
                    } else {
                        var guid = $(e.currentTarget).data('guid'),
                            categoryObj = _.find(this.data.categories, { "categoryGuid": guid });
                        this.glossary.selectedItem = { "type": "GlossaryCategory", "guid": guid, "model": categoryObj };
                        Utils.setUrl({
                            url: '#!/glossary/' + guid,
                            mergeBrowserUrl: false,
86
                            urlParams: { gType: "category", viewType: "category", fromView: "glossary" },
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                            trigger: true,
                            updateTabState: true
                        });
                    }
                };
                events["click " + this.ui.termClick] = function(e) {
                    if (e.target.nodeName.toLocaleLowerCase() == "i") {
                        this.onClickRemoveAssociationBtn(e);
                    } else {
                        var guid = $(e.currentTarget).data('guid'),
                            termObj = _.find(this.data.terms, { "termGuid": guid });
                        this.glossary.selectedItem = { "type": "GlossaryTerm", "guid": guid, "model": termObj };
                        Utils.setUrl({
                            url: '#!/glossary/' + guid,
                            mergeBrowserUrl: false,
102
                            urlParams: { gType: "term", viewType: "term", fromView: "glossary" },
103 104 105 106 107
                            trigger: true,
                            updateTabState: true
                        });
                    }
                };
108 109 110 111 112 113 114 115 116 117 118
                events["click " + this.ui.tagClick] = function(e) {
                    if (e.target.nodeName.toLocaleLowerCase() == "i") {
                        this.onClickTagCross(e);
                    } else {
                        Utils.setUrl({
                            url: '#!/tag/tagAttribute/' + e.currentTarget.textContent,
                            mergeBrowserUrl: false,
                            trigger: true
                        });
                    }
                };
119 120 121 122 123 124 125 126
                events["click " + this.ui.editButton] = function(e) {
                    var that = this,
                        model = this.glossaryCollection.fullCollection.get(this.guid);
                    if (this.isGlossaryView) {
                        CommonViewFunction.createEditGlossaryCategoryTerm({
                            "model": model,
                            "isGlossaryView": this.isGlossaryView,
                            "collection": this.glossaryCollection,
127 128 129
                            "callback": function(sModel) {
                                var data = sModel.toJSON();
                                model.set(data, { silent: true }); // update glossaryCollection
130
                                that.data = data;
131
                                that.renderDetails(that.data);
132
                                that.glossaryCollection.trigger("update:details", { isGlossaryUpdate: true });
133 134 135 136 137 138 139 140
                            }
                        });
                    } else {
                        CommonViewFunction.createEditGlossaryCategoryTerm({
                            "isTermView": this.isTermView,
                            "isCategoryView": this.isCategoryView,
                            "model": this.data,
                            "collection": this.glossaryCollection,
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
                            "callback": function(data) {
                                if (data.displayName != that.data.displayName) {
                                    var glossary = that.glossaryCollection.fullCollection.get(data.anchor.glossaryGuid);
                                    if (that.isTermView) {
                                        _.find(glossary.get('terms'), function(obj) {
                                            if (obj.termGuid == data.guid) {
                                                obj.displayText = data.displayName
                                            }
                                        });
                                    } else if (!data.parentCategory) {
                                        _.find(glossary.get('categories'), function(obj) {
                                            if (obj.categoryGuid == data.guid) {
                                                obj.displayText = data.displayName
                                            }
                                        });
                                    }
                                    that.glossaryCollection.trigger("update:details", { data: that.data });
                                }
                                that.data = data;
                                that.renderDetails(that.data);
161 162 163 164 165 166
                            }
                        });
                    }
                };
                events["click " + this.ui.addTerm] = 'onClickAddTermBtn';
                events["click " + this.ui.addCategory] = 'onClickAddCategoryBtn';
167
                events["click " + this.ui.addTag] = 'onClickAddTagBtn';
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
                return events;
            },
            /**
             * intialize a new GlossaryDetailLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
                _.extend(this, _.pick(options, 'guid', 'glossaryCollection', 'glossary', 'collection', 'typeHeaders', 'value', 'entityDefCollection', 'enumDefCollection', 'classificationDefCollection'));
                if (this.value && this.value.gType) {
                    if (this.value.gType == "category") {
                        this.isCategoryView = true;
                    } else if (this.value.gType == "term") {
                        this.isTermView = true;
                    } else {
                        this.isGlossaryView = true;
                    }
                }
185
                this.selectedTermAttribute = null;
186 187
            },
            onRender: function() {
188
                this.$('.fontLoader-relative').show();
189 190 191 192 193 194 195
                this.getData();
                this.bindEvents();
            },
            bindEvents: function() {
                var that = this;
            },
            getData: function() {
196 197 198
                if (this.isGlossaryView) {
                    if (this.glossaryCollection.fullCollection.length) {
                        this.data = this.glossaryCollection.fullCollection.get(this.guid).toJSON();
199
                        this.glossaryCollection.trigger("data:updated", $.extend(true, {}, this.data));
200 201 202
                        this.renderDetails(this.data);
                    } else {
                        this.listenTo(this.glossaryCollection.fullCollection, "reset ", function(skip) {
203 204
                            var foundGlossary = this.glossaryCollection.fullCollection.get(this.guid);
                            this.data = foundGlossary ? foundGlossary.toJSON() : null;
205
                            this.glossaryCollection.trigger("data:updated", $.extend(true, {}, this.data));
206 207 208 209 210 211 212 213 214 215
                            if (this.data == null) {
                                this.glossary.selectedItem = {};
                                Utils.setUrl({
                                    url: '#!/glossary',
                                    mergeBrowserUrl: false,
                                    urlParams: null,
                                    trigger: true,
                                    updateTabState: true
                                });
                            }
216 217 218
                            this.renderDetails(this.data);
                        }, this);
                    }
219
                } else {;
220 221 222 223 224 225 226 227 228 229
                    Utils.showTitleLoader(this.$('.page-title .fontLoader'), this.ui.details);
                    var getApiFunctionKey = "getCategory",
                        that = this;
                    if (this.isTermView) {
                        getApiFunctionKey = "getTerm";
                    }
                    this.glossaryCollection[getApiFunctionKey]({
                        "guid": this.guid,
                        "ajaxOptions": {
                            success: function(data) {
230 231 232
                                if (that.isDestroyed) {
                                    return;
                                }
233
                                if (that.isTermView) {
234 235 236 237 238 239
                                    var obj = {
                                        "guid": that.guid,
                                        "entityDefCollection": that.entityDefCollection,
                                        "typeHeaders": that.typeHeaders,
                                        "tagCollection": that.collection,
                                        "enumDefCollection": that.enumDefCollection,
240 241 242 243 244 245 246 247
                                        "classificationDefCollection": that.classificationDefCollection,
                                        "glossaryCollection": that.glossaryCollection,
                                        "getSelectedTermAttribute": function() {
                                            return that.selectedTermAttribute;
                                        },
                                        "setSelectedTermAttribute": function(val) {
                                            that.selectedTermAttribute = val;
                                        }
248 249 250
                                    }
                                    that.renderSearchResultLayoutView(obj);
                                    that.renderTagTableLayoutView(obj);
251
                                    that.renderRelationLayoutView(obj);
252 253
                                }
                                that.data = data;
254
                                that.glossaryCollection.trigger("data:updated", $.extend(true, {}, data));
255 256 257 258 259 260 261 262 263 264 265
                                that.glossary.selectedItem.model = data;
                                that.glossary.selectedItem.guid = data.guid;
                                that.renderDetails(data)
                            },
                            cust_error: function() {}
                        }
                    });
                }
            },
            renderDetails: function(data) {
                Utils.hideTitleLoader(this.$('.fontLoader'), this.ui.details);
266 267 268 269 270 271 272 273 274 275
                if (data) {
                    this.ui.title.text(data.displayName || data.displayText || data.qualifiedName);
                    this.ui.shortDescription.text(data.shortDescription);
                    this.ui.longDescription.text(data.longDescription);
                    this.generateCategories(data.categories);
                    this.generateTerm(data.terms);
                    this.generateTag(data.classifications);
                } else {
                    this.ui.title.text("No Data found");
                }
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            },
            generateCategories: function(data) {
                var that = this,
                    categories = "";
                _.each(data, function(val) {
                    var name = _.escape(val.displayText);
                    categories += '<span data-guid="' + val.categoryGuid + '"" class="btn btn-action btn-sm btn-icon btn-blue" title=' + _.escape(name) + ' data-id="categoryClick"><span>' + name + '</span><i class="fa fa-close" data-id="removeCategory" data-type="category" title="Remove Category"></i></span>';
                });
                this.ui.categoryList.find("span.btn").remove();
                this.ui.categoryList.prepend(categories);
            },
            generateTerm: function(data) {
                var that = this,
                    terms = "";
                _.each(data, function(val) {
                    var name = _.escape(val.displayText);
                    terms += '<span data-guid="' + val.termGuid + '"" class="btn btn-action btn-sm btn-icon btn-blue" title=' + _.escape(name) + ' data-id="termClick"><span>' + name + '</span><i class="fa fa-close" data-id="removeTerm" data-type="term" title="Remove Term"></i></span>';
                });
                this.ui.termList.find("span.btn").remove();
                this.ui.termList.prepend(terms);

            },
298 299 300 301
            generateTag: function(tagObject) {
                var that = this,
                    tagData = "";
                _.each(tagObject, function(val) {
302
                    tagData += '<span class="btn btn-action btn-sm btn-icon btn-blue" title=' + val.typeName + ' data-id="tagClickTerm"><span>' + val.typeName + '</span><i class="fa fa-close" data-id="removeTagTerm" data-type="tag" title="Remove Tag"></i></span>';
303 304 305 306
                });
                this.ui.tagList.find("span.btn").remove();
                this.ui.tagList.prepend(tagData);
            },
307 308 309
            onClickAddTermBtn: function(e) {
                var that = this;
                require(['views/glossary/AssignTermLayoutView'], function(AssignTermLayoutView) {
310 311 312 313 314 315 316 317 318 319 320
                    var glossary = that.glossaryCollection;
                    if (that.value && that.value.gId) {
                        var foundModel = that.glossaryCollection.find({ guid: that.value.gId });
                        if (foundModel) {
                            glossary = new VGlossaryList([foundModel.toJSON()], {
                                comparator: function(item) {
                                    return item.get("displayName");
                                }
                            });
                        }
                    }
321 322 323 324 325 326
                    var view = new AssignTermLayoutView({
                        categoryData: that.data,
                        isCategoryView: that.isCategoryView,
                        callback: function() {
                            that.getData();
                        },
327
                        glossaryCollection: glossary
328 329 330 331 332 333 334 335 336
                    });
                    view.modal.on('ok', function() {
                        that.hideLoader();
                    });
                });
            },
            onClickAddCategoryBtn: function(e) {
                var that = this;
                require(['views/glossary/AssignTermLayoutView'], function(AssignTermLayoutView) {
337 338 339 340 341 342 343 344 345 346 347
                    var glossary = that.glossaryCollection;
                    if (that.value && that.value.gId) {
                        var foundModel = that.glossaryCollection.find({ guid: that.value.gId });
                        if (foundModel) {
                            glossary = new VGlossaryList([foundModel.toJSON()], {
                                comparator: function(item) {
                                    return item.get("displayName");
                                }
                            });
                        }
                    }
348 349 350 351 352 353
                    var view = new AssignTermLayoutView({
                        termData: that.data,
                        isTermView: that.isTermView,
                        callback: function() {
                            that.getData();
                        },
354
                        glossaryCollection: glossary
355 356 357 358 359 360
                    });
                    view.modal.on('ok', function() {
                        that.hideLoader();
                    });
                });
            },
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
            onClickAddTagBtn: function(e) {
                var that = this;
                require(['views/tag/AddTagModalView'], function(AddTagModalView) {
                    var tagList = [];
                    _.map(that.data.classifications, function(obj) {
                        if (obj.entityGuid === that.guid) {
                            tagList.push(obj.typeName);
                        }
                    });
                    var view = new AddTagModalView({
                        guid: that.guid,
                        tagList: tagList,
                        callback: function() {
                            that.getData();
                        },
                        showLoader: that.showLoader.bind(that),
                        hideLoader: that.hideLoader.bind(that),
                        collection: that.classificationDefCollection,
                        enumDefCollection: that.enumDefCollection
                    });
                });
            },
            onClickTagCross: function(e) {
                var that = this,
                    tagName = $(e.currentTarget).text(),
                    termName = this.data.displayName;
                CommonViewFunction.deleteTag(_.extend({}, {
                    msg: "<div class='ellipsis'>Remove: " + "<b>" + _.escape(tagName) + "</b> assignment from" + " " + "<b>" + termName + "?</b></div>",
                    titleMessage: Messages.removeTag,
                    okText: "Remove",
                    showLoader: that.showLoader.bind(that),
                    hideLoader: that.hideLoader.bind(that),
                    tagName: tagName,
                    guid: that.guid,
                    callback: function() {
                        that.getData();
                    }
                }));
            },
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
            onClickRemoveAssociationBtn: function(e) {
                var $el = $(e.currentTarget),
                    guid = $el.data('guid'),
                    name = $el.text(),
                    that = this;
                CommonViewFunction.removeCategoryTermAssociation({
                    selectedGuid: guid,
                    model: that.data,
                    collection: that.glossaryCollection,
                    msg: "<div class='ellipsis'>Remove: " + "<b>" + _.escape(name) + "</b> assignment from" + " " + "<b>" + that.data.displayName + "?</b></div>",
                    titleMessage: Messages.glossary[that.isTermView ? "removeCategoryfromTerm" : "removeTermfromCategory"],
                    isCategoryView: that.isCategoryView,
                    isTermView: that.isTermView,
                    buttonText: "Remove",
                    showLoader: that.hideLoader.bind(that),
                    hideLoader: that.hideLoader.bind(that),
                    callback: function() {
                        that.getData();
                    }
                });
            },
            showLoader: function() {
                Utils.showTitleLoader(this.$('.page-title .fontLoader'), this.ui.details);
            },
            hideLoader: function() {
                Utils.hideTitleLoader(this.$('.page-title .fontLoader'), this.ui.details);
            },
427 428 429 430 431 432 433 434 435 436 437
            renderTagTableLayoutView: function(options) {
                var that = this;
                require(['views/tag/TagDetailTableLayoutView'], function(TagDetailTableLayoutView) {
                    that.RTagTableLayoutView.show(new TagDetailTableLayoutView(_.extend({}, options, {
                        "entityName": that.ui.title.text(),
                        "fetchCollection": that.getData.bind(that),
                        "entity": that.data
                    })));
                });
            },
            renderSearchResultLayoutView: function(options) {
438 439 440 441 442 443 444
                var that = this;
                require(['views/search/SearchResultLayoutView'], function(SearchResultLayoutView) {
                    var value = {
                        'tag': "PII",
                        'searchType': 'basic'
                    };
                    if (that.RSearchResultLayoutView) {
445 446
                        that.RSearchResultLayoutView.show(new SearchResultLayoutView(_.extend({}, options, {
                            "value": { "searchType": "basic", "term": that.data.qualifiedName },
447
                            "fromView": "glossary"
448
                        })));
449 450 451
                    }
                });
            },
452 453 454 455 456 457 458 459 460 461
            renderRelationLayoutView: function(options) {
                var that = this;
                require(['views/glossary/TermRelationAttributeLayoutView'], function(TermRelationAttributeLayoutView) {
                    that.RRelationLayoutView.show(new TermRelationAttributeLayoutView(_.extend({}, options, {
                        "entityName": that.ui.title.text(),
                        "fetchCollection": that.getData.bind(that),
                        "data": that.data
                    })));
                });
            },
462 463 464
        });
    return GlossaryDetailLayoutView;
});