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