File : data/Note.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
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15
*/
16
/*
17
Changes:
18
    - v4.0.0:
19
        - created from v3.6.0
20
Doc reviewed 202208
21
 */
22
23
import ObjId from '../data/ObjId.js';
24
import ObjType from '../data/ObjType.js';
25
import { LAT_LNG, DISTANCE, ZERO, ONE, INVALID_OBJ_ID, ICON_DIMENSIONS } from '../main/Constants.js';
26
import TravelObject from '../data/TravelObject.js';
27
import theHTMLSanitizer from '../core/htmlSanitizer/HTMLSanitizer.js';
28
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
/**
31
This class represent a note
32
*/
33
/* ------------------------------------------------------------------------------------------------------------------------- */
34
35
class Note extends TravelObject {
36
37
    /**
38
    The object type for notes
39
    @type {ObjType}
40
    */
41
42
    static #objType = new ObjType (
43
        'Note',
44
        [
45
            'iconHeight',
46
            'iconWidth',
47
            'iconContent',
48
            'popupContent',
49
            'tooltipContent',
50
            'phone',
51
            'url',
52
            'address',
53
            'iconLat',
54
            'iconLng',
55
            'lat',
56
            'lng',
57
            'distance',
58
            'chainedDistance',
59
            'objId'
60
        ]
61
    );
62
63
    /**
64
    the height of the icon associated to the note
65
    @type {Number}
66
    */
67
68
    #iconHeight = ICON_DIMENSIONS.height;
69
70
    /**
71
    the width of the icon associated to the note
72
    @type {Number}
73
    */
74
75
    #iconWidth = ICON_DIMENSIONS.width;
76
77
    /**
78
    the html needed to display the icon
79
    @type {String}
80
    */
81
82
    #iconContent = '';
83
84
    /**
85
    the html added to the icon popup
86
    @type {String}
87
    */
88
89
    #popupContent = '';
90
91
    /**
92
    the html added to the icon tooltip
93
    @type {String}
94
    */
95
96
    #tooltipContent = '';
97
98
    /**
99
    the phone number dsplayed in the Note
100
    @type {String}
101
    */
102
103
    #phone = '';
104
105
    /**
106
    the url dsplayed in the Note
107
    @type {String}
108
    */
109
110
    #url = '';
111
112
    /**
113
    the address dsplayed in the Note
114
    @type {String}
115
    */
116
117
    #address = '';
118
119
    /**
120
    the latitude of the Note icon
121
    @type {Number}
122
    */
123
124
    #iconLat = LAT_LNG.defaultValue;
125
126
    /**
127
    the longitude of the Note icon
128
    @type {Number}
129
    */
130
131
    #iconLng = LAT_LNG.defaultValue;
132
133
    /**
134
    the latitude of the Note
135
    @type {Number}
136
    */
137
138
    #lat = LAT_LNG.defaultValue;
139
140
    /**
141
    the longitude of the Note
142
    @type {Number}
143
    */
144
145
    #lng = LAT_LNG.defaultValue;
146
147
    /**
148
    the distance between the beginning of the Route and the Note
149
    @default DISTANCE.invalid
150
    @type {Number}
151
    */
152
153
    #distance = DISTANCE.invalid;
154
155
    /**
156
    the distance between the beginning of the Travel and the Note
157
    @default DISTANCE.defaultValue
158
    @type {Number}
159
    */
160
161
    #chainedDistance = DISTANCE.defaultValue;
162
163
    /**
164
    the objId of the note
165
    @type {Number}
166
    */
167
168
    #objId = INVALID_OBJ_ID;
169
170
    /**
171
    The constructor
172
    */
173
174
    constructor ( ) {
175
        super ( );
176
        this.#objId = ObjId.nextObjId;
177
    }
178
179
    /**
180
    the height of the icon associated to the note
181
    @type {Number}
182
    */
