alchemy-form 0.2.8 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,6 +34,15 @@ Form.setAttribute('method');
34
34
  */
35
35
  Form.setAttribute('model');
36
36
 
37
+ /**
38
+ * Should the entire document be submitted?
39
+ *
40
+ * @author Jelle De Loecker <jelle@elevenways.be>
41
+ * @since 0.2.9
42
+ * @version 0.2.9
43
+ */
44
+ Form.setAttribute('serialize-entire-document', {type: 'boolean'});
45
+
37
46
  /**
38
47
  * The document that is being edited
39
48
  *
@@ -88,34 +97,20 @@ Form.setProperty(function main_error_area() {
88
97
  *
89
98
  * @author Jelle De Loecker <jelle@elevenways.be>
90
99
  * @since 0.1.0
91
- * @version 0.2.0
100
+ * @version 0.2.9
92
101
  */
93
102
  Form.setProperty(function value() {
94
103
 
95
- let fields = this.queryAllNotNested('al-field'),
96
- result = {},
97
- field,
98
- key,
99
- i;
100
-
101
- if (this.document && this.document.$pk) {
102
- result[this.document.$model.primary_key] = this.document.$pk;
103
- }
104
+ let result = this.getMainValue();
104
105
 
105
- for (i = 0; i < fields.length; i++) {
106
- field = fields[i];
106
+ if (this.model) {
107
+ let model = alchemy.getModel(this.model);
107
108
 
108
- if (field && field.readonly) {
109
- continue;
109
+ if (model) {
110
+ result = {
111
+ [model.model_name] : result
112
+ };
110
113
  }
111
-
112
- result[field.field_name] = field.value;
113
- }
114
-
115
- if (this.model) {
116
- result = {
117
- [this.model] : result
118
- };
119
114
  }
120
115
 
121
116
  return result;
@@ -148,6 +143,45 @@ Form.setMethod(Hawkejs.SERIALIZE_FORM, function serializeForm() {
148
143
  return this.value;
149
144
  });
150
145
 
146
+ /**
147
+ * Get the non-wrapped value
148
+ *
149
+ * @author Jelle De Loecker <jelle@elevenways.be>
150
+ * @since 0.2.9
151
+ * @version 0.2.9
152
+ */
153
+ Form.setMethod(function getMainValue() {
154
+
155
+ let fields = this.queryAllNotNested('al-field'),
156
+ result = {},
157
+ field,
158
+ i;
159
+
160
+ if (this.document) {
161
+
162
+ if (this.serialize_entire_document) {
163
+ let value = this.document.$main;
164
+ Object.assign(result, value);
165
+ }
166
+
167
+ if (this.document.$pk) {
168
+ result[this.document.$model.primary_key] = this.document.$pk;
169
+ }
170
+ }
171
+
172
+ for (i = 0; i < fields.length; i++) {
173
+ field = fields[i];
174
+
175
+ if (field && field.readonly) {
176
+ continue;
177
+ }
178
+
179
+ result[field.field_name] = field.value;
180
+ }
181
+
182
+ return result;
183
+ });
184
+
151
185
  /**
152
186
  * Submit this form
153
187
  *
@@ -172,33 +206,131 @@ Form.setMethod(async function submit() {
172
206
  }
173
207
  }
174
208
 
209
+ if (result?.render == false && result.document) {
210
+ this.setDocument(result.document);
211
+ }
212
+
175
213
  return result;
176
214
  });
177
215
 
216
+ /**
217
+ * Get a field by its name
218
+ *
219
+ * @author Jelle De Loecker <jelle@elevenways.be>
220
+ * @since 0.2.9
221
+ * @version 0.2.9
222
+ */
223
+ Form.setMethod(function getField(name) {
224
+
225
+ let fields = this.queryAllNotNested('al-field'),
226
+ field,
227
+ i;
228
+
229
+ for (i = 0; i < fields.length; i++) {
230
+ field = fields[i];
231
+
232
+ if (field.field_name == name) {
233
+ return field;
234
+ }
235
+ }
236
+ });
237
+
238
+ /**
239
+ * Set the document
240
+ *
241
+ * @author Jelle De Loecker <jelle@elevenways.be>
242
+ * @since 0.2.9
243
+ * @version 0.2.9
244
+ */
245
+ Form.setMethod(function setDocument(document) {
246
+
247
+ let current_value = this.getMainValue();
248
+
249
+ // Set the current document
250
+ this.document = document;
251
+
252
+ console.log('Setting document', document);
253
+
254
+ for (let key in current_value) {
255
+ let original_value = current_value[key],
256
+ new_value = document[key];
257
+
258
+ if (!Object.alike(original_value, new_value)) {
259
+ let field = this.getField(key);
260
+
261
+ if (field) {
262
+ field.value = new_value;
263
+ }
264
+ }
265
+ }
266
+
267
+ });
268
+
178
269
  /**
179
270
  * Validate this form
180
271
  *
181
272
  * @author Jelle De Loecker <jelle@elevenways.be>
182
273
  * @since 0.2.2
183
- * @version 0.2.2
274
+ * @version 0.2.10
184
275
  */
185
276
  Form.setMethod(async function validate() {
186
277
 
187
- if (this.model) {
188
- let model = alchemy.getModel(this.model);
278
+ let doc = this.getValueAsDocument();
189
279
 
190
- if (model) {
191
- let doc = model.createDocument(this.value);
280
+ let violations = await doc.getViolations();
192
281
 
193
- let violations = await doc.getViolations();
282
+ this.clearErrors();
194
283
 
195
- if (violations) {
196
- this.showError(violations);
197
- throw violations;
198
- }
199
- }
284
+ if (violations) {
285
+ this.showError(violations);
286
+ throw violations;
200
287
  }
288
+ });
289
+
290
+ /**
291
+ * Get the value as a document instance
292
+ *
293
+ * @author Jelle De Loecker <jelle@elevenways.be>
294
+ * @since 0.2.9
295
+ * @version 0.2.9
296
+ */
297
+ Form.setMethod(function getValueAsDocument() {
298
+
299
+ if (!this.model) {
300
+ return;
301
+ }
302
+
303
+ let model = alchemy.getModel(this.model);
304
+
305
+ if (model) {
306
+ return model.createDocument(this.getMainValue());
307
+ }
308
+ });
309
+
310
+ /**
311
+ * Get the updated document:
312
+ * The original document's values + the form's values
313
+ *
314
+ * @author Jelle De Loecker <jelle@elevenways.be>
315
+ * @since 0.2.9
316
+ * @version 0.2.9
317
+ */
318
+ Form.setMethod(function getUpdatedDocument() {
319
+
320
+ if (!this.model) {
321
+ return;
322
+ }
323
+
324
+ const model = alchemy.getModel(this.model);
325
+ const original_value = this.document;
326
+
327
+ if (!original_value) {
328
+ return this.getValueAsDocument();
329
+ }
330
+
331
+ let main_value = Object.assign({}, original_value.$main, this.getMainValue());
201
332
 
333
+ return model.createDocument(main_value);
202
334
  });
203
335
 
204
336
  /**
@@ -1121,7 +1121,7 @@ Table.setMethod(function getRemoteFetchConfig() {
1121
1121
  *
1122
1122
  * @author Jelle De Loecker <jelle@elevenways.be>
1123
1123
  * @since 0.2.0
1124
- * @version 0.2.8
1124
+ * @version 0.2.10
1125
1125
  */
1126
1126
  Table.setMethod(function applyFetchedData(err, result, config) {
1127
1127
 
@@ -1132,7 +1132,9 @@ Table.setMethod(function applyFetchedData(err, result, config) {
1132
1132
 
1133
1133
  let records;
1134
1134
 
1135
- if (Array.isArray(result)) {
1135
+ if (!result) {
1136
+ records = [];
1137
+ } else if (Array.isArray(result)) {
1136
1138
  records = result;
1137
1139
  } else if (result.length) {
1138
1140
  // This way we keep `DocumentList` instances as they are!
@@ -42,12 +42,16 @@ AlchemyToggle.setAttribute('value', null, function setValue(value) {
42
42
  *
43
43
  * @author Jelle De Loecker <jelle@elevenways.be>
44
44
  * @since 0.1.0
45
- * @version 0.1.0
45
+ * @version 0.2.9
46
46
  */
47
47
  AlchemyToggle.setMethod(function introduced() {
48
48
 
49
49
  this.addEventListener('click', e => {
50
50
  this.value = !this.value;
51
+ this.emit('change', {
52
+ 'bubbles' : true,
53
+ 'cancelable': true
54
+ });
51
55
  });
52
56
 
53
57
  let input = this.querySelector('input');
@@ -62,5 +66,9 @@ AlchemyToggle.setMethod(function introduced() {
62
66
  }
63
67
 
64
68
  this.value = input.checked;
69
+ this.emit('change', {
70
+ 'bubbles' : true,
71
+ 'cancelable': true
72
+ });
65
73
  });
66
74
  });