File : core/ProfileDialogsManager.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 theConfig from '../data/Config.js';
26
import ProfileDialog from '../dialogs/profileDialog/ProfileDialog.js';
27
import ProfileSmoothingIron from './lib/ProfileSmoothingIron.js';
28
import theDataSearchEngine from '../data/DataSearchEngine.js';
29
import { ZERO } from '../main/Constants.js';
30
31
/* ------------------------------------------------------------------------------------------------------------------------- */
32
/**
33
This class provides methods to manage the profile dialogs
34
see theProfileDialogsManager for the one and only one instance of this class
35
*/
36
/* ------------------------------------------------------------------------------------------------------------------------- */
37
38
class ProfileDialogsManager {
39
40
    /**
41
    A map with all the profile dialogs currently displayed
42
    @type {Map.<ProfileDialog>}
43
    */
44
45
    #profileDialogs = new Map ( );
46
47
    /**
48
    The constructor
49
    */
50
51
    constructor ( ) {
52
        Object.freeze ( this );
53
    }
54
55
    /**
56
    This method creates the profile for a Route after a call to a provider
57
    and manages the dialog profile
58
    @param {Route} route The Route for witch a profile is created
59
    */
60
61
    createProfile ( route ) {
62
        const profileDialog = this.#profileDialogs.get ( route.objId );
63
64
        if ( route.itinerary.hasProfile ) {
65
            if ( theConfig.route.elev.smooth ) {
66
                new ProfileSmoothingIron ( ).smooth ( route );
67
            }
68
            let ascent = ZERO;
69
            let descent = ZERO;
70
            let previousElev = route.itinerary.itineraryPoints.first.elev;
71
            route.itinerary.itineraryPoints.forEach (
72
                itineraryPoint => {
73
                    let deltaElev = itineraryPoint.elev - previousElev;
74
                    if ( ZERO > deltaElev ) {
75
                        descent -= deltaElev;
76
                    }
77
                    else {
78
                        ascent += deltaElev;
79
                    }
80
                    previousElev = itineraryPoint.elev;
81
                }
82
            );
83
            route.itinerary.ascent = ascent;
84
            route.itinerary.descent = descent;
85
            if ( profileDialog ) {
86
                profileDialog.setContent ( route );
87
            }
88
        }
89
        else if ( profileDialog ) {
90
            profileDialog.onCancel ( );
91
        }
92
    }
93
94
    /**
95
    Update the route name in a profile dialog. Called when the updateprofilename event is send
96
    @param {Number} routeObjId The obj id of the route to update
97
    */
98
99
    updateProfileName ( routeObjId ) {
100
        const profileDialog = this.#profileDialogs.get ( routeObjId );
101
        if ( profileDialog ) {
102
            const route = theDataSearchEngine.getRoute ( routeObjId );
103
            if ( route ) {
104
                profileDialog.setContentName ( route );
105
            }
106
        }
107
    }
108
109
    /**
110
    This method updates the profile dialog for a Route
111
    @param {Number} oldRouteObjId The objId of the Route that is in the profile dialog
112
    @param {Route} newRoute The  Route for witch the profile dialog is updated
113
    */
114
115
    updateProfile ( oldRouteObjId, newRoute ) {
116
        const profileDialog = this.#profileDialogs.get ( oldRouteObjId );
117
        if ( profileDialog ) {
118
            this.#profileDialogs.delete ( oldRouteObjId );
119
            if ( newRoute && newRoute.itinerary.hasProfile ) {
120
                profileDialog.setContent ( newRoute );
121
                this.#profileDialogs.set ( newRoute.objId, profileDialog );
122
            }
123
            else {
124
                profileDialog.onCancel ( );
125
            }
126
        }
127
    }
128
129
    /**
130
    This method close the profile dialog of a route
131
    @param {Number} objId The objId of the Route that is in the profile dialog to close
132
    */
133
134
    deleteProfile ( objId ) {
135
        const profileDialog = this.#profileDialogs.get ( objId );
136
        if ( profileDialog ) {
137
            profileDialog.onCancel ( );
138
        }
139
    }
140
141
    /**
142
    This method close the all the profile dialogs
143
    */
144
145
    deleteAllProfiles ( ) {
146
        this.#profileDialogs.forEach ( profileDialog => profileDialog.onCancel ( ) );
147
    }
148
149
    /**
150
    This method creates the profile dialog for a Route
151
    @param {Number} routeObjId The Route objId for witch a profile dialog is created
152
    */
153
154
    showProfile ( routeObjId ) {
155
        const route = theDataSearchEngine.getRoute ( routeObjId );
156
        let profileDialog = this.#profileDialogs.get ( routeObjId );
157
        if ( ! profileDialog ) {
158
            profileDialog = new ProfileDialog ( );
159
            profileDialog.setContent ( route );
160
            profileDialog.show ( );
161
            this.#profileDialogs.set ( routeObjId, profileDialog );
162
        }
163
    }
164
165
    /**
166
    This method is called when a profile dialog is closed
167
    @param {Number} objId The Route objId for witch a profile dialog is created
168
    */
169
170
    onProfileClosed ( objId ) {
171
        this.#profileDialogs.delete ( objId );
172
    }
173
174
}
175
176
/* ------------------------------------------------------------------------------------------------------------------------- */
177
/**
178
The one and only one instance of ProfileDialogsManager class
179
@type {ProfileDialogsManager}
180
*/
181
/* ------------------------------------------------------------------------------------------------------------------------- */
182
183
const theProfileDialogsManager = new ProfileDialogsManager ( );
184
185
export default theProfileDialogsManager;
186
187
/* --- End of file --------------------------------------------------------------------------------------------------------- */
188