GlossaryLayoutView.js 36.7 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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/**
 * 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/GlossaryLayoutView_tmpl',
    'utils/Utils',
    'utils/Messages',
    'utils/Globals',
    'utils/CommonViewFunction',
    'jstree'
], function(require, Backbone, GlossaryLayoutViewTmpl, Utils, Messages, Globals, CommonViewFunction) {
    'use strict';

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

            template: GlossaryLayoutViewTmpl,

            /** Layout sub regions */
            regions: {},
            templateHelpers: function() {
                return {
41 42
                    isAssignView: this.isAssignView,
                    isAssignAttributeRelationView: this.isAssignAttributeRelationView
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
                };
            },

            /** ui selector cache */
            ui: {
                createGlossary: "[data-id='createGlossary']",
                refreshGlossary: "[data-id='refreshGlossary']",
                searchTerm: "[data-id='searchTerm']",
                searchCategory: "[data-id='searchCategory']",
                glossaryView: 'input[name="glossaryView"]',
                termTree: "[data-id='termTree']",
                categoryTree: "[data-id='categoryTree']"
            },
            /** ui events hash */
            events: function() {
                var events = {};
                events["change " + this.ui.glossaryView] = 'glossaryViewToggle';
                events["click " + this.ui.createGlossary] = function(e) {
                    var that = this;
                    if (e) {
                        $(e.currentTarget).attr("disabled", "true");
                    }
                    CommonViewFunction.createEditGlossaryCategoryTerm({
                        isGlossaryView: true,
                        collection: this.glossaryCollection,
                        callback: function() {
                            that.ui.createGlossary.removeAttr("disabled");
                            that.getGlossary();
                        },
                        onModalClose: function() {
                            that.ui.createGlossary.removeAttr("disabled");
                        }
                    })
                };
                events["click " + this.ui.refreshGlossary] = 'getGlossary';
                events["keyup " + this.ui.searchTerm] = function() {
                    this.ui.termTree.jstree("search", this.ui.searchTerm.val());
                };
                events["keyup " + this.ui.searchCategory] = function() {
                    this.ui.categoryTree.jstree("search", this.ui.searchCategory.val());
                };
                return events;
            },
            /**
             * intialize a new GlossaryLayoutView Layout
             * @constructs
             */
            initialize: function(options) {
91
                _.extend(this, _.pick(options, 'guid', 'value', 'glossaryCollection', 'glossary', 'isAssignTermView', 'isAssignCategoryView', 'isAssignEntityView', 'isAssignAttributeRelationView'));
92
                this.viewType = "term";
93
                this.isAssignView = this.isAssignTermView || this.isAssignCategoryView || this.isAssignEntityView || this.isAssignAttributeRelationView;
94 95
                this.bindEvents();
                this.query = {
96 97
                    term: {},
                    category: {}
98
                };
99 100
                if (Utils.getUrlState.isGlossaryTab() && this.value && this.value.viewType) {
                    this.viewType = this.value.viewType;
101
                    this.query[this.viewType] = _.extend({}, this.value, { "guid": this.guid });
102
                }
103 104 105
            },
            bindEvents: function() {
                var that = this;
106
                this.listenTo(this.glossaryCollection.fullCollection, "reset add change", function(skip) {
107
                    this.generateTree();
108
                    this.setValues();
109
                }, this);
110 111 112 113 114 115 116 117 118 119 120 121 122
                this.listenTo(this.glossaryCollection, "update:details", function(options) {
                    var isGlossaryUpdate = options.isGlossaryUpdate;
                    if (isGlossaryUpdate) {
                        if (this.ui.termTree.jstree(true).refresh) {
                            this.ui.termTree.jstree(true).refresh();
                        }
                        if (this.ui.categoryTree.jstree(true).refresh) {
                            this.ui.categoryTree.jstree(true).refresh();
                        }
                    } else {
                        var $tree = this.ui[this.viewType == "term" ? "termTree" : "categoryTree"];
                        if ($tree.jstree(true).refresh) {
                            $tree.jstree(true).refresh();
123
                            this.setValues({ trigger: false });
124 125
                        }
                    }
126
                }, this);
127 128 129 130 131 132
                if (!this.isAssignView) {
                    $('body').on('click', '.termPopoverOptions li, .categoryPopoverOptions li', function(e) {
                        that.$('.termPopover,.categoryPopover').popover('hide');
                        that[$(this).find('a').data('fn')](e)
                    });
                }
133 134 135 136 137 138 139 140 141 142 143 144
            },
            onRender: function() {
                if (this.isAssignCategoryView) {
                    this.$('.category-view').show();
                    this.$('.term-view').hide();
                }
                if (this.isAssignView && this.glossaryCollection.fullCollection.length) {
                    this.generateTree();
                } else {
                    this.getGlossary();
                }
            },
