1818
1919import _ from "lodash" ;
2020
21- import { CORE_API } from "../../../common/services/core-api-utils" ;
22- import { initialiseData } from "../../../common" ;
23- import { mkSelectionOptions } from "../../../common/selector-utils" ;
21+ import { CORE_API } from "../../../common/services/core-api-utils" ;
22+ import { initialiseData } from "../../../common" ;
2423
2524import template from "./change-initiative-section.html" ;
26- import { changeInitiative } from "../../../common/services/enums/change-initiative" ;
27- import { getEnumName } from "../../../common/services/enums" ;
25+ import { changeInitiative } from "../../../common/services/enums/change-initiative" ;
26+ import { getEnumName } from "../../../common/services/enums" ;
2827import indexByKeyForType from "../../../enum-value/enum-value-utilities" ;
2928import { isSameParentEntityRef } from "../../../common/entity-utils" ;
30- import { fakeInitiative , fakeProgramme } from "../../change-initiative-utils" ;
31- import { filterByAssessmentRating , mkAssessmentSummaries } from "../../../assessments/assessment-utils" ;
29+ import { fakeInitiative , fakeProgramme } from "../../change-initiative-utils" ;
30+ import { filterByAssessmentRating } from "../../../assessments/assessment-utils" ;
31+ import { nest } from "d3-collection" ;
32+ import { grey } from "../../../common/colors" ;
3233
3334const bindings = {
3435 parentEntityRef : "<" ,
@@ -67,19 +68,34 @@ const initialState = {
6768 } ,
6869 filterHelpText : "Select an assessment category to filter the change initiatives" ,
6970 gridOptions : {
70- columnDefs : [
71- { width : "15%" , field : "kind" , name : "Kind" } ,
72- mkRefCol ( "initiative" ) ,
73- mkRefCol ( "programme" ) ,
74- mkRefCol ( "project" ) ,
75- { width : "25%" , field : "name" , name : "Name" } ,
76- { width : "15%" , field : "lifecyclePhase" , name : "Phase" }
77- ] ,
71+ columnDefs : _ . map (
72+ [
73+ { width : "15%" , field : "kind" , name : "Kind" } ,
74+ mkRefCol ( "initiative" ) ,
75+ mkRefCol ( "programme" ) ,
76+ mkRefCol ( "project" ) ,
77+ { width : "25%" , field : "name" , name : "Name" } ,
78+ { width : "15%" , field : "lifecyclePhase" , name : "Phase" }
79+ ] ,
80+ enrichWithInactiveStyling ) ,
7881 data : [ ]
7982 }
8083} ;
8184
8285
86+ function calcCellClass ( grid , row , col , rowRenderIndex , colRenderIndex ) {
87+ return row . entity . lifecyclePhase === "Retired"
88+ ? "wcis-inactive-ci"
89+ : "" ;
90+ }
91+
92+
93+ function enrichWithInactiveStyling ( col ) {
94+ return Object . assign (
95+ { } ,
96+ col ,
97+ { cellClass : calcCellClass } ) ; }
98+
8399function determineHierarchy ( cisById = { } , ci ) {
84100 const none = null ;
85101
@@ -147,24 +163,76 @@ function mkTableData(changeInitiatives = [], lifecycleNamesByKey = {}) {
147163 kind : changeKind
148164 }
149165 } )
150- . orderBy ( d => [ "initiative.name" , "programme.name" , "project.name" , "name" ] )
166+ . orderBy ( d => [ "initiative.externalId" , "programme.externalId" , "project.externalId" , "name" ] )
167+ . value ( ) ;
168+ }
169+
170+
171+ function mkAssessmentSummaries ( definitions = [ ] , schemeItems = [ ] , ratings = [ ] , total = 0 ) {
172+ const indexedRatingSchemes = indexRatingSchemeItems ( schemeItems ) ;
173+ const definitionsById = _ . keyBy ( definitions , d => d . id ) ;
174+
175+ const nestedRatings = nest ( )
176+ . key ( d => d . assessmentDefinitionId )
177+ . key ( d => d . ratingId )
178+ . rollup ( xs => xs . length )
179+ . entries ( ratings ) ;
180+
181+ return _
182+ . chain ( nestedRatings )
183+ . map ( d => {
184+ const definition = definitionsById [ Number ( d . key ) ] ;
185+ const assignedTotal = _ . sumBy ( d . values , v => v . value ) ;
186+ const values = _
187+ . chain ( d . values )
188+ . map ( v => {
189+ const propPath = [ definition . ratingSchemeId , "ratingsById" , v . key ] ;
190+ const rating = _ . get ( indexedRatingSchemes , propPath ) ;
191+ return Object . assign ( { } , v , { rating, count : v . value } ) ;
192+ } )
193+ . concat ( [ {
194+ key : "z" ,
195+ rating : {
196+ id : - 1 ,
197+ name : "Not Provided" ,
198+ color : grey
199+ } ,
200+ count : _ . max ( [ 0 , total - assignedTotal ] )
201+ } ] )
202+ . filter ( d => d . count > 0 )
203+ . value ( ) ;
204+
205+ const extension = { definition, values } ;
206+ return Object . assign ( { } , d , extension ) ;
207+ } )
208+ . orderBy ( d => d . definition . name )
209+ . value ( ) ;
210+ }
211+
212+ /**
213+ * Given a flat list of rating schemes returns them indexed by their ids. Also each scheme has
214+ * an additional maps giving 'ratingsByCode' and 'ratingsById' .
215+ * @param schemes
216+ */
217+ function indexRatingSchemeItems ( schemeItems = [ ] ) {
218+ return _
219+ . chain ( schemeItems )
220+ . groupBy ( d => d . ratingSchemeId )
221+ . map ( ( v , k ) => Object . assign ( { } , {
222+ id : k ,
223+ ratingsByCode : _ . keyBy ( v , d => d . rating ) ,
224+ ratingsById : _ . keyBy ( v , d => d . id ) ,
225+ } ) )
226+ . keyBy ( d => d . id )
151227 . value ( ) ;
152228}
153229
154230
155231function controller ( $q , serviceBroker ) {
156232 const vm = initialiseData ( this , initialState ) ;
157233
158- function init ( ) {
159- const schemesPromise = serviceBroker
160- . loadAppData ( CORE_API . RatingSchemeStore . findAll )
161- . then ( r => vm . ratingSchemes = r . data ) ;
162-
163- const assessmentDefinitionsPromise = serviceBroker
164- . loadAppData (
165- CORE_API . AssessmentDefinitionStore . findByKind ,
166- [ "CHANGE_INITIATIVE" ] )
167- . then ( r => vm . assessmentDefinitions = r . data ) ;
234+ vm . $onChanges = ( changes ) => {
235+ const sameParent = isSameParentEntityRef ( changes ) ;
168236
169237 const enumPromise = serviceBroker
170238 . loadAppData ( CORE_API . EnumValueStore . findAll )
@@ -174,35 +242,25 @@ function controller($q, serviceBroker) {
174242 "changeInitiativeLifecyclePhase" ) ;
175243 } ) ;
176244
177- return $q
178- . all ( [ schemesPromise , assessmentDefinitionsPromise , enumPromise ] ) ;
179- }
180-
181-
182- vm . $onChanges = ( changes ) => {
183- const sameParent = isSameParentEntityRef ( changes ) ;
184-
185245 if ( vm . parentEntityRef && ! sameParent ) {
186- const selectionOptions = mkSelectionOptions ( vm . parentEntityRef ) ;
187- const ciPromise = serviceBroker
188- . loadViewData (
189- CORE_API . ChangeInitiativeStore . findHierarchyBySelector ,
190- [ selectionOptions ] )
191- . then ( r => vm . changeInitiatives = r . data ) ;
192-
193- const assessmentRatingsPromise = serviceBroker
246+ const viewPromise = serviceBroker
194247 . loadViewData (
195- CORE_API . AssessmentRatingStore . findByTargetKindForRelatedSelector ,
196- [ "CHANGE_INITIATIVE" , selectionOptions ] )
197- . then ( r => vm . assessmentRatings = r . data ) ;
248+ CORE_API . ChangeInitiativeViewStore . findByEntity ,
249+ [ vm . parentEntityRef ] )
250+ . then ( r => {
251+ const d = r . data ;
252+ vm . changeInitiatives = d . changeInitiatives ;
253+ vm . assessmentRatings = d . ratings ;
254+ vm . ratingSchemeItems = d . ratingSchemeItems ;
255+ vm . assessmentDefinitions = d . assessmentDefinitions ;
256+ } ) ;
198257
199- $q . all ( [ init ( ) , ciPromise , assessmentRatingsPromise ] )
258+ $q . all ( [ enumPromise , viewPromise ] )
200259 . then ( ( ) => applyFilters ( ) ) ;
201260 }
202261 } ;
203262
204263 function applyFilters ( ) {
205-
206264 const retiredCiFilter = vm . displayRetiredCis
207265 ? ( ) => true
208266 : ci => ci . lifecyclePhase !== "RETIRED" ;
@@ -226,14 +284,13 @@ function controller($q, serviceBroker) {
226284
227285 vm . assessmentSummaries = mkAssessmentSummaries (
228286 vm . assessmentDefinitions ,
229- vm . ratingSchemes ,
287+ vm . ratingSchemeItems ,
230288 inScopeRatings ,
231289 inScopeCis . length ) ;
232290
233291 vm . gridOptions . data = mkTableData (
234292 relevantCis ,
235293 vm . changeInitiativeLifecyclePhaseByKey ) ;
236-
237294 }
238295
239296 vm . onSelectAssessmentRating = d => {
0 commit comments