File : main/travelNotesViewer/TravelNotesViewer.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 theTravelNotesData from '../../data/TravelNotesData.js';
26
import ViewerFileLoader from '../../core/ViewerFileLoader.js';
27
import theAttributionsUI from '../../uis/attributionsUI/AttributionsUI.js';
28
import theViewerLayersToolbar from '../../toolbars/viewerLayersToolbar/ViewerLayersToolbar.js';
29
import { TWO, LAT_LNG, HTTP_STATUS_OK } from '../../main/Constants.js';
30
31
/* ------------------------------------------------------------------------------------------------------------------------- */
32
/**
33
This class is the entry point of the viewer.<br>
34
See theTravelNotesViewer for the one and only one instance of this class.
35
*/
36
/* ------------------------------------------------------------------------------------------------------------------------- */
37
38
class TravelNotesViewer {
39
40
    /**
41
    Guard to avoid a second upload
42
    @type {Boolean}
43
    */
44
45
    #travelNotesLoaded;
46
47
    /**
48
    Load a travel from the server
49
    @param {String} travelUrl The url of the TaN file to open
50
    */
51
52
    async #loadDistantTravel ( travelUrl ) {
53
        const travelResponse = await fetch ( travelUrl );
54
        if ( HTTP_STATUS_OK === travelResponse.status && travelResponse.ok ) {
55
            new ViewerFileLoader ( ).openDistantFile ( await travelResponse.json ( ) );
56
        }
57
        else {
58
            theTravelNotesData.map.setView ( [ LAT_LNG.defaultValue, LAT_LNG.defaultValue ], TWO );
59
            document.title = 'Travel & Notes';
60
        }
61
    }
62
63
    /**
64
    The constructor
65
    */
66
67
    constructor ( ) {
68
        Object.freeze ( this );
69
        this.#travelNotesLoaded = false;
70
    }
71
72
    /**
73
    This method load the TravelNotes viewer and open a read only map passed trought the url.
74
    This method can only be executed once. Others call will be ignored.
75
    @param {String} travelUrl The url of the TaN file to open
76
    @param {Boolean} addLayerToolbar A bollean indicating when the map layer toolbar must be visible
77
    */
78
79
    addReadOnlyMap ( travelUrl, addLayerToolbar ) {
80
81
        if ( this.#travelNotesLoaded ) {
82
            return;
83
        }
84
85
        this.#travelNotesLoaded = true;
86
87
        theAttributionsUI.createUI ( );
88
        if ( addLayerToolbar ) {
89
            theViewerLayersToolbar.createUI ( );
90
        }
91
92
        theViewerLayersToolbar.setMapLayer ( 'OSM - Color' );
93
        if ( travelUrl ) {
94
            this.#loadDistantTravel ( travelUrl );
95
        }
96
    }
97
}
98
99
/* ------------------------------------------------------------------------------------------------------------------------- */
100
/**
101
The one and only one instance of theTravelNotesViewer class
102
@type {TravelNotesViewer}
103
*/
104
/* ------------------------------------------------------------------------------------------------------------------------- */
105
106
const theTravelNotesViewer = new TravelNotesViewer ( );
107
108
export default theTravelNotesViewer;
109
110
/* --- End of file --------------------------------------------------------------------------------------------------------- */
111