Storage.js 5.1 KB

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