Definable.js 4.7 KB

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