/
index.ts
235 lines (202 loc) · 7.41 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { SPTypes } from "gd-sprest";
import { Components } from "../core";
import { ListForm } from "../listForm";
import { IListFormEdit } from "../listForm/types";
import { IListFormDialog, IListFormDialogProps } from "./types";
/**
* List Form Dialog
*/
export const ListFormDialog = (props: IListFormDialogProps): IListFormDialog => {
let form: IListFormEdit = null;
let modalProps = props.modalProps || {};
// Method to save the form
let saveForm = (): PromiseLike<any> => {
// Hide the form
form.el.classList.add("d-none");
// Hide the footer
elFooter.classList.add("d-none");
// Add a progress
let progress = Components.Progress({
el: form.el.parentElement,
isAnimated: true,
isStriped: true,
size: 100
}).el;
// Return a promise
return new Promise((resolve, reject) => {
// Ensure the form is valid
if (!form.isValid()) {
// Remove the progress
form.el.parentElement.removeChild(progress);
// Show the form
form.el.classList.remove("d-none");
// Show the footer
elFooter.classList.remove("d-none");
// Reject the promise
reject("Form is invalid");
// Return
return;
}
// Save the form
form.save().then(item => {
// Remove the progress
form.el.parentElement.removeChild(progress);
// Show the form
form.el.classList.remove("d-none");
// Show the footer
elFooter.classList.remove("d-none");
// Call the save event
props.onSaved ? props.onSaved(item) : null;
// Resolve the promise
resolve(item);
}, reject);
});
};
// Update the properties
modalProps.el = modalProps.el || props.el;
modalProps.title = modalProps.title || props.listName;
modalProps.body = Components.Progress({
isAnimated: true,
isStriped: true,
label: "Loading the Form...",
size: 100
}).el.outerHTML;
// Set the on render event
modalProps.onRenderBody = (el) => {
// Create an instance of the list form
ListForm.create({
excludeFields: props.excludeFields,
fields: props.fields,
item: props.item,
itemId: props.itemId,
listName: props.listName,
loadAttachments: props.loadAttachments,
query: props.query,
webUrl: props.webUrl
}).then(
// Success
info => {
// Clear the modal body
el.innerHTML = "";
// Ensure the item exists, otherwise default to the new form
let controlMode = props.item || props.itemId > 0 ? props.controlMode : SPTypes.ControlMode.New;
// Check the control mode
switch (controlMode) {
// Edit Form
case SPTypes.ControlMode.Edit:
case SPTypes.ControlMode.New:
// Render the list form
form = ListForm.renderEditForm({
controlMode: props.controlMode,
el,
onControlRendered: props.onControlRendered,
onControlRendering: props.onControlRendering,
onSaving: props.onSaving,
info,
template: props.template
});
break;
// Default - Display Form
default:
// Render the list form
ListForm.renderDisplayForm({
el,
info,
onControlRendered: props.onControlRendered,
onControlRendering: props.onControlRendering,
template: props.template
});
break;
}
// Display the actions
elFooter.classList.remove("d-none");
},
// Error
() => {
// Log the error
console.error("Error loading the list form information.");
// Clear the modal
el.innerHTML = "";
// Display an error message
Components.Alert({
el,
content: "Error loading the list form information.",
type: Components.AlertTypes.Danger
});
}
);
}
// Set the render footer event
modalProps.onRenderFooter = (el) => {
// Parse the actions
let actions = props.actions || null;
if (actions == null) {
// Default the actions based on the control mode
switch (props.controlMode) {
// Edit
case (SPTypes.ControlMode.Edit):
// Set the actions
actions = {
spacing: 3,
items: [
{
buttons: [{ text: "Close", onClick: () => { dialog.hide(); } }]
},
{
buttons: [{ text: "Update", onClick: saveForm }]
}
]
};
break;
// New
case (SPTypes.ControlMode.New):
// Set the actions
actions = {
spacing: 3,
items: [
{
buttons: [{ text: "Close", onClick: () => { dialog.hide(); } }]
},
{
buttons: [{ text: "Save", onClick: saveForm }]
}
]
};
break;
// Default - Display
default:
actions = {
spacing: 3,
items: [
{
buttons: [{ text: "Close", onClick: () => { dialog.hide(); } }]
}
]
};
break;
}
}
// Set the element
actions.el = el;
// Render the toolbar
Components.Toolbar(actions);
}
// Create the dialog
let dialog = Components.Modal(modalProps) as IListFormDialog;
// Hide the actions
let elFooter = dialog.el.querySelector(".modal-footer");
elFooter.classList.add("d-none");
// Add the class name
dialog.el.classList.add("listformdialog");
// Set the save method
form == null ? null : dialog.saveForm = saveForm;
// See if we are displaying it by default
if (props.visible) {
// Display the dialog
dialog.show();
}
// Execute the assign to event
props.assignTo ? props.assignTo(dialog) : null;
// Return the dialog
return dialog;
}