node_item.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * DevExtreme (viz/sankey/node_item.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var states = ["normal", "hover"];
  11. var isDefined = require("../../core/utils/type").isDefined;
  12. function _compileAttrs(color, itemOptions, itemBaseOptions) {
  13. var border = itemOptions.border;
  14. var baseBorder = itemBaseOptions.border;
  15. var borderVisible = isDefined(border.visible) ? border.visible : baseBorder.visible;
  16. var borderWidth = isDefined(border.width) ? border.width : baseBorder.width;
  17. var borderOpacity = isDefined(border.opacity) ? border.opacity : isDefined(baseBorder.opacity) ? baseBorder.opacity : 1;
  18. var opacity = isDefined(itemOptions.opacity) ? itemOptions.opacity : isDefined(itemBaseOptions.opacity) ? itemBaseOptions.opacity : 1;
  19. return {
  20. fill: itemOptions.color || color,
  21. "stroke-width": borderVisible ? borderWidth : 0,
  22. stroke: itemOptions.border.color || itemBaseOptions.border.color,
  23. "stroke-opacity": borderOpacity,
  24. opacity: opacity,
  25. hatching: itemOptions.hatching
  26. }
  27. }
  28. function compileLabelAttrs(labelOptions, filter, node) {
  29. var _patchFontOptions = require("../core/utils").patchFontOptions;
  30. if (labelOptions.useNodeColors) {
  31. labelOptions.font.color = node.color
  32. }
  33. var borderVisible = isDefined(labelOptions.border.visible) ? labelOptions.border.visible : false;
  34. var borderWidth = isDefined(labelOptions.border.width) ? labelOptions.border.width : 0;
  35. var borderColor = isDefined(labelOptions.border.color) ? labelOptions.border.color : labelOptions.font.color;
  36. var borderOpacity = isDefined(labelOptions.border.opacity) ? labelOptions.border.opacity : 1;
  37. var attr = {
  38. filter: filter
  39. };
  40. if (borderVisible && borderWidth) {
  41. attr.stroke = borderColor;
  42. attr["stroke-width"] = borderVisible ? borderWidth : 0;
  43. attr["stroke-opacity"] = borderOpacity
  44. }
  45. return {
  46. attr: attr,
  47. css: _patchFontOptions(labelOptions.font)
  48. }
  49. }
  50. function Node(widget, params) {
  51. var that = this;
  52. var widgetOffset = widget._renderer.getRootOffset();
  53. that.code = 0;
  54. that.widget = widget;
  55. that.color = params.color;
  56. that.options = params.options;
  57. that.rect = params.rect;
  58. that.title = params.rect._name;
  59. that.coords = {
  60. x: params.rect.x + params.rect.width / 2 + widgetOffset.left,
  61. y: params.rect.y + params.rect.height / 2 + widgetOffset.top
  62. };
  63. that.id = params.id;
  64. that.linksIn = params.linksIn;
  65. that.linksOut = params.linksOut;
  66. this.states = {
  67. normal: _compileAttrs(this.color, that.options, that.options),
  68. hover: _compileAttrs(this.color, that.options.hoverStyle, that.options)
  69. }
  70. }
  71. Node.prototype = {
  72. compileAttrs: function() {
  73. return _compileAttrs(this.color, this.options)
  74. },
  75. getState: function() {
  76. return states[this.code]
  77. },
  78. isHovered: function() {
  79. return !!(1 & this.code)
  80. },
  81. setState: function(code, state) {
  82. var _this = this;
  83. if (state) {
  84. this.code |= code
  85. } else {
  86. this.code &= ~code
  87. }
  88. if (state) {
  89. this.linksIn.concat(this.linksOut).forEach(function(adjacentLink) {
  90. _this.widget._links[adjacentLink.index].setAdjacentNodeHover(true)
  91. })
  92. } else {
  93. this.widget._links.forEach(function(link) {
  94. link.isAdjacentNodeHovered() && link.adjacentNodeHover(false)
  95. });
  96. this.hideTooltip()
  97. }
  98. this.widget._applyNodesAppearance();
  99. this.widget._applyLinksAppearance()
  100. },
  101. hover: function(state) {
  102. if (!this.widget._getOption("hoverEnabled", true) || state === this.isHovered()) {
  103. return
  104. }
  105. this.widget._suspend();
  106. state && this.widget.clearHover();
  107. this.setState(1, state);
  108. this.widget._eventTrigger("nodeHoverChanged", {
  109. target: this
  110. });
  111. this.widget._resume()
  112. },
  113. setHover: function() {
  114. this.hover(true)
  115. },
  116. showTooltip: function(coords) {
  117. this.widget._getOption("hoverEnabled", true) && this.widget._tooltip && this.widget._tooltip.show({
  118. type: "node",
  119. info: {
  120. title: this.title,
  121. weightIn: this.linksIn.reduce(function(previousValue, currentValue) {
  122. return previousValue + currentValue.weight
  123. }, 0),
  124. weightOut: this.linksOut.reduce(function(previousValue, currentValue) {
  125. return previousValue + currentValue.weight
  126. }, 0)
  127. }
  128. }, "undefined" !== typeof coords ? {
  129. x: coords[0],
  130. y: coords[1]
  131. } : this.coords)
  132. },
  133. hideTooltip: function() {
  134. this.widget._tooltip && this.widget._tooltip.hide()
  135. },
  136. getLabelAttributes: function(labelSettings, filter) {
  137. return compileLabelAttrs(labelSettings, filter, this)
  138. }
  139. };
  140. module.exports = Node;