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