import { ColumnRow , ColumnScope } from "./index" ; import { RenderContext } from "vue" ; import { ElTableTreeColumnPropsInner } from "./props" ; import { isLoadingRow , isExpandedRow } from "./utils" ; const hash = ( ) => Math . floor ( Math . random ( ) * Math . random ( ) * Math . random ( ) * Math . random ( ) * 1000 ) ; export const clone = function ( data ) { return JSON . parse ( JSON . stringify ( data ) ) ; } ; const index = ( hash , data ) => { var i = 0 ; while ( data [ i ] ) { let c = data [ i ] ; if ( c . $extra && c . $extra . hash == hash ) { break ; } i ++ ; } ; return i ; } ; const indexOf = ( val , arr ) => { var has = - 1 ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == val ) { has = i ; break ; } } return has ; } ; const descendantsIds = ( id , data , parentKey , treeKey ) => { var result = [ ] , compare = [ id ] , length = - 1 ; while ( length != compare . length ) { length = compare . length ; data . forEach ( function ( item ) { if ( indexOf ( item [ parentKey ] , compare ) > - 1 && indexOf ( item [ treeKey ] , compare ) == - 1 ) { result . push ( item [ treeKey ] ) ; compare . push ( item [ treeKey ] ) ; } } ) ; } return result ; } ; const has = function ( context , item , list ) { let key = context . props . treeKey , parentKey = context . props . parentKey ; let uniqueKey = item [ key ] ; let has = false ; list . forEach ( row => { if ( row [ key ] == uniqueKey || row [ key ] == item [ parentKey ] ) { has = true ; } } ) ; return has ; } ; const toggleExpanded = function ( context , scope , isExpended ) { let { treeKey } = context . props , { states } = scope . store ; states . _treeCachedExpanded = states . _treeCachedExpanded . filter ( r => r [ treeKey ] != scope . row [ treeKey ] ) ; if ( isExpended ) { states . _treeRowExpanded = states . _treeRowExpanded . filter ( r => r [ treeKey ] != scope . row [ treeKey ] ) ; } else { states . _treeRowExpanded . push ( scope . row ) ; } } ; const toggleLoading = function ( context , scope , isloading ) { let { treeKey } = context . props ; if ( isloading ) { scope . store . states . _treeRowLoading = scope . store . states . _treeRowLoading . filter ( r => r [ treeKey ] != scope . row [ treeKey ] ) ; } else { scope . store . states . _treeRowLoading . push ( scope . row ) ; } } ; const commit = function ( context , scope , list ) { let owner = scope . store . table ; let states = scope . store . states ; owner . store . commit ( 'setData' , list ) ; } ; const getIndex = function ( context , scope , data ) { let index = - 1 , { treeKey } = context . props ; data . forEach ( ( r , i ) => { if ( r [ treeKey ] == scope . row [ treeKey ] ) { index = i ; } } ) ; return index ; } ; const Colspand = function ( context , scope , data ) { let { parentKey , treeKey , childKey } = context . props , states = scope . store . states , row = scope . row , result = [ ] ; let removeIds = [ ] ; removeIds = descendantsIds ( row [ treeKey ] , data , parentKey , treeKey ) ; data = data . filter ( item => ! removeIds . some ( id => id == item [ treeKey ] ) ) ; let NeedToCached = states . _treeRowExpanded . filter ( item => removeIds . some ( id => id == item [ treeKey ] ) ) ; let { _treeCachedExpanded } = states ; NeedToCached . forEach ( item => { if ( ! _treeCachedExpanded . some ( i => i [ treeKey ] == item [ treeKey ] ) ) { states . _treeCachedExpanded . push ( item ) ; } } ) ; states . _treeRowExpanded = states . _treeRowExpanded . filter ( item => ! removeIds . some ( id => id == item [ treeKey ] ) ) ; commit ( context , scope , data ) ; } ; const insertRows = function ( context , scope , children , updateChild = false ) { let _data = clone ( scope . store . states . _data ) ; let _index = getIndex ( context , scope , _data ) ; var prefix = _data . slice ( 0 , _index + 1 ) ; var i = 0 ; while ( i < _index + 1 ) { _data . shift ( ) ; i ++ ; } if ( updateChild ) prefix [ _index ] [ context . props . childKey ] = children ; _data = prefix . concat ( children ) . concat ( _data ) ; return _data ; } ; const ExpandRemote = function ( context , scope , data ) { let { treeKey , remote } = context . props ; toggleLoading ( context , scope , false ) ; const CallBack = function ( children ) { let { childNumKey } = context . props ; toggleLoading ( context , scope , true ) ; let _data ; if ( children && children . length > 0 ) { let updateChild = ! context . props . allRemote ; _data = insertRows ( context , scope , children , updateChild ) ; } else { _data = clone ( scope . store . states . _data ) ; let _index = getIndex ( context , scope , _data ) ; _data [ _index ] [ childNumKey ] = 0 ; } commit ( context , scope , _data ) ; } ; commit ( context , scope , data ) ; remote ( scope . row , CallBack ) ; } ; const Expand = function ( context , scope , data ) { let { childKey } = context . props ; data = insertRows ( context , scope , scope . row [ childKey ] ) ; commit ( context , scope , data ) ; } ; export function doExpand ( context , scope ) { let data = clone ( scope . store . states . _data ) , { childKey } = context . props ; if ( isLoadingRow ( context , scope ) ) return ; let isExpended = isExpandedRow ( context , scope ) ; toggleExpanded ( context , scope , isExpended ) ; if ( isExpended ) { return Colspand ( context , scope , data ) ; } let { remote , allRemote } = context . props ; if ( remote && allRemote ) { return ExpandRemote ( context , scope , data ) ; } if ( scope . row [ childKey ] ) { return Expand ( context , scope , data ) ; } else if ( remote ) { return ExpandRemote ( context , scope , data ) ; } Expand ( context , scope , data ) ; }