alchemy-form 0.1.3 → 0.1.4

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 0.1.4 (2022-03-16)
2
+
3
+ * Make fields work with the new `Alchemy.Map.Backed` and `Alchemy.Map.Enum` class
4
+ * Add the `field_path_in_current_schema` property
5
+ * Make sure `alchemy-fields` know which `alchemy-form` they belong to
6
+ * Improve the `original_value` getters
7
+ * Set the `field-type` attribute of `alchemy-field` elements
8
+ * Fix `alchemy-field` elements sometimes not getting their value elements
9
+
1
10
  ## 0.1.3 (2022-02-20)
2
11
 
3
12
  * Fix datetime fields never showing their value
@@ -125,6 +125,38 @@ Base.setProperty(function wrapper_type() {
125
125
 
126
126
  });
127
127
 
128
+ /**
129
+ * Get the path of this field value in the current (sub)schema
130
+ *
131
+ * @author Jelle De Loecker <jelle@elevenways.be>
132
+ * @since 0.1.4
133
+ * @version 0.1.4
134
+ */
135
+ Base.setProperty(function field_path_in_current_schema() {
136
+
137
+ let result = [],
138
+ parent = this.getParentField(),
139
+ name;
140
+
141
+ name = this.getPathEntryName();
142
+
143
+ if (name) {
144
+ result.push(name);
145
+ }
146
+
147
+ while (parent && !(parent instanceof Classes.Alchemy.Element.Form.FieldSchema)) {
148
+ name = parent.getPathEntryName();
149
+
150
+ if (name) {
151
+ result.unshift(name);
152
+ }
153
+
154
+ parent = parent.getParentField();
155
+ }
156
+
157
+ return result.join('.');
158
+ });
159
+
128
160
  /**
129
161
  * Get the path of this field value in the current record
130
162
  *
@@ -162,7 +194,7 @@ Base.setProperty(function field_path_in_record() {
162
194
  *
163
195
  * @author Jelle De Loecker <jelle@elevenways.be>
164
196
  * @since 0.1.3
165
- * @version 0.1.3
197
+ * @version 0.1.4
166
198
  *
167
199
  * @return {Alchemy.Element.Form.Base}
168
200
  */
@@ -179,6 +211,10 @@ Base.setMethod(function getParentField() {
179
211
  parent = parent.parentElement;
180
212
  }
181
213
 
214
+ if (this.field_context) {
215
+ return this.field_context;
216
+ }
217
+
182
218
  return false;
183
219
  });
184
220
 
@@ -3,11 +3,9 @@
3
3
  *
4
4
  * @author Jelle De Loecker <jelle@elevenways.be>
5
5
  * @since 0.1.0
6
- * @version 0.1.0
6
+ * @version 0.1.4
7
7
  */
8
- var Field = Function.inherits('Alchemy.Element.Form.Base', function Field() {
9
- Field.super.call(this);
10
- });
8
+ const Field = Function.inherits('Alchemy.Element.Form.Base', 'Field');
11
9
 
12
10
  /**
13
11
  * The template to use for the content of this element
@@ -45,6 +43,15 @@ Field.setStatic('use_new_renderer_scope', true);
45
43
  */
46
44
  Field.setAttribute('field-name');
47
45
 
46
+ /**
47
+ * The type of the field
48
+ *
49
+ * @author Jelle De Loecker <jelle@elevenways.be>
50
+ * @since 0.1.4
51
+ * @version 0.1.4
52
+ */
53
+ Field.setAttribute('field-type');
54
+
48
55
  /**
49
56
  * The view override
50
57
  *
@@ -77,9 +84,24 @@ Field.setAssignedProperty('widget_settings');
77
84
  *
78
85
  * @author Jelle De Loecker <jelle@elevenways.be>
79
86
  * @since 0.1.0
80
- * @version 0.1.0
87
+ * @version 0.1.4
81
88
  */
