@@ -18,20 +18,58 @@ let workloadDownloadModal = function (WorkloadSummaryActions) {
1818 scope . showSnapshotWarning = false ;
1919
2020 scope . $watch ( 'userWorkgroupSnapshots' , function ( userWorkgroupsSnapshots ) {
21- const workloadSnapshotDownloadSettings = JSON . parse ( localStorage . getItem ( 'workloadSnapshotDownloadSettings' ) ) ;
21+ const downloadSettings = JSON . parse ( localStorage . getItem ( 'workloadSnapshotDownloadSettings' ) ) ;
22+ const workgroupsSnapshotsLength = Object . keys ( userWorkgroupsSnapshots || { } ) . length ;
23+
24+ if ( workgroupsSnapshotsLength > 0 ) {
25+ scope . selectableYears = scope . getSelectableYears ( userWorkgroupsSnapshots ) ;
26+ }
2227
2328 if (
24- workloadSnapshotDownloadSettings ?. year === scope . year &&
25- workloadSnapshotDownloadSettings ?. snapshots . length === Object . keys ( userWorkgroupsSnapshots ?? { } ) . length
29+ downloadSettings &&
30+ workgroupsSnapshotsLength > 0 &&
31+ ! scope . prevYear && ! scope . nextYear &&
32+ downloadSettings . selections . length === workgroupsSnapshotsLength
2633 ) {
27- scope . departmentSnapshots = workloadSnapshotDownloadSettings . snapshots ;
28- scope . isSortedByRecentActivity = workloadSnapshotDownloadSettings . isSorted ;
29- } else if ( userWorkgroupsSnapshots ) {
34+ scope . departmentSnapshots = scope . getScenarioOptions ( userWorkgroupsSnapshots ) ;
35+ scope . departmentSnapshots = scope . departmentSnapshots . map ( d => {
36+ const savedSelection = downloadSettings . selections . find ( s => s . workgroupId === d . workgroupId ) ;
37+
38+ d . selectedNext = savedSelection . selectedNext ;
39+ d . selectedPrevious = savedSelection . selectedPrevious ;
40+
41+ return d ;
42+ } ) ;
43+
44+ scope . prevYear = downloadSettings . prevYear ;
45+ scope . nextYear = downloadSettings . nextYear ;
46+ scope . isSortedByRecentActivity = downloadSettings . isSorted ;
47+ } else if ( ! downloadSettings && workgroupsSnapshotsLength > 0 ) {
48+ scope . selectableYears = scope . getSelectableYears ( userWorkgroupsSnapshots ) ;
49+ scope . prevYear = scope . selectableYears [ 0 ] ;
50+ scope . nextYear = scope . selectableYears [ 0 ] ;
51+
3052 scope . departmentSnapshots = scope . getScenarioOptions ( userWorkgroupsSnapshots ) ;
3153 scope . showSnapshotWarning = scope . isAnyWorkgroupMissingSnapshots ( userWorkgroupsSnapshots ) ;
3254 }
3355 } , true ) ;
3456
57+ scope . onPrevYearChange = function ( prevYear ) {
58+ scope . departmentSnapshots . prevYear = prevYear ;
59+ scope . departmentSnapshots . forEach ( d => {
60+ d . prevYearFilteredSnapshots = [ { id : 0 , name : "Live Data" } , ...d . snapshots . filter ( s => s . year === prevYear ) ] ;
61+ d . selectedPrevious = '0' ;
62+ } ) ;
63+ } ;
64+
65+ scope . onNextYearChange = function ( nextYear ) {
66+ scope . departmentSnapshots . nextYear = nextYear ;
67+ scope . departmentSnapshots . forEach ( d => {
68+ d . nextYearFilteredSnapshots = [ { id : 0 , name : "Live Data" } , ...d . snapshots . filter ( s => s . year === nextYear ) ] ;
69+ d . selectedNext = '0' ;
70+ } ) ;
71+ } ;
72+
3573 scope . sortDepartmentsByRecentActivity = function ( ) {
3674 if ( scope . isSortedByRecentActivity === false ) {
3775 scope . isSortedByRecentActivity = true ;
@@ -52,6 +90,11 @@ let workloadDownloadModal = function (WorkloadSummaryActions) {
5290 }
5391 } ;
5492
93+ scope . getSelectableYears = function ( userWorkgroupsSnapshots ) {
94+ const selectableYears = [ ...new Set ( Object . values ( userWorkgroupsSnapshots ) . flatMap ( s => s . years ) ) ] ;
95+ return selectableYears . length ? selectableYears . sort ( ) : [ scope . year ] ;
96+ } ;
97+
5598 scope . getScenarioOptions = function ( userWorkgroupSnapshots ) {
5699 // each row gets Live Data as an option
57100 // two columns with the same options
@@ -63,10 +106,14 @@ let workloadDownloadModal = function (WorkloadSummaryActions) {
63106 return Object . keys ( userWorkgroupSnapshots )
64107 . sort ( )
65108 . map ( ( department ) => {
109+ const snapshots = userWorkgroupSnapshots [ department ] . snapshots ;
110+
66111 return ( {
67112 name : department ,
68113 workgroupId : userWorkgroupSnapshots [ department ] . workgroupId ,
69- snapshots : [ liveDataOption , ...userWorkgroupSnapshots [ department ] . snapshots ] ,
114+ snapshots : snapshots ,
115+ prevYearFilteredSnapshots : [ liveDataOption , ...snapshots . filter ( s => s . year === scope . year ) ] ,
116+ nextYearFilteredSnapshots : [ liveDataOption , ...snapshots . filter ( s => s . year === scope . year ) ] ,
70117 selectedPrevious : '0' ,
71118 selectedNext : '0' ,
72119 download : true ,
@@ -78,9 +125,10 @@ let workloadDownloadModal = function (WorkloadSummaryActions) {
78125
79126 scope . selectLatestSnapshots = function ( ) {
80127 scope . departmentSnapshots = scope . departmentSnapshots . map ( ( department ) => {
81- const selectedNext = department . snapshots . filter ( snapshot => snapshot . id !== 0 ) . map ( snapshot => snapshot . id ) . sort ( ) [ 0 ] ?. toString ( ) ;
128+ const selectedPrevious = department . prevYearFilteredSnapshots . filter ( snapshot => snapshot . id !== 0 ) . map ( snapshot => snapshot . id ) . sort ( ) [ 0 ] ?. toString ( ) ;
129+ const selectedNext = department . nextYearFilteredSnapshots . filter ( snapshot => snapshot . id !== 0 ) . map ( snapshot => snapshot . id ) . sort ( ) [ 0 ] ?. toString ( ) ;
82130
83- return { ...department , selectedNext } ;
131+ return { ...department , selectedPrevious : selectedPrevious || '0' , selectedNext : selectedNext || '0' } ;
84132 } ) ;
85133 } ;
86134
@@ -110,9 +158,18 @@ let workloadDownloadModal = function (WorkloadSummaryActions) {
110158 scope . close = function ( ) {
111159 scope . status = null ;
112160 WorkloadSummaryActions . toggleDownloadModal ( ) ;
161+
162+ const departmentSelections = scope . departmentSnapshots . map ( d => ( {
163+ workgroupId : d . workgroupId ,
164+ selectedNext : d . selectedNext ,
165+ selectedPrevious : d . selectedPrevious
166+ } ) ) ;
167+
113168 localStorage . setItem ( 'workloadSnapshotDownloadSettings' , JSON . stringify ( {
114- snapshots : scope . departmentSnapshots ,
115- year : scope . year ,
169+ selections : departmentSelections ,
170+ // year: scope.year,
171+ prevYear : scope . prevYear ,
172+ nextYear : scope . nextYear ,
116173 isSorted : scope . isSortedByRecentActivity
117174 } )
118175 ) ;
@@ -131,7 +188,19 @@ let workloadDownloadModal = function (WorkloadSummaryActions) {
131188 scope . departmentSnapshots
132189 . filter ( department => department . download )
133190 . forEach ( ( department ) => {
134- departmentSnapshots [ department . workgroupId ] = [ parseInt ( department . selectedPrevious ) , parseInt ( department . selectedNext ) ] . filter ( id => id > 0 ) ;
191+ let downloads = { } ;
192+
193+ downloads . prev = {
194+ snapshotId : parseInt ( department . selectedPrevious ) ,
195+ liveDataYear : scope . prevYear
196+ } ;
197+
198+ downloads . next = {
199+ snapshotId : parseInt ( department . selectedNext ) ,
200+ liveDataYear : scope . nextYear
201+ } ;
202+
203+ departmentSnapshots [ department . workgroupId ] = downloads ;
135204 } ) ;
136205
137206 WorkloadSummaryActions . downloadMultiple ( departmentSnapshots , scope . workgroupId , scope . year ) ;
0 commit comments