defs.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. var __rest = (this && this.__rest) || function (s, e) {
  2. var t = {};
  3. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  4. t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  6. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  7. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  8. t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { StringExt, Dom, Vector } from '@antv/x6-common';
  13. import { Filter } from '../registry';
  14. import { Markup } from '../view';
  15. import { Base } from './base';
  16. export class DefsManager extends Base {
  17. get cid() {
  18. return this.graph.view.cid;
  19. }
  20. get svg() {
  21. return this.view.svg;
  22. }
  23. get defs() {
  24. return this.view.defs;
  25. }
  26. isDefined(id) {
  27. return this.svg.getElementById(id) != null;
  28. }
  29. filter(options) {
  30. let filterId = options.id;
  31. const name = options.name;
  32. if (!filterId) {
  33. filterId = `filter-${name}-${this.cid}-${StringExt.hashcode(JSON.stringify(options))}`;
  34. }
  35. if (!this.isDefined(filterId)) {
  36. const fn = Filter.registry.get(name);
  37. if (fn == null) {
  38. return Filter.registry.onNotFound(name);
  39. }
  40. const markup = fn(options.args || {});
  41. // Set the filter area to be 3x the bounding box of the cell
  42. // and center the filter around the cell.
  43. const attrs = Object.assign(Object.assign({ x: -1, y: -1, width: 3, height: 3, filterUnits: 'objectBoundingBox' }, options.attrs), { id: filterId });
  44. Vector.create(Markup.sanitize(markup), attrs).appendTo(this.defs);
  45. }
  46. return filterId;
  47. }
  48. gradient(options) {
  49. let id = options.id;
  50. const type = options.type;
  51. if (!id) {
  52. id = `gradient-${type}-${this.cid}-${StringExt.hashcode(JSON.stringify(options))}`;
  53. }
  54. if (!this.isDefined(id)) {
  55. const stops = options.stops;
  56. const arr = stops.map((stop) => {
  57. const opacity = stop.opacity != null && Number.isFinite(stop.opacity)
  58. ? stop.opacity
  59. : 1;
  60. return `<stop offset="${stop.offset}" stop-color="${stop.color}" stop-opacity="${opacity}"/>`;
  61. });
  62. const markup = `<${type}>${arr.join('')}</${type}>`;
  63. const attrs = Object.assign({ id }, options.attrs);
  64. Vector.create(markup, attrs).appendTo(this.defs);
  65. }
  66. return id;
  67. }
  68. marker(options) {
  69. const { id, refX, refY, markerUnits, markerOrient, tagName, children } = options, attrs = __rest(options, ["id", "refX", "refY", "markerUnits", "markerOrient", "tagName", "children"]);
  70. let markerId = id;
  71. if (!markerId) {
  72. markerId = `marker-${this.cid}-${StringExt.hashcode(JSON.stringify(options))}`;
  73. }
  74. if (!this.isDefined(markerId)) {
  75. if (tagName !== 'path') {
  76. // remove unnecessary d attribute inherit from standard edge.
  77. delete attrs.d;
  78. }
  79. const pathMarker = Vector.create('marker', {
  80. refX,
  81. refY,
  82. id: markerId,
  83. overflow: 'visible',
  84. orient: markerOrient != null ? markerOrient : 'auto',
  85. markerUnits: markerUnits || 'userSpaceOnUse',
  86. }, children
  87. ? children.map((_a) => {
  88. var { tagName } = _a, other = __rest(_a, ["tagName"]);
  89. return Vector.create(`${tagName}` || 'path', Dom.kebablizeAttrs(Object.assign(Object.assign({}, attrs), other)));
  90. })
  91. : [Vector.create(tagName || 'path', Dom.kebablizeAttrs(attrs))]);
  92. this.defs.appendChild(pathMarker.node);
  93. }
  94. return markerId;
  95. }
  96. remove(id) {
  97. const elem = this.svg.getElementById(id);
  98. if (elem && elem.parentNode) {
  99. elem.parentNode.removeChild(elem);
  100. }
  101. }
  102. }
  103. //# sourceMappingURL=defs.js.map