| 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 theDataSearchEngine from '../../../data/DataSearchEngine.js'; |
| 27 | import theEventDispatcher from '../../../core/lib/EventDispatcher.js'; |
| 28 | import theGeometry from '../../../core/lib/Geometry.js'; |
| 29 | |
| 30 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 31 | /** |
| 32 | dragend event listener for the notes bullet |
| 33 | */ |
| 34 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 35 | |
| 36 | class NoteBulletDragEndEL { |
| 37 | |
| 38 | /** |
| 39 | Event listener method |
| 40 | @param {Event} dragEndEvent The event to handle |
| 41 | */ |
| 42 | |
| 43 | static handleEvent ( dragEndEvent ) { |
| 44 | |
| 45 | // the TravelNotes note and route are searched... |
| 46 | const noteAndRoute = theDataSearchEngine.getNoteAndRoute ( dragEndEvent.target.objId ); |
| 47 | const draggedNote = noteAndRoute.note; |
| 48 | const route = noteAndRoute.route; |
| 49 | |
| 50 | // ... then the layerGroup is searched... |
| 51 | const draggedLayerGroup = theTravelNotesData.mapObjects.get ( dragEndEvent.target.objId ); |
| 52 | if ( null === route ) { |
| 53 | |
| 54 | // the note is not attached to a route, so the coordinates of the note can be directly changed |
| 55 | draggedNote.latLng = [ dragEndEvent.target.getLatLng ( ).lat, dragEndEvent.target.getLatLng ( ).lng ]; |
| 56 | theEventDispatcher.dispatch ( 'updatetravelnotes' ); |
| 57 | } |
| 58 | else { |
| 59 | |
| 60 | // the note is attached to the route, so we have to find the nearest point on the route |
| 61 | // and the distance since the start of the route |
| 62 | const latLngDistance = theGeometry.getClosestLatLngDistance ( |
| 63 | route, |
| 64 | [ dragEndEvent.target.getLatLng ( ).lat, dragEndEvent.target.getLatLng ( ).lng ] |
| 65 | ); |
| 66 | |
| 67 | // coordinates and distance are changed in the note |
| 68 | draggedNote.latLng = latLngDistance.latLng; |
| 69 | draggedNote.distance = latLngDistance.distance; |
| 70 | |
| 71 | // notes are sorted on the distance |
| 72 | route.notes.sort ( |
| 73 | ( first, second ) => first.distance - second.distance |
| 74 | ); |
| 75 | |
| 76 | // the coordinates of the bullet are adapted |
| 77 | draggedLayerGroup.getLayer ( draggedLayerGroup.bulletId ) |
| 78 | .setLatLng ( latLngDistance.latLng ); |
| 79 | } |
| 80 | |
| 81 | // in all cases, the polyline is updated |
| 82 | draggedLayerGroup.getLayer ( draggedLayerGroup.polylineId ) |
| 83 | .setLatLngs ( [ draggedNote.latLng, draggedNote.iconLatLng ] ); |
| 84 | |
| 85 | // and the HTML page is adapted |
| 86 | theEventDispatcher.dispatch ( 'updateroadbook' ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | export default NoteBulletDragEndEL; |
| 91 | |
| 92 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
| 93 |