11import { Dialog } from '@angular/cdk/dialog' ;
2+ import { NgZone } from '@angular/core' ;
23import { UntypedFormGroup } from '@angular/forms' ;
34import { surveyLocalization } from 'survey-angular' ;
45
@@ -64,12 +65,14 @@ export const buildSearchButton = (
6465 * @param question The question object
6566 * @param multiselect Indicate if we need multiselect
6667 * @param dialog The Dialog service
68+ * @param ngZone Angular Service to execute code inside Angular environment
6769 * @returns The button DOM element
6870 */
6971export const buildAddButton = (
7072 question : any ,
7173 multiselect : boolean ,
72- dialog : Dialog
74+ dialog : Dialog ,
75+ ngZone : NgZone
7376) : any => {
7477 const addButton = document . createElement ( 'button' ) ;
7578 addButton . innerText = surveyLocalization . getString (
@@ -78,60 +81,61 @@ export const buildAddButton = (
7881 ) ;
7982 if ( question . addRecord && question . addTemplate && ! question . isReadOnly ) {
8083 addButton . onclick = async ( ) => {
81- const { SafeResourceModalComponent } = await import (
82- '../../components/resource-modal/resource-modal.component'
83- ) ;
84- const dialogRef = dialog . open ( SafeResourceModalComponent , {
85- disableClose : true ,
86- data : {
87- template : question . addTemplate ,
88- locale : question . resource . value ,
89- askForConfirm : false ,
90- ...( question . prefillWithCurrentRecord && {
91- prefillData : question . survey . data ,
92- } ) ,
93- } ,
94- height : '98%' ,
95- width : '100vw' ,
96- panelClass : 'full-screen-modal' ,
97- autoFocus : false ,
98- } ) ;
99- dialogRef . closed . subscribe ( ( result : any ) => {
100- if ( result ) {
101- const { data } = result ;
102- // TODO: call reload method
103- // if (question.displayAsGrid && gridComponent) {
104- // gridComponent.availableRecords.push({
105- // value: data.id,
106- // text: data.data[question.displayField]
107- // });
108- // }
109- if ( multiselect ) {
110- const newItem = {
111- value : data . id ,
112- text : data . data [ question . displayField ] ,
113- } ;
114- question . contentQuestion . choices = [
115- newItem ,
116- ...question . contentQuestion . choices ,
117- ] ;
118- question . newCreatedRecords = question . newCreatedRecords
119- ? question . newCreatedRecords . concat ( data . id )
120- : [ data . id ] ;
121- question . value = question . value . concat ( data . id ) ;
122- } else {
123- const newItem = {
124- value : data . id ,
125- text : data . data [ question . displayField ] ,
126- } ;
127- question . contentQuestion . choices = [
128- newItem ,
129- ...question . contentQuestion . choices ,
130- ] ;
131- question . newCreatedRecords = data . id ;
132- question . value = data . id ;
84+ ngZone . run ( async ( ) => {
85+ const { SafeResourceModalComponent } = await import (
86+ '../../components/resource-modal/resource-modal.component'
87+ ) ;
88+ const dialogRef = dialog . open ( SafeResourceModalComponent , {
89+ disableClose : true ,
90+ data : {
91+ template : question . addTemplate ,
92+ locale : question . resource . value ,
93+ askForConfirm : false ,
94+ ...( question . prefillWithCurrentRecord && {
95+ prefillData : question . survey . data ,
96+ } ) ,
97+ } ,
98+ height : '98%' ,
99+ width : '100vw' ,
100+ panelClass : 'full-screen-modal' ,
101+ } ) ;
102+ dialogRef . closed . subscribe ( ( result : any ) => {
103+ if ( result ) {
104+ const { data } = result ;
105+ // TODO: call reload method
106+ // if (question.displayAsGrid && gridComponent) {
107+ // gridComponent.availableRecords.push({
108+ // value: data.id,
109+ // text: data.data[question.displayField]
110+ // });
111+ // }
112+ if ( multiselect ) {
113+ const newItem = {
114+ value : data . id ,
115+ text : data . data [ question . displayField ] ,
116+ } ;
117+ question . contentQuestion . choices = [
118+ newItem ,
119+ ...question . contentQuestion . choices ,
120+ ] ;
121+ question . newCreatedRecords = question . newCreatedRecords
122+ ? question . newCreatedRecords . concat ( data . id )
123+ : [ data . id ] ;
124+ question . value = question . value . concat ( data . id ) ;
125+ } else {
126+ const newItem = {
127+ value : data . id ,
128+ text : data . data [ question . displayField ] ,
129+ } ;
130+ question . contentQuestion . choices = [
131+ newItem ,
132+ ...question . contentQuestion . choices ,
133+ ] ;
134+ question . newCreatedRecords = data . id ;
135+ question . value = data . id ;
136+ }
133137 }
134- }
138+ } ) ;
135139 } ) ;
136140 } ;
137141 }
0 commit comments