| 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 theGeometry from '../../core/lib/Geometry.js'; |
| 26 | import theTravelNotesData from '../../data/TravelNotesData.js'; |
| 27 | import { ZERO, ONE } from '../../main/Constants.js'; |
| 28 | |
| 29 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 30 | /** |
| 31 | mouse event listener for the background |
| 32 | */ |
| 33 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 34 | |
| 35 | class BackgroundMouseEL { |
| 36 | |
| 37 | /** |
| 38 | constant for the left button |
| 39 | @type {Number} |
| 40 | */ |
| 41 | |
| 42 | static get LEFT_BUTTON ( ) { return ZERO; } |
| 43 | |
| 44 | /** |
| 45 | A flag set to true when a pan is ongoing |
| 46 | @type {Boolean} |
| 47 | */ |
| 48 | |
| 49 | #panOngoing = false; |
| 50 | |
| 51 | /** |
| 52 | The X screen coordinate of the beginning of the pan |
| 53 | @type {Number} |
| 54 | */ |
| 55 | |
| 56 | #startPanX = ZERO; |
| 57 | |
| 58 | /** |
| 59 | The Y screen coordinate of the beginning of the pan |
| 60 | @type {Number} |
| 61 | */ |
| 62 | |
| 63 | #startPanY = ZERO; |
| 64 | |
| 65 | /** |
| 66 | A leaflet LatLng object with the center of the map |
| 67 | @type {LeafletObject} |
| 68 | */ |
| 69 | |
| 70 | #mapCenter; |
| 71 | |
| 72 | /** |
| 73 | Execute the pan when a mousemove or mouseup event occurs after a mousedown event |
| 74 | @param { Event } mouseEvent The mouse event to process |
| 75 | */ |
| 76 | |
| 77 | #processPan ( mouseEvent ) { |
| 78 | const latLngAtStart = theGeometry.screenCoordToLatLng ( this.#startPanX, this.#startPanY ); |
| 79 | const latLngAtEnd = theGeometry.screenCoordToLatLng ( mouseEvent.screenX, mouseEvent.screenY ); |
| 80 | theTravelNotesData.map.panTo ( |
| 81 | [ |
| 82 | this.#mapCenter.lat + |
| 83 | latLngAtStart [ ZERO ] - |
| 84 | latLngAtEnd [ ZERO ], |
| 85 | this.#mapCenter.lng + |
| 86 | latLngAtStart [ ONE ] - |
| 87 | latLngAtEnd [ ONE ] |
| 88 | ] |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | The constructor |
| 94 | */ |
| 95 | |
| 96 | constructor ( ) { |
| 97 | Object.freeze ( this ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | Event listener method |
| 102 | @param {Event} mouseEvent The event to handle |
| 103 | */ |
| 104 | |
| 105 | handleEvent ( mouseEvent ) { |
| 106 | switch ( mouseEvent.type ) { |
| 107 | case 'mousedown' : |
| 108 | if ( |
| 109 | mouseEvent.button === BackgroundMouseEL.LEFT_BUTTON |
| 110 | && |
| 111 | mouseEvent.target === mouseEvent.currentTarget |
| 112 | ) { |
| 113 | this.#startPanX = mouseEvent.screenX; |
| 114 | this.#startPanY = mouseEvent.screenY; |
| 115 | this.#mapCenter = theTravelNotesData.map.getCenter ( ); |
| 116 | this.#panOngoing = true; |
| 117 | } |
| 118 | break; |
| 119 | case 'mousemove' : |
| 120 | if ( mouseEvent.button === BackgroundMouseEL.LEFT_BUTTON && this.#panOngoing ) { |
| 121 | if ( document.selection ) { |
| 122 | document.selection.empty (); |
| 123 | } |
| 124 | else { |
| 125 | window.getSelection ().removeAllRanges (); |
| 126 | } |
| 127 | this.#processPan ( mouseEvent ); |
| 128 | } |
| 129 | break; |
| 130 | case 'mouseup' : |
| 131 | if ( |
| 132 | mouseEvent.button === BackgroundMouseEL.LEFT_BUTTON |
| 133 | && |
| 134 | this.#panOngoing |
| 135 | && |
| 136 | ( this.#startPanX !== mouseEvent.screenX || this.#startPanY !== mouseEvent.screenY ) |
| 137 | ) { |
| 138 | this.#processPan ( mouseEvent ); |
| 139 | } |
| 140 | this.#panOngoing = false; |
| 141 | break; |
| 142 | default : |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | export default BackgroundMouseEL; |
| 149 | |
| 150 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
| 151 |