| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | import theHTMLElementsFactory from '../../core/uiLib/HTMLElementsFactory.js'; |
| 26 | import theTravelNotesData from '../../data/TravelNotesData.js'; |
| 27 | import theConfig from '../../data/Config.js'; |
| 28 | import theUtilities from '../../core/uiLib/Utilities.js'; |
| 29 | import { ZERO, SAVE_STATUS, ONE } from '../../main/Constants.js'; |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | class MouseUI { |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | #mouseUIElement; |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | #zoomPlusButton; |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | #zoomMinusButton; |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | #saveStatus; |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | #mousePosition; |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | #zoom; |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | #saveTimer; |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | static get #SAVE_TIME ( ) { return 300000; } |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | #changeZoom ( zoomIncrement ) { |
| 103 | theTravelNotesData.map.setZoom ( theTravelNotesData.map.getZoom ( ) + zoomIncrement ); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | #updateUI ( ) { |
| 111 | |
| 112 | |
| 113 | if ( this.#mouseUIElement ) { |
| 114 | this.#mouseUIElement.textContent = '\u00a0' + |
| 115 | this.#saveStatus + '\u00a0' + |
| 116 | this.#mousePosition + '\u00a0-\u00a0Zoom\u00a0:\u00a0' + |
| 117 | String ( this.#zoom ) + '\u00a0'; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | const map = theTravelNotesData.map; |
| 122 | if ( map.getMaxZoom ( ) === this.#zoom ) { |
| 123 | this.#zoomPlusButton.classList.add ( 'TravelNotes-MouseUI-Disabled' ); |
| 124 | } |
| 125 | else { |
| 126 | this.#zoomPlusButton.classList.remove ( 'TravelNotes-MouseUI-Disabled' ); |
| 127 | } |
| 128 | if ( map.getMinZoom ( ) === this.#zoom ) { |
| 129 | this.#zoomMinusButton.classList.add ( 'TravelNotes-MouseUI-Disabled' ); |
| 130 | } |
| 131 | else { |
| 132 | this.#zoomMinusButton.classList.remove ( 'TravelNotes-MouseUI-Disabled' ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | constructor ( ) { |
| 141 | Object.freeze ( this ); |
| 142 | this.#saveStatus = SAVE_STATUS.saved; |
| 143 | this.#mousePosition = ''; |
| 144 | this.#zoom = ZERO; |
| 145 | this.#saveTimer = null; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | set saveStatus ( saveStatus ) { |
| 154 | |
| 155 | |
| 156 | if ( SAVE_STATUS.modified === saveStatus && SAVE_STATUS.notSaved === this.#saveStatus ) { |
| 157 | return; |
| 158 | } |
| 159 | this.#saveStatus = saveStatus; |
| 160 | if ( SAVE_STATUS.modified === saveStatus && ! this.#saveTimer ) { |
| 161 | |
| 162 | |
| 163 | this.#saveTimer = setTimeout ( |
| 164 | ( ) => this.#saveStatus = SAVE_STATUS.notSaved, |
| 165 | MouseUI.#SAVE_TIME |
| 166 | ); |
| 167 | } |
| 168 | else if ( SAVE_STATUS.saved === saveStatus && this.#saveTimer ) { |
| 169 | |
| 170 | |
| 171 | clearTimeout ( this.#saveTimer ); |
| 172 | this.#saveTimer = null; |
| 173 | } |
| 174 | this.#updateUI ( ); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | createUI ( ) { |
| 182 | |
| 183 | |
| 184 | const mouseUImainElement = |
| 185 | theHTMLElementsFactory.create ( 'div', { id : 'TravelNotes-MouseUI' }, document.body ); |
| 186 | this.#zoomMinusButton = theHTMLElementsFactory.create ( |
| 187 | 'span', |
| 188 | { |
| 189 | id : 'TravelNotes-MouseUI-ZoomMinus', |
| 190 | textContent : '▼' |
| 191 | }, |
| 192 | mouseUImainElement |
| 193 | ); |
| 194 | this.#zoomMinusButton.addEventListener ( 'click', ( ) => this.#changeZoom ( -ONE ) ); |
| 195 | this.#mouseUIElement = theHTMLElementsFactory.create ( 'span', null, mouseUImainElement ); |
| 196 | this.#zoomPlusButton = theHTMLElementsFactory.create ( |
| 197 | 'span', |
| 198 | { |
| 199 | id : 'TravelNotes-MouseUI-ZoomPlus', |
| 200 | textContent : '▲' |
| 201 | }, |
| 202 | mouseUImainElement |
| 203 | ); |
| 204 | this.#zoomPlusButton.addEventListener ( 'click', ( ) => this.#changeZoom ( ONE ) ); |
| 205 | |
| 206 | |
| 207 | this.#zoom = theTravelNotesData.map.getZoom ( ); |
| 208 | this.#mousePosition = |
| 209 | theUtilities.formatLat ( theConfig.map.center.lat ) + |
| 210 | '\u00a0-\u00a0' + |
| 211 | theUtilities.formatLng ( theConfig.map.center.lng ); |
| 212 | |
| 213 | |
| 214 | theTravelNotesData.map.on ( |
| 215 | 'mousemove', |
| 216 | mouseMoveEvent => { |
| 217 | this.#mousePosition = |
| 218 | theUtilities.formatLatLng ( [ mouseMoveEvent.latlng.lat, mouseMoveEvent.latlng.lng ] ); |
| 219 | this.#updateUI ( ); |
| 220 | } |
| 221 | ); |
| 222 | |
| 223 | |
| 224 | theTravelNotesData.map.on ( |
| 225 | 'zoomend', |
| 226 | ( ) => { |
| 227 | this.#zoom = theTravelNotesData.map.getZoom ( ); |
| 228 | this.#updateUI ( ); |
| 229 | } |
| 230 | ); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | const theMouseUI = new MouseUI ( ); |
| 242 | |
| 243 | export default theMouseUI; |
| 244 | |
| 245 | |
| 246 | |