Group.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. "use strict";
  2. exports.__esModule = true;
  3. var tslib_1 = require("tslib");
  4. var zrUtil = require("../core/util");
  5. var Element_1 = require("../Element");
  6. var BoundingRect_1 = require("../core/BoundingRect");
  7. var Group = (function (_super) {
  8. tslib_1.__extends(Group, _super);
  9. function Group(opts) {
  10. var _this = _super.call(this) || this;
  11. _this.isGroup = true;
  12. _this.type = 'group';
  13. _this._children = [];
  14. _this.attr(opts);
  15. return _this;
  16. }
  17. Group.prototype.children = function () {
  18. return this._children;
  19. };
  20. Group.prototype.childAt = function (idx) {
  21. return this._children[idx];
  22. };
  23. Group.prototype.childOfName = function (name) {
  24. var children = this._children;
  25. for (var i = 0; i < children.length; i++) {
  26. if (children[i].name === name) {
  27. return children[i];
  28. }
  29. }
  30. };
  31. Group.prototype.childCount = function () {
  32. return this._children.length;
  33. };
  34. Group.prototype.add = function (child) {
  35. if (child && child !== this && child.parent !== this) {
  36. this._children.push(child);
  37. this._doAdd(child);
  38. }
  39. return this;
  40. };
  41. Group.prototype.addBefore = function (child, nextSibling) {
  42. if (child && child !== this && child.parent !== this
  43. && nextSibling && nextSibling.parent === this) {
  44. var children = this._children;
  45. var idx = children.indexOf(nextSibling);
  46. if (idx >= 0) {
  47. children.splice(idx, 0, child);
  48. this._doAdd(child);
  49. }
  50. }
  51. return this;
  52. };
  53. Group.prototype._doAdd = function (child) {
  54. if (child.parent) {
  55. child.parent.remove(child);
  56. }
  57. child.parent = this;
  58. var storage = this.__storage;
  59. var zr = this.__zr;
  60. if (storage && storage !== child.__storage) {
  61. storage.addToStorage(child);
  62. if (child instanceof Group) {
  63. child.addChildrenToStorage(storage);
  64. }
  65. }
  66. zr && zr.refresh();
  67. };
  68. Group.prototype.remove = function (child) {
  69. var zr = this.__zr;
  70. var storage = this.__storage;
  71. var children = this._children;
  72. var idx = zrUtil.indexOf(children, child);
  73. if (idx < 0) {
  74. return this;
  75. }
  76. children.splice(idx, 1);
  77. child.parent = null;
  78. if (storage) {
  79. storage.delFromStorage(child);
  80. if (child instanceof Group) {
  81. child.delChildrenFromStorage(storage);
  82. }
  83. }
  84. zr && zr.refresh();
  85. return this;
  86. };
  87. Group.prototype.removeAll = function () {
  88. var children = this._children;
  89. var storage = this.__storage;
  90. for (var i = 0; i < children.length; i++) {
  91. var child = children[i];
  92. if (storage) {
  93. storage.delFromStorage(child);
  94. if (child instanceof Group) {
  95. child.delChildrenFromStorage(storage);
  96. }
  97. }
  98. child.parent = null;
  99. }
  100. children.length = 0;
  101. return this;
  102. };
  103. Group.prototype.eachChild = function (cb, context) {
  104. var children = this._children;
  105. for (var i = 0; i < children.length; i++) {
  106. var child = children[i];
  107. cb.call(context, child, i);
  108. }
  109. return this;
  110. };
  111. Group.prototype.traverse = function (cb, context) {
  112. for (var i = 0; i < this._children.length; i++) {
  113. var child = this._children[i];
  114. cb.call(context, child);
  115. if (child.type === 'group') {
  116. child.traverse(cb, context);
  117. }
  118. }
  119. return this;
  120. };
  121. Group.prototype.addChildrenToStorage = function (storage) {
  122. for (var i = 0; i < this._children.length; i++) {
  123. var child = this._children[i];
  124. storage.addToStorage(child);
  125. if (child instanceof Group) {
  126. child.addChildrenToStorage(storage);
  127. }
  128. }
  129. };
  130. Group.prototype.delChildrenFromStorage = function (storage) {
  131. for (var i = 0; i < this._children.length; i++) {
  132. var child = this._children[i];
  133. storage.delFromStorage(child);
  134. if (child instanceof Group) {
  135. child.delChildrenFromStorage(storage);
  136. }
  137. }
  138. };
  139. Group.prototype.dirty = function () {
  140. this.__dirty = true;
  141. this.__zr && this.__zr.refresh();
  142. return this;
  143. };
  144. Group.prototype.getBoundingRect = function (includeChildren) {
  145. var tmpRect = new BoundingRect_1["default"](0, 0, 0, 0);
  146. var children = includeChildren || this._children;
  147. var tmpMat = [];
  148. var rect = null;
  149. for (var i = 0; i < children.length; i++) {
  150. var child = children[i];
  151. if (child.ignore || child.invisible) {
  152. continue;
  153. }
  154. var childRect = child.getBoundingRect();
  155. var transform = child.getLocalTransform(tmpMat);
  156. if (transform) {
  157. tmpRect.copy(childRect);
  158. tmpRect.applyTransform(transform);
  159. rect = rect || tmpRect.clone();
  160. rect.union(tmpRect);
  161. }
  162. else {
  163. rect = rect || childRect.clone();
  164. rect.union(childRect);
  165. }
  166. }
  167. return rect || tmpRect;
  168. };
  169. return Group;
  170. }(Element_1["default"]));
  171. exports["default"] = Group;
  172. //# sourceMappingURL=Group.js.map