Definable.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { createElement } from '../../svg/core.js';
  2. import * as zrUtil from '../../core/util.js';
  3. var MARK_UNUSED = '0';
  4. var MARK_USED = '1';
  5. var Definable = (function () {
  6. function Definable(zrId, svgRoot, tagNames, markLabel, domName) {
  7. this.nextId = 0;
  8. this._domName = '_dom';
  9. this._zrId = zrId;
  10. this._svgRoot = svgRoot;
  11. this._tagNames = typeof tagNames === 'string' ? [tagNames] : tagNames;
  12. this._markLabel = markLabel;
  13. if (domName) {
  14. this._domName = domName;
  15. }
  16. }
  17. Definable.prototype.getDefs = function (isForceCreating) {
  18. var svgRoot = this._svgRoot;
  19. var defs = this._svgRoot.getElementsByTagName('defs');
  20. if (defs.length === 0) {
  21. if (isForceCreating) {
  22. var defs_1 = svgRoot.insertBefore(createElement('defs'), svgRoot.firstChild);
  23. if (!defs_1.contains) {
  24. defs_1.contains = function (el) {
  25. var children = defs_1.children;
  26. if (!children) {
  27. return false;
  28. }
  29. for (var i = children.length - 1; i >= 0; --i) {
  30. if (children[i] === el) {
  31. return true;
  32. }
  33. }
  34. return false;
  35. };
  36. }
  37. return defs_1;
  38. }
  39. else {
  40. return null;
  41. }
  42. }
  43. else {
  44. return defs[0];
  45. }
  46. };
  47. Definable.prototype.doUpdate = function (target, onUpdate) {
  48. if (!target) {
  49. return;
  50. }
  51. var defs = this.getDefs(false);
  52. if (target[this._domName] && defs.contains(target[this._domName])) {
  53. if (typeof onUpdate === 'function') {
  54. onUpdate(target);
  55. }
  56. }
  57. else {
  58. var dom = this.add(target);
  59. if (dom) {
  60. target[this._domName] = dom;
  61. }
  62. }
  63. };
  64. Definable.prototype.add = function (target) {
  65. return null;
  66. };
  67. Definable.prototype.addDom = function (dom) {
  68. var defs = this.getDefs(true);
  69. if (dom.parentNode !== defs) {
  70. defs.appendChild(dom);
  71. }
  72. };
  73. Definable.prototype.removeDom = function (target) {
  74. var defs = this.getDefs(false);
  75. if (defs && target[this._domName]) {
  76. defs.removeChild(target[this._domName]);
  77. target[this._domName] = null;
  78. }
  79. };
  80. Definable.prototype.getDoms = function () {
  81. var defs = this.getDefs(false);
  82. if (!defs) {
  83. return [];
  84. }
  85. var doms = [];
  86. zrUtil.each(this._tagNames, function (tagName) {
  87. var tags = defs.getElementsByTagName(tagName);
  88. for (var i = 0; i < tags.length; i++) {
  89. doms.push(tags[i]);
  90. }
  91. });
  92. return doms;
  93. };
  94. Definable.prototype.markAllUnused = function () {
  95. var doms = this.getDoms();
  96. var that = this;
  97. zrUtil.each(doms, function (dom) {
  98. dom[that._markLabel] = MARK_UNUSED;
  99. });
  100. };
  101. Definable.prototype.markDomUsed = function (dom) {
  102. dom && (dom[this._markLabel] = MARK_USED);
  103. };
  104. ;
  105. Definable.prototype.markDomUnused = function (dom) {
  106. dom && (dom[this._markLabel] = MARK_UNUSED);
  107. };
  108. ;
  109. Definable.prototype.isDomUnused = function (dom) {
  110. return dom && dom[this._markLabel] !== MARK_USED;
  111. };
  112. Definable.prototype.removeUnused = function () {
  113. var _this = this;
  114. var defs = this.getDefs(false);
  115. if (!defs) {
  116. return;
  117. }
  118. var doms = this.getDoms();
  119. zrUtil.each(doms, function (dom) {
  120. if (_this.isDomUnused(dom)) {
  121. defs.removeChild(dom);
  122. }
  123. });
  124. };
  125. Definable.prototype.getSvgElement = function (displayable) {
  126. return displayable.__svgEl;
  127. };
  128. return Definable;
  129. }());
  130. export default Definable;