generateData: function()

in dashboardv2/public/js/views/glossary/GlossaryLayoutView.js [284:438]


            generateData: function(opt) {
                var that = this,
                    selectedGuid = that.guid,
                    associatedTerms = that.associatedTerms,
                    type = opt.type;

                if (opt.type == this.viewType) {
                    this.query[opt.type].isNodeNotFoundAtLoad = true;
                }
                var getSelectedState = function(options) {
                    var objGuid = options.objGuid,
                        node = options.node,
                        index = options.index;
                    if (that.isAssignView) {
                        return {
                            'opened': true
                        }
                    } else if (!that.guid) {
                        that.query[that.viewType].isNodeNotFoundAtLoad = false;
                        var selectedItem = {
                            "type": "Glossary",
                            "gType": "glossary",
                            "model": that.glossaryCollection.fullCollection.first().toJSON()
                        };
                        selectedItem.text = selectedItem.model.name;
                        selectedItem.guid = selectedItem.model.guid;
                        if (index == 0 && selectedItem.guid == objGuid) {
                            that.glossary.selectedItem = selectedItem;
                            that.query[that.viewType].model = selectedItem.model;
                            that.query[that.viewType].type = selectedItem.type;
                            return {
                                'opened': true,
                                'selected': true
                            }
                        }
                    } else {
                        if (that.guid == objGuid) {
                            that.query[that.viewType].isNodeNotFoundAtLoad = false;
                            that.glossary.selectedItem = node
                            that.query[that.viewType].model = node.model;
                            that.query[that.viewType].type = node.type;
                            return {
                                'opened': true,
                                'selected': true
                            }
                        }
                    }
                }
                return this.glossaryCollection.fullCollection.map(function(model, i) {
                    var obj = model.toJSON(),
                        parent = {
                            "text": _.escape(obj.name),
                            "icon": "fa fa-folder-o",
                            "guid": obj.guid,
                            "id": obj.guid,
                            "model": obj,
                            "type": obj.typeName ? obj.typeName : "Glossary",
                            "gType": "glossary",
                            "children": []
                        }
                    parent.state = getSelectedState({
                        index: i,
                        node: parent,
                        objGuid: obj.guid
                    });
                    if (type == "category" && obj.categories) {
                        var isSelected = false,
                            parentGuid = obj.guid,
                            parentCategoryGuid = null,
                            categoryList = [],
                            catrgoryRelation = [];
                        _.each(obj.categories, function(category) {
                            if (that.options.value) {
                                isSelected = that.options.value.guid ? that.options.value.guid == category.categoryGuid : false;
                            }

                            var typeName = category.typeName || "GlossaryCategory",
                                guid = category.categoryGuid,
                                categoryObj = {
                                    id: guid,
                                    guid: guid,
                                    text: _.escape(category.displayText),
                                    type: typeName,
                                    gType: "category",
                                    glossaryId: obj.guid,
                                    glossaryName: obj.name,
                                    children: [],
                                    model: category,
                                    icon: "fa fa-files-o"
                                };
                            categoryObj.state = getSelectedState({
                                index: i,
                                node: categoryObj,
                                objGuid: guid
                            })
                            if (category.parentCategoryGuid) {
                                catrgoryRelation.push({ parent: category.parentCategoryGuid, child: guid })
                            }
                            categoryList.push(categoryObj);
                        });
                        _.each(categoryList, function(category) {
                            var getRelation = _.find(catrgoryRelation, function(catrgoryObj) {
                                if (catrgoryObj.child == category.guid) return catrgoryObj;
                            })
                            if (getRelation) {
                                _.map(categoryList, function(catrgoryObj) {
                                    if (catrgoryObj.guid == getRelation.parent) {
                                        catrgoryObj["children"].push(category);
                                    };
                                })
                            } else {
                                parent.children.push(category)
                            }
                        })

                    }
                    if (type == "term" && obj.terms) {
                        _.each(obj.terms, function(term) {
                            if (associatedTerms) {
                                var associatedTermFound = _.find(associatedTerms, function(obj, index) {
                                    if ((obj.termGuid ? obj.termGuid : obj.guid) == term.termGuid) {
                                        return obj;
                                    }
                                });
                                if (associatedTermFound) {
                                    return;
                                }
                            }

                            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);

                        });
                    }
                    return parent;
                });
            },