@@ -21,12 +21,16 @@ var iutil = { |
21 | 21 | */ |
22 | 22 | 'externalLinkPattern': /^https?:\/\/([^\/]+?@)?([^:\/]+)/, |
23 | 23 | /** |
24 | | - * リンクホールドのための変数コンテナ |
| 24 | + * リンクスライディングのための変数コンテナ |
25 | 25 | * @type {Object} |
26 | 26 | */ |
27 | | - 'hold': { |
28 | | - 'duration': 1000, |
29 | | - 'timeoutId': -1, |
| 27 | + 'sliding': { |
| 28 | + 'start': -1, |
| 29 | + 'startX': -1, |
| 30 | + 'startY': -1, |
| 31 | + 'endX': -1, |
| 32 | + 'endY': -1, |
| 33 | + 'target': null, |
30 | 34 | 'callbacks': {}, |
31 | 35 | 'dialogs': {}, |
32 | 36 | 'dialog': null, |
@@ -406,17 +410,17 @@ iutil.stopEvent = function(event) { |
406 | 410 | }; |
407 | 411 |
|
408 | 412 | // }}} |
409 | | -// {{{ hold.onTouchStart() |
| 413 | +// {{{ sliding.onTouchStart() |
410 | 414 |
|
411 | 415 | /** |
412 | | - * リンクホールド・タッチ/マウス押し下げ時のイベントハンドラ |
| 416 | + * リンクスライド・タッチ/マウス押し下げ時のイベントハンドラ |
413 | 417 | * ダイアログ表示タイマーをセットする |
414 | 418 | * |
415 | 419 | * @param {Event} event |
416 | 420 | * @param {Element} target |
417 | 421 | * @return void |
418 | 422 | */ |
419 | | -iutil.hold.onTouchStart = function(event, target) { |
| 423 | +iutil.sliding.onTouchStart = function(event, target) { |
420 | 424 | var x, y; |
421 | 425 |
|
422 | 426 | event = event || window.event; |
@@ -450,55 +454,71 @@ iutil.hold.onTouchStart = function(event, target) { |
450 | 454 | } |
451 | 455 | } |
452 | 456 |
|
453 | | - if (iutil.hold.timeoutId != -1) { |
454 | | - window.clearTimeout(iutil.hold.timeoutId); |
455 | | - } |
456 | | - |
457 | | - iutil.hold.timeoutId = window.setTimeout(iutil.hold.showDialog, |
458 | | - iutil.hold.duration, |
459 | | - target, x, y); |
| 457 | + iutil.sliding.start = event.timeStamp; |
| 458 | + iutil.sliding.startX = x; |
| 459 | + iutil.sliding.startY = y; |
| 460 | + iutil.sliding.target = target; |
460 | 461 | }; |
461 | 462 |
|
462 | 463 | // }}} |
463 | | -// {{{ hold.onTouchMove() |
| 464 | +// {{{ sliding.onTouchMove() |
464 | 465 |
|
465 | 466 | /** |
466 | | - * リンクホールド・ムーブ/ドラッグ時のイベントハンドラ |
| 467 | + * リンクスライド・ムーブ/ドラッグ時のイベントハンドラ |
467 | 468 | * タッチ/カーソルが移動したならダイアログ表示タイマーをキャンセルする |
468 | 469 | * |
469 | 470 | * @param {Event} event |
470 | 471 | * @return void |
471 | 472 | */ |
472 | | -iutil.hold.onTouchMove = function(event) { |
473 | | - if (iutil.hold.timeoutId != -1) { |
474 | | - window.clearTimeout(iutil.hold.timeoutId); |
475 | | - iutil.hold.timeoutId = -1; |
| 473 | +iutil.sliding.onTouchMove = function(event) { |
| 474 | + var x, y; |
| 475 | + |
| 476 | + event = event || window.event; |
| 477 | + |
| 478 | + //iutil.stopEvent(event); |
| 479 | + |
| 480 | + if (event.targetTouches) { |
| 481 | + if (!event.targetTouches.length) { |
| 482 | + return; |
| 483 | + } |
| 484 | + x = event.targetTouches[0].pageX; |
| 485 | + y = event.targetTouches[0].pageY; |
| 486 | + } else { |
| 487 | + x = iutil.getPageX(event); |
| 488 | + y = iutil.getPageY(event); |
476 | 489 | } |
| 490 | + |
| 491 | + iutil.sliding.endX = x; |
| 492 | + iutil.sliding.endY = y; |
477 | 493 | }; |
478 | 494 |
|
479 | 495 | // }}} |
480 | | -// {{{ hold.onClick() |
| 496 | +// {{{ sliding.onTouchEnd() |
481 | 497 |
|
482 | 498 | /** |
483 | | - * リンクホールド・クリック時のイベントハンドラ |
| 499 | + * リンクスライド・リリース時のイベントハンドラ |
484 | 500 | * ダイアログが表示されたならクリックをキャンセルする |
485 | 501 | * |
486 | 502 | * @param {Event} event |
487 | 503 | * @return void |
488 | 504 | */ |
489 | | -iutil.hold.onClick = function(event) { |
490 | | - if (iutil.hold.timeoutId != -1) { |
491 | | - window.clearTimeout(iutil.hold.timeoutId); |
492 | | - iutil.hold.timeoutId = -1; |
493 | | - return true; |
494 | | - } else { |
495 | | - iutil.stopEvent(event || window.event); |
| 505 | +iutil.sliding.onTouchEnd = function(event) { |
| 506 | + event = event || window.event; |
| 507 | + |
| 508 | + if (Math.abs(iutil.sliding.endX - iutil.sliding.startX) > 160 && |
| 509 | + Math.abs(iutil.sliding.endY - iutil.sliding.startY) < 16 && |
| 510 | + event.timeStamp < iutil.sliding.start + 1000) |
| 511 | + { |
| 512 | + iutil.sliding.showDialog(iutil.sliding.target, iutil.sliding.startX, iutil.sliding.startY); |
| 513 | + iutil.stopEvent(event); |
496 | 514 | return false; |
| 515 | + } else { |
| 516 | + return true; |
497 | 517 | } |
498 | 518 | }; |
499 | 519 |
|
500 | 520 | // }}} |
501 | | -// {{{ hold.showDialog() |
| 521 | +// {{{ sliding.showDialog() |
502 | 522 |
|
503 | 523 | /** |
504 | 524 | * ダイアログを表示する |
@@ -508,36 +528,36 @@ iutil.hold.onClick = function(event) { |
508 | 528 | * @param {Number} y |
509 | 529 | * @return void |
510 | 530 | */ |
511 | | -iutil.hold.showDialog = function(anchor, x, y) { |
512 | | - var hold, dialog, div, text, button, m, p, left; |
| 531 | +iutil.sliding.showDialog = function(anchor, x, y) { |
| 532 | + var sliding, dialog, div, text, button, m, p, left; |
513 | 533 |
|
514 | | - hold = iutil.hold; |
515 | | - hold.timeoutId = -1; |
516 | | - hold.anchor = anchor; |
517 | | - hold.href = anchor.getAttribute('href'); |
518 | | - hold.uri = anchor.href; |
| 534 | + sliding = iutil.sliding; |
| 535 | + sliding.timeoutId = -1; |
| 536 | + sliding.anchor = anchor; |
| 537 | + sliding.href = anchor.getAttribute('href'); |
| 538 | + sliding.uri = anchor.href; |
519 | 539 |
|
520 | | - hold.hideDialog(); |
| 540 | + sliding.hideDialog(); |
521 | 541 |
|
522 | 542 | m = iutil.internalLinkPattern.exec(anchor.getAttribute('href')); |
523 | 543 | if (m === null) { |
524 | 544 | return; |
525 | 545 | } |
526 | 546 |
|
527 | | - hold.query = hold.href.substring(m[0].length); |
528 | | - p = hold.query.indexOf('#'); |
| 547 | + sliding.query = sliding.href.substring(m[0].length); |
| 548 | + p = sliding.query.indexOf('#'); |
529 | 549 | if (p !== -1) { |
530 | | - hold.query = hold.query.substring(0, p); |
| 550 | + sliding.query = sliding.query.substring(0, p); |
531 | 551 | } |
532 | 552 |
|
533 | 553 | // 特定のコールバック関数があるとき |
534 | | - if (typeof hold.callbacks[m[1]] === 'function') { |
535 | | - hold.callbacks[m[1]](anchor, event); |
| 554 | + if (typeof sliding.callbacks[m[1]] === 'function') { |
| 555 | + sliding.callbacks[m[1]](anchor, event); |
536 | 556 | return; |
537 | 557 | } |
538 | 558 |
|
539 | 559 | // デフォルトのダイアログを表示する |
540 | | - if (typeof hold.dialogs._default === 'undefined') { |
| 560 | + if (typeof sliding.dialogs._default === 'undefined') { |
541 | 561 | dialog = document.createElement('div'); |
542 | 562 | dialog.className = 'popup-dialog'; |
543 | 563 |
|
@@ -553,26 +573,26 @@ iutil.hold.showDialog = function(anchor, x, y) { |
553 | 573 | button = div.appendChild(document.createElement('input')); |
554 | 574 | button.setAttribute('type', 'button'); |
555 | 575 | button.value = 'リンクを開く'; |
556 | | - button.onclick = hold.openUri; |
| 576 | + button.onclick = sliding.openUri; |
557 | 577 |
|
558 | 578 | div.appendChild(document.createTextNode('\u3000')); |
559 | 579 |
|
560 | 580 | button = div.appendChild(document.createElement('input')); |
561 | 581 | button.setAttribute('type', 'button'); |
562 | 582 | button.value = 'タブで開く'; |
563 | | - button.onclick = hold.openUriInTab; |
| 583 | + button.onclick = sliding.openUriInTab; |
564 | 584 |
|
565 | 585 | // 「閉じる」ボタン |
566 | 586 | button = dialog.appendChild(document.createElement('img')); |
567 | 587 | button.className = 'close-button'; |
568 | 588 | button.setAttribute('src', 'img/iphone/close.png'); |
569 | | - button.onclick = hold.hideDialog; |
| 589 | + button.onclick = sliding.hideDialog; |
570 | 590 |
|
571 | | - hold.dialogs._default = document.body.appendChild(dialog); |
| 591 | + sliding.dialogs._default = document.body.appendChild(dialog); |
572 | 592 | } else { |
573 | | - dialog = hold.dialogs._default; |
| 593 | + dialog = sliding.dialogs._default; |
574 | 594 | } |
575 | | - hold.setActiveDialog(dialog); |
| 595 | + sliding.setActiveDialog(dialog); |
576 | 596 |
|
577 | 597 | text = dialog.firstChild.firstChild; |
578 | 598 | text.nodeValue = iutil.getTextNodes(anchor, true).join('').replace(/\s+/g, ' ') |
@@ -585,80 +605,81 @@ iutil.hold.showDialog = function(anchor, x, y) { |
585 | 605 | }; |
586 | 606 |
|
587 | 607 | // }}} |
588 | | -// {{{ hold.hideDialog() |
| 608 | +// {{{ sliding.hideDialog() |
589 | 609 |
|
590 | 610 | /** |
591 | 611 | * アクティブなダイアログを隠す |
592 | 612 | * |
593 | 613 | * @param void |
594 | 614 | * @return void |
595 | 615 | */ |
596 | | -iutil.hold.hideDialog = function() { |
597 | | - if (iutil.hold.dialog) { |
598 | | - iutil.hold.dialog.style.display = 'none'; |
599 | | - iutil.hold.setActiveDialog(null); |
| 616 | +iutil.sliding.hideDialog = function() { |
| 617 | + if (iutil.sliding.dialog) { |
| 618 | + iutil.sliding.dialog.style.display = 'none'; |
| 619 | + iutil.sliding.setActiveDialog(null); |
600 | 620 | } |
601 | 621 | }; |
602 | 622 |
|
603 | 623 | // }}} |
604 | | -// {{{ hold.setActiveDialog() |
| 624 | +// {{{ sliding.setActiveDialog() |
605 | 625 |
|
606 | 626 | /** |
607 | 627 | * アクティブなダイアログを設定する |
608 | 628 | * |
609 | 629 | * @param {Element|null} element |
610 | 630 | * @return void |
611 | 631 | */ |
612 | | -iutil.hold.setActiveDialog = function(element) { |
613 | | - iutil.hold.dialog = element; |
| 632 | +iutil.sliding.setActiveDialog = function(element) { |
| 633 | + iutil.sliding.dialog = element; |
614 | 634 | }; |
615 | 635 |
|
616 | 636 | // }}} |
617 | | -// {{{ hold.openUri() |
| 637 | +// {{{ sliding.openUri() |
618 | 638 |
|
619 | 639 | /** |
620 | 640 | * リンクを開く |
621 | 641 | * |
622 | 642 | * @param void |
623 | 643 | * @return void |
624 | 644 | */ |
625 | | -iutil.hold.openUri = function() { |
626 | | - window.location.href = iutil.hold.uri; |
| 645 | +iutil.sliding.openUri = function() { |
| 646 | + window.location.href = iutil.sliding.uri; |
627 | 647 | }; |
628 | 648 |
|
629 | 649 | // }}} |
630 | | -// {{{ hold.openUriInTab() |
| 650 | +// {{{ sliding.openUriInTab() |
631 | 651 |
|
632 | 652 | /** |
633 | 653 | * 新しいタブでリンクを開く |
634 | 654 | * |
635 | 655 | * @param void |
636 | 656 | * @return void |
637 | 657 | */ |
638 | | -iutil.hold.openUriInTab = function() { |
639 | | - window.open(iutil.hold.uri, null); |
| 658 | +iutil.sliding.openUriInTab = function() { |
| 659 | + window.open(iutil.sliding.uri, null); |
640 | 660 | }; |
641 | 661 |
|
642 | 662 | // }}} |
643 | | -// {{{ hold.bind() |
| 663 | +// {{{ sliding.bind() |
644 | 664 |
|
645 | 665 | /** |
646 | | - * リンクにホールドイベントハンドラを登録する |
| 666 | + * リンクにスライドイベントハンドラを登録する |
647 | 667 | * |
648 | 668 | * @param {Element} anchor |
649 | 669 | * @return void |
650 | 670 | */ |
651 | 671 | if (iutil.iphone) { |
652 | | - iutil.hold.bind = function(anchor) { |
653 | | - anchor.addEventListener('touchstart', iutil.hold.onTouchStart, false); |
654 | | - anchor.addEventListener('touchmove', iutil.hold.onTouchMove, false); |
655 | | - anchor.addEventListener('click', iutil.hold.onClick, false); |
| 672 | + iutil.sliding.bind = function(anchor) { |
| 673 | + anchor.addEventListener('touchstart', iutil.sliding.onTouchStart, false); |
| 674 | + anchor.addEventListener('touchmove', iutil.sliding.onTouchMove, false); |
| 675 | + anchor.addEventListener('touchend', iutil.sliding.onTouchEnd, false); |
| 676 | + anchor.addEventListener('touchcancel', iutil.sliding.onTouchEnd, false); |
656 | 677 | }; |
657 | 678 | } else { |
658 | | - iutil.hold.bind = function(anchor) { |
659 | | - anchor.addEventListener('mousedown', iutil.hold.onTouchStart, false); |
660 | | - anchor.addEventListener('drag', iutil.hold.onTouchMove, false); |
661 | | - anchor.addEventListener('click', iutil.hold.onClick, false); |
| 679 | + iutil.sliding.bind = function(anchor) { |
| 680 | + anchor.addEventListener('mousedown', iutil.sliding.onTouchStart, false); |
| 681 | + anchor.addEventListener('drag', iutil.sliding.onTouchMove, false); |
| 682 | + anchor.addEventListener('mosueup', iutil.sliding.onTouchEnd, false); |
662 | 683 | }; |
663 | 684 | } |
664 | 685 |
|
|