File : dialogs/routePropertiesDialog/RoutePropertiesDialog.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 ModalBaseDialog from '../baseDialog/ModalBaseDialog.js';
26
import theTranslator from '../../core/uiLib/Translator.js';
27
import CheckboxInputControl from '../../controls/checkboxInputControl/CheckboxInputControl.js';
28
import ColorControl from '../../controls/colorControl/ColorControl.js';
29
import NumberInputControl from '../../controls/numberInputControl/NumberInputControl.js';
30
import TextInputControl from '../../controls/textInputControl/TextInputControl.js';
31
import SelectControl from '../../controls/selectControl/SelectControl.js';
32
import theConfig from '../../data/Config.js';
33
import { ZERO } from '../../main/Constants.js';
34
35
/* ------------------------------------------------------------------------------------------------------------------------- */
36
/**
37
This class is the route properties dialog
38
*/
39
/* ------------------------------------------------------------------------------------------------------------------------- */
40
41
class RoutePropertiesDialog extends ModalBaseDialog {
42
43
    /**
44
    A reference to the route
45
    @type {Route}
46
    */
47
48
    #route;
49
50
    /**
51
    The colorControl object used in the dialog
52
    @type {ColorControl}
53
    */
54
55
    #colorControl;
56
57
    /**
58
    The route name control
59
    @type {TextInputControl}
60
    */
61
62
    #routeNameControl;
63
64
    /**
65
    The route width control in the dialog
66
    @type {NumberInputControl}
67
    */
68
69
    #routeWidthControl;
70
71
    /**
72
    The route dash control in the dialog
73
    @type {SelectControl}
74
    */
75
76
    #dashSelectControl;
77
78
    /**
79
    The chained route check box control in the dialog
80
    @type {CheckboxInputControl}
81
    */
82
83
    #chainedRouteControl;
84
85
    /**
86
    The minimal width for a Route polyline
87
    @type {Number}
88
    */
89
90
    // eslint-disable-next-line no-magic-numbers
91
    static get #ROUTE_MIN_WIDTH ( ) { return 1; }
92
93
    /**
94
    The maximal width for a Route polyline
95
    @type {Number}
96
    */
97
98
    // eslint-disable-next-line no-magic-numbers
99
    static get #ROUTE_MAX_WIDTH ( ) { return 40; }
100
101
    /**
102
    This method creates the color header div
103
    */
104
105
    /**
106
    The constructor
107
    @param {Route} route The route for witch the properties are edited
108
    */
109
110
    constructor ( route ) {
111
        super ( );
112
        this.#route = route;
113
    }
114
115
    /**
116
    Create all the controls needed for the dialog.
117
    Overload of the base class createContentHTML
118
    */
119
120
    createContentHTML ( ) {
121
        this.#routeNameControl = new TextInputControl (
122
            {
123
                headerText : theTranslator.getText ( 'RoutePropertiesDialog - Name' ),
124
                value : this.#route.computedName
125
            }
126
        );
127
        this.#routeWidthControl = new NumberInputControl (
128
            {
129
                beforeText : theTranslator.getText ( 'RoutePropertiesDialog - Width' ),
130
                value : this.#route.width,
131
                min : RoutePropertiesDialog.#ROUTE_MIN_WIDTH,
132
                max : RoutePropertiesDialog.#ROUTE_MAX_WIDTH
133
            }
134
        );
135
        this.#dashSelectControl = new SelectControl (
136
            {
137
                beforeText : theTranslator.getText ( 'RoutePropertiesDialog - Linetype' ),
138
                elements : Array.from ( theConfig.route.dashChoices, dashChoice => dashChoice.text )
139
            }
140
        );
141
        this.#dashSelectControl.selectedIndex =
142
            this.#route.dashIndex < theConfig.route.dashChoices.length
143
                ?
144
                this.#route.dashIndex
145
                :
146
                ZERO;
147
        this.#chainedRouteControl = new CheckboxInputControl (
148
            {
149
                beforeText : theTranslator.getText ( 'RoutePropertiesDialog - Chained route' ),
150
                checked : this.#route.chain
151
            }
152
        );
153
        this.#colorControl = new ColorControl (
154
            {
155
                cssColor : this.#route.color,
156
                headerText : theTranslator.getText ( 'RoutePropertiesDialog - Color' )
157
            }
158
        );
159
    }
160
161
    /**
162
    Overload of the ModalBaseDialog.show ( ) method.
163
    */
164
165
    show ( ) {
166
        let showPromise = super.show ( );
167
        this.addCssClass ( 'TravelNotes-RoutePropertiesDialog' );
168
        return showPromise;
169
    }
170
171
    /**
172
    Overload of the BaseDialog.onCancel ( ) method.
173
    */
174
175
    onCancel ( ) {
176
        this.#colorControl.destructor ( );
177
        super.onCancel ( );
178
    }
179
180
    /**
181
    Overload of the BaseDialog.onOk ( ) method. Called when the Ok button is clicked.
182
    Push the new route properties in the route and validate the route
183
    */
184
185
    onOk ( ) {
186
187
        this.#route.color = this.#colorControl.cssColor;
188
        if ( this.#route.computedName !== this.#routeNameControl.value ) {
189
            this.#route.name = this.#routeNameControl.value;
190
        }
191
        this.#route.width = this.#routeWidthControl.value;
192
        this.#route.chain = this.#chainedRouteControl.checked;
193
        this.#route.dashIndex = this.#dashSelectControl.selectedIndex;
194
        this.#colorControl.destructor ( );
195
196
        super.onOk ( );
197
    }
198
199
    /**
200
    Get the title of the dialog
201
    @type {String}
202
    */
203
204
    get title ( ) { return theTranslator.getText ( 'RoutePropertiesDialog - Route properties' ); }
205
206
    /**
207
    Overload of the BaseDialog.contentHTMLElements property.
208
    Get an array with the HTMLElements that have to be added in the content of the dialog.
209
    @type {Array.<HTMLElement>}
210
    */
211
212
    get contentHTMLElements ( ) {
213
        return [
214
            this.#routeNameControl.controlHTMLElement,
215
            this.#routeWidthControl.controlHTMLElement,
216
            this.#dashSelectControl.controlHTMLElement,
217
            this.#chainedRouteControl.controlHTMLElement,
218
            this.#colorControl.controlHTMLElement
219
        ];
220
    }
221
}
222
223
export default RoutePropertiesDialog;
224
225
/* --- End of file --------------------------------------------------------------------------------------------------------- */
226