183
184
    get iconHeight ( ) { return this.#iconHeight; }
185
186
    set iconHeight ( iconHeight ) {
187
        this.#iconHeight = 'number' === typeof ( iconHeight ) ? iconHeight : ICON_DIMENSIONS.height;
188
    }
189
190
    /**
191
    the width of the icon associated to the note
192
    @type {Number}
193
    */
194
195
    get iconWidth ( ) { return this.#iconWidth; }
196
197
    set iconWidth ( iconWidth ) {
198
        this.#iconWidth = 'number' === typeof ( iconWidth ) ? iconWidth : ICON_DIMENSIONS.width;
199
    }
200
201
    /**
202
    the html needed to display the icon
203
    @type {String}
204
    */
205
206
    get iconContent ( ) { return this.#iconContent; }
207
208
    set iconContent ( iconContent ) {
209
        this.#iconContent =
210
            'string' === typeof ( iconContent )
211
                ?
212
                theHTMLSanitizer.sanitizeToHtmlString ( iconContent ).htmlString
213
                :
214
                '';
215
    }
216
217
    /**
218
    the html added to the icon popup
219
    @type {String}
220
    */
221
222
    get popupContent ( ) { return this.#popupContent; }
223
224
    set popupContent ( popupContent ) {
225
        this.#popupContent =
226
            'string' === typeof ( popupContent )
227
                ?
228
                theHTMLSanitizer.sanitizeToHtmlString ( popupContent ).htmlString
229
                :
230
                '';
231
    }
232
233
    /**
234
    the html added to the icon tooltip
235
    @type {String}
236
    */
237
238
    get tooltipContent ( ) { return this.#tooltipContent; }
239
240
    set tooltipContent ( tooltipContent ) {
241
        this.#tooltipContent =
242
            'string' === typeof ( tooltipContent )
243
                ?
244
                theHTMLSanitizer.sanitizeToHtmlString ( tooltipContent ).htmlString
245
                :
246
                '';
247
    }
248
249
    /**
250
    the phone number dsplayed in the Note
251
    @type {String}
252
    */
253
254
    get phone ( ) { return this.#phone; }
255
256
    set phone ( phone ) {
257
        this.#phone =
258
            'string' === typeof ( phone )
259
                ?
260
                theHTMLSanitizer.sanitizeToHtmlString ( phone ).htmlString
261
                :
262
                '';
263
    }
264
265
    /**
266
    the url dsplayed in the Note
267
    @type {String}
268
    */
269
270
    get url ( ) { return this.#url; }
271
272
    set url ( url ) {
273
        this.#url =
274
            'string' === typeof ( url )
275
                ?
276
                encodeURI ( theHTMLSanitizer.sanitizeToUrl ( url ).url )
277
                :
278
                '';
279
    }
280
281
    /**
282
    the address dsplayed in the Note
283
    @type {String}
284
    */
285
286
    get address ( ) { return this.#address; }
287
288
    set address ( address ) {
289
        this.#address =
290
            'string' === typeof ( address )
291
                ?
292
                theHTMLSanitizer.sanitizeToHtmlString ( address ).htmlString
293
                :
294
                '';
295
    }
296
297
    /**
298
    the latitude of the Note icon
299
    @type {Number}
300
    */
301
302
    get iconLat ( ) { return this.#iconLat; }
303
304
    set iconLat ( iconLat ) {
305
        this.#iconLat = 'number' === typeof ( iconLat ) ? iconLat : LAT_LNG.defaultValue;
306
    }
307
308
    /**
309
    the longitude of the Note icon
310
    @type {Number}
311
    */
312
313
    get iconLng ( ) { return this.#iconLng; }
314
315
    set iconLng ( iconLng ) {
316
        this.#iconLng = 'number' === typeof ( iconLng ) ? iconLng : LAT_LNG.defaultValue;
317
    }
318
319
    /**
320
    the latitude of the Note
321
    @type {Number}
322
    */
323
324
    get lat ( ) { return this.#lat; }
325
326
    set lat ( lat ) {
327
        this.#lat = 'number' === typeof ( lat ) ? lat : LAT_LNG.defaultValue;
328
    }
329
330
    /**
331
    the longitude of the Note
332
    @type {Number}
333
    */
334
335
    get lng ( ) { return this.#lng; }
336
337
    set lng ( lng ) {
338
        this.#lng = 'number' === typeof ( lng ) ? lng : LAT_LNG.defaultValue;
339
    }
340
341
    /**
342
    the distance between the beginning of the Route and the Note
343
    @default DISTANCE.invalid
344
    @type {Number}
345
    */
346
347
    get distance ( ) { return this.#distance; }
348
349
    set distance ( distance ) {
350
        this.#distance = 'number' === typeof ( distance ) ? distance : DISTANCE.invalid;
351
    }
352
353
    /**
354
    the distance between the beginning of the Travel and the Note
355
    @default DISTANCE.defaultValue
356
    @type {Number}
357
    */
358
359
    get chainedDistance ( ) { return this.#chainedDistance; }
360
361
    set chainedDistance ( chainedDistance ) {
362
        this.#chainedDistance = 'number' === typeof ( chainedDistance ) ? chainedDistance : DISTANCE.defaultValue;
363
    }
364
365
    /**
366
    is true when the note is linked with a route
367
    @type {Boolean}
368
    */
369
370
    get isRouteNote ( ) { return this.#distance !== DISTANCE.invalid; }
371
372
    /**
373
    the latitude and longitude of the Note icon
374
    @type {Array.<Number>}
375
    */
376
377
    get iconLatLng ( ) { return [ this.iconLat, this.iconLng ]; }
378
379
    set iconLatLng ( iconLatLng ) {
380
        if (
381
            'number' === typeof ( iconLatLng [ ZERO ] )
382
            &&
383
            'number' === typeof ( iconLatLng [ ONE ] )
384
        ) {
385
            this.#iconLat = iconLatLng [ ZERO ];
386
            this.#iconLng = iconLatLng [ ONE ];
387
        }
388
        else {
389
            this.#iconLat = LAT_LNG.defaultValue;
390
            this.#iconLng = LAT_LNG.defaultValue;
391
        }
392
    }
393
394
    /**
395
    the latitude and longitude of the Note
396
    @type {Array.<Number>}
397
    */
398
399
    get latLng ( ) { return [ this.lat, this.lng ]; }
400
401
    set latLng ( latLng ) {
402
        if (
403
            'number' === typeof ( latLng [ ZERO ] )
404
            &&
405
            'number' === typeof ( latLng [ ONE ] )
406
        ) {
407
            this.#lat = latLng [ ZERO ];
408
            this.#lng = latLng [ ONE ];
409
        }
410
        else {
411
            this.#lat = LAT_LNG.defaultValue;
412
            this.#lng = LAT_LNG.defaultValue;
413
        }
414
    }
415
416
    /**
417
    the ObjType of the Note.
418
    @type {ObjType}
419
    */
420
421
    get objType ( ) { return Note.#objType; }
422
423
    /**
424
    the objId of the Note. objId are unique identifier given by the code
425
    @type {Number}
426
    */
427
428
    get objId ( ) { return this.#objId; }
429
430
    /**
431
    An object literal with the Note properties and without any methods.
432
    This object can be used with the JSON object
433
    @type {JsonObject}
434
    */
435
436
    get jsonObject ( ) {
437
        return {
438
            iconHeight : this.iconHeight,
439
            iconWidth : this.iconWidth,
440
            iconContent : this.iconContent,
441
            popupContent : this.popupContent,
442
            tooltipContent : this.tooltipContent,
443
            phone : this.phone,
444
            url : this.url,
445
            address : this.address,
446
            iconLat : parseFloat ( this.iconLat.toFixed ( LAT_LNG.fixed ) ),
447
            iconLng : parseFloat ( this.iconLng.toFixed ( LAT_LNG.fixed ) ),
448
            lat : parseFloat ( this.lat.toFixed ( LAT_LNG.fixed ) ),
449
            lng : parseFloat ( this.lng.toFixed ( LAT_LNG.fixed ) ),
450
            distance : parseFloat ( this.distance.toFixed ( DISTANCE.fixed ) ),
451
            chainedDistance : parseFloat ( this.chainedDistance.toFixed ( DISTANCE.fixed ) ),
452
            objId : this.#objId,
453
            objType : this.objType.jsonObject
454
        };
455
    }
456
    set jsonObject ( something ) {
457
        const otherthing = this.validateObject ( something );
458
        this.iconHeight = otherthing.iconHeight;
459
        this.iconWidth = otherthing.iconWidth;
460
        this.iconContent = otherthing.iconContent;
461
        this.popupContent = otherthing.popupContent;
462
        this.tooltipContent = otherthing.tooltipContent;
463
        this.phone = otherthing.phone;
464
        this.url = otherthing.url;
465
        this.address = otherthing.address;
466
        this.iconLat = otherthing.iconLat;
467
        this.iconLng = otherthing.iconLng;
468
        this.lat = otherthing.lat;
469
        this.lng = otherthing.lng;
470
        this.distance = otherthing.distance;
471
        this.chainedDistance = otherthing.chainedDistance;
472
        this.#objId = ObjId.nextObjId;
473
    }
474
}
475
476
export default Note;
477
478
/* --- End of file --------------------------------------------------------------------------------------------------------- */
479