145
            setValues: function(options) {
146 147
                if (this.viewType == "category") {
                    if (!this.ui.glossaryView.prop("checked")) {
148
                        this.ui.glossaryView.prop("checked", true).trigger("change", options);
149 150 151
                    }
                } else {
                    if (this.ui.glossaryView.prop("checked")) {
152
                        this.ui.glossaryView.prop("checked", false).trigger("change", options);
153 154 155
                    }
                }
            },
156
            glossaryViewToggle: function(e, options) {
157
                var that = this;
158 159 160 161 162 163 164 165 166
                if (e.currentTarget.checked) {
                    this.$('.category-view').show();
                    this.$('.term-view').hide();
                    this.viewType = "category";
                } else {
                    this.$('.term-view').show();
                    this.$('.category-view').hide();
                    this.viewType = "term";
                }
167
                var setDefaultSelector = function() {
168 169 170
                    if (!that.value) {
                        return;
                    }
171 172 173 174 175 176 177 178 179 180 181 182
                    var model = null;
                    if (that.value.gId) {
                        model = that.glossaryCollection.fullCollection.get(that.value.gId);
                    } else {
                        model = that.glossaryCollection.fullCollection.first();
                    }
                    model = model.toJSON ? model.toJSON() : model;
                    that.glossary.selectedItem = {
                        type: "Glossary",
                        guid: model.guid,
                        id: model.guid,
                        model: model,
183
                        text: model.displayName,
184 185 186
                        gType: "glossary"
                    }
                }
187
                if (Utils.getUrlState.isGlossaryTab()) {
188 189 190 191 192 193
                    var obj = this.query[this.viewType],
                        $tree = this.ui[(this.viewType == "term" ? "termTree" : "categoryTree")]
                    if (obj.guid) {
                        var node = $tree.jstree(true).get_node(obj.guid);
                        if (node) {
                            this.glossary.selectedItem = node.original;
194
                            $tree.jstree('activate_node', obj.guid);
195 196 197 198 199
                        }
                    } else {
                        setDefaultSelector();
                        $tree.jstree('activate_node', that.glossary.selectedItem.guid);
                    }
200
                    this.query[this.viewType] = _.extend(obj, _.pick(this.glossary.selectedItem, 'model', 'guid', 'gType', 'type'), { "viewType": this.viewType, "isNodeNotFoundAtLoad": this.query[this.viewType].isNodeNotFoundAtLoad });
201
                    var url = _.isEmpty(this.glossary.selectedItem) ? '#!/glossary' : '#!/glossary/' + this.glossary.selectedItem.guid;
202
                    Utils.setUrl({
203 204 205
                        "url": url,
                        "urlParams": _.extend({}, _.omit(obj, 'guid', 'model', 'type', 'isNodeNotFoundAtLoad')),
                        "mergeBrowserUrl": false,
206
                        "trigger": (options && !_.isUndefined(options.trigger) ? options.trigger : true),
207
                        "updateTabState": true
208
                    });
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
                }
            },
            getGlossary: function() {
                this.glossaryCollection.fetch({ reset: true });
            },
            generateCategoryData: function(options) {
                return _.map(options.data, function(obj) {
                    return {
                        "text": obj.displayText,
                        "icon": "fa fa-files-o",
                        "guid": obj.categoryGuid,
                        "id": obj.categoryGuid,
                        "glossaryId": options.node.glossaryId,
                        "glossaryName": options.node.glossaryName,
                        "model": obj,
                        "type": "GlossaryCategory",
225
                        "gType": "category",
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                        "children": true
                    }
                });
            },
            getCategory: function(options) {
                var that = this;
                this.glossaryCollection.getCategory({
                    "guid": options.node.guid,
                    "related": true,
                    "ajaxOptions": {
                        success: function(data) {
                            if (data && data.children) {
                                options.callback(that.generateCategoryData(_.extend({}, { "data": data.children }, options)));
                            } else {
                                options.callback([]);
                            }
                        },
                        cust_error: function() {
                            options.callback([]);
                        }
                    }
                });
            },
            generateData: function(opt) {
                var that = this,
                    type = opt.type;
252 253 254
                if (opt.type == this.viewType) {
                    this.query[opt.type].isNodeNotFoundAtLoad = true;
                }
255 256 257 258
                var getSelectedState = function(options) {
                    var objGuid = options.objGuid,
                        node = options.node,
                        index = options.index;
259 260 261 262 263
                    if (that.isAssignView) {
                        return {
                            'opened': true
                        }
                    } else if (!that.guid) {
264
                        that.query[that.viewType].isNodeNotFoundAtLoad = false;
265 266
                        var selectedItem = {
                            "type": "Glossary",
267 268
                            "gType": "glossary",
                            "model": that.glossaryCollection.fullCollection.first().toJSON()
269 270 271 272 273
                        };
                        selectedItem.text = selectedItem.model.displayName;
                        selectedItem.guid = selectedItem.model.guid;
                        if (index == 0 && selectedItem.guid == objGuid) {
                            that.glossary.selectedItem = selectedItem;
274
                            that.query[that.viewType].model = selectedItem.model;
275
                            that.query[that.viewType].type = selectedItem.type;
276 277 278 279 280 281 282
                            return {
                                'opened': true,
                                'selected': true
                            }
                        }
                    } else {
                        if (that.guid == objGuid) {
283
                            that.query[that.viewType].isNodeNotFoundAtLoad = false;
284
                            that.glossary.selectedItem = node
285
                            that.query[that.viewType].model = node.model;
286
                            that.query[that.viewType].type = node.type;
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                            return {
                                'opened': true,
                                'selected': true
                            }
                        }
                    }
                }
                return this.glossaryCollection.fullCollection.map(function(model, i) {
                    var obj = model.toJSON(),
                        parent = {
                            "text": obj.displayName,
                            "icon": "fa fa-folder-o",
                            "guid": obj.guid,
                            "id": obj.guid,
                            "model": obj,
                            "type": obj.typeName ? obj.typeName : "Glossary",
303
                            "gType": "glossary",
304 305 306 307 308 309 310 311 312 313 314 315 316
                            "children": []
                        }
                    parent.state = getSelectedState({
                        index: i,
                        node: parent,
                        objGuid: obj.guid
                    });

                    if (type == "category" && obj.categories) {
                        _.each(obj.categories, function(category) {
                            if (category.parentCategoryGuid) {
                                return;
                            }
317
                            var typeName = category.typeName || "GlossaryCategory",
318 319 320
                                guid = category.categoryGuid,
                                categoryObj = {
                                    "text": category.displayText,
321 322
                                    "type": typeName,
                                    "gType": "category",
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
                                    "guid": guid,
                                    "id": guid,
                                    "parent": obj,
                                    "glossaryId": obj.guid,
                                    "glossaryName": obj.displayName,
                                    "model": category,
                                    "children": true,
                                    "icon": "fa fa-files-o",
                                };
                            categoryObj.state = getSelectedState({
                                index: i,
                                node: categoryObj,
                                objGuid: guid
                            })
                            parent.children.push(categoryObj)
                        });
                    }
                    if (type == "term" && obj.terms) {
                        _.each(obj.terms, function(term) {
342
                            var typeName = term.typeName || "GlossaryTerm",
343 344 345
                                guid = term.termGuid,
                                termObj = {
                                    "text": term.displayText,
346 347
                                    "type": typeName,
                                    "gType": "term",
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
                                    "guid": guid,
                                    "id": guid,
                                    "parent": obj,
                                    "glossaryName": obj.displayName,
                                    "glossaryId": obj.guid,
                                    "model": term,
                                    "icon": "fa fa-file-o"
                                }
                            termObj.state = getSelectedState({
                                index: i,
                                node: termObj,
                                objGuid: guid
                            })
                            parent.children.push(termObj);
                        });
                    }
                    return parent;
                });
            },
            manualRender: function(options) {
368 369 370 371 372 373 374 375
                _.extend(this, _.omit(options, 'isTrigger'));
                if (this.value && this.value.viewType) {
                    this.viewType = this.value.viewType;
                }
                if (this.guid && this.value && ((this.value.fromView && this.value.fromView) || (this.value.updateView))) {
                    var $tree = this.ui[this.viewType == "term" ? "termTree" : "categoryTree"],
                        node = $tree.jstree(true).get_node(this.guid);
                    if (node) {
376
                        $tree.jstree('activate_node', this.guid, { skipTrigger: true });
377 378 379
                        delete this.value.fromView;
                        delete this.value.updateView;
                        this.glossary.selectedItem = node.original;
380
                        this.query[this.viewType] = _.extend({}, _.pick(this.glossary.selectedItem, 'model', 'guid', 'gType', 'type'), { "viewType": this.viewType });
381 382 383 384 385 386 387 388 389
                        Utils.setUrl({
                            url: '#!/glossary/' + this.guid,
                            urlParams: this.value,
                            mergeBrowserUrl: false,
                            trigger: false,
                            updateTabState: true
                        });
                        this.glossaryCollection.trigger("update:details", { isGlossaryUpdate: this.value.gType == "glossary" });
                    }
390 391
                } else {
                    this.setValues();
392 393 394 395
                }
                if (options.isTrigger) {
                    this.triggerUrl();
                }
396 397 398 399 400 401 402 403 404
            },
            generateTree: function() {
                var $termTree = this.ui.termTree,
                    $categoryTree = this.ui.categoryTree,
                    that = this,
                    getTreeConfig = function(options) {
                        return {
                            "plugins": ["search", "themes", "core", "wholerow", "sort", "conditionalselect"],
                            "conditionalselect": function(node) {
405 406 407 408
                                var obj = node && node.original && node.original.type;
                                if (!obj) {
                                    return;
                                }
409
                                if (that.isAssignView) {
410
                                    return obj != "Glossary" ? true : false;
411
                                } else {
412
                                    return obj != "NoAction" ? true : false;
413 414
                                }
                            },
415 416 417
                            "search": {
                                "show_only_matches": true
                            },
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
                            "core": {
                                "data": function(node, cb) {
                                    if (node.id === "#") {
                                        cb(that.generateData(options));
                                    } else {
                                        that.getCategory({ "node": node.original, "callback": cb });
                                    }
                                },
                                "themes": {
                                    "name": that.isAssignView ? "default" : "default-dark",
                                    "dots": true
                                },
                            }
                        }
                    },
433 434
                    treeLoaded = function(options) {
                        if (that.query[options.type].isNodeNotFoundAtLoad == true) {
435
                            var id = that.glossary.selectedItem.guid;
436
                            options.$el.jstree('activate_node', id);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
                        }
                    },
                    createAction = function(options) {
                        var $el = options.el,
                            type = options.type,
                            popoverClassName = type == "term" ? "termPopover" : "categoryPopover";
                        if (!that.isAssignView) {
                            var wholerowEl = $el.find("li[role='treeitem'] > .jstree-wholerow:not(:has(>div.tools))")
                            wholerowEl.append('<div class="tools"><i class="fa fa-ellipsis-h ' + popoverClassName + '"></i></div>');

                            if (type == "term") {
                                that.createTermAction();
                            } else if (type == "category") {
                                that.createCategoryAction();
                            }
                        }
                    },
                    initializeTree = function(options) {
                        var $el = options.el,
                            type = options.type;

                        $el.jstree(getTreeConfig({
                                type: type
                            })).on("load_node.jstree", function(e, data) {
                                createAction(_.extend({}, options, data));
                            }).on("open_node.jstree", function(e, data) {
                                createAction(_.extend({}, options, data));
                            })
                            .on("select_node.jstree", function(e, data) {
466 467
                                if (that.isAssignView) {
                                    that.glossary.selectedItem = data.node.original;
468
                                    that.glossaryCollection.trigger("node_selected");
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
                                } else {
                                    var popoverClassName = (type == "term" ? '.termPopover' : '.categoryPopover'),
                                        currentClickedPopoverEl = "";
                                    if (data.event) {
                                        if ($(data.event.currentTarget).parent().hasClass('jstree-leaf')) {
                                            currentClickedPopoverEl = $(data.event.currentTarget).parent().find(popoverClassName);
                                        } else {
                                            currentClickedPopoverEl = $(data.event.currentTarget).parent().find(">div " + popoverClassName);
                                        }
                                        $(popoverClassName).not(currentClickedPopoverEl).popover('hide');
                                    }
                                    if (that.query[type].isNodeNotFoundAtLoad == true) {
                                        that.query[type].isNodeNotFoundAtLoad = false;
                                    } else if (type == that.viewType) {
                                        if (data && data.event && data.event.skipTrigger) {
                                            return;
                                        } else if (that.glossary.selectedItem.guid !== data.node.original.guid) {
                                            that.glossary.selectedItem = data.node.original;
                                            that.triggerUrl();
                                        }
489 490
                                    }
                                }
491 492 493 494
                            }).on("search.jstree", function(e, data) {
                                createAction(_.extend({}, options, data));
                            }).on("clear_search.jstree", function(e, data) {
                                createAction(_.extend({}, options, data));
495
                            }).bind('loaded.jstree', function(e, data) {
496 497 498
                                if (that.query[type].isNodeNotFoundAtLoad == true) {
                                    treeLoaded({ "$el": $el, "type": type });
                                }
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
                            });
                    },
                    initializeTermTree = function() {
                        if ($termTree.data('jstree')) {
                            $('.termPopover').popover('destroy');
                            $termTree.jstree(true).refresh();
                        } else {
                            initializeTree({
                                el: $termTree,
                                type: "term"
                            });
                        }
                    },
                    initializeCategoryTree = function() {
                        if ($categoryTree.data('jstree')) {
514
                            $('.categoryPopover').popover('destroy');
515 516 517 518 519 520 521 522 523
                            $categoryTree.jstree(true).refresh();
                        } else {
                            initializeTree({
                                el: $categoryTree,
                                type: "category"
                            })
                        }
                    }
                if (this.isAssignView) {
524
                    if (this.isAssignTermView || this.isAssignEntityView || this.isAssignAttributeRelationView) {
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
                        initializeTermTree();
                    } else if (this.isAssignCategoryView) {
                        initializeCategoryTree();
                    }
                } else {
                    initializeTermTree();
                    initializeCategoryTree();
                }


                if (Utils.getUrlState.isGlossaryTab()) {
                    this.triggerUrl();
                }
                this.glossaryCollection.trigger("render:done");
            },
            createTermAction: function() {
                var that = this;
                Utils.generatePopover({
                    el: this.$('.termPopover'),
                    contentClass: 'termPopoverOptions',
                    popoverOptions: {
                        content: function() {
547
                            var node = that.query[that.viewType],
548 549
                                liString = "";
                            if (node.type == "Glossary") {
550
                                liString = "<li data-type=" + node.type + " class='listTerm'><i class='fa fa-plus'></i> <a href='javascript:void(0)' data-fn='createSubNode'>Create Term</a></li>" +
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
                                    "<li data-type=" + node.type + " class='listTerm'><i class='fa fa-trash-o'></i><a href='javascript:void(0)' data-fn='deleteNode'>Delete Glossary</a></li>"
                            } else {
                                liString = "<li data-type=" + node.type + " class='listTerm'><i class='fa fa-trash-o'></i><a href='javascript:void(0)' data-fn='deleteNode'>Delete Term</a></li>"
                            }
                            return "<ul>" + liString + "</ul>";
                        }
                    }
                });
            },
            createCategoryAction: function() {
                var that = this;
                Utils.generatePopover({
                    el: this.$('.categoryPopover'),
                    contentClass: 'categoryPopoverOptions',
                    popoverOptions: {
                        content: function() {
567
                            var node = that.query[that.viewType],
568 569
                                liString = "";
                            if (node.type == "Glossary") {
570
                                liString = "<li data-type=" + node.type + " class='listTerm'><i class='fa fa-plus'></i> <a href='javascript:void(0)' data-fn='createSubNode'>Create Category</a></li>" +
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
                                    "<li data-type=" + node.type + " class='listTerm'><i class='fa fa-trash-o'></i><a href='javascript:void(0)' data-fn='deleteNode'>Delete Glossary</a></li>"
                            } else {
                                liString = "<li data-type=" + node.type + " class='listTerm'><i class='fa fa-plus'></i> <a href='javascript:void(0)' data-fn='createSubNode'>Create Sub-Category</a></li>" +
                                    "<li data-type=" + node.type + " class='listTerm'><i class='fa fa-trash-o'></i><a href='javascript:void(0)' data-fn='deleteNode'>Delete Category</a></li>"
                            }
                            return "<ul>" + liString + "</ul>";
                        }
                    }
                });
            },
            createSubNode: function(opt) {
                var that = this,
                    type = this.glossary.selectedItem.type;
                if ((type == "Glossary" || type == "GlossaryCategory") && this.viewType == "category") {
                    CommonViewFunction.createEditGlossaryCategoryTerm({
                        "isCategoryView": true,
                        "collection": that.glossaryCollection,
                        "callback": function() {
589 590 591 592 593
                            if (that.value.gType == "glossary") {
                                that.getGlossary();
                            } else {
                                that.ui.categoryTree.jstree(true).refresh();
                            }
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
                        },
                        "node": this.glossary.selectedItem
                    })
                } else {
                    CommonViewFunction.createEditGlossaryCategoryTerm({
                        "isTermView": true,
                        "callback": function() {
                            that.getGlossary();
                        },
                        "collection": that.glossaryCollection,
                        "node": this.glossary.selectedItem
                    })
                }
            },
            deleteNode: function(opt) {
                var that = this,
                    messageType = "",
611 612 613
                    type = this.glossary.selectedItem.type,
                    guid = this.glossary.selectedItem.guid,
                    gId = this.glossary.selectedItem.glossaryId,
614 615
                    options = {
                        success: function(rModel, response) {
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
                            if (!gId) {
                                gId = guid;
                            }
                            var glossary = that.glossaryCollection.fullCollection.get(gId);
                            if (that.value) {
                                if (that.value.gType == "term") {
                                    glossary.set('terms', _.reject(glossary.get('terms'), function(obj) {
                                        return obj.termGuid == guid;
                                    }), { silent: true });
                                } else if (that.value.gType == "category") {
                                    glossary.set('categories', _.reject(glossary.get('categories'), function(obj) {
                                        return obj.categoryGuid == guid;
                                    }), { silent: true });
                                } else {
                                    glossary = that.glossaryCollection.fullCollection.first();
                                    if (glossary) {
                                        gId = glossary.get('guid');
                                    } else {
                                        gId = null
                                    }
                                }
                            }
638 639 640
                            Utils.notifySuccess({
                                content: messageType + Messages.deleteSuccessMessage
                            });
641 642 643
                            var url = gId ? '#!/glossary/' + gId : '#!/glossary';
                            if (gId == null) {
                                that.glossary.selectedItem = {};
644 645 646 647 648
                                that.value = null;
                                that.query = {
                                    term: {},
                                    category: {}
                                };
649 650 651
                                that.ui.categoryTree.jstree(true).refresh();
                                that.ui.termTree.jstree(true).refresh();
                            }
652
                            Utils.setUrl({
653
                                url: url,
654 655 656 657 658 659 660 661 662
                                mergeBrowserUrl: false,
                                trigger: true,
                                urlParams: gId ? _.extend({}, that.value, {
                                    gType: 'glossary',
                                    updateView: true,
                                    gId: null
                                }) : null,
                                updateTabState: true
                            });
663 664
                        }
                    },
665 666 667 668 669 670 671 672 673 674 675 676 677
                    notifyObj = {
                        modal: true,
                        ok: function(argument) {
                            if (type == "Glossary") {
                                that.glossaryCollection.fullCollection.get(guid).destroy(options, { silent: true, reset: false });
                            } else if (type == "GlossaryCategory") {
                                new that.glossaryCollection.model().deleteCategory(guid, options);
                            } else if (type == "GlossaryTerm") {
                                new that.glossaryCollection.model().deleteTerm(guid, options);
                            }
                        },
                        cancel: function(argument) {}
                    };
678 679 680 681 682 683 684
                if (type == "Glossary") {
                    messageType = "Glossary";
                } else if (type == "GlossaryCategory") {
                    messageType = "Category"
                } else if (type == "GlossaryTerm") {
                    messageType = "Term";
                }
685 686
                notifyObj['text'] = "Are you sure you want to delete the " + messageType;;
                Utils.notifyConfirm(notifyObj);
687
            },
