@ajf/material 18.0.5 → 18.0.6

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.
@@ -53,7 +53,7 @@ import { DataSource } from '@angular/cdk/collections';
53
53
  import { BehaviorSubject, of, Subject, Subscription, combineLatest } from 'rxjs';
54
54
  import * as i6$1 from '@ngneat/transloco';
55
55
  import { take, filter, map, scan, shareReplay, withLatestFrom, finalize, switchMap, startWith, pairwise, distinctUntilChanged, sample } from 'rxjs/operators';
56
- import { createCondition, alwaysCondition, createFormula, neverCondition } from '@ajf/core/models';
56
+ import { createFormula, createCondition, alwaysCondition, neverCondition } from '@ajf/core/models';
57
57
  import { deepCopy } from '@ajf/core/utils';
58
58
  import { ENTER, COMMA } from '@angular/cdk/keycodes';
59
59
  import * as i5$1 from '@angular/material/core';
@@ -278,21 +278,70 @@ function getNodeContainer(c, node) {
278
278
  }
279
279
  return null;
280
280
  }
281
+ function toArray(input) {
282
+ if (!input)
283
+ return [];
284
+ input = input.replace(/\[|\]/g, '').trim();
285
+ return input
286
+ .split(',')
287
+ .map(s => s.trim())
288
+ .map(s => s.replace(/^['"]|['"]$/g, ''))
289
+ .filter(s => s);
290
+ }
291
+ /**
292
+ * Take the defaultValue from the properties box and return the new value to save in the ajf form defaultValue field properties
293
+ * @param value
294
+ * @param node
295
+ * @returns
296
+ * {"formula": "'colazione note'"}
297
+ * {"formula": "'[\"colazione\", \"docce\"]'"}
298
+ * {"formula": "3"}
299
+ * {"formula": "(1 === 1)"}
300
+ */
281
301
  function getDefaultValue(value, node) {
282
302
  let defaultValue = value && value.trim() != '' ? value : null;
283
303
  if (defaultValue) {
284
304
  switch (node.fieldType) {
285
305
  case AjfFieldType.Boolean:
286
- return defaultValue === 'true' || defaultValue === '1';
306
+ if (defaultValue === 'true' || defaultValue === '1') {
307
+ return true;
308
+ }
309
+ if (defaultValue === 'false' || defaultValue === '0') {
310
+ return false;
311
+ }
312
+ return createFormula({ formula: defaultValue });
287
313
  case AjfFieldType.MultipleChoice:
288
- return [defaultValue];
289
- case AjfFieldType.Number:
290
- const v = +defaultValue;
291
- return isNaN(v) ? 0 : v;
314
+ // return a string[]
315
+ return toArray(defaultValue);
292
316
  }
317
+ return createFormula({ formula: defaultValue });
293
318
  }
294
319
  return defaultValue;
295
320
  }
321
+ /**
322
+ * Take the defaultValue from the ajf form defaultValue prop (non formula)
323
+ * and return the value to be shown in the properties box
324
+ * @param value
325
+ * @param node
326
+ * @returns
327
+ */
328
+ function cleanDefaultValue(value, node) {
329
+ if (!value || String(value).trim() === '') {
330
+ return null;
331
+ }
332
+ switch (node.fieldType) {
333
+ case AjfFieldType.String:
334
+ case AjfFieldType.Text:
335
+ case AjfFieldType.SingleChoice:
336
+ if (/^"[^"]*"$/.test(String(value)) || /^'[^']*'$/.test(String(value))) {
337
+ return String(value);
338
+ }
339
+ return `'${String(value)}'`;
340
+ case AjfFieldType.MultipleChoice:
341
+ return JSON.stringify(value);
342
+ }
343
+ return String(value);
344
+ }
296
345
  function buildFormBuilderNodesSubtree(nodes, parent, ignoreConditionalBranches = false) {
297
346
  const entries = nodes
298
347
  .filter(n => n.parent === parent.id)
@@ -2378,8 +2427,11 @@ class AjfFbNodeProperties {
2378
2427
  });
2379
2428
  }
2380
2429
  const formula = node.formula != null ? node.formula.formula : null;
2430
+ const defaultValue = node.defaultValue && node.defaultValue.formula != null
2431
+ ? node.defaultValue.formula
2432
+ : cleanDefaultValue(node.defaultValue, node);
2381
2433
  controls.description = node.description;
2382
- controls.defaultValue = node.defaultValue;
2434
+ controls.defaultValue = defaultValue;
2383
2435
  controls.hint = node.hint;
2384
2436
  controls.size = node.size;
2385
2437
  controls.formula = formula;
@@ -3376,5 +3428,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImpo
3376
3428
  * Generated bundle index. Do not edit.
3377
3429
  */
3378
3430
 
3379
- export { AjfFormBuilder, AjfFormBuilderModule, AjfFormBuilderService, disableFieldDropPredicate, disableSlideDropPredicate, flattenNodes, onDropProcess };
3431
+ export { AjfFormBuilder, AjfFormBuilderModule, AjfFormBuilderService, cleanDefaultValue, disableFieldDropPredicate, disableSlideDropPredicate, flattenNodes, onDropProcess };
3380
3432
  //# sourceMappingURL=ajf-material-form-builder.mjs.map