File : dialogs/notesDialog/NoteDialog.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 NoteDialogToolbar from './toolbar/NoteDialogToolbar.js';
27
28
import MapIconData from './toolbar/MapIconData.js';
29
30
import TextAreaControl from '../../controls/textAreaControl/TextAreaControl.js';
31
import TextInputControl from '../../controls/textInputControl/TextInputControl.js';
32
import AddressControl from '../../controls/addressControl/AddressControl.js';
33
import GeoCoderHelper from '../../controls/addressControl/GeoCoderHelper.js';
34
35
import NoteDialogIconDimsControl from './controls/NoteDialogIconDimsControl.js';
36
import NoteDialogLinkControl from './controls/NoteDialogLinkControl.js';
37
import NoteDialogPreviewControl from './controls/NoteDialogPreviewControl.js';
38
39
import NoteDialogEventListeners from './eventListeners/NoteDialogEventListeners.js';
40
41
import theHTMLSanitizer from '../../core/htmlSanitizer/HTMLSanitizer.js';
42
import theTranslator from '../../core/uiLib/Translator.js';
43
import theConfig from '../../data/Config.js';
44
import Note from '../../data/Note.js';
45
46
/* ------------------------------------------------------------------------------------------------------------------------- */
47
/**
48
This class create and manage the NoteDialog
49
*/
50
/* ------------------------------------------------------------------------------------------------------------------------- */
51
52
class NoteDialog extends ModalBaseDialog {
53
54
    /**
55
    A reference to the currently edited note
56
    @type {Note}
57
    */
58
59
    #note;
60
61
    /**
62
    A reference to the route on witch the note is attached
63
    @type {Route}
64
    */
65
66
    #route;
67
68
    /**
69
    A clone of the #note used to store the modifications and display the preview
70
    @type {Note}
71
    */
72
73
    #previewNote;
74
75
    /**
76
    The icon dims control
77
    @type {NoteDialogIconDimsControl}
78
    */
79
80
    #iconDimsControl;
81
82
    /**
83
    The icon control
84
    @type {TextAreaControl}
85
    */
86
87
    #iconControl;
88
89
    /**
90
    The tooltip control
91
    @type {TextInputControl}
92
    */
93
94
    #tooltipControl;
95
96
    /**
97
    The popup control
98
    @type {TextAreaControl}
99
    */
100
101
    #popupControl;
102
103
    /**
104
    The address control
105
    @type {AddressControl}
106
    */
107
108
    #addressControl;
109
110
    /**
111
    The link control
112
    @type {NoteDialogLinkControl}
113
    */
114
115
    #linkControl;
116
117
    /**
118
    The phone control
119
    @type {TextInputControl}
120
    */
121
122
    #phoneControl;
123
124
    /**
125
    The preview control
126
    @type {NoteDialogPreviewControl}
127
    */
128
129
    #previewControl;
130
131
    /**
132
    the toolbar
133
    @type {NoteDialogToolbar}
134
    */
135
136
    #toolbar;
137
138
    /**
139
    The control that have currently the focusControl
140
    @type {HTMLElement}
141
    */
142
143
    #focusControl;
144
145
    /**
146
    Event listeners
147
    @type {NoteDialogEventListeners}
148
    */
149
150
    #eventListeners;
151
152
    /**
153
    Destructor. Remove event listeners before closing the dialog and set event listeners objects
154
    to null, so all references to the dialog are released
155
    */
156
157
    #destructor ( ) {
158
        this.#toolbar.destructor ( this.#eventListeners );
159
        this.#iconDimsControl.destructor ( this.#eventListeners );
160
        this.#iconControl.destructor ( this.#eventListeners );
161
        this.#tooltipControl.destructor ( this.#eventListeners );
162
        this.#popupControl.destructor ( this.#eventListeners );
163
        this.#addressControl.destructor ( this.#eventListeners );
164
        this.#linkControl.destructor ( this.#eventListeners );
165
        this.#phoneControl.destructor ( this.#eventListeners );
166
        this.#eventListeners.destructor ( );
167
    }
168
169
    /**
170
    The constructor
171
    @param {Note} note The edited note
172
    @param {Route} route The route to witch the note is linked
173
    */
