'use strict' ; import { app } from '../config' ; import { rootZone , inject } from './Utils' ; import { IProjectData , IRootScope } from 'teambition' ; import { Notify } from '../et' ; const viewsMap = { } ; const initedViews = { } ; let pending ; let firstPending = true ; let currentModal ; export interface IScope extends angular . IScope { ViewName : string ; } @ inject ( [ '$rootScope' , '$q' , '$state' , '$ionicLoading' , '$location' , '$ionicModal' , '$ionicListDelegate' , '$ionicActionSheet' , '$ionicPopup' , '$ionicTabsDelegate' , '$ionicScrollDelegate' , 'notify' ] ) export class View { public static $inject = [ '$scope' ] ; public zone : any ; public ViewName : string ; public $$id : string ; public parentName : string ; public parent : any ; public project : IProjectData ; protected $rootScope : IRootScope ; protected $scope : IScope ; protected $q : angular . IQService ; protected $location : angular . ILocationService ; protected $state : angular . ui . IStateService | angular . ui . IState ; protected $ionicLoading : ionic . loading . IonicLoadingService ; protected $ionicListDelegate : ionic . list . IonicListDelegate ; protected $ionicActionSheet : ionic . actionSheet . IonicActionSheetService ; protected $ionicPopup : ionic . popup . IonicPopupService ; protected $ionicScrollDelegate : ionic . scroll . IonicScrollDelegate ; protected $ionicModal : ionic . modal . IonicModalService ; protected loading = false ; protected notify : Notify ; public static afterTaskHook ( project ) { angular . noop ( ) ; } constructor ( $scope ) { if ( this . parentName ) { this . parent = viewsMap [ this . parentName ] ; } this . $scope = $scope ; this . ViewName = this . $scope . ViewName ; this . initZone ( ) ; } public onInit ( ) { return this . $rootScope . pending ; } public onChange ( ) { angular . noop ( ) ; } public onAllChangesDone ( ) { angular . noop ( ) ; } public onDestroy ( ) { angular . noop ( ) ; } protected showMsg ( type , title , content , href ? ) { this . notify . show ( type , title , content , href ) ; } protected showLoading ( ) { if ( this . $rootScope . loaded ) { this . $ionicLoading . show ( { noBackdrop : true , delay : 500 } ) ; } } protected hideLoading ( ) { this . $ionicLoading . hide ( ) ; } protected openModal ( templateUrl , options ) { let _options = angular . extend ( { animation : 'slide-in-up' , focusFirstInput : false , backdropClickToClose : true , hardwareBackButtonClose : true } , options ) ; this . $ionicModal . fromTemplateUrl ( templateUrl , _options ) . then ( ( modal ) => { currentModal = modal ; modal . show ( ) ; } ) ; } protected cancelModal ( ) { currentModal . hide ( ) ; } protected getFailureReason ( reason ) { let message ; if ( typeof reason === 'string' ) { try { reason = JSON . parse ( reason ) ; if ( reason . data && reason . data . message ) { message = reason . data . message ; } else if ( reason . status ) { message = reason . status ; } } catch ( error ) { message = reason ; console . error ( error ) ; } } else { if ( reason . data && reason . data . message ) { message = reason . data . message ; } else { message = reason . status ; } } return message ; } protected cancelPending ( ) { pending = null ; } private initZone ( ) { let parentZone = ( this . parent ) ? this . parent . zone : rootZone ; this . zone = parentZone . fork ( { 'name' : `${ this . ViewName }-zone` , 'onInvokeTask' : ( ) => { console . log ( 'afterTask' ) ; View . afterTaskHook ( this . project ) ; this . onChange ( ) ; } , 'onInvoke' : ( ) => { console . log ( 'on invoke' ) ; let $$id ; $$id = this . $$id || this . $state . params . _id ; if ( ! initedViews [ this . ViewName + $$id ] ) { this . _onInit ( ) . then ( ( ) => { if ( ( pending && pending . $$state . status === 0 ) || this . ViewName === 'RootView' ) { return ; } else { this . $rootScope . loaded = true ; this . hideLoading ( ) ; } } ) ; } else { if ( ! pending || ( pending && pending . $$state . status === 1 ) ) { this . hideLoading ( ) ; if ( app . NAME === 'teambition-qqgroup' && ! this . $rootScope . loaded ) { this . $rootScope . loaded = true ; } } } } } ) ; this . zone . run ( angular . noop ) ; } private _onInit ( ) { let $$id = this . $$id || this . $state . params . _id ; let bindPromise ; this . $$id = $$id ; this . showLoading ( ) ; viewsMap [ this . ViewName ] = this ; if ( typeof ( this . $scope ) === 'object' ) { this . $scope . $on ( '$destroy' , ( ) => { this . _onDestroy ( ) ; } ) ; } this . $rootScope . $on ( '$stateChangeStart' , ( ) => { initedViews [ this . ViewName + this . $$id ] = false ; if ( currentModal ) { currentModal . hide ( ) ; } } ) ; bindPromise = ( ) => { if ( ! pending ) { const _pending = this . $rootScope . pending || this . $q . resolve ( ) ; if ( firstPending ) { pending = _pending . then ( ( ) => { return this . $rootScope . pending = this . onInit ( ) ; } ) ; } else { pending = _pending ; } } return pending . then ( ( ) => { if ( firstPending ) { firstPending = false ; return ; } const _pending = this . onInit ( ) ; if ( _pending !== this . $rootScope . pending ) { pending = _pending ; } return pending ; } ) . then ( ( ) => { initedViews [ this . ViewName + $$id ] = true ; if ( this . $ionicScrollDelegate ) { this . $ionicScrollDelegate . resize ( ) ; } return this . onAllChangesDone ( ) ; } ) . catch ( reason => { pending = this . $q . resolve ( ) ; const message = this . getFailureReason ( reason ) ; this . showMsg ( 'error' , '初始化失败' , message ) ; } ) ; } ; console . log ( this . ViewName , 'run' , moment ( ) . format ( 'HH:MM:SS' ) ) ; return bindPromise ( ) ; } private _onDestroy ( ) { initedViews [ this . ViewName + this . $$id ] = false ; this . onDestroy ( ) ; } }