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

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

            template: GlossaryLayoutViewTmpl,

            /** Layout sub regions */
            regions: {},
            templateHelpers: function() {
                return {
42
                    isAssignView: this.isAssignView,
43
                    importTmplUrl: UrlLinks.glossaryImportTempUrl(),
44
                    isAssignAttributeRelationView: this.isAssignAttributeRelationView
45 46 47 48 49 50 51 52 53 54 55
                };
            },

            /** 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']",
56
                categoryTree: "[data-id='categoryTree']",
57 58 59
                importGlossary: "[data-id='importGlossary']",
                glossaryTreeLoader: "[data-id='glossaryTreeLoader']",
                glossaryTreeView: "[data-id='glossaryTreeView']"
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
            },
            /** 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");
                        }
                    })
                };
82 83 84 85
                events["click " + this.ui.refreshGlossary] = function() {
                    this.ui.refreshGlossary.attr("disabled", true);
                    this.getGlossary();
                };
86
                events["click " + this.ui.importGlossary] = 'onClickImportGlossary';
87 88 89 90 91 92 93 94 95 96 97 98 99
                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) {
100
                _.extend(this, _.pick(options, 'associatedTerms', 'guid', 'value', 'glossaryCollection', 'glossary', 'isAssignTermView', 'isAssignCategoryView', 'isAssignEntityView', 'isAssignAttributeRelationView'));
101
                this.viewType = "term";
102
                this.isAssignView = this.isAssignTermView || this.isAssignCategoryView || this.isAssignEntityView || this.isAssignAttributeRelationView;
103 104
                this.bindEvents();
                this.query = {
105 106
                    term: {},
                    category: {}
107
                };
108 109
                if (Utils.getUrlState.isGlossaryTab() && this.value && this.value.viewType) {
                    this.viewType = this.value.viewType;
110
                    this.query[this.viewType] = _.extend({}, this.value, { "guid": this.guid });
111
                }
112 113 114
            },
            bindEvents: function() {
                var that = this;
115
                this.listenTo(this.glossaryCollection.fullCollection, "reset add change", function(skip) {
116
                    this.generateTree();
117
                    this.setValues();
118
                    this.changeLoaderState(false);
119
                    this.ui.refreshGlossary.attr("disabled", false);
120
                }, this);
121 122 123 124 125 126 127 128 129 130 131 132 133
                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();
134
                            this.setValues({ trigger: false });
135 136
                        }
                    }
137
                    this.changeLoaderState(false);
138
                }, this);
139 140 141 142 143 144
                if (!this.isAssignView) {
                    $('body').on('click', '.termPopoverOptions li, .categoryPopoverOptions li', function(e) {
                        that.$('.termPopover,.categoryPopover').popover('hide');
                        that[$(this).find('a').data('fn')](e)
                    });
                }
145 146
            },
            onRender: function() {
147
                this.changeLoaderState(true);
148 149 150 151 152 153 154 155 156 157
                if (this.isAssignCategoryView) {
                    this.$('.category-view').show();
                    this.$('.term-view').hide();
                }
                if (this.isAssignView && this.glossaryCollection.fullCollection.length) {
                    this.generateTree();
                } else {
                    this.getGlossary();
                }
            },
158 159 160 161 162 163 164 165 166
            changeLoaderState: function(showLoader) {
                if (showLoader) {
                    this.ui.glossaryTreeLoader.show();
                    this.ui.glossaryTreeView.hide();
                } else {
                    this.ui.glossaryTreeLoader.hide();
                    this.ui.glossaryTreeView.show();
                }
            },
167
            setValues: function(options) {
168 169
                if (this.viewType == "category") {
                    if (!this.ui.glossaryView.prop("checked")) {
170
                        this.ui.glossaryView.prop("checked", true).trigger("change", options);
171 172 173
                    }
                } else {
                    if (this.ui.glossaryView.prop("checked")) {
174
                        this.ui.glossaryView.prop("checked", false).trigger("change", options);
175 176 177
                    }
                }
            },
178
            glossaryViewToggle: function(e, options) {
179
                var that = this;
180 181 182 183 184 185 186 187 188
                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";
                }
189
                var setDefaultSelector = function() {
190 191 192
                    if (!that.value) {
                        return;
                    }
193 194 195 196 197 198 199 200 201 202 203 204
                    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,
205
                        text: model.name,
206 207 208
                        gType: "glossary"
                    }
                }
209
                if (Utils.getUrlState.isGlossaryTab()) {
210 211 212 213 214 215
                    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;
216
                            $tree.jstree('activate_node', obj.guid);
217 218 219 220 221
                        }
                    } else {
                        setDefaultSelector();
                        $tree.jstree('activate_node', that.glossary.selectedItem.guid);
                    }
222
                    this.query[this.viewType] = _.extend(obj, _.pick(this.glossary.selectedItem, 'model', 'guid', 'gType', 'type'), { "viewType": this.viewType, "isNodeNotFoundAtLoad": this.query[this.viewType].isNodeNotFoundAtLoad });
223
                    var url = _.isEmpty(this.glossary.selectedItem) ? '#!/glossary' : '#!/glossary/' + this.glossary.selectedItem.guid;
224
                    Utils.setUrl({
225 226 227
                        "url": url,
                        "urlParams": _.extend({}, _.omit(obj, 'guid', 'model', 'type', 'isNodeNotFoundAtLoad')),
                        "mergeBrowserUrl": false,
228
                        "trigger": (options && !_.isUndefined(options.trigger) ? options.trigger : true),
229
                        "updateTabState": true
230
                    });
231 232 233
                }
            },
            getGlossary: function() {
234
                this.changeLoaderState(true);
235 236 237 238 239
                this.glossaryCollection.fetch({ reset: true });
            },
            generateCategoryData: function(options) {
                return _.map(options.data, function(obj) {
                    return {
240
                        "text": _.escape(obj.displayText),
241 242 243 244 245 246 247
                        "icon": "fa fa-files-o",
                        "guid": obj.categoryGuid,
                        "id": obj.categoryGuid,
                        "glossaryId": options.node.glossaryId,
                        "glossaryName": options.node.glossaryName,
                        "model": obj,
                        "type": "GlossaryCategory",
248
                        "gType": "category",
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
                        "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,
274
                    selectedGuid = that.guid,
275
                    associatedTerms = that.associatedTerms,
276
                    type = opt.type;
277

278 279 280
                if (opt.type == this.viewType) {
                    this.query[opt.type].isNodeNotFoundAtLoad = true;
                }
281 282 283 284
                var getSelectedState = function(options) {
                    var objGuid = options.objGuid,
                        node = options.node,
                        index = options.index;
285 286 287 288 289
                    if (that.isAssignView) {
                        return {
                            'opened': true
                        }
                    } else if (!that.guid) {
290
                        that.query[that.viewType].isNodeNotFoundAtLoad = false;
291 292
                        var selectedItem = {
                            "type": "Glossary",
293 294
                            "gType": "glossary",
                            "model": that.glossaryCollection.fullCollection.first().toJSON()
295
                        };
296
                        selectedItem.text = selectedItem.model.name;
297 298 299
                        selectedItem.guid = selectedItem.model.guid;
                        if (index == 0 && selectedItem.guid == objGuid) {
                            that.glossary.selectedItem = selectedItem;
300
                            that.query[that.viewType].model = selectedItem.model;
301
                            that.query[that.viewType].type = selectedItem.type;
302 303 304 305 306 307 308
                            return {
                                'opened': true,
                                'selected': true
                            }
                        }
                    } else {
                        if (that.guid == objGuid) {
309
                            that.query[that.viewType].isNodeNotFoundAtLoad = false;
310
                            that.glossary.selectedItem = node
311
                            that.query[that.viewType].model = node.model;
312
                            that.query[that.viewType].type = node.type;
313 314 315 316 317 318 319 320 321 322
                            return {
                                'opened': true,
                                'selected': true
                            }
                        }
                    }
                }
                return this.glossaryCollection.fullCollection.map(function(model, i) {
                    var obj = model.toJSON(),
                        parent = {
323
                            "text": _.escape(obj.name),
324 325 326 327 328
                            "icon": "fa fa-folder-o",
                            "guid": obj.guid,
                            "id": obj.guid,
                            "model": obj,
                            "type": obj.typeName ? obj.typeName : "Glossary",
329
                            "gType": "glossary",
330 331 332 333 334 335 336 337 338 339 340 341 342
                            "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;
                            }
343
                            var typeName = category.typeName || "GlossaryCategory",
344 345
                                guid = category.categoryGuid,
                                categoryObj = {
346
                                    "text": _.escape(category.displayText),
347 348
                                    "type": typeName,
                                    "gType": "category",
349 350 351 352
                                    "guid": guid,
                                    "id": guid,
                                    "parent": obj,
                                    "glossaryId": obj.guid,
353
                                    "glossaryName": obj.name,
354 355 356 357 358 359 360 361 362 363 364 365 366 367
                                    "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) {
368 369 370 371
                            if (associatedTerms) {
                                var associatedTermFound = _.find(associatedTerms, function(obj, index) {
                                    if ((obj.termGuid ? obj.termGuid : obj.guid) == term.termGuid) {
                                        return obj;
372
                                    }
373 374 375 376
                                });
                                if (associatedTermFound) {
                                    return;
                                }
377
                            }
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

                            var typeName = term.typeName || "GlossaryTerm",
                                guid = term.termGuid,
                                termObj = {
                                    "text": _.escape(term.displayText),
                                    "type": typeName,
                                    "gType": "term",
                                    "guid": guid,
                                    "id": guid,
                                    "parent": obj,
                                    "glossaryName": obj.name,
                                    "glossaryId": obj.guid,
                                    "model": term,
                                    "icon": "fa fa-file-o"
                                }
                            termObj.state = getSelectedState({
                                index: i,
                                node: termObj,
                                objGuid: guid
                            })
                            parent.children.push(termObj);

400 401 402 403 404 405
                        });
                    }
                    return parent;
                });
            },
            manualRender: function(options) {
406 407 408 409 410 411 412 413
                _.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) {
414
                        $tree.jstree('activate_node', this.guid, { skipTrigger: true });
415 416 417
                        delete this.value.fromView;
                        delete this.value.updateView;
                        this.glossary.selectedItem = node.original;
418
                        this.query[this.viewType] = _.extend({}, _.pick(this.glossary.selectedItem, 'model', 'guid', 'gType', 'type'), { "viewType": this.viewType });
419 420 421 422 423 424 425 426 427
                        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" });
                    }
428 429
                } else {
                    this.setValues();
430 431 432 433
                }
                if (options.isTrigger) {
                    this.triggerUrl();
                }
434 435 436 437 438
            },
            generateTree: function() {
                var $termTree = this.ui.termTree,
                    $categoryTree = this.ui.categoryTree,
                    that = this,
439
                    this_guid = that.guid,
440 441 442 443
                    getTreeConfig = function(options) {
                        return {
                            "plugins": ["search", "themes", "core", "wholerow", "sort", "conditionalselect"],
                            "conditionalselect": function(node) {
444 445 446 447
                                var obj = node && node.original && node.original.type;
                                if (!obj) {
                                    return;
                                }
448
                                if (that.isAssignView) {
449
                                    return obj != "Glossary" ? true : false;
450
                                } else {
451
                                    return obj != "NoAction" ? true : false;
452 453
                                }
                            },
454 455 456
                            "search": {
                                "show_only_matches": true
                            },
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
                            "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
                                },
                            }
                        }
                    },
472 473
                    treeLoaded = function(options) {
                        if (that.query[options.type].isNodeNotFoundAtLoad == true) {
474
                            var id = that.glossary.selectedItem.guid;
475 476 477 478
                            if (id) {
                                options.$el.jstree('activate_node', id);
                            }
                            that.changeLoaderState(false);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
                        }
                    },
                    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) {
508 509
                                if (that.isAssignView) {
                                    that.glossary.selectedItem = data.node.original;
510
                                    that.glossaryCollection.trigger("node_selected");
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
                                } 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();
                                        }
531 532
                                    }
                                }
533 534 535 536
                            }).on("search.jstree", function(e, data) {
                                createAction(_.extend({}, options, data));
                            }).on("clear_search.jstree", function(e, data) {
                                createAction(_.extend({}, options, data));
537
                            }).bind('loaded.jstree', function(e, data) {
538 539 540
                                if (that.query[type].isNodeNotFoundAtLoad == true) {
                                    treeLoaded({ "$el": $el, "type": type });
                                }
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
                            });
                    },
                    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')) {
556
                            $('.categoryPopover').popover('destroy');
557 558 559 560 561 562 563 564 565
                            $categoryTree.jstree(true).refresh();
                        } else {
                            initializeTree({
                                el: $categoryTree,
                                type: "category"
                            })
                        }
                    }
                if (this.isAssignView) {
566
                    if (this.isAssignTermView || this.isAssignEntityView || this.isAssignAttributeRelationView) {
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
                        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() {
589
                            var node = that.query[that.viewType],
590 591
                                liString = "";
                            if (node.type == "Glossary") {
592
                                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>" +
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
                                    "<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() {
609
                            var node = that.query[that.viewType],
610 611
                                liString = "";
                            if (node.type == "Glossary") {
612
                                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>" +
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
                                    "<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() {
631 632 633 634 635
                            if (that.value.gType == "glossary") {
                                that.getGlossary();
                            } else {
                                that.ui.categoryTree.jstree(true).refresh();
                            }
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
                        },
                        "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 = "",
653 654 655
                    type = this.glossary.selectedItem.type,
                    guid = this.glossary.selectedItem.guid,
                    gId = this.glossary.selectedItem.glossaryId,
656 657
                    options = {
                        success: function(rModel, response) {
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
                            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
                                    }
                                }
                            }
680
                            Utils.notifySuccess({
681
                                content: messageType + Messages.getAbbreviationMsg(false, 'deleteSuccessMessage')
682
                            });
683 684 685
                            var url = gId ? '#!/glossary/' + gId : '#!/glossary';
                            if (gId == null) {
                                that.glossary.selectedItem = {};
686 687 688 689 690
                                that.value = null;
                                that.query = {
                                    term: {},
                                    category: {}
                                };
691 692 693
                                that.ui.categoryTree.jstree(true).refresh();
                                that.ui.termTree.jstree(true).refresh();
                            }
694
                            Utils.setUrl({
695
                                url: url,
696 697 698 699 700 701 702 703 704
                                mergeBrowserUrl: false,
                                trigger: true,
                                urlParams: gId ? _.extend({}, that.value, {
                                    gType: 'glossary',
                                    updateView: true,
                                    gId: null
                                }) : null,
                                updateTabState: true
                            });
705 706
                        }
                    },
707 708 709
                    notifyObj = {
                        modal: true,
                        ok: function(argument) {
710
                            that.changeLoaderState(true);
711 712 713 714 715 716 717
                            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);
                            }
718
                            that.changeLoaderState(false);
719 720 721
                        },
                        cancel: function(argument) {}
                    };
722 723 724 725 726 727 728
                if (type == "Glossary") {
                    messageType = "Glossary";
                } else if (type == "GlossaryCategory") {
                    messageType = "Category"
                } else if (type == "GlossaryTerm") {
                    messageType = "Term";
                }
729 730
                notifyObj['text'] = "Are you sure you want to delete the " + messageType;;
                Utils.notifyConfirm(notifyObj);
731
            },
732
            triggerUrl: function(options) {
733 734 735 736
                if (this.isAssignView) {
                    return;
                }
                var selectedItem = this.glossary.selectedItem;
737
                if (this.glossaryCollection.length && (_.isEmpty(selectedItem) || this.query[this.viewType].isNodeNotFoundAtLoad)) {
738
                    var model = selectedItem.model
739
                    if (model && !_.isUndefined(model.parentCategory || model.parentCategoryGuid)) {
740 741 742
                        selectedItem = { "model": this.glossaryCollection.first().toJSON() };
                        selectedItem.guid = selectedItem.model.guid;
                        selectedItem.type = "Glossary";
743
                        selectedItem.gType = "glossary";
744
                        selectedItem.text = model.name;
745 746
                        this.glossary.selectedItem = selectedItem;
                        this.query[this.viewType].model = selectedItem.model;
747 748
                        this.query[this.viewType].gType = "glossary";
                        this.query[this.viewType].type = "Glossary";
749 750
                        delete this.query[this.viewType].gId;
                    }
751 752 753 754 755
                }
                if (_.isEmpty(selectedItem)) {
                    return;
                }
                if (Utils.getUrlState.isGlossaryTab() || Utils.getUrlState.isDetailPage()) {
756
                    var obj = {};
757
                    if (selectedItem.glossaryId) {
758
                        obj["gId"] = selectedItem.glossaryId;
759 760
                    } else if (selectedItem.type == "Glossary") {
                        obj["gId"] = selectedItem.guid;
761
                    }
762
                    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 });
763
                    Utils.setUrl({
764
                        url: '#!/glossary/' + obj.guid,
765 766
                        mergeBrowserUrl: false,
                        trigger: true,
767
                        urlParams: _.omit(obj, 'model', 'guid', 'type', 'isNodeNotFoundAtLoad'),
768 769 770
                        updateTabState: true
                    });
                }
771 772 773 774 775 776 777 778 779 780 781 782
            },
            onClickImportGlossary: function() {
                var that = this;
                require([
                    'views/glossary/ImportGlossaryLayoutView'
                ], function(ImportGlossaryLayoutView) {
                    var view = new ImportGlossaryLayoutView({
                        callback: function() {
                            that.getGlossary();
                        }
                    });
                });
783 784 785
            }
        });
    return GlossaryLayoutView;
786
});