688
            triggerUrl: function(options) {
689 690 691 692
                if (this.isAssignView) {
                    return;
                }
                var selectedItem = this.glossary.selectedItem;
693
                if (this.glossaryCollection.length && (_.isEmpty(selectedItem) || this.query[this.viewType].isNodeNotFoundAtLoad)) {
694
                    var model = selectedItem.model
695
                    if (model && !_.isUndefined(model.parentCategory || model.parentCategoryGuid)) {
696 697 698
                        selectedItem = { "model": this.glossaryCollection.first().toJSON() };
                        selectedItem.guid = selectedItem.model.guid;
                        selectedItem.type = "Glossary";
699 700
                        selectedItem.gType = "glossary";
                        selectedItem.text = model.displayName;
701 702
                        this.glossary.selectedItem = selectedItem;
                        this.query[this.viewType].model = selectedItem.model;
703 704
                        this.query[this.viewType].gType = "glossary";
                        this.query[this.viewType].type = "Glossary";
705 706
                        delete this.query[this.viewType].gId;
                    }
707 708 709 710 711
                }
                if (_.isEmpty(selectedItem)) {
                    return;
                }
                if (Utils.getUrlState.isGlossaryTab() || Utils.getUrlState.isDetailPage()) {
712
                    var obj = {};
713
                    if (selectedItem.glossaryId) {
714
                        obj["gId"] = selectedItem.glossaryId;
715 716
                    } else if (selectedItem.type == "Glossary") {
                        obj["gId"] = selectedItem.guid;
717
                    }
718
                    this.query[this.viewType] = _.extend(obj, _.omit(this.value, 'gId'), _.pick(this.glossary.selectedItem, 'model', 'type', 'gType', 'guid'), { "viewType": this.viewType, "isNodeNotFoundAtLoad": this.query[this.viewType].isNodeNotFoundAtLoad });
719
                    Utils.setUrl({
720
                        url: '#!/glossary/' + obj.guid,
721 722
                        mergeBrowserUrl: false,
                        trigger: true,
723
                        urlParams: _.omit(obj, 'model', 'guid', 'type', 'isNodeNotFoundAtLoad'),
724 725 726 727 728 729 730
                        updateTabState: true
                    });
                }
            }
        });
    return GlossaryLayoutView;
});