-
Couldn't load subscription status.
- Fork 13
[EDMTP-281] DependsOn: add fill-select option #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
16245f5
b7e91cf
9af474f
68945f5
06fafa2
0dedeb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * @author Natasha Gorshunova | ||
| * | ||
| * DependsOn Coral 3 Select Action. | ||
| * | ||
| * A simple action to fill select with options with provided values | ||
| * | ||
| * Action callback params: | ||
| * {Array<{value:string, text:string}>} options | ||
| * {string} config.selected - a dependsOn query, whose evaluation result should be a value or an array of selected values | ||
| */ | ||
| (function (Granite, $, DependsOn) { | ||
| 'use strict'; | ||
|
|
||
| const ACTION_NAME = 'fill-select'; | ||
|
|
||
| DependsOn.ActionRegistry.register(ACTION_NAME, function (options, { selected }) { | ||
| if (!options || !options.length) { | ||
| return; | ||
| } | ||
|
|
||
| const $select = this.$el; | ||
| const selectedValue = selected ? DependsOn.QueryProcessor.calculateQuery(selected, $select) : ''; | ||
|
|
||
| DependsOn.SelectUtils.setOptions($select, options, selectedValue); | ||
| setTimeout(() => $select.trigger('change.dependsOn')); | ||
| }); | ||
| })(Granite, Granite.$, Granite.DependsOnPlugin); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /*** | ||
| * Coral 3 Select Utils | ||
| * */ | ||
| (function (Granite, $, DependsOn) { | ||
| 'use strict'; | ||
|
|
||
| class SelectUtils { | ||
| /** | ||
| * Creates a new Granite option | ||
| * | ||
| * @param option - an option object | ||
| * @param {string} option.text - text to display | ||
| * @param {string} option.value - actual value of an option | ||
| * @param selectedValue - value or array of values to trigger 'selected' state of the option | ||
| */ | ||
| static createOption(option, selectedValue) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return { | ||
| value: option.value, | ||
| content: { | ||
| textContent: option.text | ||
| }, | ||
| selected: (!selectedValue && !option.value) || selectedValue === option.value || (Array.isArray(selectedValue) && selectedValue.includes(option.value)) | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Sets a new option set to the Granite Select component | ||
| * | ||
| * @param {JQuery} $select - Select widget to set options for | ||
| * @param {Array} options - new option set, represented by "raw" (non-Granite) entities | ||
| * @param selectedValue - the value to mark as selected in the option set | ||
| */ | ||
| static setOptions($select, options, selectedValue) { | ||
| const itemCollection = $select.get(0).items; | ||
| itemCollection.clear(); | ||
| options.map(option => SelectUtils.createOption(option, selectedValue)).forEach(option => itemCollection.add(option)); | ||
| } | ||
| } | ||
|
|
||
| DependsOn.SelectUtils = SelectUtils; | ||
| })(Granite, Granite.$, Granite.DependsOnPlugin); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A question is:
Are we always going to fill only selects? No raidoGroups/ buttonGroups / autocompletes? May it be, we would want to give a more generic name, kind of "fill-options"?