index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @fileoverview defs
  3. * @author dengfuping_develop@163.com
  4. */
  5. import { uniqueId } from '@antv/util';
  6. import Gradient from './gradient';
  7. import Shadow from './shadow';
  8. import Arrow from './arrow';
  9. import Clip from './clip';
  10. import Pattern from './pattern';
  11. import { createSVGElement } from '../util/dom';
  12. var Defs = /** @class */ (function () {
  13. function Defs(canvas) {
  14. var el = createSVGElement('defs');
  15. var id = uniqueId('defs_');
  16. el.id = id;
  17. canvas.appendChild(el);
  18. this.children = [];
  19. this.defaultArrow = {};
  20. this.el = el;
  21. this.canvas = canvas;
  22. }
  23. Defs.prototype.find = function (type, attr) {
  24. var children = this.children;
  25. var result = null;
  26. for (var i = 0; i < children.length; i++) {
  27. if (children[i].match(type, attr)) {
  28. result = children[i].id;
  29. break;
  30. }
  31. }
  32. return result;
  33. };
  34. Defs.prototype.findById = function (id) {
  35. var children = this.children;
  36. var flag = null;
  37. for (var i = 0; i < children.length; i++) {
  38. if (children[i].id === id) {
  39. flag = children[i];
  40. break;
  41. }
  42. }
  43. return flag;
  44. };
  45. Defs.prototype.add = function (item) {
  46. this.children.push(item);
  47. item.canvas = this.canvas;
  48. item.parent = this;
  49. };
  50. Defs.prototype.getDefaultArrow = function (attrs, name) {
  51. var stroke = attrs.stroke || attrs.strokeStyle;
  52. if (this.defaultArrow[stroke]) {
  53. return this.defaultArrow[stroke].id;
  54. }
  55. var arrow = new Arrow(attrs, name);
  56. this.defaultArrow[stroke] = arrow;
  57. this.el.appendChild(arrow.el);
  58. this.add(arrow);
  59. return arrow.id;
  60. };
  61. Defs.prototype.addGradient = function (cfg) {
  62. var gradient = new Gradient(cfg);
  63. this.el.appendChild(gradient.el);
  64. this.add(gradient);
  65. return gradient.id;
  66. };
  67. Defs.prototype.addArrow = function (attrs, name) {
  68. var arrow = new Arrow(attrs, name);
  69. this.el.appendChild(arrow.el);
  70. this.add(arrow);
  71. return arrow.id;
  72. };
  73. Defs.prototype.addShadow = function (cfg) {
  74. var shadow = new Shadow(cfg);
  75. this.el.appendChild(shadow.el);
  76. this.add(shadow);
  77. return shadow.id;
  78. };
  79. Defs.prototype.addPattern = function (cfg) {
  80. var pattern = new Pattern(cfg);
  81. this.el.appendChild(pattern.el);
  82. this.add(pattern);
  83. return pattern.id;
  84. };
  85. Defs.prototype.addClip = function (cfg) {
  86. var clip = new Clip(cfg);
  87. this.el.appendChild(clip.el);
  88. this.add(clip);
  89. return clip.id;
  90. };
  91. return Defs;
  92. }());
  93. export default Defs;
  94. //# sourceMappingURL=index.js.map