Skip to content

Commit 17f26e6

Browse files
committed
[EAK-521] Ability to pass a path to set/set-if-blank actions to access nested fields
1 parent bc18e28 commit 17f26e6

File tree

4 files changed

+100
-24
lines changed

4 files changed

+100
-24
lines changed

docs/content/dev-tools/depends-on/api.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ order: 3
1010

1111
Built-in plug-in actions are:
1212
* `visibility` - hide the element if the Query result is 'falsy'. <u>This is the default action that is applied when no action is specified.</u>
13+
1314
* `tab-visibility` - hide the tab or the element's parent tab if the Query result is 'falsy'
14-
* `set` - set the Query result as the field's value (undefined Query result skipped)
15-
* `set-if-blank` - set the Query result as the field's value only if the current value is blank (undefined Query result skipped)
15+
16+
* `set` - set the Query result as the field's value (`undefined` Query result skipped).
17+
Supports optional `path` parameter to set the value to the nested property of the field.
18+
19+
* `set-if-blank` - set the Query result as the field's value only if the current value is blank (`undefined` Query result skipped).
20+
Supports optional `path` parameter to set the value to the nested property of the field.
21+
1622
* `readonly` - set the readonly marker of the field from the Query result.
23+
1724
* `required` - set the required marker of the field from the Query result.
25+
1826
* `validate` - set the validation state of the field from the Query result.
27+
1928
* `disabled` - set the field's disabled state from the Query result. Supports multiple actions effect for the same field.
2029

2130
### Async actions

ui.apps/src/main/content/jcr_root/apps/etoolbox-authoring-kit/depends-on/js.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ js/accessors/dependsOnCoralFieldsetAccessor.js
3030

3131
# Actions
3232
js/actions/dependsOnCoralBasicActions.js
33+
js/actions/dependsOnCoralSetActions.js
3334
js/actions/dependsOnCoralValidateAction.js
3435
js/actions/dependsOnFetchAction.js
3536
js/actions/dependsOnCoralTabAction.js

ui.apps/src/main/content/jcr_root/apps/etoolbox-authoring-kit/depends-on/js/actions/dependsOnCoralBasicActions.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,4 @@
6161
ns.ActionRegistry.register('disabled', function setDisabled(state) {
6262
ns.ElementAccessors.requestDisable(this.$el, state, this);
6363
});
64-
65-
/**
66-
* Set the field value from the query result, skip undefined query results
67-
* query type: string
68-
* */
69-
ns.ActionRegistry.register('set', function setValue(value) {
70-
if (value !== undefined) {
71-
ns.ElementAccessors.setValue(this.$el, value);
72-
}
73-
});
74-
75-
/**
76-
* Set the field value from the query result only if the field value is blank,
77-
* skip undefined query results
78-
* query type: string
79-
* */
80-
ns.ActionRegistry.register('set-if-blank', function setValueIfBlank(value) {
81-
const current = ns.ElementAccessors.getValue(this.$el);
82-
if ((current === '' || current === null || current === undefined) && value !== undefined) {
83-
ns.ElementAccessors.setValue(this.$el, value);
84-
}
85-
});
8664
})(Granite.$, Granite.DependsOnPlugin = (Granite.DependsOnPlugin || {}));
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License").
3+
* You may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
/**
16+
* @author Alexey Stsefanovich (ala'n), Yana Bernatskaya (YanaBr)
17+
*
18+
* DependsOn Coral 3 Basic Actions.
19+
*
20+
* Defined actions:
21+
* - visibility - set the field visibility (and also hide the form field wrapper);
22+
* - required - set the required state of the field;
23+
* - readonly - set the readonly state of the field;
24+
* - disabled - set the disabled state of the field;
25+
* - set - set the field value from the query result;
26+
* - set-if-blank - set the field value from the query result only if the field value is blank
27+
* */
28+
(function ($, ns) {
29+
'use strict';
30+
31+
const FORM_FIELDS_SELECTOR = 'input, textarea, select';
32+
const CORAL_FIELD_SELECTOR = '.coral-Form-field';
33+
34+
/**
35+
* Creates CSS selector to find by name with support of simple wildcards
36+
* @param path
37+
*/
38+
function createSearchQuery(path) {
39+
if (path.endsWith('*')) return `[name^="${path.slice(0, -1)}"]`;
40+
if (path.startsWith('*')) return `[name$="${path.slice(1)}"]`;
41+
return `[name="${path}"]`;
42+
}
43+
44+
/**
45+
* Find form field by a path. If the path is not provided, returns $root.
46+
* Supports simple wildcard syntax to find fields by name start / end:
47+
* - *name - find fields with name ending with 'name'
48+
* - name* - find fields with name starting with 'name'
49+
* - name - find fields with name 'name'
50+
*
51+
* @param {jQuery} $root - root element to search in
52+
* @param {string} [path] - path to the target field
53+
* */
54+
function resolveTarget($root, path) {
55+
if (!path) return $root;
56+
const $fields = $root.find(FORM_FIELDS_SELECTOR).filter(createSearchQuery(path));
57+
// Resolves input field to coral form field
58+
return $fields.closest(CORAL_FIELD_SELECTOR);
59+
}
60+
61+
/**
62+
* Set the field value from the query result, skip undefined query results
63+
* query type: string
64+
* optional parameters:
65+
* - path: string - path to the target field (supports wildcards)
66+
* */
67+
ns.ActionRegistry.register('set', function setValue(value, { path }) {
68+
if (value === undefined) return;
69+
const $target = resolveTarget(this.$el, path);
70+
ns.ElementAccessors.setValue($target, value);
71+
});
72+
73+
/**
74+
* Set the field value from the query result only if the field value is blank,
75+
* skip undefined query results
76+
* query type: string
77+
* optional parameters:
78+
* - path: string - path to the target field (supports wildcards)
79+
* */
80+
ns.ActionRegistry.register('set-if-blank', function setValueIfBlank(value, { path }) {
81+
if (value === undefined) return;
82+
const $target = resolveTarget(this.$el, path);
83+
const current = ns.ElementAccessors.getValue($target);
84+
if (current === '' || current === null || current === undefined) {
85+
ns.ElementAccessors.setValue($target, value);
86+
}
87+
});
88+
})(Granite.$, Granite.DependsOnPlugin = (Granite.DependsOnPlugin || {}));

0 commit comments

Comments
 (0)