Group.js 5.6 KB

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