RelationshipLayoutView.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. define([ "require", "backbone", "hbs!tmpl/graph/RelationshipLayoutView_tmpl", "collection/VLineageList", "models/VEntity", "utils/Utils", "utils/CommonViewFunction", "d3", "d3-tip", "utils/Enums", "utils/UrlLinks", "platform" ], function(require, Backbone, RelationshipLayoutViewtmpl, VLineageList, VEntity, Utils, CommonViewFunction, d3, d3Tip, Enums, UrlLinks, platform) {
  2. "use strict";
  3. var RelationshipLayoutView = Backbone.Marionette.LayoutView.extend({
  4. _viewName: "RelationshipLayoutView",
  5. template: RelationshipLayoutViewtmpl,
  6. className: "resizeGraph",
  7. regions: {},
  8. ui: {
  9. relationshipDetailClose: '[data-id="close"]',
  10. searchNode: '[data-id="searchNode"]',
  11. relationshipViewToggle: 'input[name="relationshipViewToggle"]',
  12. relationshipDetailTable: "[data-id='relationshipDetailTable']",
  13. relationshipSVG: "[data-id='relationshipSVG']",
  14. relationshipDetailValue: "[data-id='relationshipDetailValue']",
  15. zoomControl: "[data-id='zoomControl']",
  16. boxClose: '[data-id="box-close"]',
  17. noValueToggle: "[data-id='noValueToggle']",
  18. relationshipDetails: ".relationship-details",
  19. noData: ".no-data"
  20. },
  21. events: function() {
  22. var events = {};
  23. return events["click " + this.ui.relationshipDetailClose] = function() {
  24. this.toggleInformationSlider({
  25. close: !0
  26. });
  27. }, events["keyup " + this.ui.searchNode] = "searchNode", events["click " + this.ui.boxClose] = "toggleBoxPanel",
  28. events["change " + this.ui.relationshipViewToggle] = function(e) {
  29. this.relationshipViewToggle(e.currentTarget.checked);
  30. }, events["click " + this.ui.noValueToggle] = function(e) {
  31. Utils.togglePropertyRelationshipTableEmptyValues({
  32. inputType: this.ui.noValueToggle,
  33. tableEl: this.ui.relationshipDetailValue
  34. });
  35. }, events;
  36. },
  37. initialize: function(options) {
  38. _.extend(this, _.pick(options, "entity", "entityName", "guid", "actionCallBack", "attributeDefs")),
  39. this.graphData = this.createData(this.entity);
  40. },
  41. createData: function(entity) {
  42. var that = this, links = [], nodes = {};
  43. return entity && entity.relationshipAttributes && _.each(entity.relationshipAttributes, function(obj, key) {
  44. _.isEmpty(obj) || links.push({
  45. source: nodes[that.entity.typeName] || (nodes[that.entity.typeName] = _.extend({
  46. name: that.entity.typeName
  47. }, {
  48. value: entity
  49. })),
  50. target: nodes[key] || (nodes[key] = _.extend({
  51. name: key
  52. }, {
  53. value: obj
  54. })),
  55. value: obj
  56. });
  57. }), {
  58. nodes: nodes,
  59. links: links
  60. };
  61. },
  62. onRender: function() {
  63. this.ui.zoomControl.hide(), this.$el.addClass("auto-height");
  64. },
  65. onShow: function(argument) {
  66. this.graphData && _.isEmpty(this.graphData.links) ? this.noRelationship() : this.createGraph(this.graphData),
  67. this.createTable();
  68. },
  69. noRelationship: function() {
  70. this.ui.relationshipDetails.hide(), this.ui.noData.show();
  71. },
  72. toggleInformationSlider: function(options) {
  73. options.open && !this.$(".relationship-details").hasClass("open") ? this.$(".relationship-details").addClass("open") : options.close && this.$(".relationship-details").hasClass("open") && (d3.selectAll("circle").attr("stroke", "none"),
  74. this.$(".relationship-details").removeClass("open"));
  75. },
  76. toggleBoxPanel: function(options) {
  77. var el = options && options.el;
  78. options && options.nodeDetailToggler, options.currentTarget;
  79. this.$el.find(".show-box-panel").removeClass("show-box-panel"), el && el.addClass && el.addClass("show-box-panel"),
  80. this.$("circle.node-detail-highlight").removeClass("node-detail-highlight");
  81. },
  82. searchNode: function(e) {
  83. var $el = $(e.currentTarget);
  84. this.updateRelationshipDetails(_.extend({}, $el.data(), {
  85. searchString: $el.val()
  86. }));
  87. },
  88. updateRelationshipDetails: function(options) {
  89. var data = options.obj.value, typeName = data.typeName || options.obj.name, searchString = _.escape(options.searchString), listString = "", getEntityTypelist = function(options) {
  90. var activeEntityColor = "#4a90e2", deletedEntityColor = "#BB5838", entityTypeHtml = "<pre>", getdefault = function(obj) {
  91. var options = obj.options, status = Enums.entityStateReadOnly[options.entityStatus || options.status] ? " deleted-relation" : "", guid = options.guid, entityColor = obj.color, name = obj.name, typeName = options.typeName;
  92. return "AtlasGlossaryTerm" === typeName ? "<li class=" + status + '><a style="color:' + entityColor + '" href="#!/glossary/' + guid + "?guid=" + guid + '&gType=term&viewType=term&fromView=entity">' + name + " (" + typeName + ")</a></li>" : "<li class=" + status + "><a style='color:" + entityColor + "' href=#!/detailPage/" + guid + "?tabActive=relationship>" + name + " (" + typeName + ")</a></li>";
  93. }, getWithButton = function(obj) {
  94. var options = obj.options, status = Enums.entityStateReadOnly[options.entityStatus || options.status] ? " deleted-relation" : "", entityColor = (options.guid,
  95. obj.color), name = obj.name, relationship = (options.typeName, obj.relationship || !1), icon = (obj.entity || !1,
  96. '<i class="fa fa-trash"></i>'), title = "Deleted";
  97. return relationship && (icon = '<i class="fa fa-long-arrow-right"></i>', status = Enums.entityStateReadOnly[options.relationshipStatus || options.status] ? "deleted-relation" : "",
  98. title = "Relationship Deleted"), "<li class=" + status + "><a style='color:" + entityColor + "' href=#!/detailPage/" + options.guid + "?tabActive=relationship>" + _.escape(name) + " (" + options.typeName + ')</a><button type="button" title="' + title + '" class="btn btn-sm deleteBtn deletedTableBtn btn-action ">' + icon + "</button></li>";
  99. }, name = options.entityName ? options.entityName : Utils.getName(options, "displayText");
  100. return "ACTIVE" == options.entityStatus ? "ACTIVE" == options.relationshipStatus ? entityTypeHtml = getdefault({
  101. color: activeEntityColor,
  102. options: options,
  103. name: name
  104. }) : "DELETED" == options.relationshipStatus && (entityTypeHtml = getWithButton({
  105. color: activeEntityColor,
  106. options: options,
  107. name: name,
  108. relationship: !0
  109. })) : entityTypeHtml = "DELETED" == options.entityStatus ? getWithButton({
  110. color: deletedEntityColor,
  111. options: options,
  112. name: name,
  113. entity: !0
  114. }) : getdefault({
  115. color: activeEntityColor,
  116. options: options,
  117. name: name
  118. }), entityTypeHtml + "</pre>";
  119. };
  120. this.ui.searchNode.hide(), this.$("[data-id='typeName']").text(typeName);
  121. var getElement = function(options) {
  122. var entityTypeButton = (options.entityName ? options.entityName : Utils.getName(options, "displayText"),
  123. getEntityTypelist(options));
  124. return entityTypeButton;
  125. };
  126. _.isArray(data) ? (data.length > 1 && this.ui.searchNode.show(), _.each(_.sortBy(data, "displayText"), function(val) {
  127. var name = Utils.getName(val, "displayText"), valObj = _.extend({}, val, {
  128. entityName: name
  129. });
  130. if (searchString) {
  131. if (name.search(new RegExp(searchString, "i")) == -1) return;
  132. listString += getElement(valObj);
  133. } else listString += getElement(valObj);
  134. })) : listString += getElement(data), this.$("[data-id='entityList']").html(listString);
  135. },
  136. createGraph: function(data) {
  137. function update() {
  138. path = container.append("svg:g").selectAll("path").data(links).enter().append("svg:path").attr("class", "relatioship-link").attr("stroke", function(d) {
  139. return getPathColor({
  140. data: d,
  141. type: "path"
  142. });
  143. }).attr("marker-end", function(d) {
  144. return "url(#" + (isAllEntityRelationDeleted({
  145. data: d
  146. }) ? "deletedLink" : "activeLink") + ")";
  147. }), node = container.selectAll(".node").data(nodes).enter().append("g").attr("class", "node").on("mousedown", function() {
  148. console.log(d3.event), d3.event.preventDefault();
  149. }).on("click", function(d) {
  150. if (!d3.event.defaultPrevented) {
  151. if (d && d.value && d.value.guid == that.guid) return void that.ui.boxClose.trigger("click");
  152. that.toggleBoxPanel({
  153. el: that.$(".relationship-node-details")
  154. }), that.ui.searchNode.data({
  155. obj: d
  156. }), $(this).find("circle").addClass("node-detail-highlight"), that.updateRelationshipDetails({
  157. obj: d
  158. });
  159. }
  160. }).call(d3.drag().on("start", dragstarted).on("drag", dragged));
  161. var circleContainer = node.append("g");
  162. circleContainer.append("circle").attr("cx", 0).attr("cy", 0).attr("r", function(d) {
  163. return d.radius = 25, d.radius;
  164. }).attr("fill", function(d) {
  165. return d && d.value && d.value.guid == that.guid ? isAllEntityRelationDeleted({
  166. data: d,
  167. type: "node"
  168. }) ? deletedEntityColor : selectedNodeColor : isAllEntityRelationDeleted({
  169. data: d,
  170. type: "node"
  171. }) ? deletedEntityColor : activeEntityColor;
  172. }).attr("typename", function(d) {
  173. return d.name;
  174. }), circleContainer.append("text").attr("x", 0).attr("y", 0).attr("dy", 8).attr("text-anchor", "middle").style("font-family", "FontAwesome").style("font-size", function(d) {
  175. return "25px";
  176. }).text(function(d) {
  177. var iconObj = Enums.graphIcon[d.name];
  178. return iconObj && iconObj.textContent ? iconObj.textContent : d && _.isArray(d.value) && d.value.length > 1 ? "" : "";
  179. }).attr("fill", function(d) {
  180. return "#fff";
  181. });
  182. var countBox = circleContainer.append("g");
  183. countBox.append("circle").attr("cx", 18).attr("cy", -20).attr("r", function(d) {
  184. if (_.isArray(d.value) && d.value.length > 1) return 10;
  185. }), countBox.append("text").attr("dx", 18).attr("dy", -16).attr("text-anchor", "middle").attr("fill", defaultEntityColor).text(function(d) {
  186. if (_.isArray(d.value) && d.value.length > 1) return d.value.length;
  187. }), node.append("text").attr("x", -15).attr("y", "35").text(function(d) {
  188. return d.name;
  189. }), simulation.nodes(nodes).on("tick", ticked), simulation.force("link").links(links);
  190. }
  191. function ticked() {
  192. path.attr("d", function(d) {
  193. var diffX = d.target.x - d.source.x, diffY = d.target.y - d.source.y, pathLength = Math.sqrt(diffX * diffX + diffY * diffY), offsetX = diffX * d.target.radius / pathLength, offsetY = diffY * d.target.radius / pathLength;
  194. return "M" + d.source.x + "," + d.source.y + "A" + pathLength + "," + pathLength + " 0 0,1 " + (d.target.x - offsetX) + "," + (d.target.y - offsetY);
  195. }), node.attr("transform", function(d) {
  196. return "translate(" + d.x + "," + d.y + ")";
  197. });
  198. }
  199. function dragstarted(d) {
  200. d3.event.sourceEvent.stopPropagation(), d && d.value && d.value.guid != that.guid && (d3.event.active || simulation.alphaTarget(.3).restart(),
  201. d.fx = d.x, d.fy = d.y);
  202. }
  203. function dragged(d) {
  204. d && d.value && d.value.guid != that.guid && (d.fx = d3.event.x, d.fy = d3.event.y);
  205. }
  206. function getPathColor(options) {
  207. return isAllEntityRelationDeleted(options) ? deletedEntityColor : activeEntityColor;
  208. }
  209. function isAllEntityRelationDeleted(options) {
  210. var data = options.data, type = options.type, d = $.extend(!0, {}, data);
  211. return d && !_.isArray(d.value) && (d.value = [ d.value ]), _.findIndex(d.value, function(val) {
  212. return "node" == type ? "ACTIVE" == (val.entityStatus || val.status) : "ACTIVE" == val.relationshipStatus;
  213. }) == -1;
  214. }
  215. var node, path, that = this, width = this.$("svg").width(), height = this.$("svg").height(), nodes = d3.values(data.nodes), links = data.links, activeEntityColor = "#00b98b", deletedEntityColor = "#BB5838", defaultEntityColor = "#e0e0e0", selectedNodeColor = "#4a90e2", svg = d3.select(this.$("svg")[0]).attr("viewBox", "0 0 " + width + " " + height).attr("enable-background", "new 0 0 " + width + " " + height), container = svg.append("g").attr("id", "container").attr("transform", "translate(0,0)scale(1,1)"), zoom = d3.zoom().scaleExtent([ .1, 4 ]).on("zoom", function() {
  216. container.attr("transform", d3.event.transform);
  217. });
  218. svg.call(zoom).on("dblclick.zoom", null), container.append("svg:defs").selectAll("marker").data([ "deletedLink", "activeLink" ]).enter().append("svg:marker").attr("id", String).attr("viewBox", "-0 -5 10 10").attr("refX", 10).attr("refY", -.5).attr("orient", "auto").attr("markerWidth", 6).attr("markerHeight", 6).append("svg:path").attr("d", "M 0,-5 L 10 ,0 L 0,5").attr("fill", function(d) {
  219. return "deletedLink" == d ? deletedEntityColor : activeEntityColor;
  220. }).style("stroke", "none");
  221. var forceLink = d3.forceLink().id(function(d) {
  222. return d.id;
  223. }).distance(function(d) {
  224. return 100;
  225. }).strength(1), simulation = d3.forceSimulation().force("link", forceLink).force("charge", d3.forceManyBody()).force("center", d3.forceCenter(width / 2, height / 2));
  226. update();
  227. var zoomClick = function() {
  228. var scaleFactor = .8;
  229. "zoom_in" === this.id && (scaleFactor = 1.3), zoom.scaleBy(svg.transition().duration(750), scaleFactor);
  230. };
  231. d3.selectAll(this.$(".lineageZoomButton")).on("click", zoomClick);
  232. },
  233. createTable: function() {
  234. this.entityModel = new VEntity({});
  235. var table = CommonViewFunction.propertyTable({
  236. scope: this,
  237. valueObject: this.entity.relationshipAttributes,
  238. attributeDefs: this.attributeDefs
  239. });
  240. this.ui.relationshipDetailValue.html(table), Utils.togglePropertyRelationshipTableEmptyValues({
  241. inputType: this.ui.noValueToggle,
  242. tableEl: this.ui.relationshipDetailValue
  243. });
  244. },
  245. relationshipViewToggle: function(checked) {
  246. this.ui.relationshipDetailTable.toggleClass("visible invisible"), this.ui.relationshipSVG.toggleClass("visible invisible"),
  247. checked ? (this.ui.zoomControl.hide(), this.$el.addClass("auto-height")) : (this.ui.zoomControl.show(),
  248. this.$el.removeClass("auto-height"));
  249. }
  250. });
  251. return RelationshipLayoutView;
  252. });