|
| 1 | +import {PolymerElement, html} from '@polymer/polymer/polymer-element.js'; |
| 2 | +import {Debouncer} from '@polymer/polymer/lib/utils/debounce.js'; |
| 3 | +import {CommonFunctionality} from './mixins/common-mixin.js'; |
| 4 | +import {MissingOptions} from './mixins/missing-options-mixin.js'; |
| 5 | +import {timeOut} from '@polymer/polymer/lib/utils/async.js'; |
| 6 | +import '@polymer/iron-icons/iron-icons.js'; |
| 7 | +import '@polymer/iron-dropdown/iron-dropdown.js'; |
| 8 | +import '@polymer/neon-animation/neon-animations.js'; |
| 9 | +import '@polymer/paper-input/paper-input.js'; |
| 10 | +import '@polymer/paper-styles/element-styles/paper-material-styles.js'; |
| 11 | +import EtoolsLogsMixin from '@unicef-polymer/etools-behaviors/etools-logs-mixin.js'; |
| 12 | +import '@unicef-polymer/etools-ajax/etools-ajax.js'; |
| 13 | +import './scripts/es6-polyfills.js'; |
| 14 | +import './elements/searchbox-input.js'; |
| 15 | +import './elements/options-list.js'; |
| 16 | +import './styles/esmm-shared-styles.js'; |
| 17 | + |
| 18 | +/** |
| 19 | + * @polymer |
| 20 | + * @mixinFunction |
| 21 | + * @appliesMixin EsmmMixins.MissingOptions |
| 22 | + * @appliesMixin EsmmMixins.CommonFunctionality |
| 23 | + * @appliesMixin EtoolsLogsMixin |
| 24 | + */ |
| 25 | +const DropdownRequiredMixins = MissingOptions(CommonFunctionality( |
| 26 | + EtoolsLogsMixin(PolymerElement))); |
| 27 | + |
| 28 | +/** |
| 29 | + * `etools-dropdown` |
| 30 | + * |
| 31 | + * @customElement |
| 32 | + * @polymer |
| 33 | + * @appliesMixin DropdownRequiredMixins |
| 34 | + * @demo demo/index.html |
| 35 | + */ |
| 36 | +export class EtoolsDropdown extends DropdownRequiredMixins { |
| 37 | + static get template() { |
| 38 | + // language=HTML |
| 39 | + return html` |
| 40 | + <style include="paper-material-styles esmm-shared-styles"> |
| 41 | +
|
| 42 | + :host { |
| 43 | + --paper-input-container-input: { |
| 44 | + cursor: var(--esmm-select-cursor); |
| 45 | + } |
| 46 | + } |
| 47 | +
|
| 48 | + #main { |
| 49 | + width: 100%; |
| 50 | + } |
| 51 | +
|
| 52 | + #main iron-icon { |
| 53 | + @apply --esmm-icons; |
| 54 | + } |
| 55 | +
|
| 56 | + </style> |
| 57 | +
|
| 58 | + <etools-ajax id="missingOptionsAjax" params="[[ajaxParams]]" on-success="handleMissingOptionsReqResponse" |
| 59 | + on-fail="handleMissingOptionsReqError"></etools-ajax> |
| 60 | +
|
| 61 | + <paper-input id="main" label="[[label]]" placeholder="[[placeholder]]" always-float-label="[[alwaysFloatLabel]]" |
| 62 | + no-label-float="[[noLabelFloat]]" |
| 63 | + value="[[getLabel(selectedItem)]]" disabled="[[disabled]]" |
| 64 | + invalid="[[invalid]]" error-message="[[_getErrorMessage(errorMessage, invalid)]]" readonly |
| 65 | + on-focus="onInputFocus" on-tap="_openMenu"> |
| 66 | + <iron-icon icon="arrow-drop-down" slot="suffix" hidden\$="[[readonly]]"></iron-icon> |
| 67 | + </paper-input> |
| 68 | +
|
| 69 | + <iron-dropdown id="dropdownMenu" horizontal-align="[[horizontalAlign]]" vertical-offset="[[verticalOffset]]" |
| 70 | + dynamic-align="[[!noDynamicAlign]]" on-iron-overlay-opened="_onDropdownOpen" |
| 71 | + on-iron-overlay-closed="_onDropdownClose" disabled="[[_menuBtnIsDisabled(disabled, readonly)]]" |
| 72 | + no-cancel-on-outside-click allow-click-through |
| 73 | + with-backdrop="[[withBackdrop]]"> |
| 74 | +
|
| 75 | + <div id="ironDrContent" class="paper-material" elevation="1" slot="dropdown-content"> |
| 76 | + <esmm-searchbox-input id="searchbox" search="{{search}}" hidden\$="{{hideSearch}}"></esmm-searchbox-input> |
| 77 | +
|
| 78 | + <esmm-options-list id="optionsList" shown-options="[[shownOptions]]" selected="{{selected}}" |
| 79 | + two-lines-label="[[twoLinesLabel]]" option-value="[[optionValue]]" |
| 80 | + option-label="[[optionLabel]]" |
| 81 | + show-no-search-results-warning="[[showNoSearchResultsWarning]]" |
| 82 | + show-limit-warning="[[showLimitWarning]]" shown-options-limit="[[shownOptionsLimit]]" |
| 83 | + no-options-available="[[noOptionsAvailable]]" none-option-label="[[noneOptionLabel]]" |
| 84 | + capitalize="[[capitalize]]" on-close-etools-dropdown="_closeMenu"></esmm-options-list> |
| 85 | + </div> |
| 86 | +
|
| 87 | + </iron-dropdown> |
| 88 | + `; |
| 89 | + } |
| 90 | + |
| 91 | + static get is() { |
| 92 | + return 'etools-dropdown'; |
| 93 | + } |
| 94 | + |
| 95 | + static get properties() { |
| 96 | + return { |
| 97 | + /** Dropdown selected value `optionValue` prop of the selected option */ |
| 98 | + selected: { |
| 99 | + type: Number, |
| 100 | + notify: true, |
| 101 | + observer: '_selectedChanged' |
| 102 | + }, |
| 103 | + /** Selected option object */ |
| 104 | + selectedItem: { |
| 105 | + type: Object, |
| 106 | + value: null, |
| 107 | + notify: true |
| 108 | + }, |
| 109 | + /** Selected value not found in options */ |
| 110 | + notFoundOption: { |
| 111 | + type: String |
| 112 | + }, |
| 113 | + /** Element title attribute */ |
| 114 | + title: { |
| 115 | + type: String, |
| 116 | + reflectToAttribute: true, |
| 117 | + computed: 'getLabel(selectedItem)' |
| 118 | + }, |
| 119 | + /* withBackdrop property was added in order to trap the focus within the light DOM of the iron-dropdown. |
| 120 | + Setting this to true solves a bug in PRP where when you have the etools-dropdown in a paper-dialog, |
| 121 | + and you click on the opened drodpdown's scroll, the dropdown closes. |
| 122 | + **/ |
| 123 | + withBackdrop: { |
| 124 | + type: Boolean, |
| 125 | + reflectToAttribute: true, |
| 126 | + value: false |
| 127 | + } |
| 128 | + }; |
| 129 | + } |
| 130 | + |
| 131 | + static get observers() { |
| 132 | + return [ |
| 133 | + '_selectedAndOptionsChanged(selected, options)', |
| 134 | + '_notFoundOptionAndUrlChanged(notFoundOption, url)' |
| 135 | + ]; |
| 136 | + } |
| 137 | + |
| 138 | + _selectedAndOptionsChanged(selected, options) { |
| 139 | + this._setSelectedItem(); |
| 140 | + if (!this.triggerValueChangeEvent) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + this._debouncer = Debouncer.debounce( |
| 145 | + this._debouncer, |
| 146 | + timeOut.after(20), |
| 147 | + () => { |
| 148 | + this._fireChangeEvent(); |
| 149 | + } |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + _setSelectedItem(selected, selectedItem) { |
| 154 | + this.set('notFoundOption', null); |
| 155 | + if (selectedItem) { |
| 156 | + this.set('selectedItem', selectedItem); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + selected = selected || this.selected; |
| 161 | + |
| 162 | + if (!selected && this._noOptions()) { |
| 163 | + this.set('selectedItem', null); |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + if (selected && this._noOptions()) { |
| 168 | + this.set('notFoundOption', this.selected); |
| 169 | + this.set('selectedItem', null); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + selectedItem = this._getItemFromOptions(selected); |
| 174 | + if (!selectedItem) { |
| 175 | + this.set('notFoundOption', this.selected); |
| 176 | + this.set('selectedItem', null); |
| 177 | + } else { |
| 178 | + this.set('selectedItem', selectedItem); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + _getItemFromOptions(value) { |
| 183 | + if (this._noOptions()) { |
| 184 | + return null; |
| 185 | + } |
| 186 | + return this.options.find(item => item[this.optionValue] == value); |
| 187 | + } |
| 188 | + |
| 189 | + _notFoundOptionAndUrlChanged(notFoundOption, url) { |
| 190 | + if (url && notFoundOption) { |
| 191 | + this._handleSelectedNotFoundInOptions(this.notFoundOption); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + _handleSelectedNotFoundInOptions(selected) { |
| 196 | + this.requestMissingOptions([selected]); |
| 197 | + // show warning |
| 198 | + let warnMsg = 'Selected value '; |
| 199 | + warnMsg += selected + ' not found in dropdown\'s options!'; |
| 200 | + this.logWarn(warnMsg, 'etools-esmm ' + this.label, null, true); |
| 201 | + } |
| 202 | + |
| 203 | + _onDropdownClose() { |
| 204 | + super._onDropdownClose(); |
| 205 | + if (this.autoValidate) { |
| 206 | + this.validate(this.selected); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Validate dropdown selection |
| 212 | + * @param selected |
| 213 | + * @returns {boolean} |
| 214 | + */ |
| 215 | + validate(selected) { |
| 216 | + if (!this.hasAttribute('required') || this.readonly) { |
| 217 | + this.set('invalid', false); |
| 218 | + return true; |
| 219 | + } |
| 220 | + selected = selected || this.selected; |
| 221 | + let valid = true; |
| 222 | + if (!selected) { |
| 223 | + if (parseInt(selected) !== 0) { |
| 224 | + valid = false; |
| 225 | + } |
| 226 | + } |
| 227 | + this.set('invalid', !valid); |
| 228 | + return valid; |
| 229 | + } |
| 230 | + |
| 231 | + _selectedChanged(selected) { |
| 232 | + // elemAttached condition is to prevent eager validation |
| 233 | + if (this.autoValidate && this.elemAttached) { |
| 234 | + this.validate(selected); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + _fireChangeEvent() { |
| 239 | + this.dispatchEvent(new CustomEvent('etools-selected-item-changed', { |
| 240 | + detail: {selectedItem: this.selectedItem}, |
| 241 | + bubbles: true, |
| 242 | + composed: true |
| 243 | + })); |
| 244 | + } |
| 245 | +} |
| 246 | + |
0 commit comments