ts/display/DisplayObjectContainer.ts - StructureJS

StructureJS

0.15.2

A class based utility library for building modular and scalable web platform applications. Features opt-in classes and utilities which provide a solid foundation and toolset to build your next project.

File: ts/display/DisplayObjectContainer.ts

  1. import DisplayObject from './DisplayObject';
  2.  
  3. /**
  4. * The {{#crossLink "DisplayObjectContainer"}}{{/crossLink}} class is the base class for all objects that can be placed on the display list.
  5. *
  6. * @class DisplayObjectContainer
  7. * @extends DisplayObject
  8. * @module StructureJS
  9. * @submodule view
  10. * @requires Extend
  11. * @requires DisplayObject
  12. * @constructor
  13. * @author Robert S. (www.codeBelt.com)
  14. */
  15. class DisplayObjectContainer extends DisplayObject
  16. {
  17. /**
  18. * Returns the number of children of this object.
  19. *
  20. * @property numChildren
  21. * @type {int}
  22. * @default 0
  23. * @readOnly
  24. * @public
  25. */
  26. public numChildren:number = 0;
  27.  
  28. /**
  29. * A reference to the child DisplayObject instances to this parent object instance.
  30. *
  31. * @property children
  32. * @type {Array.<DisplayObject>}
  33. * @readOnly
  34. * @public
  35. */
  36. public children:Array<DisplayObject> = [];
  37.  
  38. /**
  39. * Determines whether or not the children of the object are mouse enabled.
  40. *
  41. * @property mouseChildren
  42. * @type {boolean}
  43. * @public
  44. */
  45. public mouseChildren:boolean = false;
  46.  
  47. constructor()
  48. {
  49. super();
  50. }
  51.  
  52. /**
  53. * Adds a child DisplayObject instance to this parent object instance. The child is added to the front (top) of all other
  54. * children in this parent object instance. (To add a child to a specific index position, use the addChildAt() method.)
  55. *
  56. * If you add a child object that already has a different parent, the object is removed from the child
  57. * list of the other parent object.
  58. *
  59. * @method addChild
  60. * @param child {DisplayObject} The DisplayObject instance to add as a child of this DisplayObjectContainer instance.
  61. * @returns {DisplayObjectContainer} Returns an instance of itself.
  62. * @public
  63. * @chainable
  64. */
  65. public addChild(child:DisplayObject):any
  66. {
  67. //If the child being passed in already has a parent then remove the reference from there.
  68. if (child.parent)
  69. {
  70. child.parent.removeChild(child);
  71. }
  72.  
  73. this.children.push(child);
  74. this.numChildren = this.children.length;
  75.  
  76. child.parent = this;
  77.  
  78. return this;
  79. }
  80.  
  81. /**
  82. * Adds a child DisplayObject instance to this DisplayObjectContainerContainer instance.
  83. * The child is added at the index position specified. An index of 0 represents the back
  84. * (bottom) of the display list for this DisplayObjectContainerContainer object.
  85. *
  86. * @method addChildAt
  87. * @param child {DisplayObject} The DisplayObject instance to add as a child of this object instance.
  88. * @param index {int} The index position to which the child is added. If you specify a currently occupied index position, the child object that exists at that position and all higher positions are moved up one position in the child list.
  89. * @returns {DisplayObjectContainer} Returns an instance of itself.
  90. * @public
  91. * @chainable
  92. */
  93. public addChildAt(child:DisplayObject, index:number):any
  94. {
  95. //If the child being passed in already has a parent then remove the reference from there.
  96. if (child.parent)
  97. {
  98. child.parent.removeChild(child);
  99. }
  100.  
  101. this.children.splice(index, 0, child);
  102. this.numChildren = this.children.length;
  103.  
  104. child.parent = this;
  105.  
  106. return this;
  107. }
  108.  
  109. /**
  110. * Removes the specified child object instance from the child list of the parent object instance.
  111. * The parent property of the removed child is set to null , and the object is garbage collected if no other references
  112. * to the child exist. The index positions of any objects above the child in the parent object are decreased by 1.
  113. *
  114. * @method removeChild
  115. * @param child {DisplayObject} The DisplayObject instance to remove.
  116. * @returns {DisplayObjectContainer} Returns an instance of itself.
  117. * @public
  118. * @chainable
  119. */
  120. public removeChild(child:DisplayObject):any
  121. {
  122. const index = this.getChildIndex(child);
  123. if (index !== -1)
  124. {
  125. // Removes the child object from the parent.
  126. this.children.splice(index, 1);
  127. }
  128.  
  129. this.numChildren = this.children.length;
  130.  
  131. child.parent = null;
  132.  
  133. return this;
  134. }
  135.  
  136. /**
  137. * Removes all child DisplayObject instances from the child list of the DisplayObjectContainerContainer instance.
  138. * The parent property of the removed children is set to null , and the objects are garbage collected if
  139. * no other references to the children exist.
  140. *
  141. * @method removeChildren
  142. * @returns {DisplayObjectContainer} Returns an instance of itself.
  143. * @public
  144. * @chainable
  145. */
  146. public removeChildren():any
  147. {
  148. while (this.children.length > 0)
  149. {
  150. this.removeChild(this.children.pop());
  151. }
  152.  
  153. return this;
  154. }
  155.  
  156. /**
  157. * Swaps two DisplayObject's with each other.
  158. *
  159. * @method swapChildren
  160. * @param child1 {DisplayObject} The DisplayObject instance to be swap.
  161. * @param child2 {DisplayObject} The DisplayObject instance to be swap.
  162. * @returns {DisplayObjectContainer} Returns an instance of itself.
  163. * @public
  164. * @chainable
  165. */
  166. public swapChildren(child1:DisplayObject, child2:DisplayObject):any
  167. {
  168. const child1Index = this.getChildIndex(child1);
  169. const child2Index = this.getChildIndex(child2);
  170.  
  171. this.addChildAt(child1, child2Index);
  172. this.addChildAt(child2, child1Index);
  173. }
  174.  
  175. /**
  176. * Swaps child objects at the two specified index positions in the child list. All other child objects in the display object container remain in the same index positions.
  177. *
  178. * @method swapChildrenAt
  179. * @param index1 {int} The index position of the first child object.
  180. * @param index2 {int} The index position of the second child object.
  181. * @returns {DisplayObjectContainer} Returns an instance of itself.
  182. * @public
  183. * @chainable
  184. */
  185. public swapChildrenAt(index1:number, index2:number):any
  186. {
  187. if (index1 < 0 || index1 < 0 || index1 >= this.numChildren || index2 >= this.numChildren)
  188. {
  189. throw new TypeError('[' + this.getQualifiedClassName() + '] index value(s) cannot be out of bounds. index1 value is ' + index1 + ' index2 value is ' + index2);
  190. }
  191.  
  192. const child1:DisplayObject = this.getChildAt(index1);
  193. const child2:DisplayObject = this.getChildAt(index2);
  194.  
  195. this.swapChildren(child1, child2);
  196.  
  197. return this;
  198. }
  199.  
  200. /**
  201. * Returns the index position of a child DisplayObject instance.
  202. *
  203. * @method getChildIndex
  204. * @param child {DisplayObject} The DisplayObject instance to identify.
  205. * @returns {int} The index position of the child display object to identify.
  206. * @public
  207. */
  208. public getChildIndex(child:DisplayObject):number
  209. {
  210. return this.children.indexOf(child);
  211. }
  212.  
  213. /**
  214. * Determines whether the specified display object is a child of the DisplayObject instance or the instance itself. The search includes the entire display list including this DisplayObject instance.
  215. *
  216. * @method contains
  217. * @param child {DisplayObject} The child object to test.
  218. * @returns {boolean} true if the child object is a child of the DisplayObject or the container itself; otherwise false.
  219. * @public
  220. */
  221. public contains(child:DisplayObject):boolean
  222. {
  223. return this.children.indexOf(child) >= 0;
  224. }
  225.  
  226. /**
  227. * Returns the child display object instance that exists at the specified index.
  228. *
  229. * @method getChildAt
  230. * @param index {int} The index position of the child object.
  231. * @returns {DisplayObject} The child display object at the specified index position.
  232. */
  233. public getChildAt(index:number):DisplayObject
  234. {
  235. return this.children[index];
  236. }
  237.  
  238. /**
  239. * Gets a DisplayObject by its sjsId.
  240. *
  241. * @method getChildByCid
  242. * @param sjsId {number}
  243. * @returns {DisplayObject|null}
  244. * @override
  245. * @public
  246. */
  247. public getChildByCid(sjsId:number):DisplayObject
  248. {
  249. let child:DisplayObject = null;
  250.  
  251. for (let i:number = this.numChildren - 1; i >= 0; i--)
  252. {
  253. if (this.children[i].sjsId == sjsId)
  254. {
  255. child = this.children[i];
  256. break;
  257. }
  258. }
  259.  
  260. return child;
  261. }
  262.  
  263. }
  264.  
  265. export default DisplayObjectContainer;
  266.