174
175
    constructor ( note, route ) {
176
        super ( );
177
178
        this.#focusControl = null;
179
180
        // Saving parameters
181
        this.#note = note;
182
        this.#route = route;
183
184
        // Cloning the note
185
        this.#previewNote = new Note ( );
186
        this.#previewNote.jsonObject = note.jsonObject;
187
188
    }
189
190
    /**
191
    Create all the controls needed for the dialog.
192
    Overload of the base class createContentHTML
193
    */
194
195
    createContentHTML ( ) {
196
        this.#eventListeners = new NoteDialogEventListeners ( this, this.#previewNote.latLng );
197
198
        // creting toolbar and controls
199
        this.#toolbar = new NoteDialogToolbar ( this.#eventListeners );
200
        this.#iconDimsControl = new NoteDialogIconDimsControl ( this.#eventListeners );
201
        this.#iconControl = new TextAreaControl (
202
            {
203
                placeholder : '?????',
204
                datasetName : 'iconContent',
205
                headerText : theTranslator.getText ( 'NoteDialogIconControl - Icon content' )
206
            },
207
            this.#eventListeners
208
        );
209
        this.#tooltipControl = new TextInputControl (
210
            {
211
                headerText : theTranslator.getText ( 'NoteDialogTooltipControl - Tooltip content' ),
212
                datasetName : 'tooltipContent'
213
            },
214
            this.#eventListeners
215
        );
216
        this.#popupControl = new TextAreaControl (
217
            {
218
                rows : theConfig.noteDialog.areaHeight.popupContent,
219
                datasetName : 'popupContent',
220
                headerText : theTranslator.getText ( 'NoteDialogPopupControl - Text' )
221
            },
222
            this.#eventListeners
223
        );
224
        this.#addressControl = new AddressControl (    this.#eventListeners );
225
        this.#linkControl = new NoteDialogLinkControl ( this.#eventListeners, this.#previewNote.latLng );
226
        this.#phoneControl = new TextInputControl (
227
            {
228
                headerText : theTranslator.getText ( 'NoteDialogPhoneControl - Phone' ),
229
                datasetName : 'phone'
230
            },
231
            this.#eventListeners
232
        );
233
        this.#previewControl = new NoteDialogPreviewControl ( this.#previewNote );
234
235
        // copy the notes values into the controls
236
        this.setControlsValues ( this.#previewNote );
237
    }
238
239
    /**
240
    The control that have currently the focus. Used for toolbar buttons
241
    @type {HTMLElement}
242
    */
