'use strict' ; import { parentView , inject , View , ProjectsAPI , ProjectDetailAPI , EventAPI , MemberAPI , ProjectHomeActivity , Ding } from '../../index' ; import { IProjectActivitiesData , IMemberData , IProjectData , ITaskData , IEventData } from 'teambition' ; let loaded = false ; let projectId ; let lastCacheText = '' ; let counter = 0 ; let cacheActivities ; cacheActivities = { } ; const typeMap = { 'task' : '任务' , 'event' : '日程' , 'entry' : '账目' , 'post' : '分享' , 'work' : '文件' } ; @ parentView ( 'TabsView' ) @ inject ( [ 'ProjectsAPI' , 'ProjectDetailAPI' , 'EventAPI' , 'MemberAPI' , 'ProjectHomeActivity' ] ) export class PanelHomeView extends View { public selectedMembers = { all : true , text : '全部成员' , count : 0 , members : < IMemberData > { } , cacheText : null } ; public selectedTypes = { all : true , text : '全部类型' , count : 0 , types : { } , cacheText : null } ; public project : IProjectData ; public dueTasks : ITaskData [ ] ; public _dueTasks : ITaskData [ ] ; public noneExecutorTasks : ITaskData [ ] ; public _noneExecutorTasks : ITaskData [ ] ; public membersMap : { [ index ] : IMemberData ; } ; public organizationMembers : { [ index ] : IMemberData ; } ; public membersLength : number ; public memberLimit : string ; private ProjectsAPI : ProjectsAPI ; private ProjectDetailAPI : ProjectDetailAPI ; private EventAPI : EventAPI ; private MemberAPI : MemberAPI ; private infinite = true ; private activities : IProjectActivitiesData [ ] ; private eventGroup : IEventData [ ] ; private ProjectHomeActivity : ProjectHomeActivity ; private state : string ; public onInit ( ) { projectId = this . $state . params . _id ; return this . initFetch ( ) ; } public onAllChangesDone ( ) { this . state = 'origin' ; this . setHeader ( ) ; this . ProjectHomeActivity . show ( this ) ; } public scrollHandler ( ) { if ( ! this . infinite ) { return true ; } let thisView = this . $ionicScrollDelegate . getScrollView ( ) ; let height ; if ( typeof ( thisView ) !== 'undefined' ) { height = thisView . __maxScrollTop ; if ( height < thisView . __clientHeight ) { this . infinite = false ; } } if ( typeof ( this . $ionicScrollDelegate . getScrollPosition ) !== 'undefined' ) { let position = this . $ionicScrollDelegate . getScrollPosition ( ) ; let top = position ? position . top : null ; if ( height - top < 45 ) { this . loadMoreData ( ) ; } } } public loadDue ( ) { this . dueTasks = this . _dueTasks ; } public loadNoneExecutor ( ) { this . noneExecutorTasks = this . _noneExecutorTasks ; } public openDetail ( href , type ) { if ( href === 'deleted' ) { this . showMsg ( 'error' , '不能查看详情' , `该${ typeMap [ type ] }已经被删除` ) ; } else if ( href !== '#' ) { window . location . hash = href ; } } public openMembersFilterModal ( ) { this . state = 'memberFilter' ; this . setHeader ( ) ; this . openModal ( 'project-tabs/project-home/members-filter-modal.html' , { scope : this . $scope } ) ; } public openTasksFilterModal ( ) { this . state = 'typeFilter' ; this . setHeader ( ) ; this . openModal ( 'project-tabs/project-home/tasks-filter-modal.html' , { scope : this . $scope } ) ; } public checkItem ( filter , type , item ? ) { if ( ! item ) { if ( ! filter . count ) { filter . all = true ; } else { filter [ type ] = { } ; filter . count = 0 ; } } else { if ( filter [ type ] [ item ] ) { filter . count += 1 ; } else { filter . count -= 1 ; } filter . all = ! filter . count ; } } public selectMember ( id ) { if ( id === 'all' ) { this . selectedMembers . all = true ; angular . forEach ( this . membersMap , ( member ) => { member . isSelected = false ; } ) ; this . checkItem ( this . selectedMembers , 'members' ) ; } else { this . selectedMembers . all = false ; this . membersMap [ id ] . isSelected = ! this . membersMap [ id ] . isSelected ; this . selectedMembers . members [ id ] = this . selectedMembers . members [ id ] ? null : this . membersMap [ id ] . name ; this . checkItem ( this . selectedMembers , 'members' , this . membersMap [ id ] ) ; } } public filterMembers ( ) { let cacheText ; this . filterResultParser ( this . selectedMembers , this . selectedMembers . members , this . selectedMembers . members , 'members' ) ; cacheText = this . selectedMembers . cacheText + ' ' + this . selectedTypes . cacheText ; if ( cacheText !== lastCacheText ) { this . showLoading ( ) ; this . getActivities ( 20 , this . selectedMembers . cacheText , this . selectedTypes . cacheText ) . then ( ( ) => { this . hideLoading ( ) ; this . cancelModal ( ) ; } ) ; } this . infinite = true ; } public filterTasks ( ) { let cacheText ; this . filterResultParser ( this . selectedTypes , this . selectedTypes . types , typeMap , 'tasks' ) ; cacheText = this . selectedMembers . cacheText + ' ' + this . selectedTypes . cacheText ; if ( cacheText !== lastCacheText ) { this . showLoading ( ) ; this . getActivities ( 20 , this . selectedMembers . cacheText , this . selectedTypes . cacheText ) . then ( ( ) => { this . hideLoading ( ) ; this . cancelModal ( ) ; } ) ; } this . infinite = true ; } public openMembersModel ( ) { this . state = 'addMember' ; this . setHeader ( ) ; this . openModal ( 'project-tabs/project-home/add-member-modal.html' , { scope : this . $scope } ) ; this . showLoading ( ) ; this . MemberAPI . getOrganizationMembers ( this . project . organizationId ) . then ( ( members ) => { this . organizationMembers = members ; angular . forEach ( this . membersMap , ( member ) => { if ( this . organizationMembers [ member . _id ] ) { this . organizationMembers [ member . _id ] . isSelected = true ; } } ) ; this . hideLoading ( ) ; } ) . catch ( ( reason ) => { let message = this . getFailureReason ( reason ) ; this . showMsg ( 'error' , '获取数据失败' , message ) ; this . hideLoading ( ) ; } ) ; } public addMember ( id ) { this . organizationMembers [ id ] . isSelected = ! this . organizationMembers [ id ] . isSelected ; } public getMemberLength ( ) { if ( this . membersMap ) { return Object . keys ( this . membersMap ) . length - 1 ; } else { return 0 ; } } public openCreator ( type ) { if ( type !== 'file' ) { window . location . hash = `/project/${ projectId }/${ type }/create` ; } else { window . location . hash = `/project/${ projectId }/work` ; } } private setHeader ( ) { if ( Ding ) { switch ( this . state ) { case 'origin' : Ding . setTitle ( 'Teambition' ) ; Ding . setLeft ( '返回' , true , true , ( ) => { location . href = location . href . replace ( window . location . hash , '' ) + '#/projects' ; } ) ; Ding . setRight ( '' , false , false ) ; break ; case 'memberFilter' : Ding . setTitle ( '选择成员' ) ; Ding . setLeft ( '取消' , true , false , ( ) => { this . cancelModal ( ) ; this . state = 'origin' ; this . setHeader ( ) ; } ) ; Ding . setRight ( '确定' , true , false , ( ) => { this . filterMembers ( ) ; this . cancelModal ( ) ; this . state = 'origin' ; this . setHeader ( ) ; } ) ; break ; case 'typeFilter' : Ding . setTitle ( '选择类型' ) ; Ding . setLeft ( '取消' , true , false , ( ) => { this . cancelModal ( ) ; this . state = 'origin' ; this . setHeader ( ) ; } ) ; Ding . setRight ( '确定' , true , false , ( ) => { this . filterTasks ( ) ; this . cancelModal ( ) ; this . state = 'origin' ; this . setHeader ( ) ; } ) ; break ; case 'addMember' : Ding . setTitle ( '添加成员' ) ; Ding . setLeft ( '取消' , true , false , ( ) => { this . cancelModal ( ) ; this . state = 'origin' ; this . setHeader ( ) ; } ) ; Ding . setRight ( '确定' , true , false , ( ) => { this . addMembers ( ) ; this . cancelModal ( ) ; this . state = 'origin' ; this . setHeader ( ) ; } ) ; break ; } } } private loadMoreData ( ) { if ( loaded ) { return ; } let membersFilter = this . selectedMembers . cacheText ; let typesFilter = this . selectedTypes . cacheText ; this . getActivities ( 20 , membersFilter , typesFilter ) ; } private getActivities ( count , membersFilter ? , typesFilter ? , page ? ) { let _membersFilter = membersFilter ? membersFilter : 'all' ; let _typesFilter = typesFilter ? typesFilter : 'all' ; let cacheText = `${ projectId }:${ _membersFilter }:${ _typesFilter }` ; lastCacheText = cacheText ; let cache = cacheActivities [ cacheText ] ; loaded = true ; return this . ProjectDetailAPI . fetchActivities ( projectId , count , membersFilter , typesFilter , page ) . then ( ( data ) => { if ( ! cache ) { cacheActivities [ cacheText ] = data ; cache = data ; } else if ( cache . length === data . length ) { counter ++ ; if ( counter > 3 ) { this . infinite = false ; } } if ( this . ProjectHomeActivity ) { this . ProjectHomeActivity . update ( ) ; } this . activities = cache ; loaded = false ; } ) ; } private getEvents ( _projectId , now ) { return this . EventAPI . fetch ( _projectId , now ) . then ( ( eventGroup ) => { this . eventGroup = eventGroup . raw ; } ) ; } private getMembers ( ) { return this . MemberAPI . fetch ( projectId ) . then ( ( members ) => { this . membersMap = members ; } ) ; } private getNoneExecutorTasks ( ) { return this . ProjectDetailAPI . fetchNoExecutorOrDuedateTasks ( projectId , 'noneExecutor' ) . then ( ( tasks ) => { if ( ! tasks ) { return ; } this . _noneExecutorTasks = tasks ; if ( tasks . length > 4 ) { let _tasks = [ ] ; angular . forEach ( tasks , ( task , index ) => { if ( index < 3 ) { _tasks . push ( task ) ; } else { return false ; } } ) ; this . noneExecutorTasks = _tasks ; } else { this . noneExecutorTasks = tasks ; } } ) ; } private getOverDueTasks ( ) { return this . ProjectDetailAPI . fetchNoExecutorOrDuedateTasks ( projectId , 'due' ) . then ( ( tasks ) => { if ( ! tasks ) { return ; } this . _dueTasks = tasks ; if ( tasks . length > 4 ) { let _tasks = [ ] ; angular . forEach ( tasks , ( task , index ) => { if ( index < 3 ) { _tasks . push ( task ) ; } else { return false ; } } ) ; this . dueTasks = _tasks ; } else { this . dueTasks = tasks ; } return ; } ) ; } private getProjectTasks ( _projectId ) { return this . $q . all ( [ this . getMembers ( ) , this . getNoneExecutorTasks ( ) , this . getOverDueTasks ( ) ] ) ; } private initFetch ( ) { let now = moment ( ) . startOf ( 'day' ) . toISOString ( ) ; return this . $q . all ( [ this . getEvents ( projectId , now ) , this . getProjectTasks ( projectId ) , this . ProjectsAPI . fetchById ( projectId ) . then ( ( project ) => { this . project = project ; } ) ] ) . then ( ( ) => { return this . getActivities ( 20 , null , null , 1 ) ; } ) ; } private filterResultParser ( obj , arr , map , type ) { if ( obj . all ) { obj . cacheText = '' ; obj . text = ( type === 'members' ) ? '全部成员' : '全部类型' ; return ; } let text = '' ; let result = [ ] ; for ( let id in arr ) { if ( arr . hasOwnProperty ( id ) ) { let value = arr [ id ] ; if ( value ) { text = text + map [ id ] + ',' ; result . push ( id ) ; } } } obj . text = text . slice ( 0 , text . length - 1 ) ; obj . cacheText = result . sort ( ) . toString ( ) ; return obj ; } private addMembers ( ) { let emails = [ ] ; angular . forEach ( this . organizationMembers , ( member ) => { if ( member . isSelected ) { emails . push ( member . email ) ; } } ) ; this . showLoading ( ) ; this . ProjectDetailAPI . addMembers ( projectId , emails ) . then ( ( ) => { this . hideLoading ( ) ; } ) . catch ( ( reason ) => { alert ( JSON . stringify ( reason ) ) ; let message = this . getFailureReason ( reason ) ; this . showMsg ( 'error' , '更新成员失败' , message ) ; this . hideLoading ( ) ; } ) ; } } angular . module ( 'teambition' ) . controller ( 'PanelHomeView' , PanelHomeView ) ;