82
- Field.addParentTypeGetter('alchemy_form', 'alchemy-form');
89
+ Field.enforceProperty(function alchemy_form(new_value) {
90
+
91
+ if (new_value == null) {
92
+ new_value = this.queryUp('alchemy-form');
93
+
94
+ if (!new_value && this.field_context) {
95
+ new_value = this.field_context.queryUp('alchemy-form');
96
+ }
97
+
98
+ if (!new_value && this.alchemy_field_schema && this.alchemy_field_schema.alchemy_field) {
99
+ new_value = this.alchemy_field_schema.alchemy_field.alchemy_form;
100
+ }
101
+ }
102
+
103
+ return new_value;
104
+ });
83
105
 
84
106
  /**
85
107
  * Get the error area
@@ -111,7 +133,7 @@ Field.enforceProperty(function alchemy_field_schema(new_value, old_value) {
111
133
  *
112
134
  * @author Jelle De Loecker <jelle@elevenways.be>
113
135
  * @since 0.1.0
114
- * @version 0.1.0
136
+ * @version 0.1.4
115
137
  */
116
138
  Field.enforceProperty(function config(new_value, old_value) {
117
139
 
@@ -124,6 +146,12 @@ Field.enforceProperty(function config(new_value, old_value) {
124
146
  }
125
147
  }
126
148
 
149
+ if (new_value && new_value.constructor && new_value.constructor.type_name) {
150
+ this.field_type = new_value.constructor.type_name;
151
+ } else {
152
+ this.field_type = null;
153
+ }
154
+
127
155
  return new_value;
128
156
  });
129
157
 
