/****************************************************************** * Copyright(C) 2019 - 2020 Nx Studio * * 主界面 --- 弹窗队列辅助 * * ******************************************************************/ const BridgeComponent = require( "bridge.component" ); const { SCENE_TAG } = require( "define" ); cc.Class( { extends: BridgeComponent, properties: { }, // 显示 onEnable: function() { // 弹窗队列 this.cur = null; this.queue = []; this.pushs = []; // 弹窗层 this.popRoot = nx.bridge.ui.getSceneNode( SCENE_TAG.popup ); if( !this.popRoot ) { nx.error( "$Popups:辅助失败,缺少弹窗层popup_tag!" ); return; } // 监听 this.popRoot.on( "child-removed", this.onRemoveChild.bind( this ) ); // 全局支持 nx.bridge.popups = this; }, // 关闭 onDisable: function() { // 监听解除 this.popRoot.off( "child-removed", this.onRemoveChild.bind( this ) ); // 全局支持 nx.bridge.popups = null; }, // 子节点移除 onRemoveChild: function() { if( !this.popRoot ){ return; } // 当前清理 this.cur = null; // 引导事件 if( nx.bridge.plot && this.queue.length == 0 && this.pushs.length == 0 && this.popRoot.childrenCount == 0 ) { // 发布引导事件 nx.bridge.plot.postKey( "popups_clean" ); // 如果当前不在引导中,尝试触发引导(比如:升级) let cur = nx.bridge.plot.vget( "id" ); if( nx.dt.strEmpty( cur ) ) { nx.bridge.mainui.tryPlot(); return; } } // 下一个 this.tryPopup(); }, // 添加弹窗 push: function( _wname, _params, _cfgs ) { nx.debug( "$Popups:添加弹窗:", _wname ); if( _cfgs && _cfgs.push ) { this.pushs.push( { wid: _wname, args: _params || {} } ); } else { this.queue.push( { wid: _wname, args: _params || {} } ); } this.tryPopup(); }, // 清空弹窗 clean: function( _deep = false ) { nx.debug( "$Popups:清空弹窗队列" ); this.queue = []; this.cur = null; let chds = this.popRoot.children; for( let i in chds ) { let cmp = chds[i].getComponent( "bridge.window" ); if( cmp ) { cmp.close(); } } // 深度清理 if( _deep ) { this.pushs = []; } }, // 尝试弹出 tryPopup: function() { // 空队列/节点非空 if( nx.dt.arrEmpty( this.queue ) || this.popRoot.childrenCount > 0 ) { this.tryPush(); return; } // 加载中 if( this.cur ) { nx.warn( "Popups:载入中:", this.cur._wname ); return; } this.cur = this.queue.shift(); nx.bridge.ui.openWindow( this.cur.wid, this.cur.args, ( _ret, _params ) => { if( !_ret ) { nx.error( "$Popups:加载失败:", this.cur._wname ); this.cur = null; this.tryPopup(); return; } this.cur = null; }, true ); }, // 尝试推送 tryPush: function() { // 空队列/节点非空 if( nx.dt.arrEmpty( this.pushs ) || this.popRoot.childrenCount > 0 ) { return; } // 引导阶段不弹出 if( nx.bridge.plot ) { let cur = nx.bridge.plot.vget( "id" ); if( nx.dt.strNEmpty( cur ) ) { return; } } // 加载中 if( this.cur ) { nx.warn( "Popups:载入中:", this.cur._wname ); return; } this.cur = this.pushs.shift(); nx.bridge.ui.openWindow( this.cur.wid, this.cur.args, ( _ret, _params ) => { if( !_ret ) { nx.error( "$Popups:加载失败:", this.cur._wname ); this.cur = null; this.tryPush(); return; } this.cur = null; }, true ); }, } );