File : viewsFactories/TravelHTMLViewsFactory.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 theHTMLElementsFactory from '../core/uiLib/HTMLElementsFactory.js';
26
import theHTMLSanitizer from '../core/htmlSanitizer/HTMLSanitizer.js';
27
import theUtilities from '../core/uiLib/Utilities.js';
28
import theConfig from '../data/Config.js';
29
import theTranslator from '../core/uiLib/Translator.js';
30
import theTravelNotesData from '../data/TravelNotesData.js';
31
import theNoteHTMLViewsFactory from '../viewsFactories/NoteHTMLViewsFactory.js';
32
import theRouteHTMLViewsFactory from '../viewsFactories/RouteHTMLViewsFactory.js';
33
import { DISTANCE, ZERO } from '../main/Constants.js';
34
35
/* ------------------------------------------------------------------------------------------------------------------------- */
36
/**
37
This class creates HTMLElements for travels
38
See theTravelHTMLViewsFactory for the one and only one instance of this class
39
*/
40
/* ------------------------------------------------------------------------------------------------------------------------- */
41
42
class TravelHTMLViewsFactory {
43
44
    /**
45
    Gives an HTMLElement with the travel name, distance, ascent (if any), descent (if any) and a list with all the routes
46
    of the travel
47
    @param {String} classPrefix A string that will be added to all the className of the created HTMLElements
48
    @return {HTMLElement} An HTMLElement with the travel header
49
    */
50
51
    #getTravelHeaderHTML ( classPrefix ) {
52
        const travelHeaderHTML = theHTMLElementsFactory.create ( 'div', { className : classPrefix + 'Travel-Header' } );
53
54
        theHTMLSanitizer.sanitizeToHtmlElement (
55
            theTravelNotesData.travel.name,
56
            theHTMLElementsFactory.create (
57
                'div',
58
                {
59
                    className : classPrefix + 'Travel-Header-Name'
60
                },
61
                travelHeaderHTML
62
            )
63
        );
64
65
        let travelDistance = DISTANCE.defaultValue;
66
        let travelAscent = ZERO;
67
        let travelDescent = ZERO;
68
        const routesIterator = theTravelNotesData.travel.routes.iterator;
69
        while ( ! routesIterator.done ) {
70
            const route =
71
                ( routesIterator.value.objId === theTravelNotesData.editedRouteObjId
72
                    &&
73
                    theConfig.routeEditor.showEditedRouteInRoadbook
74
                )
75
                    ?
76
                    theTravelNotesData.travel.editedRoute
77
                    :
78
                    routesIterator.value;
79
80
            theHTMLSanitizer.sanitizeToHtmlElement (
81
                '<a href="\u0023route' + route.objId + '" >' + route.computedName +
82
                '</a>\u00a0:\u00a0' + theUtilities.formatDistance ( route.distance ) + '.',
83
                theHTMLElementsFactory.create (
84
                    'div',
85
                    {
86
                        className : classPrefix + 'Travel-Header-RouteName'
87
                    },
88
                    travelHeaderHTML
89
                )
90
            );
91
92
            if ( route.chain ) {
93
                travelDistance += route.distance;
94
                travelAscent += route.itinerary.ascent;
95
                travelDescent += route.itinerary.descent;
96
            }
97
        }
98
99
        theHTMLSanitizer.sanitizeToHtmlElement (
100
            '<span>' +
101
                theTranslator.getText ( 'TravelHTMLViewsFactory - Travel distance' ) +
102
                '</span>\u00A0:\u00A0' +
103
                theUtilities.formatDistance ( travelDistance ),
104
            theHTMLElementsFactory.create (
105
                'div',
106
                {
107
                    className : classPrefix + 'Travel-Header-TravelDistance'
108
                },
109
                travelHeaderHTML
110
            )
111
        );
112
113
        if ( ZERO !== travelAscent ) {
114
            theHTMLSanitizer.sanitizeToHtmlElement (
115
                '<span>' +
116
                    theTranslator.getText ( 'travelHTMLViewsFactory - Travel ascent' ) +
117
                    '</span>\u00A0:\u00A0' +
118
                    String ( travelAscent.toFixed ( ZERO ) ) +
119
                    ' m.',
120
                theHTMLElementsFactory.create (
121
                    'div',
122
                    {
123
                        className : classPrefix + 'Travel-Header-TravelAscent'
124
                    },
125
                    travelHeaderHTML
126
                )
127
            );
128
        }
129
130
        if ( ZERO !== travelDescent ) {
131
            theHTMLSanitizer.sanitizeToHtmlElement (
132
                '<span>' +
133
                    theTranslator.getText ( 'TravelHTMLViewsFactory - Travel descent' ) +
134
                    '</span>\u00A0:\u00A0' +
135
                    String ( travelDescent.toFixed ( ZERO ) ) +
136
                    ' m.',
137
                theHTMLElementsFactory.create (
138
                    'div',
139
                    {
140
                        className : classPrefix + 'Travel-Header-TravelDescent'
141
                    },
142
                    travelHeaderHTML
143
                )
144
            );
145
        }
146
        return travelHeaderHTML;
147
    }
