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 theUtilities from '../core/uiLib/Utilities.js'; |
26 | import theHTMLSanitizer from '../core/htmlSanitizer/HTMLSanitizer.js'; |
27 | import TravelObject from '../data/TravelObject.js'; |
28 | |
29 | import { LAT_LNG, ZERO, ONE, INVALID_OBJ_ID } from '../main/Constants.js'; |
30 | |
31 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
32 | /** |
33 | This class represent a way point |
34 | */ |
35 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
36 | |
37 | class WayPoint extends TravelObject { |
38 | |
39 | /** |
40 | The object type for wayPoints |
41 | @type {ObjType} |
42 | */ |
43 | |
44 | static #objType = new ObjType ( 'WayPoint', [ 'address', 'name', 'lat', 'lng', 'objId' ] ); |
45 | |
46 | /** |
47 | the name of the WayPoint |
48 | @type {String} |
49 | */ |
50 | |
51 | #name = ''; |
52 | |
53 | /** |
54 | the address of the WayPoint |
55 | @type {String} |
56 | */ |
57 | |
58 | #address = ''; |
59 | |
60 | /** |
61 | the latitude of the WayPoint |
62 | @type {Number} |
63 | */ |
64 | |
65 | #lat = LAT_LNG.defaultValue; |
66 | |
67 | /** |
68 | the longitude of the WayPoint |
69 | @type {Number} |
70 | */ |
71 | |
72 | #lng = LAT_LNG.defaultValue; |
73 | |
74 | /** |
75 | the objId of the WayPoint. |
76 | @type {Number} |
77 | */ |
78 | |
79 | #objId = INVALID_OBJ_ID; |
80 | |
81 | /** |
82 | The constructor |
83 | */ |
84 | |
85 | constructor ( ) { |
86 | super ( ); |
87 | this.#objId = ObjId.nextObjId; |
88 | } |
89 | |
90 | /** |
91 | the name of the WayPoint |
92 | @type {String} |
93 | */ |
94 | |
95 | get name ( ) { return this.#name; } |
96 | |
97 | set name ( Name ) { |
98 | this.#name = |
99 | 'string' === typeof ( Name ) |
100 | ? |
101 | theHTMLSanitizer.sanitizeToJsString ( Name ) |
102 | : |
103 | ''; |
104 | } |
105 | |
106 | /** |
107 | the address of the WayPoint |
108 | @type {String} |
109 | */ |
110 | |
111 | get address ( ) { return this.#address; } |
112 | |
113 | set address ( address ) { |
114 | this.#address = |
115 | 'string' === typeof ( address ) |
116 | ? |
117 | theHTMLSanitizer.sanitizeToJsString ( address ) |
118 | : |
119 | ''; |
120 | } |
121 | |
122 | /** |
123 | the latitude of the WayPoint |
124 | @type {Number} |
125 | */ |
126 | |
127 | get lat ( ) { return this.#lat; } |
128 | |
129 | set lat ( lat ) { |
130 | this.#lat = 'number' === typeof ( lat ) ? lat : LAT_LNG.defaultValue; |
131 | } |
132 | |
133 | /** |
134 | the longitude of the WayPoint |
135 | @type {Number} |
136 | */ |
137 | |
138 | get lng ( ) { return this.#lng; } |
139 | |
140 | set lng ( lng ) { |
141 | this.#lng = 'number' === typeof ( lng ) ? lng : LAT_LNG.defaultValue; |
142 | } |
143 | |
144 | /** |
145 | the full name of the WayPoint. Full name is created with the name and address or latitude and longitude |
146 | of the WayPoint |
147 | @type {String} |
148 | */ |
149 | |
150 | get fullName ( ) { |
151 | let fullName = ( '' === this.name ? this.address : this.name + ', ' + this.address ); |
152 | if ( '' === fullName ) { |
153 | fullName = theUtilities.formatLatLng ( [ this.lat, this.lng ] ); |
154 | } |
155 | |
156 | return fullName; |
157 | } |
158 | |
159 | /** |
160 | the latitude and longitude of the WayPoint |
161 | @type {Array.<number>} |
162 | */ |
163 | |
164 | get latLng ( ) { return [ this.lat, this.lng ]; } |
165 | |
166 | set latLng ( latLng ) { |
167 | if ( |
168 | 'number' === typeof ( latLng [ ZERO ] ) |
169 | && |
170 | 'number' === typeof ( latLng [ ONE ] ) |
171 | ) { |
172 | this.#lat = latLng [ ZERO ]; |
173 | this.#lng = latLng [ ONE ]; |
174 | } |
175 | else { |
176 | this.#lat = LAT_LNG.defaultValue; |
177 | this.#lng = LAT_LNG.defaultValue; |
178 | } |
179 | } |
180 | |
181 | /** |
182 | the objId of the WayPoint. objId are unique identifier given by the code |
183 | @type {Number} |
184 | */ |
185 | |
186 | get objId ( ) { return this.#objId; } |
187 | |
188 | /** |
189 | the ObjType of the WayPoint. |
190 | @type {ObjType} |
191 | */ |
192 | |
193 | get objType ( ) { return WayPoint.#objType; } |
194 | |
195 | /** |
196 | An object literal with the WayPoint properties and without any methods. |
197 | This object can be used with the JSON object |
198 | @type {JsonObject} |
199 | */ |
200 | |
201 | get jsonObject ( ) { |
202 | return { |
203 | name : this.name, |
204 | address : this.address, |
205 | lat : parseFloat ( this.lat.toFixed ( LAT_LNG.fixed ) ), |
206 | lng : parseFloat ( this.lng.toFixed ( LAT_LNG.fixed ) ), |
207 | objId : this.#objId, |
208 | objType : this.objType.jsonObject |
209 | }; |
210 | } |
211 | |
212 | set jsonObject ( something ) { |
213 | const otherthing = this.validateObject ( something ); |
214 | this.address = otherthing.address; |
215 | this.name = otherthing.name; |
216 | this.lat = otherthing.lat; |
217 | this.lng = otherthing.lng; |
218 | this.#objId = ObjId.nextObjId; |
219 | } |
220 | } |
221 | |
222 | export default WayPoint; |
223 | |
224 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
225 |