@@ -465,17 +493,20 @@ Field.setProperty(function wrapper_files() {
465
493
  *
466
494
  * @author Jelle De Loecker <jelle@elevenways.be>
467
495
  * @since 0.1.0
468
- * @version 0.1.0
496
+ * @version 0.1.4
469
497
  */
470
498
  Field.setProperty(function original_value() {
471
499
 
472
- let alchemy_field_schema = this.alchemy_field_schema;
500
+ let alchemy_field_schema = this.alchemy_field_schema,
501
+ path = this.field_path_in_current_schema;
473
502
 
474
503
  if (alchemy_field_schema) {
475
504
  let original_schema_value = alchemy_field_schema.original_value;
476
505
 
477
506
  if (original_schema_value) {
478
- return original_schema_value[this.field_name];
507
+ if (path) {
508
+ return Object.path(original_schema_value, path);
509
+ }
479
510
  }
480
511
 
481
512
  return;
@@ -484,7 +515,7 @@ Field.setProperty(function original_value() {
484
515
  let form = this.alchemy_form;
485
516
 
486
517
  if (form && form.document) {
487
- return form.document[this.field_name];
518
+ return Object.path(form.document, path);
488
519
  }
489
520
  });
490
521
 
@@ -493,7 +524,7 @@ Field.setProperty(function original_value() {
493
524
  *
494
525
  * @author Jelle De Loecker <jelle@elevenways.be>
495
526
  * @since 0.1.0
496
- * @version 0.1.3
527
+ * @version 0.1.4
497
528
  */
498
529
  Field.setProperty(function value_element() {
499
530
 
@@ -506,7 +537,9 @@ Field.setProperty(function value_element() {
506
537
  input = this.querySelector('alchemy-field-array');
507
538
  } else if (this.contains_schema) {
508
539
  input = this.querySelector('alchemy-field-schema');
509
- } else {
540
+ }
541
+
542
+ if (!input) {
510
543
  input = this.querySelector('.alchemy-field-value');
511
544
  }
512
545
 
@@ -23,7 +23,7 @@ FieldSchema.setTemplateFile('form/elements/alchemy_field_schema');
23
23
  *
24
24
  * @author Jelle De Loecker <jelle@elevenways.be>
25
25
  * @since 0.1.0
26
- * @version 0.1.3
26
+ * @version 0.1.4
27
27
  */
28
28
  FieldSchema.setProperty(function schema() {
29
29
 
@@ -34,13 +34,16 @@ FieldSchema.setProperty(function schema() {
34
34
  let other_field = this.getSchemaSupplierField();
35
35
  schema = null;
36
36
 
37
- console.log('Other field:', other_field, 'of', this)
38
-
39
37
  if (other_field && other_field.value && other_field.config && other_field.config.options) {
40
38
  let values = other_field.config.options.values;
41
39
 
42
40
  if (values) {
43
- schema = values[other_field.value];
41
+
42
+ if (values instanceof Classes.Alchemy.Map.Backed) {
43
+ schema = values.get(other_field.value);
44
+ } else {
45
+ schema = values[other_field.value];
46
+ }
44
47
 
45
48
  if (schema && schema.schema) {
46
49
  schema = schema.schema;
@@ -113,14 +116,11 @@ FieldSchema.setProperty(function value() {
113
116
  FieldSchema.setMethod(function getSchemaSupplierField() {
114
117
 
115
118
  if (!this.alchemy_field || !this.alchemy_field.config || !this.alchemy_field.config.options) {
116
- console.log('--no config found?')
117
119
  return;
118
120
  }
119
121
 
120
122
  let schema = this.alchemy_field.config.options.schema;
121
123
 
122
- console.log('Schema is...', schema);
123
-
124
124
  if (typeof schema == 'string') {
125
125
  let other_field_path = this.resolvePath(schema);
126
126
 
@@ -128,10 +128,7 @@ FieldSchema.setMethod(function getSchemaSupplierField() {
128
128
 
129
129
  let form = this.alchemy_field.alchemy_form;
130
130
 
131
- console.log(' -- Got form:', form);
132
-
133
131
  if (form) {
134
- console.log(' -- Looking for path:', other_field_path)
135
132
  return form.findFieldByPath(other_field_path);
136
133
  }
137
134
  }
@@ -155,4 +152,41 @@ FieldSchema.setMethod(function introduced() {
155
152
 
156
153
  this.rerender();
157
154
  }
158
- });
155
+ });
156
+
157
+ /**
158
+ * Get the original value
159
+ *
160
+ * @author Jelle De Loecker <jelle@elevenways.be>
161
+ * @since 0.1.4
162
+ * @version 0.1.4
163
+ */
164
+ FieldSchema.setProperty(function original_value() {
165
+
166
+ let field = this.alchemy_field,
167
+ path = this.field_path_in_record;
168
+
169
+ if (field && path) {
170
+ let form = field.alchemy_form || this.alchemy_form || this.field_context.alchemy_form;
171
+
172
+ if (form) {
173
+ return Object.path(form.document, path);
174
+ }
175
+ }
176
+
177
+ let context = this.field_context.alchemy_field_schema,
178
+ data;
179
+
180
+ if (context) {
181
+ data = context.original_value;
182
+ } else {
183
+ context = this.field_context.alchemy_form || this.alchemy_field.alchemy_form;
184
+ data = context.document;
185
+ }
186
+
187
+ path = this.field_path_in_current_schema;
188
+
189
+ let result = Object.path(data, path);
190
+
191
+ return result;
192
+ });
@@ -5,18 +5,7 @@
5
5
  * @since 0.1.0
6
6
  * @version 0.1.0
7
7
  */
8
- var Form = Function.inherits('Alchemy.Element.Form.Base', function Form() {
9
- Form.super.call(this);
10
- });
11
-
12
- /**
13
- * The stylesheet to load for this element
14
- *
15
- * @author Jelle De Loecker <jelle@develry.be>
16
- * @since 0.1.0
17
- * @version 0.1.0
18
- */
19
- Form.setStylesheetFile('form/form');
8
+ var Form = Function.inherits('Alchemy.Element.Form.Base', 'Form');
20
9
 
21
10
  /**
22
11
  * The stylesheet to load for this element
@@ -244,12 +244,16 @@ AlchemySelect.setProperty(function loaded_items() {
244
244
  *
245
245
  * @author Jelle De Loecker <jelle@develry.be>
246
246
  * @since 0.1.0
247
- * @version 0.1.0
247
+ * @version 0.1.4
248
248
  *
249
249
  * @type {Number}
250
250
  */
251
251
  AlchemySelect.setProperty(function loaded_item_count() {
252
- return Object.size(this.options.values);
252
+ if (this.options.values) {
253
+ return this.options.values.size;
254
+ } else {
255
+ return 0;
256
+ }
253
257
  });
254
258
 
255
259
  /**
@@ -766,7 +770,7 @@ AlchemySelect.setMethod(function _ensureValueData(value) {
766
770
  *
767
771
  * @author Jelle De Loecker <jelle@develry.be>
768
772
  * @since 0.1.0
769
- * @version 0.1.0
773
+ * @version 0.1.4
770
774
  */
771
775
  AlchemySelect.setMethod(function _processPreloadedValues() {
772
776
 
@@ -784,9 +788,14 @@ AlchemySelect.setMethod(function _processPreloadedValues() {
784
788
  let item,
785
789
  key;
786
790
 
787
- for (key in values) {
791
+ let enum_values = new Classes.Alchemy.Map.Enum(values),
792
+ value;
793
+
794
+ for (let key of enum_values.keys()) {
795
+ value = enum_values.get(key);
796
+
788
797
  response.available++;
789
- item = Object.assign({}, values[key]);
798
+ item = Object.assign({}, value);
790
799
  item.id = key;
791
800
 
792
801
  response.items.push(item);
@@ -915,7 +924,7 @@ AlchemySelect.setMethod(function _loadRemote(config) {
915
924
  *
916
925
  * @author Jelle De Loecker <jelle@develry.be>
917
926
  * @since 0.1.0
918
- * @version 0.1.0
927
+ * @version 0.1.4
919
928
  */
920
929
  AlchemySelect.setMethod(function recreateDropdownElements() {
921
930
 
@@ -925,8 +934,8 @@ AlchemySelect.setMethod(function recreateDropdownElements() {
925
934
 
926
935
  Hawkejs.removeChildren(this.dropdown_content);
927
936
 
928
- for (key in items) {
929
- item = items[key];
937
+ for (let key of items.keys()) {
938
+ item = items.get(key);
930
939
  item = this._makeOption(item.id, item);
931
940
  this.addToDropdown(item);
932
941
  }
@@ -1157,7 +1166,7 @@ AlchemySelect.setMethod(function close(event) {
1157
1166
  *
1158
1167
  * @author Jelle De Loecker <jelle@develry.be>
1159
1168
  * @since 0.1.0
1160
- * @version 0.1.0
1169
+ * @version 0.1.4
1161
1170
  *
1162
1171
  * @param {String} type "value" or "option"
1163
1172
  * @param {Mixed} value The actual value of this item
@@ -1188,7 +1197,11 @@ AlchemySelect.setMethod(function _makeValueItem(type, value, data) {
1188
1197
  this.options.values = {};
1189
1198
  }
1190
1199
 
1191
- this.options.values[value] = data;
1200
+ if (this.options.values instanceof Classes.Alchemy.Map.Enum) {
1201
+ this.options.values.set(value, data);
1202
+ } else {
1203
+ this.options.values[value] = data;
1204
+ }
1192
1205
  }
1193
1206
 
1194
1207
  // And the associated data
@@ -1207,8 +1220,12 @@ AlchemySelect.setMethod(function _makeValueItem(type, value, data) {
1207
1220
  return
1208
1221
  }
1209
1222
 
1210
- if (that.options.values && that.options.values[value]) {
1211
- data = that.options.values[value];
1223
+ if (that.options.values) {
1224
+ if (that.options.values instanceof Classes.Alchemy.Map.Enum) {
1225
+ data = that.options.values.get(value);
1226
+ } else if (that.options.values[value]) {
1227
+ data = that.options.values[value];
1228
+ }
1212
1229
  }
1213
1230
 
1214
1231
  this.data = data;
@@ -28,18 +28,65 @@ AlchemyField.constitute(function prepareSchema() {
28
28
  // this.schema.addField('widgets', widgets, {array: true});
29
29
  });
30
30
 
31
+ /**
32
+ * Find the alchemy-form parent
33
+ *
34
+ * @author Jelle De Loecker <jelle@elevenways.be>
35
+ * @since 0.1.4
36
+ * @version 0.1.4
37
+ */
38
+ AlchemyField.enforceProperty(function alchemy_form(new_value) {
39
+
40
+ if (!new_value && this.config && this.config.alchemy_form) {
41
+ new_value = this.config.alchemy_form;
42
+ }
43
+
44
+ if (!new_value) {
45
+
46
+ let parent = this.parent_instance;
47
+
48
+ while (parent) {
49
+
50
+ new_value = parent.alchemy_form;
51
+
52
+ if (new_value) {
53
+ break;
54
+ }
55
+
56
+ if (parent.element) {
57
+ new_value = parent.element.querySelector('alchemy-form');
58
+
59
+ if (new_value) {
60
+ break;
61
+ }
62
+ }
63
+
64
+ parent = parent.parent_instance;
65
+ }
66
+ }
67
+
68
+ return new_value;
69
+ });
70
+
31
71
  /**
32
72
  * Populate the widget
33
73
  *
34
74
  * @author Jelle De Loecker <jelle@elevenways.be>
35
75
  * @since 0.1.0
36
- * @version 0.1.2
76
+ * @version 0.1.4
37
77
  */
38
78
  AlchemyField.setMethod(function populateWidget() {
39
79
 
40
80
  let config = this.config;
41
81
 
82
+ let alchemy_form = this.alchemy_form;
83
+
42
84
  let field_el = this.createElement('alchemy-field');
85
+
86
+ if (alchemy_form) {
87
+ field_el.alchemy_form = alchemy_form;
88
+ }
89
+
43
90
  field_el.field_name = config.field;
44
91
 
45
92
  if (config.view) {
@@ -16,7 +16,7 @@ const AlchemyForm = Function.inherits('Alchemy.Widget', 'AlchemyForm');
16
16
  *
17
17
  * @author Jelle De Loecker <jelle@elevenways.be>
18
18
  * @since 0.1.0
19
- * @version 0.1.3
19
+ * @version 0.1.4
20
20
  */
21
21
  AlchemyForm.setMethod(function populateWidget() {
22
22
 
@@ -31,7 +31,22 @@ AlchemyForm.setMethod(function populateWidget() {
31
31
  form.classList.add('alchemy-widgets-container');
32
32
 
33
33
  if (this.config && this.config.widgets) {
34
- col.widget.value = this.config.widgets;
34
+ let widgets = this.config.widgets.slice(0),
35
+ widget,
36
+ i;
37
+
38
+ for (i = 0; i < widgets.length; i++) {
39
+ widget = widgets[i];
40
+
41
+ if (widget.type == 'alchemy_field') {
42
+ widget = Object.assign({}, widget);
43
+ widget.config = Object.assign({}, widget.config);
44
+ widget.config.alchemy_form = form;
45
+ widgets[i] = widget;
46
+ }
47
+ }
48
+
49
+ col.widget.value = widgets;
35
50
  }
36
51
 
37
52
  let record = this.element.getContextVariable('record');
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "alchemy-form",
3
3
  "description": "Form plugin for Alchemy",
4
- "version": "0.1.3",
4
+ "version": "0.1.4",
5
5
  "repository": {
6
6
  "type" : "git",
7
7
  "url" : "https://github.com/11ways/alchemy-form.git"
8
8
  },
9
9
  "peerDependencies": {
10
- "alchemymvc" : "~1.1.3"
10
+ "alchemymvc" : "~1.2.0"
11
11
  },
12
12
  "license": "MIT",
13
13
  "engines": {
@@ -1,9 +1,7 @@
1
- {% with self.sub_fields as sub_field %}
2
- {% each %}
1
+ {% each self.sub_fields as sub_field %}
3
2
  <alchemy-field
4
3
  #alchemy_field_schema=<% self %>
5
4
  #schema=<% self.schema %>
6
5
  field-name=<% sub_field.name %>
7
6
  ></alchemy-field>
8
- {% /each %}
9
- {% /with %}
7
+ {% /each %}