| 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 { ZERO, ONE, TWO } from '../../main/Constants.js'; |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | class TouchListItemEL { |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | #dropFunction; |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | #isDoubleClick; |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | #lastTouchStartTimeStamp; |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | #lastScrollTimeStamp; |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | #clonedListItemHTMLElement; |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | #sortableListHTMLElement; |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | #scrolledContainerHTMLElement; |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | #topScrollPosition; |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | #bottomScrollPosition; |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | #touchY; |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | #dropTargetHTMLElement; |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | #dropOnTop; |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | #deltaMoveX; |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | #deltaMoveY; |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | static get #DBL_CLICK_MAX_DELAY ( ) { return 1000; } |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | static get #SCROLL_DELAY ( ) { return 40; } |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | static get #SCROLL_VALUE ( ) { return 5; } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | static get #TOP_SCROLL_DISTANCE ( ) { return 200; } |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | static get #BOTTOM_SCROLL_DISTANCE ( ) { return 100; } |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | #scrollContainerToTop ( scrollTimeStamp ) { |
| 182 | if ( |
| 183 | scrollTimeStamp !== this.#lastScrollTimeStamp |
| 184 | && |
| 185 | TouchListItemEL.#SCROLL_DELAY < scrollTimeStamp - this.#lastScrollTimeStamp |
| 186 | ) { |
| 187 | this.#scrolledContainerHTMLElement.scrollTop -= TouchListItemEL.#SCROLL_VALUE; |
| 188 | this.#lastScrollTimeStamp = scrollTimeStamp; |
| 189 | } |
| 190 | if ( |
| 191 | this.#topScrollPosition > this.#touchY |
| 192 | && |
| 193 | ZERO < this.#scrolledContainerHTMLElement.scrollTop |
| 194 | ) { |
| 195 | window.requestAnimationFrame ( |
| 196 | scrollTime => { this.#scrollContainerToTop ( scrollTime ); } |
| 197 | ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | #scrollContainerToBottom ( scrollTimeStamp ) { |
| 207 | if ( |
| 208 | scrollTimeStamp !== this.#lastScrollTimeStamp |
| 209 | && |
| 210 | TouchListItemEL.#SCROLL_DELAY < scrollTimeStamp - this.#lastScrollTimeStamp |
| 211 | ) { |
| 212 | this.#scrolledContainerHTMLElement.scrollTop += TouchListItemEL.#SCROLL_VALUE; |
| 213 | this.#lastScrollTimeStamp = scrollTimeStamp; |
| 214 | } |
| 215 | let isFullyScrolledDown = |
| 216 | ONE > Math.abs ( |
| 217 | this.#scrolledContainerHTMLElement.scrollHeight - |
| 218 | this.#scrolledContainerHTMLElement.clientHeight - |
| 219 | this.#scrolledContainerHTMLElement.scrollTop |
| 220 | ); |
| 221 | if ( |
| 222 | this.#bottomScrollPosition < this.#touchY |
| 223 | && |
| 224 | ! isFullyScrolledDown |
| 225 | ) { |
| 226 | window.requestAnimationFrame ( |
| 227 | scrollTime => { this.#scrollContainerToBottom ( scrollTime ); } |
| 228 | ); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | #handleStartEvent ( touchEvent ) { |
| 238 | const touch = touchEvent.changedTouches.item ( ZERO ); |
| 239 | if ( ONE === touchEvent.touches.length ) { |
| 240 | if ( |
| 241 | TouchListItemEL.#DBL_CLICK_MAX_DELAY < touchEvent.timeStamp - this.#lastTouchStartTimeStamp |
| 242 | || |
| 243 | ZERO === this.#lastTouchStartTimeStamp |
| 244 | ) { |
| 245 | |
| 246 | |
| 247 | this.#lastTouchStartTimeStamp = touchEvent.timeStamp; |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | touchEvent.preventDefault ( ); |
| 253 | |
| 254 | this.#isDoubleClick = true; |
| 255 | |
| 256 | |
| 257 | this.#sortableListHTMLElement = touchEvent.currentTarget.parentNode; |
| 258 | this.#scrolledContainerHTMLElement = touchEvent.currentTarget.parentNode.parentNode.parentNode; |
| 259 | this.#touchY = touch.screenY; |
| 260 | |
| 261 | |
| 262 | const boundingClientRect = touchEvent.currentTarget.getBoundingClientRect ( ); |
| 263 | this.#deltaMoveX = touch.screenX - boundingClientRect.left; |
| 264 | this.#deltaMoveY = touch.screenY - boundingClientRect.top; |
| 265 | |
| 266 | |
| 267 | this.#clonedListItemHTMLElement = touchEvent.currentTarget.cloneNode ( true ); |
| 268 | this.#clonedListItemHTMLElement.classList.add ( 'TravelNotes-SortableList-DraggedListItemHTMLElement' ); |
| 269 | document.body.appendChild ( this.#clonedListItemHTMLElement ); |
| 270 | this.#clonedListItemHTMLElement.style.left = String ( touch.screenX - this.#deltaMoveX ) + 'px'; |
| 271 | this.#clonedListItemHTMLElement.style.top = String ( touch.screenY - this.#deltaMoveY ) + 'px'; |
| 272 | this.#topScrollPosition = |
| 273 | this.#sortableListHTMLElement.getBoundingClientRect ( ).y - |
| 274 | this.#scrolledContainerHTMLElement.getBoundingClientRect ( ).y + |
| 275 | this.#scrolledContainerHTMLElement.scrollTop + |
| 276 | TouchListItemEL.#TOP_SCROLL_DISTANCE; |
| 277 | this.#bottomScrollPosition = |
| 278 | this.#scrolledContainerHTMLElement.getBoundingClientRect ( ).bottom - |
| 279 | TouchListItemEL.#BOTTOM_SCROLL_DISTANCE; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | #handleMoveEvent ( touchEvent ) { |
| 289 | if ( ! this.#isDoubleClick ) { |
| 290 | return; |
| 291 | } |
| 292 | const touch = touchEvent.changedTouches.item ( ZERO ); |
| 293 | if ( ONE === touchEvent.touches.length ) { |
| 294 | if ( this.#clonedListItemHTMLElement ) { |
| 295 | |
| 296 | |
| 297 | this.#clonedListItemHTMLElement.style.left = String ( touch.screenX - this.#deltaMoveX ) + 'px'; |
| 298 | this.#clonedListItemHTMLElement.style.top = String ( touch.screenY - this.#deltaMoveY ) + 'px'; |
| 299 | |
| 300 | |
| 301 | this.#touchY = touch.screenY; |
| 302 | if ( |
| 303 | this.#topScrollPosition > this.#touchY |
| 304 | && |
| 305 | ZERO < this.#scrolledContainerHTMLElement.scrollTop |
| 306 | ) { |
| 307 | window.requestAnimationFrame ( |
| 308 | scrollTime => { this.#scrollContainerToTop ( scrollTime ); } |
| 309 | ); |
| 310 | } |
| 311 | |
| 312 | |
| 313 | let isFullyScrolledDown = |
| 314 | ONE > Math.abs ( |
| 315 | this.#scrolledContainerHTMLElement.scrollHeight - |
| 316 | this.#scrolledContainerHTMLElement.clientHeight - |
| 317 | this.#scrolledContainerHTMLElement.scrollTop |
| 318 | ); |
| 319 | if ( |
| 320 | this.#bottomScrollPosition < this.#touchY |
| 321 | && |
| 322 | ! isFullyScrolledDown |
| 323 | ) { |
| 324 | window.requestAnimationFrame ( |
| 325 | scrollTime => { this.#scrollContainerToBottom ( scrollTime ); } |
| 326 | ); |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | #setDropTargetAndPosition ( touch ) { |
| 338 | |
| 339 | |
| 340 | this.#sortableListHTMLElement.childNodes.forEach ( |
| 341 | listItem => { |
| 342 | let clientRect = listItem.getBoundingClientRect ( ); |
| 343 | |
| 344 | |
| 345 | if ( |
| 346 | clientRect.left < touch.clientX |
| 347 | && |
| 348 | clientRect.right > touch.clientX |
| 349 | && |
| 350 | clientRect.top < touch.clientY |
| 351 | && |
| 352 | clientRect.bottom > touch.clientY |
| 353 | ) { |
| 354 | |
| 355 | |
| 356 | this.#dropTargetHTMLElement = listItem; |
| 357 | |
| 358 | |
| 359 | this.#dropOnTop = touch.clientY - clientRect.top < clientRect.height / TWO; |
| 360 | } |
| 361 | } |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | #handleEndEvent ( touchEvent ) { |
| 371 | if ( this.#isDoubleClick ) { |
| 372 | let touch = touchEvent.changedTouches.item ( ZERO ); |
| 373 | this.#setDropTargetAndPosition ( touch ); |
| 374 | if ( this.#dropTargetHTMLElement ) { |
| 375 | |
| 376 | |
| 377 | |
| 378 | try { |
| 379 | this.#dropFunction ( |
| 380 | Number.parseInt ( touchEvent.currentTarget.dataset.tanObjId ), |
| 381 | Number.parseInt ( this.#dropTargetHTMLElement.dataset.tanObjId ), |
| 382 | this.#dropOnTop |
| 383 | ); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | catch { } |
| 388 | } |
| 389 | } |
| 390 | this.#reset ( ); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | #reset ( ) { |
| 398 | if ( this.#clonedListItemHTMLElement ) { |
| 399 | document.body.removeChild ( this.#clonedListItemHTMLElement ); |
| 400 | } |
| 401 | this.#clonedListItemHTMLElement = null; |
| 402 | this.#scrolledContainerHTMLElement = null; |
| 403 | this.#isDoubleClick = false; |
| 404 | this.#lastScrollTimeStamp = ZERO; |
| 405 | this.#sortableListHTMLElement = null; |
| 406 | this.#topScrollPosition = ZERO; |
| 407 | this.#bottomScrollPosition = ZERO; |
| 408 | this.#touchY = ZERO; |
| 409 | this.#dropTargetHTMLElement = null; |
| 410 | this.#dropOnTop = true; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | constructor ( dropFunction ) { |
| 419 | Object.freeze ( this ); |
| 420 | this.#dropFunction = dropFunction; |
| 421 | this.#clonedListItemHTMLElement = null; |
| 422 | this.#lastTouchStartTimeStamp = ZERO; |
| 423 | this.#reset ( ); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | handleEvent ( touchEvent ) { |
| 432 | switch ( touchEvent.type ) { |
| 433 | case 'touchstart' : |
| 434 | this.#handleStartEvent ( touchEvent ); |
| 435 | break; |
| 436 | case 'touchmove' : |
| 437 | this.#handleMoveEvent ( touchEvent ); |
| 438 | break; |
| 439 | case 'touchend' : |
| 440 | this.#handleEndEvent ( touchEvent ); |
| 441 | break; |
| 442 | case 'touchcancel' : |
| 443 | this.#reset ( ); |
| 444 | break; |
| 445 | default : |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | export default TouchListItemEL; |
| 452 | |
| 453 | |
| 454 | |