243
244
    get focusControl ( ) { return this.#focusControl; }
245
246
    set focusControl ( focusControl ) { this.#focusControl = focusControl; }
247
248
    /**
249
    Data needed for the MapIconFromOsmFactory
250
    @type {MapIconData}
251
    */
252
253
    get mapIconData ( ) { return new MapIconData ( this.#previewNote.latLng, this.#route ); }
254
255
    /**
256
    Update the preview of the note. Used by event listeners
257
    @param {Object} noteData An object with properties to copy in the preview note
258
    */
259
260
    updatePreview ( noteData ) {
261
        for ( const property in noteData ) {
262
            this.#previewNote [ property ] = noteData [ property ];
263
        }
264
        this.#previewControl.update ( );
265
    }
266
267
    /**
268
    Set the address of the note. Called by IconSelectorChangeEL
269
    */
270
271
    setAddress ( ) {
272
        if ( '' === this.#note.address ) {
273
            new GeoCoderHelper ( this ).setAddressWithGeoCoder ( this.#previewNote.latLng );
274
        }
275
    }
276
277
    /**
278
    Overload of the BaseDialog.canClose ( ) method. Verify that the url is valid and the iconContent completed
279
    @return {Boolean} true when the url is valid and the iconContent completed
280
    */
281
282
    canClose ( ) {
283
        if ( '' === this.#iconControl.value ) {
284
            this.showError ( theTranslator.getText ( 'Notedialog - The icon content cannot be empty' ) );
285
            return false;
286
        }
287
        if ( '' !== this.#linkControl.url ) {
288
            if ( '' === theHTMLSanitizer.sanitizeToUrl ( this.#linkControl.url ).url ) {
289
                this.showError ( theTranslator.getText ( 'NoteDialog - invalidUrl' ) );
290
                return false;
291
            }
292
        }
293
294
        return true;
295
    }
296
297
    /**
298
    Overload of the BaseDialog.onCancel ( ) method.
299
    */
300
301
    onCancel ( ) {
302
        this.#destructor ( );
303
        super.onCancel ( );
304
    }
305
306
    /**
307
    Overload of the BaseDialog.onOk ( ) method.
308
    */
309
310
    onOk ( ) {
311
        if ( super.onOk ( ) ) {
312
313
            // saving values in the note.
314
            this.#note.iconWidth = this.#iconDimsControl.iconWidth;
315
            this.#note.iconHeight = this.#iconDimsControl.iconHeight;
316
            this.#note.iconContent = this.#iconControl.value;
317
            this.#note.tooltipContent = this.#tooltipControl.value;
318
            this.#note.popupContent = this.#popupControl.value;
319
            this.#note.address = this.#addressControl.address;
320
            this.#note.url = this.#linkControl.url;
321
            this.#note.phone = this.#phoneControl.value;
322
323
            // latLng can change for map icons, so we save the value from the preview note
324
            this.#note.latLng = this.#previewNote.latLng;
325
326
            this.#destructor ( );
327
        }
328
    }
329
330
    /**
331
    The dialog title. Overload of the BaseDialog.title property
332
    @type {String}
333
    */
334
335
    get title ( ) { return theTranslator.getText ( 'NoteDialog - Note' ); }
336
337
    /**
338
    An HTMLElement that have to be added as toolbar for the dialog.
339
    Overload of the BaseDialog.toolbarHTMLElement property
340
    @type {HTMLElement}
341
    */
342
343
    get toolbarHTMLElement ( ) {
344
        return this.#toolbar.rootHTMLElement;
345
    }
346
347
    /**
348
    An array with the HTMLElements that have to be added in the content of the dialog.
349
    Overload of the BaseDialog.contentHTMLElements property
350
    @type {Array.<HTMLElement>}
351
    */
352
353
    get contentHTMLElements ( ) {
354
        return [
355
            this.#iconDimsControl.controlHTMLElement,
356
            this.#iconControl.controlHTMLElement,
357
            this.#tooltipControl.controlHTMLElement,
358
            this.#popupControl.controlHTMLElement,
359
            this.#addressControl.controlHTMLElement,
360
            this.#linkControl.controlHTMLElement,
361
            this.#phoneControl.controlHTMLElement
362
        ];
363
    }
364
365
    /**
366
    An array with the HTMLElements that have to be added in the footer of the dialog
367
    Overload of the BaseDialog.footerHTMLElements property
368
    @type {Array.<HTMLElement>}
369
    */
370
371
    get footerHTMLElements ( ) {
372
        return this.#previewControl.HTMLElements;
373
    }
374
375
    /**
376
    set the control's values
377
    @param {Object} source An object with all the properties to update
378
    */
379
380
    setControlsValues ( source ) {
381
        this.#iconDimsControl.iconHeight = source.iconHeight || this.#iconDimsControl.iconHeight;
382
        this.#iconDimsControl.iconWidth = source.iconWidth || this.#iconDimsControl.iconWidth;
383
        this.#iconControl.value = source.iconContent || this.#iconControl.value;
384
        this.#tooltipControl.value = source.tooltipContent || this.#tooltipControl.value;
385
        this.#popupControl.value = source.popupContent || this.#popupControl.value;
386
        this.#addressControl.address = source.address || this.#addressControl.address;
387
        this.#linkControl.url = source.url || this.#linkControl.url;
388
        this.#phoneControl.value = source.phone || this.#phoneControl.value;
389
    }
390
391
    /**
392
    Update the toolbar after an upload of a config file
393
    */
394
395
    updateToolbar ( ) {
396
        this.#toolbar.update ( this.#eventListeners );
397
    }
398
399
}
400
401
export default NoteDialog;
402
403
/* --- End of file --------------------------------------------------------------------------------------------------------- */
404