bindAttr.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ( function () {
  2. 'use strict';
  3. if ( CC_EDITOR ) {
  4. return;
  5. }
  6. function on_child_added ( event ) {
  7. var detail = event.detail || event;
  8. bindAttr._bindDescendantsNode( this, detail );
  9. bindAttr.init( detail );
  10. }
  11. function on_child_removed ( event ) {
  12. bindAttr._removeDescendantsNode( this, event.detail || event );
  13. }
  14. function push ( component ) {
  15. bindAttr._bindComponent( component.node, component );
  16. return this._push.call( this, component );
  17. }
  18. function splice ( i, num ) {
  19. bindAttr._removeComponent( this[ i ].node, this[ i ] );
  20. return this._splice.call( this, i, num );
  21. }
  22. function getName () {
  23. return this._name;
  24. }
  25. function setName ( val ) {
  26. if ( !this.parent || !this.parent.isValid ) {
  27. return;
  28. }
  29. bindAttr._removeDescendantsNode( this.parent, this );
  30. this._name = val;
  31. bindAttr._bindDescendantsNode( this.parent, this );
  32. }
  33. window.bindAttr = {
  34. /**移除子孙节点信息
  35. *
  36. * @param target 目标节点
  37. * @param descendantsNode 子孙节点
  38. */
  39. _removeDescendantsNode ( target, descendantsNode ) {
  40. delete target[ `$${descendantsNode.name}` ];
  41. },
  42. /**绑定子孙节点
  43. *
  44. * @param target 目标节点
  45. * @param descendantsNode 子孙节点
  46. */
  47. _bindDescendantsNode ( target, descendantsNode ) {
  48. target[ `$${descendantsNode.name}` ] = descendantsNode;
  49. },
  50. /**绑定组件
  51. *
  52. * @param target 目标节点
  53. * @param component 目标组件
  54. */
  55. _bindComponent ( target, component ) {
  56. var name = this._getComponentName( component );
  57. name = `${name}`;
  58. target[ name ] = component;
  59. if ( component instanceof Script ) {
  60. target[ 'Script' ] = component;
  61. }
  62. },
  63. /**移除组件
  64. *
  65. * @param target 目标节点
  66. * @param component 目标组件
  67. */
  68. _removeComponent ( target, component ) {
  69. var name = this._getComponentName( component );
  70. name = `${name}`;
  71. delete target[ name ];
  72. if ( component instanceof Script ) {
  73. delete target[ 'Script' ];
  74. }
  75. },
  76. /**
  77. * 获取组件名字
  78. * @param {cc.Component} component
  79. */
  80. _getComponentName ( component ) {
  81. return component.name.match( /<.*>$/ )[ 0 ].slice( 1, -1 );
  82. },
  83. /**初始化
  84. *
  85. * @param target 需要绑定的节点或组件
  86. */
  87. init ( target ) {
  88. if ( !( target instanceof cc.Node ) ) {
  89. target = target.node;
  90. }
  91. if ( !target ) {
  92. throw new Error( '初始化ui没有传入节点或组件' );
  93. }
  94. if ( target.$$isInitUi ) {
  95. return;
  96. }
  97. target.$$isInitUi = true;
  98. this.bindGetSet( target );
  99. this.bindComponent( target );
  100. this.bindDescendantsNode( target );
  101. },
  102. /**绑定getset事件,用于监听
  103. *
  104. */
  105. bindGetSet ( target ) {
  106. var components = target._components;
  107. components._push = components.push;
  108. components.push = push;
  109. components._splice = components.splice;
  110. components.splice = splice;
  111. target._name = target.name;
  112. delete target.name;
  113. Object.defineProperties( target, { name : { get : getName, set : setName } } );
  114. target.on( 'child-added', on_child_added, target );
  115. target.on( 'child-removed', on_child_removed, target );
  116. },
  117. /**绑定组件
  118. *
  119. * @param target 需要绑定组件的节点
  120. */
  121. bindComponent ( target ) {
  122. var self = this,
  123. components = target._components,
  124. i = components.length - 1;
  125. for ( ; i >= 0; i-- ) {
  126. self._bindComponent( target, components[ i ] );
  127. }
  128. components = null;
  129. },
  130. /**绑定子节点
  131. *
  132. * @param target 目标节点
  133. */
  134. bindDescendantsNode ( target ) {
  135. var self = this,
  136. childrens = target.children,
  137. i = childrens.length - 1;
  138. for ( ; i >= 0; i-- ) {
  139. self._bindDescendantsNode( target, childrens[ i ] );
  140. self.init( childrens[ i ] );
  141. }
  142. }
  143. };
  144. cc.director.on( cc.Director.EVENT_BEFORE_SCENE_LAUNCH, function ( e ) {
  145. bindAttr.init( e.detail || e );
  146. } );
  147. }() );