@@ -36,19 +36,25 @@ function createNew(
3636 } ;
3737}
3838
39- function createDraftFromDashboard (
39+ function createNewFromDashboard (
4040 dashboard : Dashboard ,
41- user : User ,
42- version : number
41+ id : string ,
42+ name : string ,
43+ version : number ,
44+ parentDashboardId : string ,
45+ user : User
4346) : Dashboard {
44- const id = uuidv4 ( ) ;
45-
4647 let widgets : Array < Widget > = [ ] ;
48+ const tableOfContents : { [ key : string ] : boolean } = { } ;
4749 if ( dashboard . widgets ) {
4850 // Duplicate all widgets related to this dashboard
49- widgets = dashboard . widgets . map ( ( widget ) =>
50- WidgetFactory . createFromWidget ( id , widget )
51- ) ;
51+ widgets = dashboard . widgets . map ( ( widget ) => {
52+ const newWidget = WidgetFactory . createFromWidget ( id , widget ) ;
53+ if ( dashboard . tableOfContents ) {
54+ tableOfContents [ newWidget . id ] = dashboard . tableOfContents [ widget . id ] ;
55+ }
56+ return newWidget ;
57+ } ) ;
5258 for ( const widget of widgets ) {
5359 if ( widget . section ) {
5460 const sectionIndex = dashboard . widgets . findIndex (
@@ -69,13 +75,13 @@ function createDraftFromDashboard(
6975
7076 return {
7177 id,
72- name : dashboard . name ,
73- version : version ,
74- parentDashboardId : dashboard . parentDashboardId ,
78+ name,
79+ version,
80+ parentDashboardId,
7581 topicAreaId : dashboard . topicAreaId ,
7682 topicAreaName : dashboard . topicAreaName ,
7783 displayTableOfContents : dashboard . displayTableOfContents ,
78- tableOfContents : dashboard . tableOfContents ,
84+ tableOfContents,
7985 description : dashboard . description ,
8086 state : DashboardState . Draft ,
8187 createdBy : user . userId ,
@@ -86,53 +92,26 @@ function createDraftFromDashboard(
8692 } ;
8793}
8894
95+ function createDraftFromDashboard (
96+ dashboard : Dashboard ,
97+ user : User ,
98+ version : number
99+ ) : Dashboard {
100+ return createNewFromDashboard (
101+ dashboard ,
102+ uuidv4 ( ) ,
103+ dashboard . name ,
104+ version ,
105+ dashboard . parentDashboardId ,
106+ user
107+ ) ;
108+ }
109+
89110function createCopyFromDashboard ( dashboard : Dashboard , user : User ) : Dashboard {
90111 const id = uuidv4 ( ) ;
91-
92- // Copy all widgets associated with the dashboard.
93- let widgets : Array < Widget > = [ ] ;
94- if ( dashboard . widgets ) {
95- widgets = dashboard . widgets . map ( ( widget ) =>
96- WidgetFactory . createFromWidget ( id , widget )
97- ) ;
98- for ( const widget of widgets ) {
99- if ( widget . section ) {
100- const sectionIndex = dashboard . widgets . findIndex (
101- ( w ) => w . id === widget . section
102- ) ;
103- widget . section = widgets [ sectionIndex ] . id ;
104- }
105- if ( widget . content && widget . content . widgetIds ) {
106- const widgetIds : string [ ] = [ ] ;
107- for ( const id of widget . content . widgetIds ) {
108- const widgetIndex = dashboard . widgets . findIndex ( ( w ) => w . id === id ) ;
109- widgetIds . push ( widgets [ widgetIndex ] . id ) ;
110- }
111- widget . content . widgetIds = widgetIds ;
112- }
113- }
114- }
115-
116- return {
117- id,
118- name : "Copy of " + dashboard . name ,
119- version : 1 ,
120- parentDashboardId : id ,
121- topicAreaId : dashboard . topicAreaId ,
122- topicAreaName : dashboard . topicAreaName ,
123- displayTableOfContents : dashboard . displayTableOfContents ,
124- tableOfContents : dashboard . tableOfContents ,
125- description : dashboard . description ,
126- state : DashboardState . Draft ,
127- createdBy : user . userId ,
128- updatedAt : new Date ( ) ,
129- updatedBy : user . userId ,
130- submittedBy : undefined ,
131- publishedBy : undefined ,
132- archivedBy : undefined ,
133- widgets,
134- friendlyURL : undefined ,
135- } ;
112+ const name = "Copy of " + dashboard . name ;
113+ const version = 1 ;
114+ return createNewFromDashboard ( dashboard , id , name , version , id , user ) ;
136115}
137116
138117/**
0 commit comments