1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | import theTranslator from '../core/uiLib/Translator.js'; |
26 | import theIndexedDb from '../core/uiLib/IndexedDb.js'; |
27 | import theRoadbookUpdater from './RoadbookUpdater.js'; |
28 | import ShowManeuverNotesChangeEL from './ShowManeuverNotesChangeEL.js'; |
29 | import ShowRouteNotesChangeEL from './ShowRouteNotesChangeEL.js'; |
30 | import ShowTravelNotesChangeEL from './ShowTravelNotesChangeEL.js'; |
31 | import ShowSmallNotesChangeEL from './ShowSmallNotesChangeEL.js'; |
32 | import ShowProfilesChangeEL from './ShowProfilesChangeEL.js'; |
33 | import PrintRoadbookClickEL from './PrintRoadbookClickEL.js'; |
34 | import SaveFileButtonClickEL from './SaveFileButtonClickEL.js'; |
35 | import StorageEL from './StorageEL.js'; |
36 | |
37 | import { ZERO, ONE, HTTP_STATUS_OK } from '../main/Constants.js'; |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | class RoadbookLoader { |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | #UUID = null; |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | #language = 'fr'; |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | #saveButton = null; |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | #initCheckboxes ( ) { |
73 | document.getElementById ( 'TravelNotes-Travel-ShowNotes' ).checked = theRoadbookUpdater.showTravelNotes; |
74 | document.getElementById ( 'TravelNotes-Routes-ShowNotes' ).checked = theRoadbookUpdater.showRouteNotes; |
75 | document.getElementById ( 'TravelNotes-Routes-ShowManeuvers' ).checked = theRoadbookUpdater.showManeuversNotes; |
76 | document.getElementById ( 'TravelNotes-Routes-ShowSmallNotes' ).checked = theRoadbookUpdater.showSmallNotes; |
77 | document.getElementById ( 'TravelNotes-Routes-ShowProfiles' ).checked = theRoadbookUpdater.showProfiles; |
78 | } |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | #addEventListeners ( ) { |
85 | document.getElementById ( 'TravelNotes-Travel-ShowNotes' ) |
86 | .addEventListener ( 'change', new ShowTravelNotesChangeEL ( ) ); |
87 | document.getElementById ( 'TravelNotes-Routes-ShowNotes' ) |
88 | .addEventListener ( 'change', new ShowRouteNotesChangeEL ( ) ); |
89 | document.getElementById ( 'TravelNotes-Routes-ShowManeuvers' ) |
90 | .addEventListener ( 'change', new ShowManeuverNotesChangeEL ( ) ); |
91 | document.getElementById ( 'TravelNotes-Routes-ShowSmallNotes' ) |
92 | .addEventListener ( 'change', new ShowSmallNotesChangeEL ( ) ); |
93 | document.getElementById ( 'TravelNotes-Routes-ShowProfiles' ) |
94 | .addEventListener ( 'change', new ShowProfilesChangeEL ( ) ); |
95 | document.getElementById ( 'TravelNotes-PrintButton' ) |
96 | .addEventListener ( 'click', new PrintRoadbookClickEL ( ) ); |
97 | |
98 | } |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | #addSaveButton ( ) { |
105 | this.#saveButton = document.createElement ( 'div' ); |
106 | this.#saveButton.id = 'TravelNotes-SaveButton'; |
107 | this.#saveButton.textContent = '💾'; |
108 | this.#saveButton.className = 'TravelNotes-RoadbookButton'; |
109 | this.#saveButton.addEventListener ( 'click', new SaveFileButtonClickEL ( ) ); |
110 | document.getElementById ( 'TravelNotes-ButtonsDiv' ).appendChild ( this.#saveButton ); |
111 | } |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | #openIndexedDb ( ) { |
118 | theIndexedDb.getOpenPromise ( ) |
119 | .then ( ( ) => theIndexedDb.getReadPromise ( this.#UUID ) ) |
120 | .then ( pageContent => theRoadbookUpdater.updateRoadbook ( pageContent ) ) |
121 | .catch ( |
122 | err => { |
123 | if ( err instanceof Error ) { |
124 | console.error ( err ); |
125 | } |
126 | } |
127 | ); |
128 | window.addEventListener ( 'storage', new StorageEL ( this.#UUID ) ); |
129 | window.addEventListener ( 'unload', ( ) => theIndexedDb.closeDb ( ) ); |
130 | } |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | #loadTranslations ( ) { |
137 | fetch ( |
138 | window.location.href.substring ( ZERO, window.location.href.lastIndexOf ( '/' ) + ONE ) + |
139 | 'TravelNotes' + |
140 | this.#language.toUpperCase ( ) + |
141 | '.json' |
142 | ) |
143 | .then ( |
144 | response => { |
145 | if ( HTTP_STATUS_OK === response.status && response.ok ) { |
146 | response.json ( ) |
147 | .then ( translations => theTranslator.setTranslations ( translations ) ) |
148 | .then ( ( ) => this.#translatePage ( ) ) |
149 | .catch ( |
150 | err => { |
151 | if ( err instanceof Error ) { |
152 | console.error ( err ); |
153 | } |
154 | } |
155 | ); |
156 | } |
157 | } |
158 | ) |
159 | .catch ( |
160 | ); |
161 | } |
162 | |
163 | |
164 | |
165 | |
166 | |
167 | #translatePage ( ) { |
168 | document.getElementById ( 'TravelNotes-Travel-ShowNotesLabel' ).textContent = |
169 | theTranslator.getText ( 'RoadbookLoader - show travel notes' ); |
170 | document.getElementById ( 'TravelNotes-Routes-ShowManeuversLabel' ).textContent = |
171 | theTranslator.getText ( 'RoadbookLoader - show maneuver' ); |
172 | document.getElementById ( 'TravelNotes-Routes-ShowNotesLabel' ).textContent = |
173 | theTranslator.getText ( 'RoadbookLoader - show routes notes' ); |
174 | document.getElementById ( 'TravelNotes-Routes-ShowSmallNotesLabel' ).textContent = |
175 | theTranslator.getText ( 'RoadbookLoader - show small routes notes' ); |
176 | document.getElementById ( 'TravelNotes-Routes-ShowProfilesLabel' ).textContent = |
177 | theTranslator.getText ( 'RoadbookLoader - show profiles' ); |
178 | } |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | constructor ( ) { |
185 | Object.freeze ( this ); |
186 | let params = new URLSearchParams ( document.location.search.substring ( ONE ) ); |
187 | this.#UUID = params.get ( 'page' ); |
188 | this.#language = params.get ( 'lng' ) || 'fr'; |
189 | } |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | loadRoadbook ( ) { |
196 | this.#initCheckboxes ( ); |
197 | this.#addEventListeners ( ); |
198 | if ( this.#UUID ) { |
199 | this.#addSaveButton ( ); |
200 | this.#openIndexedDb ( ); |
201 | this.#loadTranslations ( ); |
202 | } |
203 | |
204 | theRoadbookUpdater.updateNotesAndProfiles ( ); |
205 | } |
206 | } |
207 | |
208 | export default RoadbookLoader; |
209 | |
210 | |
211 | |