File : toolbars/travelNotesToolbar/TravelNotesToolbar.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 AboutDialog from '../../dialogs/aboutDialog/AboutDialog.js';
26
import BaseToolbar from '../baseToolbar/BaseToolbar.js';
27
import ToolbarItem from '../baseToolbar/ToolbarItem.js';
28
import theApiKeysManager from '../../core/ApiKeysManager.js';
29
import theGeoLocator from '../../core/GeoLocator.js';
30
import theConfig from '../../data/Config.js';
31
import theTranslator from '../../core/uiLib/Translator.js';
32
import theTravelNotesData from '../../data/TravelNotesData.js';
33
import theUtilities from '../../core/uiLib/Utilities.js';
34
import theErrorsUI from '../../uis/errorsUI/ErrorsUI.js';
35
import theTravelEditor from '../../core/TravelEditor.js';
36
import theDockableDialogsManager from '../../core/DockableDialogsManager.js';
37
import theFullScreenUI from '../../uis/fullScreenUI/FullScreenUI.js';
38
import theFontSizeManager from '../../core/FontSizeManager.js';
39
import OpenInputChangeEL from './OpenInputChangeEL.js';
40
import ImportInputChangeEL from './ImportInputChangeEL.js';
41
import theDevice from '../../core/lib/Device.js';
42
import { INVALID_OBJ_ID, TOOLBAR_POSITION, GEOLOCATION_STATUS, ZERO, ONE } from '../../main/Constants.js';
43
44
/* ------------------------------------------------------------------------------------------------------------------------- */
45
/**
46
This class is the TravelNotes toolbar
47
*/
48
/* ------------------------------------------------------------------------------------------------------------------------- */
49
50
class TravelNotesToolbar extends BaseToolbar {
51
52
    /**
53
    The constructor
54
    */
55
56
    constructor ( ) {
57
        super ( );
58
    }
59
60
    /**
61
    Create the UI, adding buttons
62
    */
63
64
    createUI ( ) {
65
        super.createUI ( 'Travel & Notes', TOOLBAR_POSITION.topRight );
66
    }
67
68
    /**
69
    Add the ToolbarItems to the toolbar. Called by the #show ( ) method of the vase class
70
    */
71
72
    addToolbarItems ( ) {
73
        const writeExtension = theDevice.isTouch ? theConfig.files.writeTouch : theConfig.files.writeOthers;
74
        const translationVar = Object.seal ( { openTaN : '', openGpx : '' } );
75
        theConfig.files.openTaN.forEach ( fileExtension => translationVar.openTaN += '.' + fileExtension + ',' );
76
        theConfig.files.openGpx.forEach ( fileExtension => translationVar.openGpx += '.' + fileExtension + ',' );
77
        const fileExtensions = ( translationVar.openTaN + translationVar.openGpx ).slice ( ZERO, -ONE );
78
        translationVar.openTaN = translationVar.openTaN.slice ( ZERO, -ONE );
79
        translationVar.openGpx = translationVar.openGpx.slice ( ZERO, -ONE );
80
        translationVar.openTaN.replaceAll ( ',', ', ' );
81
        translationVar.openGpx.replaceAll ( ',', ', ' );
82
83
        this.addToolbarItem (
84
            theGeoLocator.status === GEOLOCATION_STATUS.active
85
                ?
86
                new ToolbarItem (
87
                    '🏀',
88
                    theTranslator.getText ( 'TravelNotesToolbar - Stop Geo location' ),
89
                    ( ) => { theGeoLocator.switch ( ); }
90
                )
91
                :
92
                new ToolbarItem (
93
                    '🌐',
94
                    theTranslator.getText ( 'TravelNotesToolbar - Start Geo location' ),
95
                    ( ) => { theGeoLocator.switch ( ); }
96
                )
97
        );
98
        this.addToolbarItem (
99
            new ToolbarItem (
100
                '❌',
101
                theTranslator.getText ( 'TravelNotesToolbar - Cancel travel' ),
102
                ( ) => {
103
                    theTravelEditor.newTravel ( );
104
                    document.title =
105
                        'Travel & Notes' +
106
                        ( '' === theTravelNotesData.travel.name ? '' : ' - ' + theTravelNotesData.travel.name );
107
                }
108
            )
109
        );
110
        this.addToolbarItem (
111
            new ToolbarItem (
112
                '💾',
113
                theTranslator.getText ( 'TravelNotesToolbar - Save travel', { extension : writeExtension } ),
114
                ( ) => { theTravelEditor.saveTravel ( ); }
115
            )
116
        );
117
        this.addToolbarItem (
118
            new ToolbarItem (
119
                '📂',
120
                theTranslator.getText ( 'TravelNotesToolbar - Open travel', translationVar ),
121
                ( ) => {
122
                    if (
123
                        theConfig.travelNotes.haveBeforeUnloadWarning
124
                        &&
125
                        (
126
                            ! window.confirm ( theTranslator.getText (
127
                                'TravelNotesToolbar - This page ask to close; data are perhaps not saved.' )
128
                            )
129
                        )
130
                    ) {
131
                        return;
132
                    }
133
                    theUtilities.openFile ( new OpenInputChangeEL ( ), fileExtensions );
134
                }
135
            )
136
        );
137
        this.addToolbarItem (
138
            new ToolbarItem (
139
                '🌏',
140
                theTranslator.getText ( 'TravelNotesToolbar - Import travel', translationVar ),
141
                ( ) => {
142
                    if ( INVALID_OBJ_ID === theTravelNotesData.editedRouteObjId ) {
143
                        theUtilities.openFile ( new ImportInputChangeEL ( ), fileExtensions );
144
                    }
145
                    else {
146
                        theErrorsUI.showError (
147
                            theTranslator.getText (
148
                                'TravelNotesToolbar - Not possible to merge a travel when a route is edited'
149
                            )
150
                        );
151
                    }
152
                }
153
            )
154
        );
155
        this.addToolbarItem (
156
            new ToolbarItem (
157
                '📙',
158
                theTranslator.getText ( 'TravelNotesToolbar - Open travel roadbook' ),
159
                'TravelNotesRoadbook.html?lng=' +
160
                        theConfig.travelNotes.language + '&page=' +
161
                        theTravelNotesData.UUID
162
            )
163
        );
164
        if ( theConfig.ApiKeysDialog.showButton ) {
165
            this.addToolbarItem (
166
                new ToolbarItem (
167
                    '🔑',
168
                    theTranslator.getText ( 'TravelNotesToolbar - api keys' ),
169
                    ( ) => { theApiKeysManager.setKeysFromDialog ( ); }
170
                )
171
            );
172
        }
173
        this.addToolbarItem (
174
            new ToolbarItem (
175
                '🛄',
176
                theTranslator.getText ( 'TravelNotesToolbar - Travel properties' ),
177
                ( ) => { theDockableDialogsManager.travelPropertiesDialog.show ( ); }
178
            )
179
        );
180
        this.addToolbarItem (
181
            new ToolbarItem (
182
                '🗨️',
183
                theTranslator.getText ( 'TravelNotesToolbar - Travel notes' ),
184
                ( ) => { theDockableDialogsManager.travelNotesDialog.show ( ); }
185
            )
186
        );
187
        this.addToolbarItem (
188
            new ToolbarItem (
189
                '🔍',
190
                theTranslator.getText ( 'Search with OpenStreetMap' ),
191
                ( ) => { theDockableDialogsManager.osmSearchDialog.show ( ); }
192
            )
193
        );
194
        this.addToolbarItem (
195
            new ToolbarItem (
196
                '☢️',
197
                theTranslator.getText ( 'TravelNotesToolbar - Save as travel', { extension : writeExtension } ),
198
                ( ) => { theTravelEditor.saveAsTravel ( ); }
199
            )
200
        );
201
        this.addToolbarItem (
202
            new ToolbarItem (
203
                '🛠️',
204
                theTranslator.getText ( 'TravelNotesToolbar - About Travel & Notes' ),
205
                ( ) => new AboutDialog ( ).show ( )
206
            )
207
        );
208
        if ( document.fullscreenEnabled ) {
209
            this.addToolbarItem (
210
                document.fullscreenElement
211
                    ?
212
                    new ToolbarItem (
213
                        '🔻',
214
                        theTranslator.getText ( 'TravelNotesToolbar - disable fullscreen' ),
215
                        ( ) => theFullScreenUI.toggle ( )
216
                    )
217
                    :
218
                    new ToolbarItem (
219
                        '🔺',
220
                        theTranslator.getText ( 'TravelNotesToolbar - enable fullscreen' ),
221
                        ( ) => theFullScreenUI.toggle ( )
222
                    )
223
            );
224
        }
225
        this.addToolbarItem (
226
            new ToolbarItem (
227
                '+',
228
                theTranslator.getText ( 'TravelNotesToolbar - Increment the font size' ),
229
                ( ) => { theFontSizeManager.increment ( ); }
230
            )
231
        );
232
        this.addToolbarItem (
233
            new ToolbarItem (
234
                '-',
235
                theTranslator.getText ( 'TravelNotesToolbar - Decrement the font size' ),
236
                ( ) => { theFontSizeManager.decrement ( ); }
237
            )
238
        );
239
        this.addToolbarItem (
240
            new ToolbarItem (
241
                '🏠',
242
                'Home',
243
                window.location.origin
244
            )
245
        );
246
        this.addToolbarItem (
247
            new ToolbarItem (
248
                '?',
249
                'Help',
250
                'https://wwwouaiebe.github.io/TravelNotes/userGuides/README.html\u0023'
251
            )
252
        );
253
        this.addToolbarItem (
254
            new ToolbarItem (
255
                '@',
256
                'Contact',
257
                ( theConfig.travelNotesToolbar.contactMail.url || window.location.origin )
258
            )
259
        );
260
    }
261
}
262
263
/* ------------------------------------------------------------------------------------------------------------------------- */
264
/**
265
The one and only one instance of TravelNotesToolbar class
266
@type {TravelNotesToolbar}
267
*/
268
/* ------------------------------------------------------------------------------------------------------------------------- */
269
270
const theTravelNotesToolbar = new TravelNotesToolbar ( );
271
272
export default theTravelNotesToolbar;
273
274
/* --- End of file --------------------------------------------------------------------------------------------------------- */
275