import { cloneObject , without , getProp } from "@esri/hub-common" ; import { IInitiativeModel , IInitiativeItem } from "@esri/hub-common" ; import { CURRENT_SCHEMA_VERSION } from "./migrator" ; import { INITIATIVE_TYPE_NAME } from "./add" ; export interface export interface IInitiativeTemplateOptions { title : string ; initiativeKey : string ; groupIds ? : IInitiativeGroupIds ; } export interface export interface IInitiativeGroupIds { [ s ] : string ; } export param { IInitiativeModel } template param { * } options param { IRequestOptions } requestOptions returns { Promise < IInitiativeModel > } export function createInitiativeModelFromTemplate ( template , options ) { const model = { item : cloneObject ( template . item ) as IInitiativeItem , data : { } } as IInitiativeModel ; model . item . title = options . title ; model . item . tags = [ "Hub Initiative" ] ; model . item . type = INITIATIVE_TYPE_NAME ; model . item . typeKeywords = without ( model . item . typeKeywords , "hubInitiativeTemplate" ) ; model . item . typeKeywords . push ( "hubInitiative" ) ; [ "id" , "owner" , "created_at" , "modified_at" ] . forEach ( prop => delete model . item [ prop ] ) ; model . item . properties = { source : template . item . id , schemaVersion : CURRENT_SCHEMA_VERSION , initialParent : template . item . id } ; Object . assign ( model . item . properties , options . groupIds ) ; model . data = { assets : cloneObject ( template . data . assets ) , steps : cloneObject ( template . data . steps ) , indicators : [ ] , source : template . item . id , values : { initiativeKey : options . initiativeKey , bannerImage : cloneObject ( template . data . values . bannerImage ) } } ; if ( getProp ( template , "data.recommendedTemplates" ) ) { model . data . recommendedTemplates = template . data . recommendedTemplates ; } Object . assign ( model . data . values , options . groupIds ) ; if ( ! model . data . values . bannerImage ) { model . data . values . bannerImage = { source : "bannerImage" , display : { position : { x : "50%" , y : "10%" } } } ; } return model ; }