EntityTreeLayoutView.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. define([ "require", "hbs!tmpl/search/tree/EntityTreeLayoutView_tmpl", "utils/Utils", "utils/Globals", "utils/UrlLinks", "utils/CommonViewFunction", "collection/VSearchList", "collection/VGlossaryList", "utils/Enums", "jstree" ], function(require, EntityLayoutViewTmpl, Utils, Globals, UrlLinks, CommonViewFunction, VSearchList, VGlossaryList, Enums) {
  2. "use strict";
  3. var EntityTreeLayoutview = Marionette.LayoutView.extend({
  4. template: EntityLayoutViewTmpl,
  5. regions: {},
  6. ui: {
  7. refreshTree: '[data-id="refreshTree"]',
  8. groupOrFlatTree: '[data-id="groupOrFlatTreeView"]',
  9. entitySearchTree: '[data-id="entitySearchTree"]',
  10. showEmptyServiceType: '[data-id="showEmptyServiceType"]',
  11. entityTreeLoader: '[data-id="entityTreeLoader"]',
  12. importBusinessMetadata: "[data-id='importBusinessMetadata']",
  13. downloadBusinessMetadata: "[data-id='downloadBusinessMetadata']"
  14. },
  15. templateHelpers: function() {
  16. return {
  17. apiBaseUrl: UrlLinks.apiBaseUrl,
  18. importTmplUrl: UrlLinks.businessMetadataImportTempUrl()
  19. };
  20. },
  21. events: function() {
  22. var events = {}, that = this;
  23. return events["click " + this.ui.refreshTree] = function(e) {
  24. that.changeLoaderState(!0), this.ui.refreshTree.attr("disabled", !0).tooltip("hide");
  25. var type = $(e.currentTarget).data("type");
  26. e.stopPropagation(), that.ui[type + "SearchTree"].jstree(!0).destroy(), that.refresh({
  27. type: type
  28. });
  29. }, events["click " + this.ui.showEmptyServiceType] = function(e) {
  30. e.stopPropagation(), this.isEmptyServicetype = !this.isEmptyServicetype, this.entitySwitchBtnUpdate();
  31. }, events["click " + this.ui.groupOrFlatTree] = function(e) {
  32. var type = $(e.currentTarget).data("type");
  33. e.stopPropagation(), this.isGroupView = !this.isGroupView, this.ui.groupOrFlatTree.tooltip("hide"),
  34. 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"),
  35. that.ui[type + "SearchTree"].jstree(!0).destroy(), that.renderEntityTree();
  36. }, events["click " + this.ui.importBusinessMetadata] = function(e) {
  37. e.stopPropagation(), that.onClickImportBusinessMetadata();
  38. }, events["click " + this.ui.downloadBusinessMetadata] = function(e) {
  39. e.stopPropagation();
  40. }, events;
  41. },
  42. bindEvents: function() {
  43. var that = this;
  44. $("body").on("click", ".entityPopoverOptions li", function(e) {
  45. that.$(".entityPopover").popover("hide"), that[$(this).find("a").data("fn") + "Entity"](e);
  46. }), this.searchVent.on("Entity:Count:Update", function(options) {
  47. that.changeLoaderState(!0);
  48. var opt = options || {};
  49. opt && !opt.metricData ? that.metricCollection.fetch({
  50. complete: function() {
  51. that.entityCountObj = _.first(that.metricCollection.toJSON()), that.fromManualRender = !0,
  52. that.ui.entitySearchTree.jstree(!0).refresh(), that.changeLoaderState(!1);
  53. }
  54. }) : (that.entityCountObj = opt.metricData, that.ui.entitySearchTree.jstree(!0).refresh(),
  55. that.changeLoaderState(!1));
  56. }), this.classificationAndMetricEvent.on("metricCollection:Update", function(options) {
  57. that.changeLoaderState(!0), that.ui.refreshTree.attr("disabled", !0).tooltip("hide"),
  58. that.ui.entitySearchTree.jstree(!0).destroy(), that.refresh({
  59. type: "entity",
  60. apiCount: 0
  61. });
  62. });
  63. },
  64. initialize: function(options) {
  65. this.options = options, _.extend(this, _.pick(options, "typeHeaders", "searchVent", "entityDefCollection", "enumDefCollection", "classificationDefCollection", "searchTableColumns", "searchTableFilters", "metricCollection", "classificationAndMetricEvent")),
  66. this.bindEvents(), this.entityCountObj = _.first(this.metricCollection.toJSON()) || {
  67. entity: {
  68. entityActive: {},
  69. entityDeleted: {}
  70. },
  71. tag: {
  72. tagEntities: {}
  73. }
  74. }, this.isEmptyServicetype = !0, this.entityTreeData = {}, this.typeId = null, this.isGroupView = !0;
  75. },
  76. onRender: function() {
  77. this.changeLoaderState(!0), this.renderEntityTree(), this.createEntityAction(),
  78. this.changeLoaderState(!1);
  79. },
  80. changeLoaderState: function(showLoader) {
  81. showLoader ? (this.ui.entitySearchTree.hide(), this.ui.entityTreeLoader.show()) : (this.ui.entitySearchTree.show(),
  82. this.ui.entityTreeLoader.hide());
  83. },
  84. createEntityAction: function() {
  85. Utils.generatePopover({
  86. el: this.$el,
  87. contentClass: "entityPopoverOptions",
  88. popoverOptions: {
  89. selector: ".entityPopover",
  90. content: function() {
  91. var liString = ($(this).data("detail"), "<li><i class='fa fa-search'></i><a href='javascript:void(0)' data-fn='onSelectedSearch'>Search</a></li>");
  92. return "<ul>" + liString + "</ul>";
  93. }
  94. }
  95. });
  96. },
  97. renderEntityTree: function() {
  98. var that = this;
  99. this.generateSearchTree({
  100. $el: that.ui.entitySearchTree
  101. });
  102. },
  103. onSearchEntityNode: function(showEmptyType) {
  104. this.isEmptyServicetype = showEmptyType, this.entitySwitchBtnUpdate();
  105. },
  106. entitySwitchBtnUpdate: function() {
  107. this.ui.showEmptyServiceType.attr("data-original-title", (this.isEmptyServicetype ? "Show" : "Hide") + " empty service types"),
  108. this.ui.showEmptyServiceType.tooltip("hide"), this.ui.showEmptyServiceType.find("i").toggleClass("fa-toggle-on fa-toggle-off"),
  109. this.ui.entitySearchTree.jstree(!0).refresh();
  110. },
  111. manualRender: function(options) {
  112. var that = this;
  113. if (_.extend(this.options, options), void 0 === this.options.value && (this.options.value = {}),
  114. this.options.value.type) {
  115. if ("_ALL_ENTITY_TYPES" === that.options.value.type && "_ALL_ENTITY_TYPES" !== this.typeId) this.fromManualRender = !0,
  116. this.typeId && this.ui.entitySearchTree.jstree(!0).deselect_node(this.typeId), this.typeId = Globals[that.options.value.type].guid,
  117. this.ui.entitySearchTree.jstree(!0).select_node(this.typeId); else if ("_ALL_ENTITY_TYPES" !== this.typeId && that.options.value.type !== this.typeId) {
  118. var dataFound = this.typeHeaders.fullCollection.find(function(obj) {
  119. return obj.get("name") === that.options.value.type;
  120. });
  121. dataFound && (this.typeId && this.typeId !== dataFound.get("guid") || null === this.typeId) && (this.typeId && this.ui.entitySearchTree.jstree(!0).deselect_node(this.typeId),
  122. this.fromManualRender = !0, this.typeId = dataFound.get("guid"), this.ui.entitySearchTree.jstree(!0).select_node(dataFound.get("guid")));
  123. }
  124. } else this.ui.entitySearchTree.jstree(!0).deselect_all(), this.typeId = null;
  125. },
  126. onNodeSelect: function(options) {
  127. var that = this, name = options.node.original.name, selectedNodeId = options.node.id, typeValue = null, params = {
  128. searchType: "basic",
  129. dslChecked: !1
  130. };
  131. this.options.value && (this.options.value.type && (params.type = this.options.value.type),
  132. this.options.value.isCF && (this.options.value.isCF = null), this.options.value.entityFilters && (params.entityFilters = null));
  133. var getUrl = Utils.getUrlState.isSearchTab();
  134. if (getUrl || (that.typeId = null), that.typeId != selectedNodeId) that.typeId = selectedNodeId,
  135. typeValue = name, params.type = typeValue; else if (that.typeId = params.type = null,
  136. that.ui.entitySearchTree.jstree(!0).deselect_all(!0), !(that.options.value.type || that.options.value.tag || that.options.value.term || that.options.value.query || this.options.value.udKeys || this.options.value.ugLabels)) return void that.showDefaultPage();
  137. var searchParam = _.extend({}, this.options.value, params);
  138. this.triggerSearch(searchParam);
  139. },
  140. showDefaultPage: function() {
  141. Utils.setUrl({
  142. url: "!/search",
  143. mergeBrowserUrl: !1,
  144. trigger: !0,
  145. updateTabState: !0
  146. });
  147. },
  148. triggerSearch: function(params, url) {
  149. var serachUrl = url ? url : "#!/search/searchResult";
  150. Utils.setUrl({
  151. url: serachUrl,
  152. urlParams: params,
  153. mergeBrowserUrl: !1,
  154. trigger: !0,
  155. updateTabState: !0
  156. });
  157. },
  158. onSelectedSearchEntity: function() {
  159. var params = {
  160. searchType: "basic",
  161. dslChecked: !1,
  162. type: this.options.value.type
  163. };
  164. this.triggerSearch(params);
  165. },
  166. getEntityTree: function() {
  167. var that = this, serviceTypeArr = [], serviceTypeWithEmptyEntity = [], type = "ENTITY", generateTreeData = (this.ui.entitytreeStructure,
  168. function(data) {
  169. that.typeHeaders.fullCollection.each(function(model) {
  170. var serviceType = model.toJSON().serviceType, isSelected = !1, categoryType = model.toJSON().category, generateServiceTypeArr = function(entityCountArr, serviceType, children, entityCount) {
  171. that.isGroupView ? entityCountArr[serviceType] ? (entityCountArr[serviceType].children.push(children),
  172. entityCountArr[serviceType].totalCounter = +entityCountArr[serviceType].totalCounter + entityCount) : (entityCountArr[serviceType] = [],
  173. entityCountArr[serviceType].name = serviceType, entityCountArr[serviceType].children = [],
  174. entityCountArr[serviceType].children.push(children), entityCountArr[serviceType].totalCounter = entityCount) : entityCountArr.push(children);
  175. };
  176. if (serviceType || (serviceType = "other_types"), "ENTITY" == categoryType) {
  177. var entityCount = that.entityCountObj ? (that.entityCountObj.entity.entityActive[model.get("name")] || 0) + (that.entityCountObj.entity.entityDeleted[model.get("name")] || 0) : 0, modelname = entityCount ? model.get("name") + " (" + _.numberFormatWithComma(entityCount) + ")" : model.get("name");
  178. that.options.value && (isSelected = !!that.options.value.type && that.options.value.type == model.get("name"),
  179. that.typeId || (that.typeId = isSelected ? model.get("guid") : null));
  180. var children = {
  181. text: _.escape(modelname),
  182. name: model.get("name"),
  183. type: model.get("category"),
  184. gType: "Entity",
  185. guid: model.get("guid"),
  186. id: model.get("guid"),
  187. model: model,
  188. parent: "#",
  189. icon: "fa fa-file-o",
  190. state: {
  191. disabled: !1,
  192. selected: isSelected
  193. }
  194. };
  195. entityCount = _.isNaN(entityCount) ? 0 : entityCount, generateServiceTypeArr(serviceTypeArr, serviceType, children, entityCount),
  196. entityCount && generateServiceTypeArr(serviceTypeWithEmptyEntity, serviceType, children, entityCount);
  197. }
  198. });
  199. var serviceTypeData = that.isEmptyServicetype ? serviceTypeWithEmptyEntity : serviceTypeArr;
  200. if (that.isGroupView) {
  201. var serviceDataWithRootEntity = pushRootEntityToJstree.call(that, "group", serviceTypeData);
  202. return getParentsData.call(that, serviceDataWithRootEntity);
  203. }
  204. return pushRootEntityToJstree.call(that, null, serviceTypeData);
  205. }), pushRootEntityToJstree = function(type, data) {
  206. var rootEntity = Globals[Enums.addOnEntities[0]], isSelected = !(!this.options.value || !this.options.value.type) && this.options.value.type == rootEntity.name, rootEntityNode = {
  207. text: _.escape(rootEntity.name),
  208. name: rootEntity.name,
  209. type: rootEntity.category,
  210. gType: "Entity",
  211. guid: rootEntity.guid,
  212. id: rootEntity.guid,
  213. model: rootEntity,
  214. parent: "#",
  215. icon: "fa fa-file-o",
  216. state: {
  217. selected: isSelected
  218. }
  219. };
  220. return "group" === type ? (void 0 === data.other_types && (data.other_types = {
  221. name: "other_types",
  222. children: []
  223. }), data.other_types.children.push(rootEntityNode)) : data.push(rootEntityNode),
  224. data;
  225. }, getParentsData = function(data) {
  226. for (var parents = Object.keys(data), treeData = [], withoutEmptyServiceType = [], treeCoreData = null, openEntityNodesState = function(treeDate) {
  227. 1 == treeDate.length && _.each(treeDate, function(model) {
  228. model.state = {
  229. opened: !0
  230. };
  231. });
  232. }, generateNode = function(children) {
  233. var nodeStructure = {
  234. text: "Service Types",
  235. children: children,
  236. icon: "fa fa-folder-o",
  237. type: "ENTITY",
  238. state: {
  239. opened: !0
  240. },
  241. parent: "#"
  242. };
  243. return nodeStructure;
  244. }, i = 0; i < parents.length; i++) {
  245. var checkEmptyServiceType = !1, getParrent = data[parents[i]], totalCounter = getParrent.totalCounter, textName = getParrent.totalCounter ? parents[i] + " (" + _.numberFormatWithComma(totalCounter) + ")" : parents[i], parent = {
  246. icon: "fa fa-folder-o",
  247. type: type,
  248. gType: "ServiceType",
  249. children: getParrent.children,
  250. text: _.escape(textName),
  251. name: data[parents[i]].name,
  252. id: i,
  253. state: {
  254. opened: !0
  255. }
  256. };
  257. that.isEmptyServicetype && 0 == data[parents[i]].totalCounter && (checkEmptyServiceType = !0),
  258. treeData.push(parent), checkEmptyServiceType || withoutEmptyServiceType.push(parent);
  259. }
  260. return that.entityTreeData = {
  261. withoutEmptyServiceTypeEntity: generateNode(withoutEmptyServiceType),
  262. withEmptyServiceTypeEntity: generateNode(treeData)
  263. }, treeCoreData = that.isEmptyServicetype ? withoutEmptyServiceType : treeData,
  264. openEntityNodesState(treeCoreData), treeCoreData;
  265. };
  266. return generateTreeData();
  267. },
  268. generateSearchTree: function(options) {
  269. var $el = options && options.$el, that = (options && options.data, options && options.type,
  270. this), getEntityTreeConfig = function(opt) {
  271. return {
  272. plugins: [ "search", "core", "sort", "conditionalselect", "changed", "wholerow", "node_customize" ],
  273. conditionalselect: function(node) {
  274. var type = node.original.type;
  275. return "ENTITY" != type && "GLOSSARY" != type || !node.children.length && "GLOSSARY" != type;
  276. },
  277. state: {
  278. opened: !0
  279. },
  280. search: {
  281. show_only_matches: !0,
  282. case_sensitive: !1
  283. },
  284. node_customize: {
  285. default: function(el) {
  286. var aType = $(el).find(">a.jstree-anchor");
  287. aType.append("<span class='tree-tooltip'>" + aType.text() + "</span>"), 0 === $(el).find(".fa-ellipsis-h").length && $(el).append('<div class="tools"><i class="fa fa-ellipsis-h entityPopover" rel="popover"></i></div>');
  288. }
  289. },
  290. core: {
  291. multiple: !1,
  292. data: function(node, cb) {
  293. "#" === node.id && cb(that.getEntityTree());
  294. }
  295. }
  296. };
  297. };
  298. $el.jstree(getEntityTreeConfig({
  299. type: ""
  300. })).on("open_node.jstree", function(e, data) {
  301. that.isTreeOpen = !0;
  302. }).on("select_node.jstree", function(e, data) {
  303. that.fromManualRender ? that.fromManualRender = !1 : that.onNodeSelect(data);
  304. }).on("search.jstree", function(nodes, str, res) {
  305. 0 === str.nodes.length ? ($el.jstree(!0).hide_all(), $el.parents(".panel").addClass("hide")) : $el.parents(".panel").removeClass("hide");
  306. }).on("hover_node.jstree", function(nodes, str, res) {
  307. var aType = that.$("#" + str.node.a_attr.id), typeOffset = aType.find(">.jstree-icon").offset();
  308. that.$(".tree-tooltip").removeClass("show"), setTimeout(function() {
  309. aType.hasClass("jstree-hovered") && typeOffset.top && typeOffset.left && aType.find(">span.tree-tooltip").css({
  310. top: "calc(" + typeOffset.top + "px - 45px)",
  311. left: "24px"
  312. }).addClass("show");
  313. }, 1200);
  314. }).on("dehover_node.jstree", function(nodes, str, res) {
  315. that.$(".tree-tooltip").removeClass("show");
  316. });
  317. },
  318. refresh: function(options) {
  319. var that = this, apiCount = options && 0 == options.apiCount ? options.apiCount : 3, renderTree = function() {
  320. 0 === apiCount && (that.renderEntityTree(), that.changeLoaderState(!1), that.ui.refreshTree.attr("disabled", !1));
  321. };
  322. 0 == apiCount ? (that.entityDefCollection.fullCollection.sort({
  323. silent: !0
  324. }), that.entityCountObj = _.first(that.metricCollection.toJSON()), that.typeHeaders.fullCollection.sort({
  325. silent: !0
  326. }), renderTree()) : (this.entityDefCollection.fetch({
  327. complete: function() {
  328. that.entityDefCollection.fullCollection.comparator = function(model) {
  329. return model.get("name").toLowerCase();
  330. }, that.entityDefCollection.fullCollection.sort({
  331. silent: !0
  332. }), --apiCount, renderTree();
  333. }
  334. }), this.metricCollection.fetch({
  335. complete: function() {
  336. --apiCount, that.entityCountObj = _.first(that.metricCollection.toJSON()), renderTree();
  337. }
  338. }), this.typeHeaders.fetch({
  339. complete: function() {
  340. that.typeHeaders.fullCollection.comparator = function(model) {
  341. return model.get("name").toLowerCase();
  342. }, that.typeHeaders.fullCollection.sort({
  343. silent: !0
  344. }), --apiCount, renderTree();
  345. }
  346. }));
  347. },
  348. onClickImportBusinessMetadata: function() {
  349. require([ "views/import/ImportLayoutView" ], function(ImportLayoutView) {
  350. new ImportLayoutView({});
  351. });
  352. }
  353. });
  354. return EntityTreeLayoutview;
  355. });