@ -43,13 +43,9 @@ import {WorkPackagesListService} from "../../wp-list/wp-list.service";
import { WorkPackageTableTimelineService } from "../../wp-fast-table/state/wp-table-timeline.service" ;
import { WorkPackageTableTimelineService } from "../../wp-fast-table/state/wp-table-timeline.service" ;
import { WorkPackageTableBaseService } from "../../wp-fast-table/state/wp-table-base.service" ;
import { WorkPackageTableBaseService } from "../../wp-fast-table/state/wp-table-base.service" ;
import { HalResource } from "../../api/api-v3/hal-resources/hal-resource.service" ;
import { HalResource } from "../../api/api-v3/hal-resources/hal-resource.service" ;
import { WorkPackageTableBaseState } from "../../wp-fast-table/wp-table-base" ;
interface ObserverSetupOptions {
attrName :string ;
keyFunc ? : ( a :any ) = > any ;
triggerUpdate? :boolean ;
halMap? :boolean ;
}
function WorkPackagesListController ( $scope :any ,
function WorkPackagesListController ( $scope :any ,
$rootScope :ng.IRootScopeService ,
$rootScope :ng.IRootScopeService ,
@ -109,77 +105,50 @@ function WorkPackagesListController($scope:any,
}
}
} ) ;
} ) ;
setupChangeObserver ( wpTableSortBy , { attrName : 'sortBy' , triggerUpdate : true , halMap : true } ) ;
setupChangeObserver ( wpTableFilters , 'filters' ) ;
setupChangeObserver ( wpTableSum , { attrName : 'sums' , triggerUpdate : true } ) ;
setupChangeObserver ( wpTableGroupBy , 'groupBy' ) ;
setupChangeObserver ( wpTableTimeline , { attrName : 'timelineVisible' , triggerUpdate : false } ) ;
setupChangeObserver ( wpTableSortBy , 'sortBy' ) ;
setupChangeObserver ( wpTableHierarchies , { attrName : 'showHierarchies' , triggerUpdate : false } ) ;
setupChangeObserver ( wpTableSum , 'sums' ) ;
setupChangeObserver ( wpTableColumns , { attrName : 'columns' , triggerUpdate : false , halMap : true } ) ;
setupChangeObserver ( wpTableTimeline , 'timelineVisible' , false ) ;
setupChangeObserver ( wpTableHierarchies , 'showHierarchies' , false ) ;
// Filter objects do not have hrefs to compare them by, but we can use a copy of their source
setupChangeObserver ( wpTableColumns , 'columns' , false ) ;
// (copying is necessary while the state objects are still mutable)
setupChangeObserver (
wpTableFilters ,
{
attrName : 'filters' ,
triggerUpdate : true ,
keyFunc : ( stateValue : any ) = > stateValue . current && stateValue . current . map ( ( el :HalResource ) = > el . $plain ( ) )
}
) ;
// Group by is a single HAL resource instead of an array
setupChangeObserver (
wpTableGroupBy ,
{
attrName : 'groupBy' ,
triggerUpdate : true ,
keyFunc : ( stateValue : any ) = > stateValue . current && stateValue . current . href
}
) ;
}
}
function setupChangeObserver ( service :WorkPackageTableBaseService , options :ObserverSetupOptions ) {
function setupChangeObserver ( service :WorkPackageTableBaseService , name :string , triggerUpdate :boolean = true ) {
let keyFunc ;
if ( options . keyFunc ) {
keyFunc = options . keyFunc ;
} else if ( options . halMap ) {
keyFunc = ( stateValue :any ) = > stateValue . current && stateValue . current . map ( ( el :HalResource ) = > el . href ) ;
} else {
keyFunc = ( stateValue :any ) = > stateValue . current ;
}
service
service
. observeUntil ( scopeDestroyed $ ( $scope ) )
. observeUntil ( scopeDestroyed $ ( $scope ) )
. distinctUntilChanged (
. distinctUntilChanged (
( a , b ) = > _ . isEqual ( a , b ) ,
( a , b ) = > _ . isEqual ( a , b ) ,
keyFunc
( stateValue :WorkPackageTableBaseState < any > ) = > stateValue . extractedCompareValue
)
)
. skip ( 1 ) // Skip the first value to avoid distinctUntilChanged firing for empty
. subscribe ( ( stateValue :WorkPackageTableBaseState < any > ) = > {
. subscribe ( ( stateValue :any ) = > {
// Avoid updating while not all states are initialized
updateAndExecuteIfAltered ( stateValue . current , options . attrName , options . triggerUpdate ) ;
if ( isAnyDependentStateClear ( ) ) {
return ;
}
// Avoid updating if the query isn't yet available
const query = states . table . query . value ;
if ( ! query ) {
return ;
}
// Avoid updating if the query is up-to-date
// (Loaded into query elsewhere)
const queryValue = stateValue . comparerFunction ( ) ( query [ name ] ) ;
if ( _ . isEqual ( queryValue , stateValue . extractedCompareValue ) ) {
return ;
}
query [ name ] = _ . cloneDeep ( stateValue . current ) ;
states . table . query . putValue ( query ) ;
if ( triggerUpdate ) {
updateResultsVisibly ( true ) ;
}
} ) ;
} ) ;
}
}
function updateAndExecuteIfAltered ( updateData :any , name :string , triggerUpdate :boolean = false ) {
if ( isAnyDependentStateClear ( ) ) {
return ;
}
let query = states . table . query . value ;
if ( ! query ) {
return ;
}
query [ name ] = _ . cloneDeep ( updateData ) ;
states . table . query . putValue ( query ) ;
if ( triggerUpdate ) {
updateResultsVisibly ( true ) ;
}
}
function loadQuery() {
function loadQuery() {
wpListChecksumService . clear ( ) ;
wpListChecksumService . clear ( ) ;
loadingIndicator . table . promise = wpListService . fromQueryParams ( $state . params , $scope . projectIdentifier ) ;
loadingIndicator . table . promise = wpListService . fromQueryParams ( $state . params , $scope . projectIdentifier ) ;