File : core/mapEditor/mapMouseELs/MapMouseELs.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 theDataSearchEngine from '../../../data/DataSearchEngine.js';
26
import theDevice from '../../lib/Device.js';
27
import theTravelNotesData from '../../../data/TravelNotesData.js';
28
import { LAT, LNG, ROUTE_EDITION_STATUS } from '../../../main/Constants.js';
29
import theConfig from '../../../data/Config.js';
30
import MapContextMenu from '../../../contextMenus/MapContextMenu.js';
31
import RouteContextMenu from '../../../contextMenus/RouteContextMenu.js';
32
import EditedRouteMouseOverEL from '../editedRouteEL/EditedRouteMouseOverEL.js';
33
34
import { LeafletLatLng } from '../../../leaflet/LeafletImports.js';
35
36
/* ------------------------------------------------------------------------------------------------------------------------- */
37
/**
38
click and contextmenu event listeners for the map
39
*/
40
/* ------------------------------------------------------------------------------------------------------------------------- */
41
42
class MapMouseELs {
43
44
    /**
45
    Search a route near the given event listener and create a fake event if a route
46
    is found near the event point
47
    @param {Event} clickOrContextMenuEvent The event to use
48
    */
49
50
    static createFakeEvent ( clickOrContextMenuEvent ) {
51
        const nearestRouteData = theDataSearchEngine.getNearestRouteData (
52
            [ clickOrContextMenuEvent.latlng.lat, clickOrContextMenuEvent.latlng.lng ]
53
        );
54
55
        if ( ! nearestRouteData.route ) {
56
            return;
57
        }
58
59
        const routeLatLng = new LeafletLatLng (
60
            nearestRouteData.latLngOnRoute [ LAT ], nearestRouteData.latLngOnRoute [ LNG ]
61
        );
62
        const routeContainerPoint = theTravelNotesData.map.latLngToContainerPoint ( routeLatLng );
63
        const routeDistance = routeContainerPoint.distanceTo ( clickOrContextMenuEvent.containerPoint );
64
        const maxRouteDistance =
65
            theDevice.isTouch
66
                ?
67
                theConfig.mapContextMenu.touchMaxRouteDistance
68
                :
69
                theConfig.mapContextMenu.mouseMaxRouteDistance;
70
71
        if ( routeDistance > maxRouteDistance ) {
72
            return;
73
        }
74
75
        return Object.freeze (
76
            {
77
                clientX : routeContainerPoint.x,
78
                clientY : routeContainerPoint.y,
79
                latlng : routeLatLng,
80
                target : nearestRouteData.route
81
            }
82
        );
83
84
    }
85
86
    /**
87
    Click event listener for the map. Search an edited route near the click point and add a
88
    temp way point if any
89
    @param {Event} clickEvent The event to handle
90
    */
91
92
    static handleClickEvent ( clickEvent ) {
93
        const fakeEvent = MapMouseELs.createFakeEvent ( clickEvent );
94
        if (
95
            fakeEvent
96
            &&
97
            theDevice.isTouch
98
            &&
99
            fakeEvent.target.editionStatus !== ROUTE_EDITION_STATUS.notEdited
100
        ) {
101
            EditedRouteMouseOverEL.handleEvent ( fakeEvent );
102
        }
103
    }
104
105
    /**
106
    Context event listener for the map. Search an edited route near the click point and show a
107
    route context menu if any, otherwise show a map context menu
108
    @param {Event} contextMenuEvent The event to handle
109
    */
110
111
    static handleContextMenuEvent ( contextMenuEvent ) {
112
113
        const fakeEvent = MapMouseELs.createFakeEvent ( contextMenuEvent );
114
        if ( fakeEvent ) {
115
            new RouteContextMenu ( fakeEvent ).show ( );
116
        }
117
        else {
118
            new MapContextMenu ( contextMenuEvent ).show ( );
119
        }
120
    }
121
122
}
123
124
export default MapMouseELs;
125
126
/* --- End of file --------------------------------------------------------------------------------------------------------- */
127