Group.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { __extends } from "tslib";
  2. import * as zrUtil from '../core/util.js';
  3. import Element from '../Element.js';
  4. import BoundingRect from '../core/BoundingRect.js';
  5. var Group = (function (_super) {
  6. __extends(Group, _super);
  7. function Group(opts) {
  8. var _this = _super.call(this) || this;
  9. _this.isGroup = true;
  10. _this._children = [];
  11. _this.attr(opts);
  12. return _this;
  13. }
  14. Group.prototype.childrenRef = function () {
  15. return this._children;
  16. };
  17. Group.prototype.children = function () {
  18. return this._children.slice();
  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) {
  36. if (child !== this && child.parent !== this) {
  37. this._children.push(child);
  38. this._doAdd(child);
  39. }
  40. if (process.env.NODE_ENV !== 'production') {
  41. if (child.__hostTarget) {
  42. throw 'This elemenet has been used as an attachment';
  43. }
  44. }
  45. }
  46. return this;
  47. };
  48. Group.prototype.addBefore = function (child, nextSibling) {
  49. if (child && child !== this && child.parent !== this
  50. && nextSibling && nextSibling.parent === this) {
  51. var children = this._children;
  52. var idx = children.indexOf(nextSibling);
  53. if (idx >= 0) {
  54. children.splice(idx, 0, child);
  55. this._doAdd(child);
  56. }
  57. }
  58. return this;
  59. };
  60. Group.prototype.replace = function (oldChild, newChild) {
  61. var idx = zrUtil.indexOf(this._children, oldChild);
  62. if (idx >= 0) {
  63. this.replaceAt(newChild, idx);
  64. }
  65. return this;
  66. };
  67. Group.prototype.replaceAt = function (child, index) {
  68. var children = this._children;
  69. var old = children[index];
  70. if (child && child !== this && child.parent !== this && child !== old) {
  71. children[index] = child;
  72. old.parent = null;
  73. var zr = this.__zr;
  74. if (zr) {
  75. old.removeSelfFromZr(zr);
  76. }
  77. this._doAdd(child);
  78. }
  79. return this;
  80. };
  81. Group.prototype._doAdd = function (child) {
  82. if (child.parent) {
  83. child.parent.remove(child);
  84. }
  85. child.parent = this;
  86. var zr = this.__zr;
  87. if (zr && zr !== child.__zr) {
  88. child.addSelfToZr(zr);
  89. }
  90. zr && zr.refresh();
  91. };
  92. Group.prototype.remove = function (child) {
  93. var zr = this.__zr;
  94. var children = this._children;
  95. var idx = zrUtil.indexOf(children, child);
  96. if (idx < 0) {
  97. return this;
  98. }
  99. children.splice(idx, 1);
  100. child.parent = null;
  101. if (zr) {
  102. child.removeSelfFromZr(zr);
  103. }
  104. zr && zr.refresh();
  105. return this;
  106. };
  107. Group.prototype.removeAll = function () {
  108. var children = this._children;
  109. var zr = this.__zr;
  110. for (var i = 0; i < children.length; i++) {
  111. var child = children[i];
  112. if (zr) {
  113. child.removeSelfFromZr(zr);
  114. }
  115. child.parent = null;
  116. }
  117. children.length = 0;
  118. return this;
  119. };
  120. Group.prototype.eachChild = function (cb, context) {
  121. var children = this._children;
  122. for (var i = 0; i < children.length; i++) {
  123. var child = children[i];
  124. cb.call(context, child, i);
  125. }
  126. return this;
  127. };
  128. Group.prototype.traverse = function (cb, context) {
  129. for (var i = 0; i < this._children.length; i++) {
  130. var child = this._children[i];
  131. var stopped = cb.call(context, child);
  132. if (child.isGroup && !stopped) {
  133. child.traverse(cb, context);
  134. }
  135. }
  136. return this;
  137. };
  138. Group.prototype.addSelfToZr = function (zr) {
  139. _super.prototype.addSelfToZr.call(this, zr);
  140. for (var i = 0; i < this._children.length; i++) {
  141. var child = this._children[i];
  142. child.addSelfToZr(zr);
  143. }
  144. };
  145. Group.prototype.removeSelfFromZr = function (zr) {
  146. _super.prototype.removeSelfFromZr.call(this, zr);
  147. for (var i = 0; i < this._children.length; i++) {
  148. var child = this._children[i];
  149. child.removeSelfFromZr(zr);
  150. }
  151. };
  152. Group.prototype.getBoundingRect = function (includeChildren) {
  153. var tmpRect = new BoundingRect(0, 0, 0, 0);
  154. var children = includeChildren || this._children;
  155. var tmpMat = [];
  156. var rect = null;
  157. for (var i = 0; i < children.length; i++) {
  158. var child = children[i];
  159. if (child.ignore || child.invisible) {
  160. continue;
  161. }
  162. var childRect = child.getBoundingRect();
  163. var transform = child.getLocalTransform(tmpMat);
  164. if (transform) {
  165. BoundingRect.applyTransform(tmpRect, childRect, transform);
  166. rect = rect || tmpRect.clone();
  167. rect.union(tmpRect);
  168. }
  169. else {
  170. rect = rect || childRect.clone();
  171. rect.union(childRect);
  172. }
  173. }
  174. return rect || tmpRect;
  175. };
  176. return Group;
  177. }(Element));
  178. Group.prototype.type = 'group';
  179. export default Group;