123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- ( function () {
- 'use strict';
- if ( CC_EDITOR ) {
- return;
- }
- function on_child_added ( event ) {
- var detail = event.detail || event;
- bindAttr._bindDescendantsNode( this, detail );
- bindAttr.init( detail );
- }
- function on_child_removed ( event ) {
- bindAttr._removeDescendantsNode( this, event.detail || event );
- }
- function push ( component ) {
- bindAttr._bindComponent( component.node, component );
- return this._push.call( this, component );
- }
- function splice ( i, num ) {
- bindAttr._removeComponent( this[ i ].node, this[ i ] );
- return this._splice.call( this, i, num );
- }
- function getName () {
- return this._name;
- }
- function setName ( val ) {
- if ( !this.parent || !this.parent.isValid ) {
- return;
- }
- bindAttr._removeDescendantsNode( this.parent, this );
- this._name = val;
- bindAttr._bindDescendantsNode( this.parent, this );
- }
- window.bindAttr = {
- /**移除子孙节点信息
- *
- * @param target 目标节点
- * @param descendantsNode 子孙节点
- */
- _removeDescendantsNode ( target, descendantsNode ) {
- delete target[ `$${descendantsNode.name}` ];
- },
- /**绑定子孙节点
- *
- * @param target 目标节点
- * @param descendantsNode 子孙节点
- */
- _bindDescendantsNode ( target, descendantsNode ) {
- target[ `$${descendantsNode.name}` ] = descendantsNode;
- },
- /**绑定组件
- *
- * @param target 目标节点
- * @param component 目标组件
- */
- _bindComponent ( target, component ) {
- var name = this._getComponentName( component );
- name = `${name}`;
- target[ name ] = component;
- if ( component instanceof Script ) {
- target[ 'Script' ] = component;
- }
- },
- /**移除组件
- *
- * @param target 目标节点
- * @param component 目标组件
- */
- _removeComponent ( target, component ) {
- var name = this._getComponentName( component );
- name = `${name}`;
- delete target[ name ];
- if ( component instanceof Script ) {
- delete target[ 'Script' ];
- }
- },
- /**
- * 获取组件名字
- * @param {cc.Component} component
- */
- _getComponentName ( component ) {
- return component.name.match( /<.*>$/ )[ 0 ].slice( 1, -1 );
- },
- /**初始化
- *
- * @param target 需要绑定的节点或组件
- */
- init ( target ) {
- if ( !( target instanceof cc.Node ) ) {
- target = target.node;
- }
- if ( !target ) {
- throw new Error( '初始化ui没有传入节点或组件' );
- }
- if ( target.$$isInitUi ) {
- return;
- }
- target.$$isInitUi = true;
- this.bindGetSet( target );
- this.bindComponent( target );
- this.bindDescendantsNode( target );
- },
- /**绑定getset事件,用于监听
- *
- */
- bindGetSet ( target ) {
- var components = target._components;
- components._push = components.push;
- components.push = push;
- components._splice = components.splice;
- components.splice = splice;
- target._name = target.name;
- delete target.name;
- Object.defineProperties( target, { name : { get : getName, set : setName } } );
- target.on( 'child-added', on_child_added, target );
- target.on( 'child-removed', on_child_removed, target );
- },
- /**绑定组件
- *
- * @param target 需要绑定组件的节点
- */
- bindComponent ( target ) {
- var self = this,
- components = target._components,
- i = components.length - 1;
- for ( ; i >= 0; i-- ) {
- self._bindComponent( target, components[ i ] );
- }
- components = null;
- },
- /**绑定子节点
- *
- * @param target 目标节点
- */
- bindDescendantsNode ( target ) {
- var self = this,
- childrens = target.children,
- i = childrens.length - 1;
- for ( ; i >= 0; i-- ) {
- self._bindDescendantsNode( target, childrens[ i ] );
- self.init( childrens[ i ] );
- }
- }
- };
- cc.director.on( cc.Director.EVENT_BEFORE_SCENE_LAUNCH, function ( e ) {
- bindAttr.init( e.detail || e );
- } );
- }() );
|