File : roadbook/RoadbookLoader.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
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
*/
18
/*
19
Changes:
20
    - v4.0.0:
21
        - created from v3.6.0
22
Doc reviewed 202208
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
This class load the roadbook,
42
*/
43
/* ------------------------------------------------------------------------------------------------------------------------- */
44
45
class RoadbookLoader {
46
47
    /**
48
    UUID of the page
49
    @type {String}
50
    */
51
52
    #UUID = null;
53
54
    /**
55
    The user language
56
    @type {String}
57
    */
58
59
    #language = 'fr';
60
61
    /**
62
    A reference to the save button
63
    @type {HTMLElement}
64
    */
65
66
    #saveButton = null;
67
68
    /**
69
    checkboxes init
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
    Adding event listeners
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
    Adding save button
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
    Opening the indexed db
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
    Loading translations from server
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
    Translating the page
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
    The constructor
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
    Loading the roadbook
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
/* --- End of file --------------------------------------------------------------------------------------------------------- */
211