148
149
    /**
150
    Gives an HTMLElement with the Copyright notice and OSM attributions
151
    @param {String} classPrefix A string that will be added to all the className of the created HTMLElements
152
    @return {HTMLElement} An HTMLElement with the travel footer
153
    */
154
155
    #getTravelFooterHTML ( classPrefix ) {
156
        const footerText =
157
            theTranslator.getText ( 'TravelHTMLViewsFactory - Travel footer' ) +
158
            '<a href="https://github.com/wwwouaiebe/TravelNotes"' +
159
            ' target="_blank" title="https://github.com/wwwouaiebe/TravelNotes" >Travel & Notes</a>, © ' +
160
            '<a href="https://www.ouaie.be/"' +
161
            ' target="_blank" title="https://www.ouaie.be/" >wwwouaiebe 2017 ' +
162
            new Date ( Date.now () ).getFullYear () +
163
            '</a> © ' +
164
            '<a href="https://www.openstreetmap.org/copyright"' +
165
            ' target="_blank" title="https://www.openstreetmap.org/copyright">' +
166
            theTranslator.getText ( 'TravelHTMLViewsFactory - OpenStreetMap contributors' ) + '</a>';
167
        const footerHTML = theHTMLElementsFactory.create ( 'div', { className : classPrefix + 'TravelFooter' } );
168
169
        theHTMLSanitizer.sanitizeToHtmlElement ( footerText, footerHTML );
170
171
        return footerHTML;
172
    }
173
174
    /**
175
    The constructor
176
    */
177
178
    constructor ( ) {
179
        Object.freeze ( this );
180
    }
181
182
    /**
183
    Gives an HTMLElement with the travel header, the travel notes, all the routes of the travel
184
    with route header, route notes, route maneuvers, route footer and travel footer
185
    @param {String} classPrefix A string that will be added to all the className of the created HTMLElements
186
    @return {HTMLElement} An HTMLElement with the complete travel
187
    */
188
189
    getTravelHTML ( classPrefix ) {
190
        const travelHTML = theHTMLElementsFactory.create ( 'div', { className : classPrefix + 'Travel' } );
191
192
        travelHTML.appendChild ( this.#getTravelHeaderHTML ( classPrefix ) );
193
        travelHTML.appendChild ( theNoteHTMLViewsFactory.getTravelNotesHTML ( classPrefix ) );
194
195
        const travelRoutesIterator = theTravelNotesData.travel.routes.iterator;
196
        while ( ! travelRoutesIterator.done ) {
197
            const useEditedRoute =
198
                theConfig.routeEditor.showEditedRouteInRoadbook
199
                &&
200
                travelRoutesIterator.value.objId === theTravelNotesData.editedRouteObjId;
201
            const route = useEditedRoute ? theTravelNotesData.travel.editedRoute : travelRoutesIterator.value;
202
            travelHTML.appendChild ( theRouteHTMLViewsFactory.getRouteHeaderHTML ( classPrefix, route ) );
203
            if ( route.itinerary.hasProfile ) {
204
                travelHTML.appendChild ( theRouteHTMLViewsFactory.getRouteProfileHTML ( classPrefix, route ) );
205
            }
206
            travelHTML.appendChild ( theRouteHTMLViewsFactory.getRouteManeuversAndNotesHTML ( classPrefix, route, false ) );
207
            travelHTML.appendChild ( theRouteHTMLViewsFactory.getRouteFooterHTML ( classPrefix, route ) );
208
        }
209
210
        travelHTML.appendChild ( this.#getTravelFooterHTML ( classPrefix ) );
211
212
        return travelHTML;
213
    }
214
215
}
216
217
/* ------------------------------------------------------------------------------------------------------------------------- */
218
/**
219
The one and only one instance of TravelHTMLViewsFactory  class
220
@type {TravelHTMLViewsFactory }
221
*/
222
/* ------------------------------------------------------------------------------------------------------------------------- */
223
224
const theTravelHTMLViewsFactory = new TravelHTMLViewsFactory ( );
225
226
export default theTravelHTMLViewsFactory;
227
228
/* --- End of file --------------------------------------------------------------------------------------------------------- */
229