|
| 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