File : controls/radioInputControl/RadioInputControl.js

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
Changes:
20
    - v4.0.0:
21
        - created from v3.6.0
22
Doc reviewed 202208
23
 */
24
25
import BaseControl from '../baseControl/BaseControl.js';
26
import theHTMLElementsFactory from '../../core/uiLib/HTMLElementsFactory.js';
27
import { NOT_FOUND, ZERO } from '../../main/Constants.js';
28
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
/**
31
This class is a generic control with radio input elements
32
/* ------------------------------------------------------------------------------------------------------------------------- */
33
34
class RadioInputControl extends BaseControl {
35
36
    /**
37
    A static field used to give a unique id for each button and for each radio group
38
    @type {Number}
39
    */
40
41
    static #objId = ZERO;
42
43
    /**
44
    An array with the radio buttons
45
    @type {Array.<HTMLElement>}
46
    */
47
48
    #buttons;
49
50
    /**
51
    The constructor
52
    @param {Object} options An object with the  options ( placeholder, rows, datasetName, headerText )
53
    */
54
55
    constructor ( options ) {
56
        super ( );
57
        this.#buttons = [];
58
        theHTMLElementsFactory.create (
59
            'div',
60
            {
61
                className : 'TravelNotes-BaseDialog-FlexRow',
62
                textContent : options?.headerText || ''
63
            },
64
            this.controlHTMLElement
65
        );
66
        let controlName = 'controlName' + ( ++ RadioInputControl.#objId );
67
        let value = ZERO;
68
        options.buttons.forEach (
69
            button => {
70
                let buttonId = 'buttonId' + ( ++ RadioInputControl.#objId );
71
                let buttonDiv = theHTMLElementsFactory.create (
72
                    'div',
73
                    {
74
                        className : 'TravelNotes-BaseDialog-FlexRow'
75
                    },
76
                    this.controlHTMLElement
77
                );
78
                this.#buttons.push (
79
                    theHTMLElementsFactory.create (
80
                        'input',
81
                        {
82
                            type : 'radio',
83
                            className : 'TravelNotes-RadioInputControl-Radio',
84
                            checked : button.checked,
85
                            id : buttonId,
86
                            name : controlName,
87
                            value : String ( value ++ ),
88
                            dataset : { Name : options?.datasetName || 'RadioInputControl' }
89
                        },
90
                        buttonDiv
91
                    )
92
                );
93
                theHTMLElementsFactory.create (
94
                    'label',
95
                    {
96
                        htmlFor : buttonId,
97
                        innerText : button.label
98
                    },
99
                    buttonDiv
100
                );
101
            }
102
        );
103
    }
104
105
    /**
106
    The value in the control
107
    @type {String}
108
    */
109
110
    get value ( ) {
111
        let value = NOT_FOUND;
112
        this.#buttons.forEach (
113
            button => { value = button.checked ? Number.parseInt ( button.value ) : value; }
114
        );
115
        return value;
116
    }
117
}
118
119
export default RadioInputControl;
120
121
/* --- End of file --------------------------------------------------------------------------------------------------------- */
122