| 1 | /* |
| 2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
| 3 | |
| 4 | This program is free software; |
| 5 | you can redistribute it and/or modify it under the terms of the |
| 6 | GNU General Public License as published by the Free Software Foundation; |
| 7 | either version 3 of the License, or any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | Changes: |
| 21 | - v4.0.0: |
| 22 | - created |
| 23 | Doc reviewed 20220828 |
| 24 | Tests ... |
| 25 | */ |
| 26 | |
| 27 | import BaseControl from '../baseControl/BaseControl.js'; |
| 28 | import theHTMLElementsFactory from '../../core/uiLib/HTMLElementsFactory.js'; |
| 29 | |
| 30 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 31 | /** |
| 32 | This class is a generic control with a select element |
| 33 | */ |
| 34 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 35 | |
| 36 | class SelectControl extends BaseControl { |
| 37 | |
| 38 | /** |
| 39 | The select HTMLElement |
| 40 | @type {HTMLElement} |
| 41 | */ |
| 42 | |
| 43 | #selectHTMLElement; |
| 44 | |
| 45 | /** |
| 46 | The constructor |
| 47 | @param {Object} options An object with the options ( beforeText, value, min, max, datasetName, afterText ) |
| 48 | */ |
| 49 | |
| 50 | constructor ( options ) { |
| 51 | super ( ); |
| 52 | theHTMLElementsFactory.create ( |
| 53 | 'text', |
| 54 | { |
| 55 | value : options?.beforeText || '' |
| 56 | }, |
| 57 | this.controlHTMLElement |
| 58 | ); |
| 59 | this.#selectHTMLElement = theHTMLElementsFactory.create ( 'select', null, this.controlHTMLElement ); |
| 60 | options?.elements.forEach ( |
| 61 | element => { |
| 62 | this.#selectHTMLElement.add ( |
| 63 | theHTMLElementsFactory.create ( 'option', { text : element } ) |
| 64 | ); |
| 65 | } |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | The index of the selected item in the control. |
| 71 | @type {Number} |
| 72 | */ |
| 73 | |
| 74 | get selectedIndex ( ) { return this.#selectHTMLElement.selectedIndex; } |
| 75 | |
| 76 | set selectedIndex ( selectedIndex ) { this.#selectHTMLElement.selectedIndex = selectedIndex; } |
| 77 | } |
| 78 | |
| 79 | export default SelectControl; |
| 80 | |
| 81 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
| 82 |