Storage.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import * as util from './core/util.js';
  2. import timsort from './core/timsort.js';
  3. import { REDRAW_BIT } from './graphic/constants.js';
  4. var invalidZErrorLogged = false;
  5. function logInvalidZError() {
  6. if (invalidZErrorLogged) {
  7. return;
  8. }
  9. invalidZErrorLogged = true;
  10. console.warn('z / z2 / zlevel of displayable is invalid, which may cause unexpected errors');
  11. }
  12. function shapeCompareFunc(a, b) {
  13. if (a.zlevel === b.zlevel) {
  14. if (a.z === b.z) {
  15. return a.z2 - b.z2;
  16. }
  17. return a.z - b.z;
  18. }
  19. return a.zlevel - b.zlevel;
  20. }
  21. var Storage = (function () {
  22. function Storage() {
  23. this._roots = [];
  24. this._displayList = [];
  25. this._displayListLen = 0;
  26. this.displayableSortFunc = shapeCompareFunc;
  27. }
  28. Storage.prototype.traverse = function (cb, context) {
  29. for (var i = 0; i < this._roots.length; i++) {
  30. this._roots[i].traverse(cb, context);
  31. }
  32. };
  33. Storage.prototype.getDisplayList = function (update, includeIgnore) {
  34. includeIgnore = includeIgnore || false;
  35. var displayList = this._displayList;
  36. if (update || !displayList.length) {
  37. this.updateDisplayList(includeIgnore);
  38. }
  39. return displayList;
  40. };
  41. Storage.prototype.updateDisplayList = function (includeIgnore) {
  42. this._displayListLen = 0;
  43. var roots = this._roots;
  44. var displayList = this._displayList;
  45. for (var i = 0, len = roots.length; i < len; i++) {
  46. this._updateAndAddDisplayable(roots[i], null, includeIgnore);
  47. }
  48. displayList.length = this._displayListLen;
  49. timsort(displayList, shapeCompareFunc);
  50. };
  51. Storage.prototype._updateAndAddDisplayable = function (el, clipPaths, includeIgnore) {
  52. if (el.ignore && !includeIgnore) {
  53. return;
  54. }
  55. el.beforeUpdate();
  56. el.update();
  57. el.afterUpdate();
  58. var userSetClipPath = el.getClipPath();
  59. if (el.ignoreClip) {
  60. clipPaths = null;
  61. }
  62. else if (userSetClipPath) {
  63. if (clipPaths) {
  64. clipPaths = clipPaths.slice();
  65. }
  66. else {
  67. clipPaths = [];
  68. }
  69. var currentClipPath = userSetClipPath;
  70. var parentClipPath = el;
  71. while (currentClipPath) {
  72. currentClipPath.parent = parentClipPath;
  73. currentClipPath.updateTransform();
  74. clipPaths.push(currentClipPath);
  75. parentClipPath = currentClipPath;
  76. currentClipPath = currentClipPath.getClipPath();
  77. }
  78. }
  79. if (el.childrenRef) {
  80. var children = el.childrenRef();
  81. for (var i = 0; i < children.length; i++) {
  82. var child = children[i];
  83. if (el.__dirty) {
  84. child.__dirty |= REDRAW_BIT;
  85. }
  86. this._updateAndAddDisplayable(child, clipPaths, includeIgnore);
  87. }
  88. el.__dirty = 0;
  89. }
  90. else {
  91. var disp = el;
  92. if (clipPaths && clipPaths.length) {
  93. disp.__clipPaths = clipPaths;
  94. }
  95. else if (disp.__clipPaths && disp.__clipPaths.length > 0) {
  96. disp.__clipPaths = [];
  97. }
  98. if (isNaN(disp.z)) {
  99. logInvalidZError();
  100. disp.z = 0;
  101. }
  102. if (isNaN(disp.z2)) {
  103. logInvalidZError();
  104. disp.z2 = 0;
  105. }
  106. if (isNaN(disp.zlevel)) {
  107. logInvalidZError();
  108. disp.zlevel = 0;
  109. }
  110. this._displayList[this._displayListLen++] = disp;
  111. }
  112. var decalEl = el.getDecalElement && el.getDecalElement();
  113. if (decalEl) {
  114. this._updateAndAddDisplayable(decalEl, clipPaths, includeIgnore);
  115. }
  116. var textGuide = el.getTextGuideLine();
  117. if (textGuide) {
  118. this._updateAndAddDisplayable(textGuide, clipPaths, includeIgnore);
  119. }
  120. var textEl = el.getTextContent();
  121. if (textEl) {
  122. this._updateAndAddDisplayable(textEl, clipPaths, includeIgnore);
  123. }
  124. };
  125. Storage.prototype.addRoot = function (el) {
  126. if (el.__zr && el.__zr.storage === this) {
  127. return;
  128. }
  129. this._roots.push(el);
  130. };
  131. Storage.prototype.delRoot = function (el) {
  132. if (el instanceof Array) {
  133. for (var i = 0, l = el.length; i < l; i++) {
  134. this.delRoot(el[i]);
  135. }
  136. return;
  137. }
  138. var idx = util.indexOf(this._roots, el);
  139. if (idx >= 0) {
  140. this._roots.splice(idx, 1);
  141. }
  142. };
  143. Storage.prototype.delAllRoots = function () {
  144. this._roots = [];
  145. this._displayList = [];
  146. this._displayListLen = 0;
  147. return;
  148. };
  149. Storage.prototype.getRoots = function () {
  150. return this._roots;
  151. };
  152. Storage.prototype.dispose = function () {
  153. this._displayList = null;
  154. this._roots = null;
  155. };
  156. return Storage;
  157. }());
  158. export default Storage;