link_item.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * DevExtreme (viz/sankey/link_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 _constants = require("./constants");
  11. var states = ["normal", "adjacentNodeHover", "hover"];
  12. var isDefined = require("../../core/utils/type").isDefined;
  13. function compileAttrs(color, itemOptions, itemBaseOptions, gradient) {
  14. var border = itemOptions.border;
  15. var baseBorder = itemBaseOptions.border;
  16. var borderVisible = isDefined(border.visible) ? border.visible : baseBorder.visible;
  17. var borderWidth = isDefined(border.width) ? border.width : baseBorder.width;
  18. var borderOpacity = isDefined(border.opacity) ? border.opacity : isDefined(baseBorder.opacity) ? baseBorder.opacity : 1;
  19. var opacity = isDefined(itemOptions.opacity) ? itemOptions.opacity : isDefined(itemBaseOptions.opacity) ? itemBaseOptions.opacity : 1;
  20. var fill = itemOptions.color || color;
  21. if (itemBaseOptions.colorMode === _constants.COLOR_MODE_TARGET || itemBaseOptions.colorMode === _constants.COLOR_MODE_SOURCE) {
  22. fill = color
  23. } else {
  24. if (itemBaseOptions.colorMode === _constants.COLOR_MODE_GRADIENT && gradient && isDefined(gradient.id)) {
  25. fill = gradient.id
  26. }
  27. }
  28. return {
  29. fill: fill,
  30. "stroke-width": borderVisible ? borderWidth : 0,
  31. stroke: itemOptions.border.color || itemBaseOptions.border.color,
  32. "stroke-opacity": borderOpacity,
  33. opacity: opacity,
  34. hatching: itemOptions.hatching
  35. }
  36. }
  37. function Link(widget, params) {
  38. var that = this;
  39. var widgetOffset = widget._renderer.getRootOffset();
  40. that.code = 0;
  41. that.widget = widget;
  42. that.color = params.color;
  43. that.connection = params.connection;
  44. that.d = params.d;
  45. that.options = params.options;
  46. that.boundingRect = params.boundingRect, that.coords = {
  47. x: params.boundingRect.x + params.boundingRect.width / 2 + widgetOffset.left,
  48. y: params.boundingRect.y + params.boundingRect.height / 2 + widgetOffset.top
  49. };
  50. that.states = {
  51. normal: compileAttrs(that.color, that.options, that.options, params.gradient),
  52. adjacentNodeHover: compileAttrs(that.color, {
  53. opacity: 0,
  54. border: {}
  55. }, that.options, params.gradient),
  56. hover: compileAttrs(that.color, {
  57. opacity: 0,
  58. border: {}
  59. }, that.options, params.gradient)
  60. };
  61. that.overlayStates = {
  62. normal: compileAttrs(that.color, {
  63. opacity: 0,
  64. border: {}
  65. }, that.options),
  66. adjacentNodeHover: compileAttrs(that.color, that.options.hoverStyle, that.options),
  67. hover: compileAttrs(that.color, that.options.hoverStyle, that.options)
  68. }
  69. }
  70. Link.prototype = {
  71. getState: function() {
  72. return states[this.code]
  73. },
  74. isHovered: function() {
  75. return 2 === this.code
  76. },
  77. isAdjacentNodeHovered: function() {
  78. return 1 === this.code
  79. },
  80. setState: function(code, state) {
  81. if (state) {
  82. this.code = code
  83. } else {
  84. this.code = 0;
  85. this.hideTooltip()
  86. }
  87. this.widget._applyLinksAppearance()
  88. },
  89. setHover: function() {
  90. this.hover(true)
  91. },
  92. hover: function(state) {
  93. if (!this.widget._getOption("hoverEnabled", true) || state === this.isHovered()) {
  94. return
  95. }
  96. this.widget._suspend();
  97. state && this.widget.clearHover();
  98. this.setState(2, state);
  99. this.widget._eventTrigger("linkHoverChanged", {
  100. target: this
  101. });
  102. this.widget._resume()
  103. },
  104. adjacentNodeHover: function(state) {
  105. if (!this.widget._getOption("hoverEnabled", true) || state === this.isAdjacentNodeHovered()) {
  106. return
  107. }
  108. this.widget._suspend();
  109. this.setState(1, state);
  110. this.widget._resume()
  111. },
  112. setAdjacentNodeHover: function() {
  113. this.adjacentNodeHover(true)
  114. },
  115. showTooltip: function(coords) {
  116. this.widget._getOption("hoverEnabled", true) && this.widget._tooltip && this.widget._tooltip.show({
  117. type: "link",
  118. info: {
  119. source: this.connection.source,
  120. target: this.connection.target,
  121. weight: this.connection.weight
  122. }
  123. }, "undefined" !== typeof coords ? {
  124. x: coords[0],
  125. y: coords[1]
  126. } : this.coords)
  127. },
  128. hideTooltip: function() {
  129. this.widget._tooltip && this.widget._tooltip.hide()
  130. }
  131. };
  132. module.exports = Link;