define([ "require", "hbs!tmpl/search/tree/ClassificationTreeLayoutView_tmpl", "utils/Utils", "utils/Messages", "utils/Globals", "utils/UrlLinks", "utils/CommonViewFunction", "collection/VSearchList", "collection/VGlossaryList", "utils/Enums", "collection/VTagList", "jstree" ], function(require, ClassificationTreeLayoutViewTmpl, Utils, Messages, Globals, UrlLinks, CommonViewFunction, VSearchList, VGlossaryList, Enums, VTagList) {
"use strict";
var ClassificationTreeLayoutView = Marionette.LayoutView.extend({
template: ClassificationTreeLayoutViewTmpl,
regions: {},
ui: {
refreshTree: '[data-id="refreshTree"]',
groupOrFlatTree: '[data-id="groupOrFlatTreeView"]',
classificationSearchTree: '[data-id="classificationSearchTree"]',
showEmptyClassifications: '[data-id="showEmptyClassifications"]',
createTag: '[data-id="createTag"]',
wildCardClick: '[data-id="wildCardClick"]',
wildCardSearch: '[data-id="wildCardSearch"]',
wildCardValue: '[data-id="wildCardValue"]',
wildCardContainer: '[data-id="wildCardContainer"]',
clearWildCard: '[data-id="clearWildCard"]',
classificationTreeLoader: '[data-id="classificationTreeLoader"]'
},
templateHelpers: function() {
return {
apiBaseUrl: UrlLinks.apiBaseUrl
};
},
events: function() {
var events = {}, that = this;
return events["click " + this.ui.refreshTree] = function(e) {
that.changeLoaderState(!0), that.ui.refreshTree.attr("disabled", !0).tooltip("hide");
var type = $(e.currentTarget).data("type");
e.stopPropagation(), that.refresh({
type: type
});
}, events["click " + this.ui.createTag] = function(e) {
e.stopPropagation(), that.onClickCreateTag();
}, events["click " + this.ui.showEmptyClassifications] = function(e) {
e.stopPropagation(), this.isEmptyClassification = !this.isEmptyClassification, this.classificationSwitchBtnUpdate();
}, events["click " + this.ui.groupOrFlatTree] = function(e) {
var type = $(e.currentTarget).data("type");
e.stopPropagation(), this.isGroupView = !this.isGroupView, this.ui.groupOrFlatTree.tooltip("hide"),
this.ui.groupOrFlatTree.find("i").toggleClass("fa-sitemap fa-list-ul"), this.ui.groupOrFlatTree.find("span").html(this.isGroupView ? "Show flat tree" : "Show group tree"),
that.ui[type + "SearchTree"].jstree(!0).destroy(), that.renderClassificationTree();
}, events["click " + this.ui.wildCardClick] = function(e) {
e.stopPropagation();
}, events["click " + this.ui.wildCardSearch] = function(e) {
e.stopPropagation();
var tagValue = this.ui.wildCardValue.val();
tagValue.indexOf("*") != -1 && that.findSearchResult(tagValue);
}, events["click " + this.ui.wildCardValue] = function(e) {
e.stopPropagation();
}, events["click " + this.ui.clearWildCard] = function(e) {
e.stopPropagation(), that.ui.wildCardValue.val(""), that.ui.clearWildCard.addClass("hide-icon");
}, events["click " + this.ui.wildCardContainer] = function(e) {
e.stopPropagation();
}, events["keydown " + this.ui.wildCardValue] = function(e) {
e.stopPropagation();
var code = e.which;
if (this.ui.wildCardValue.val().length > 0 ? this.ui.clearWildCard.removeClass("hide-icon") : this.ui.clearWildCard.addClass("hide-icon"),
13 == code) {
e.preventDefault();
var tagValue = this.ui.wildCardValue.val();
tagValue.indexOf("*") != -1 && that.findSearchResult(tagValue);
}
}, events["keyup " + this.ui.wildCardValue] = function(e) {
e.stopPropagation(), e.preventDefault();
}, events;
},
initialize: function(options) {
this.options = options, _.extend(this, _.pick(options, "typeHeaders", "searchVent", "entityDefCollection", "enumDefCollection", "classificationDefCollection", "searchTableColumns", "searchTableFilters", "metricCollection")),
this.bindEvents(), this.entityCountObj = _.first(this.metricCollection.toJSON()),
this.isEmptyClassification = !1, this.entityTreeData = {}, this.tagId = null, this.isGroupView = !0;
},
onRender: function() {
this.changeLoaderState(!0), this.checkTagOnRefresh(), this.createClassificationAction(),
this.ui.clearWildCard.addClass("hide-icon"), this.changeLoaderState(!1);
},
checkTagOnRefresh: function() {
var that = this, tagName = this.options && this.options.value ? this.options.value.tag : null, presentTag = this.classificationDefCollection.fullCollection.findWhere({
name: tagName
}), tag = new VTagList();
!presentTag && tagName ? (tag.url = UrlLinks.classificationDefApiUrl(tagName), tag.fetch({
success: function(dataOrCollection, tagDetails) {
that.classificationDefCollection.fullCollection.add(tagDetails);
},
cust_error: function(model, response) {
that.renderClassificationTree();
}
})) : this.renderClassificationTree();
},
changeLoaderState: function(showLoader) {
showLoader ? (this.ui.classificationSearchTree.hide(), this.ui.classificationTreeLoader.show()) : (this.ui.classificationSearchTree.show(),
this.ui.classificationTreeLoader.hide());
},
bindEvents: function() {
var that = this;
this.listenTo(this.classificationDefCollection.fullCollection, "reset add", function(model) {
that.classificationTreeUpdate = !0, that.classificationTreeRefresh();
}, this), this.listenTo(this.classificationDefCollection.fullCollection, "remove", function(model) {
that.classificationTreeUpdate = !1, that.classificationTreeRefresh();
}, this), $("body").on("click", ".classificationPopoverOptions li", function(e) {
that.$(".classificationPopover").popover("hide"), that[$(this).find("a").data("fn") + "Classification"](e);
}), this.searchVent.on("Classification:Count:Update", function(options) {
that.changeLoaderState(!0);
var opt = options || {};
opt && !opt.metricData ? that.metricCollection.fetch({
complete: function() {
that.entityCountObj = _.first(that.metricCollection.toJSON()), that.classificationTreeUpdate = !0,
that.ui.classificationSearchTree.jstree(!0).refresh(), that.changeLoaderState(!1);
}
}) : (that.entityCountObj = opt.metricData, that.ui.classificationSearchTree.jstree(!0).refresh(),
that.changeLoaderState(!1));
});
},
classificationTreeRefresh: function() {
this.ui.classificationSearchTree.jstree(!0) ? this.ui.classificationSearchTree.jstree(!0).refresh() : this.renderClassificationTree();
},
findSearchResult: function(tagValue) {
if (!tagValue) return void Utils.notifyInfo({
content: "Search should not be empty!"
});
var params = {
searchType: "basic",
dslChecked: !1
};
this.options.value && (params.tag = tagValue);
var searchParam = _.extend({}, this.options.value, params);
this.triggerSearch(searchParam);
},
onSearchClassificationNode: function(showEmptyTag) {
this.isEmptyClassification = showEmptyTag, this.classificationSwitchBtnUpdate();
},
classificationSwitchBtnUpdate: function() {
this.ui.showEmptyClassifications.attr("data-original-title", (this.isEmptyClassification ? "Show" : "Hide") + " unused classification"),
this.ui.showEmptyClassifications.tooltip("hide"), this.ui.showEmptyClassifications.find("i").toggleClass("fa-toggle-on fa-toggle-off"),
this.ui.showEmptyClassifications.find("span").html((this.isEmptyClassification ? "Show" : "Hide") + " unused classification"),
this.ui.classificationSearchTree.jstree(!0).refresh();
},
createClassificationAction: function() {
Utils.generatePopover({
el: this.$el,
contentClass: "classificationPopoverOptions",
popoverOptions: {
selector: ".classificationPopover",
content: function() {
var name = this.dataset.name || null, searchString = "
Search";
if (name && Enums.addOnClassification.includes(name)) return "";
var liString = " Create Sub-classificationView/EditDelete";
return "" + liString + searchString + "
";
}
}
});
},
renderClassificationTree: function() {
this.generateSearchTree({
$el: this.ui.classificationSearchTree
});
},
manualRender: function(options) {
var that = this;
if (_.extend(this.options, options), void 0 === this.options.value && (this.options.value = {}),
this.options.value.tag) {
if ("_ALL_CLASSIFICATION_TYPES" === that.options.value.tag && "_ALL_CLASSIFICATION_TYPES" !== this.tagId || "_NOT_CLASSIFIED" === that.options.value.tag && "_NOT_CLASSIFIED" !== this.tagId || "_CLASSIFIED" === that.options.value.tag && "_CLASSIFIED" !== this.tagId) this.fromManualRender = !0,
this.tagId && this.ui.classificationSearchTree.jstree(!0).deselect_node(this.tagId),
this.tagId = Globals[that.options.value.tag].guid, this.ui.classificationSearchTree.jstree(!0).select_node(this.tagId); else if ("_ALL_CLASSIFICATION_TYPES" !== this.tagId && that.options.value.tag !== this.tagId || "_NOT_CLASSIFIED" !== this.tagId && that.options.value.tag !== this.tagId || "_CLASSIFIED" !== this.tagId && that.options.value.tag !== this.tagId) {
that.options.value.tag.indexOf("*") != -1 && (that.ui.classificationSearchTree.jstree(!0).deselect_all(),
that.ui.wildCardValue.val(that.options.value.tag));
var dataFound = this.classificationDefCollection.fullCollection.find(function(obj) {
return obj.get("name") === that.options.value.tag;
});
dataFound && (this.tagId && this.tagId !== dataFound.get("guid") || null === this.tagId) && (this.tagId && this.ui.classificationSearchTree.jstree(!0).deselect_node(this.tagId),
this.fromManualRender = !0, this.tagId = dataFound.get("guid"), this.ui.classificationSearchTree.jstree(!0).select_node(dataFound.get("guid")));
}
} else this.ui.classificationSearchTree.jstree(!0).deselect_all(), this.tagId = null;
},
onNodeSelect: function(options) {
if (this.classificationTreeUpdate) return void (this.classificationTreeUpdate = !1);
var name, selectedNodeId, that = this;
that.ui.wildCardValue.val(""), options ? (name = options.node.original.name, selectedNodeId = options.node.id) : name = this.options.value.type || this.options.value.tag;
var tagValue = null, params = {
searchType: "basic",
dslChecked: !1
};
if (this.options.value && (this.options.value.tag && (params.tag = this.options.value.tag),
this.options.value.isCF && (this.options.value.isCF = null), this.options.value.tagFilters && (params.tagFilters = null)),
that.tagId != selectedNodeId) that.tagId = selectedNodeId, tagValue = name, params.tag = tagValue; else if (that.options.value.tag = that.tagId = params.tag = null,
that.ui.classificationSearchTree.jstree(!0).deselect_all(!0), !(that.options.value.type || that.options.value.tag || that.options.value.term || that.options.value.query)) {
var defaultUrl = "#!/search";
return void that.onClassificationUpdate(defaultUrl);
}
var searchParam = _.extend({}, this.options.value, params);
this.triggerSearch(searchParam);
},
triggerSearch: function(params, url) {
var serachUrl = url ? url : "#!/search/searchResult";
Utils.setUrl({
url: serachUrl,
urlParams: params,
mergeBrowserUrl: !1,
trigger: !0,
updateTabState: !0
});
},
onClassificationUpdate: function(url) {
Utils.setUrl({
url: url,
mergeBrowserUrl: !1,
trigger: !0,
updateTabState: !0
});
},
refresh: function(options) {
var that = this, apiCount = 2, renderTree = function() {
0 === apiCount && (that.classificationDefCollection.fullCollection.comparator = function(model) {
return model.get("name").toLowerCase();
}, that.classificationDefCollection.fullCollection.sort({
silent: !0
}), that.classificationTreeUpdate = !0, that.ui.classificationSearchTree.jstree(!0).refresh(),
that.changeLoaderState(!1), that.ui.refreshTree.attr("disabled", !1));
};
this.classificationDefCollection.fetch({
silent: !0,
complete: function() {
--apiCount, renderTree();
}
}), this.metricCollection.fetch({
complete: function() {
--apiCount, that.entityCountObj = _.first(that.metricCollection.toJSON()), renderTree();
}
});
},
getClassificationTree: function(options) {
var that = this, collection = options && options.collection || this.classificationDefCollection.fullCollection, listOfParents = [], listWithEmptyParents = [], listWithEmptyParentsFlatView = [], flatViewList = [], isSelectedChild = !1, generateNode = function(nodeOptions, options, isChild) {
var nodeStructure = {
text: _.escape(nodeOptions.name),
name: _.escape(nodeOptions.name),
children: that.isGroupView ? getChildren({
children: isChild ? nodeOptions.model.subTypes : nodeOptions.model.get("subTypes"),
parent: isChild ? options.parentName : nodeOptions.name
}) : null,
type: isChild ? nodeOptions.children.get("category") : nodeOptions.model.get("category"),
id: isChild ? nodeOptions.children.get("guid") : nodeOptions.model.get("guid"),
icon: "fa fa-tag",
gType: "Classification"
};
return nodeStructure;
}, getChildren = function(options) {
var children = options.children, data = [], dataWithoutEmptyTag = [];
children && children.length && _.each(children, function(name) {
var child = collection.find({
name: name
}), tagEntityCount = that.entityCountObj ? that.entityCountObj.tag.tagEntities[name] : null, tagname = tagEntityCount ? name + " (" + _.numberFormatWithComma(tagEntityCount) + ")" : name;
if (that.options.value && (isSelectedChild = !!that.options.value.tag && that.options.value.tag == name,
that.tagId || (that.tagId = isSelectedChild ? child.get("guid") : null)), child) {
var modelJSON = child.toJSON(), nodeDetails = {
name: _.escape(name),
model: modelJSON,
children: child,
isSelectedChild: isSelectedChild
}, nodeProperties = {
parent: options.parentName,
text: _.escape(tagname),
guid: child.get("guid"),
model: child,
state: {
selected: isSelectedChild,
opened: !0
}
}, isChild = !0, getNodeDetails = generateNode(nodeDetails, options, isChild), classificationNode = _.extend(getNodeDetails, nodeProperties);
if (data.push(classificationNode), that.isEmptyClassification) {
var isTagEntityCount = _.isNaN(tagEntityCount) ? 0 : tagEntityCount;
isTagEntityCount && dataWithoutEmptyTag.push(classificationNode);
}
}
});
var tagData = that.isEmptyClassification ? dataWithoutEmptyTag : data;
return tagData;
};
collection.each(function(model) {
var modelJSON = model.toJSON(), name = modelJSON.name, tagEntityCount = that.entityCountObj ? that.entityCountObj.tag.tagEntities[name] : null, tagname = tagEntityCount ? name + " (" + _.numberFormatWithComma(tagEntityCount) + ")" : name, isSelected = !1;
that.options.value && (isSelected = !!that.options.value.tag && that.options.value.tag == name,
that.tagId || (that.tagId = isSelected ? model.get("guid") : null));
var getParentNodeDetails, classificationParentNode, getParentFlatView, classificationParentFlatView, parentNodeDetails = {
name: _.escape(name),
model: model,
isSelectedChild: isSelectedChild
}, parentNodeProperties = {
text: _.escape(tagname),
state: {
disabled: 0 == tagEntityCount,
selected: isSelected,
opened: !0
}
}, isChild = !1;
if (0 == modelJSON.superTypes.length && (getParentNodeDetails = generateNode(parentNodeDetails, model, isChild),
classificationParentNode = _.extend(getParentNodeDetails, parentNodeProperties),
listOfParents.push(classificationParentNode)), getParentFlatView = generateNode(parentNodeDetails, model),
classificationParentFlatView = _.extend(getParentFlatView, parentNodeProperties),
flatViewList.push(classificationParentFlatView), that.isEmptyClassification) {
var isTagEntityCount = _.isNaN(tagEntityCount) ? 0 : tagEntityCount;
isTagEntityCount && (0 == modelJSON.superTypes.length && listWithEmptyParents.push(classificationParentNode),
listWithEmptyParentsFlatView.push(classificationParentFlatView));
}
});
var classificationTreeData = that.isEmptyClassification ? listWithEmptyParents : listOfParents, flatViewClassificaton = that.isEmptyClassification ? listWithEmptyParentsFlatView : flatViewList, classificationData = that.isGroupView ? that.pushRootClassificationToJstree.call(that, classificationTreeData) : that.pushRootClassificationToJstree.call(that, flatViewClassificaton);
return classificationData;
},
pushRootClassificationToJstree: function(data) {
var that = this;
return _.each(Enums.addOnClassification, function(addOnClassification) {
var rootClassification = Globals[addOnClassification], isSelected = !(!that.options.value || !that.options.value.tag) && that.options.value.tag == rootClassification.name, rootClassificationNode = {
text: _.escape(rootClassification.name),
name: rootClassification.name,
type: rootClassification.category,
gType: "Classification",
guid: rootClassification.guid,
id: rootClassification.guid,
model: rootClassification,
children: [],
icon: "fa fa-tag",
state: {
selected: isSelected
}
};
data.push(rootClassificationNode);
}), data;
},
generateSearchTree: function(options) {
var $el = options && options.$el, that = (options && options.type, this), getEntityTreeConfig = function(opt) {
return {
plugins: [ "search", "core", "sort", "conditionalselect", "changed", "wholerow", "node_customize" ],
conditionalselect: function(node) {
var type = node.original.type;
return "ENTITY" != type && "GLOSSARY" != type || !node.children.length && "GLOSSARY" != type;
},
state: {
opened: !0
},
search: {
show_only_matches: !0,
case_sensitive: !1
},
node_customize: {
default: function(el) {
var aTag = $(el).find(">a.jstree-anchor"), nameText = aTag.text();
aTag.append("" + nameText + ""), $(el).append('