LEFT | RIGHT |
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved. | 1 // Copyright 2006 The Closure Library Authors. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS-IS" BASIS, | 10 // distributed under the License is distributed on an "AS-IS" BASIS, |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 * @param {boolean} enabled Whether dragger is enabled. | 362 * @param {boolean} enabled Whether dragger is enabled. |
363 */ | 363 */ |
364 goog.fx.Dragger.prototype.setEnabled = function(enabled) { | 364 goog.fx.Dragger.prototype.setEnabled = function(enabled) { |
365 this.enabled_ = enabled; | 365 this.enabled_ = enabled; |
366 }; | 366 }; |
367 | 367 |
368 | 368 |
369 /** @override */ | 369 /** @override */ |
370 goog.fx.Dragger.prototype.disposeInternal = function() { | 370 goog.fx.Dragger.prototype.disposeInternal = function() { |
371 goog.fx.Dragger.superClass_.disposeInternal.call(this); | 371 goog.fx.Dragger.superClass_.disposeInternal.call(this); |
372 | |
373 goog.events.unlisten(this.handle, | 372 goog.events.unlisten(this.handle, |
374 [goog.events.EventType.TOUCHSTART, goog.events.EventType.MOUSEDOWN], | 373 [goog.events.EventType.TOUCHSTART, goog.events.EventType.MOUSEDOWN], |
375 this.startDrag, false, this); | 374 this.startDrag, false, this); |
376 this.eventHandler_.dispose(); | 375 this.cleanUpAfterDragging_(); |
377 | 376 |
378 delete this.target; | 377 this.target = null; |
379 delete this.handle; | 378 this.handle = null; |
380 delete this.eventHandler_; | 379 this.eventHandler_ = null; |
381 }; | 380 }; |
382 | 381 |
383 | 382 |
384 /** | 383 /** |
385 * Whether the DOM element being manipulated is rendered right-to-left. | 384 * Whether the DOM element being manipulated is rendered right-to-left. |
386 * @return {boolean} True if the DOM element is rendered right-to-left, false | 385 * @return {boolean} True if the DOM element is rendered right-to-left, false |
387 * otherwise. | 386 * otherwise. |
388 * @private | 387 * @private |
389 */ | 388 */ |
390 goog.fx.Dragger.prototype.isRightToLeft_ = function() { | 389 goog.fx.Dragger.prototype.isRightToLeft_ = function() { |
(...skipping 12 matching lines...) Expand all Loading... |
403 var isMouseDown = e.type == goog.events.EventType.MOUSEDOWN; | 402 var isMouseDown = e.type == goog.events.EventType.MOUSEDOWN; |
404 | 403 |
405 // Dragger.startDrag() can be called by AbstractDragDrop with a mousemove | 404 // Dragger.startDrag() can be called by AbstractDragDrop with a mousemove |
406 // event and IE does not report pressed mouse buttons on mousemove. Also, | 405 // event and IE does not report pressed mouse buttons on mousemove. Also, |
407 // it does not make sense to check for the button if the user is already | 406 // it does not make sense to check for the button if the user is already |
408 // dragging. | 407 // dragging. |
409 | 408 |
410 if (this.enabled_ && !this.dragging_ && | 409 if (this.enabled_ && !this.dragging_ && |
411 (!isMouseDown || e.isMouseActionButton())) { | 410 (!isMouseDown || e.isMouseActionButton())) { |
412 if (this.hysteresisDistanceSquared_ == 0) { | 411 if (this.hysteresisDistanceSquared_ == 0) { |
413 this.initializeDrag_(e); | 412 if (this.fireDragStart_(e)) { |
414 if (this.dragging_) { | 413 this.dragging_ = true; |
415 e.preventDefault(); | 414 e.preventDefault(); |
416 } else { | 415 } else { |
417 // If the start drag is cancelled, don't setup for a drag. | 416 // If the start drag is cancelled, don't setup for a drag. |
418 return; | 417 return; |
419 } | 418 } |
420 } else { | 419 } else { |
421 // Need to preventDefault for hysteresis to prevent page getting selected. | 420 // Need to preventDefault for hysteresis to prevent page getting selected. |
422 e.preventDefault(); | 421 e.preventDefault(); |
423 } | 422 } |
424 this.setupDragHandlers(); | 423 this.setupDragHandlers(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 } | 478 } |
480 | 479 |
481 if (this.scrollTarget_) { | 480 if (this.scrollTarget_) { |
482 this.eventHandler_.listen(this.scrollTarget_, goog.events.EventType.SCROLL, | 481 this.eventHandler_.listen(this.scrollTarget_, goog.events.EventType.SCROLL, |
483 this.onScroll_, useCapture); | 482 this.onScroll_, useCapture); |
484 } | 483 } |
485 }; | 484 }; |
486 | 485 |
487 | 486 |
488 /** | 487 /** |
489 * Event handler that is used to start the drag | 488 * Fires a goog.fx.Dragger.EventType.START event. |
490 * @param {goog.events.BrowserEvent|goog.events.Event} e Event object. | 489 * @param {goog.events.BrowserEvent} e Browser event that triggered the drag. |
491 * @private | 490 * @return {boolean} False iff preventDefault was called on the DragEvent. |
492 */ | 491 * @private |
493 goog.fx.Dragger.prototype.initializeDrag_ = function(e) { | 492 */ |
494 var rv = this.dispatchEvent(new goog.fx.DragEvent( | 493 goog.fx.Dragger.prototype.fireDragStart_ = function(e) { |
495 goog.fx.Dragger.EventType.START, this, e.clientX, e.clientY, | 494 return this.dispatchEvent(new goog.fx.DragEvent( |
496 /** @type {goog.events.BrowserEvent} */(e))); | 495 goog.fx.Dragger.EventType.START, this, e.clientX, e.clientY, e)); |
497 if (rv !== false) { | 496 }; |
498 this.dragging_ = true; | 497 |
499 } | 498 |
500 }; | 499 /** |
501 | 500 * Unregisters the event handlers that are only active during dragging, and |
502 | 501 * releases mouse capture. |
503 /** | 502 * @private |
504 * Event handler that is used to end the drag | 503 */ |
| 504 goog.fx.Dragger.prototype.cleanUpAfterDragging_ = function() { |
| 505 this.eventHandler_.removeAll(); |
| 506 if (goog.fx.Dragger.HAS_SET_CAPTURE_) { |
| 507 this.document_.releaseCapture(); |
| 508 } |
| 509 }; |
| 510 |
| 511 |
| 512 /** |
| 513 * Event handler that is used to end the drag. |
505 * @param {goog.events.BrowserEvent} e Event object. | 514 * @param {goog.events.BrowserEvent} e Event object. |
506 * @param {boolean=} opt_dragCanceled Whether the drag has been canceled. | 515 * @param {boolean=} opt_dragCanceled Whether the drag has been canceled. |
507 */ | 516 */ |
508 goog.fx.Dragger.prototype.endDrag = function(e, opt_dragCanceled) { | 517 goog.fx.Dragger.prototype.endDrag = function(e, opt_dragCanceled) { |
509 this.eventHandler_.removeAll(); | 518 this.cleanUpAfterDragging_(); |
510 | |
511 if (goog.fx.Dragger.HAS_SET_CAPTURE_) { | |
512 this.document_.releaseCapture(); | |
513 } | |
514 | |
515 var x = this.limitX(this.deltaX); | |
516 var y = this.limitY(this.deltaY); | |
517 | 519 |
518 if (this.dragging_) { | 520 if (this.dragging_) { |
519 this.dragging_ = false; | 521 this.dragging_ = false; |
520 | 522 |
521 var dragCancelled = opt_dragCanceled || | 523 var x = this.limitX(this.deltaX); |
522 e.type == goog.events.EventType.TOUCHCANCEL; | 524 var y = this.limitY(this.deltaY); |
| 525 var dragCanceled = opt_dragCanceled || |
| 526 e.type == goog.events.EventType.TOUCHCANCEL; |
523 this.dispatchEvent(new goog.fx.DragEvent( | 527 this.dispatchEvent(new goog.fx.DragEvent( |
524 goog.fx.Dragger.EventType.END, this, e.clientX, e.clientY, e, x, y, | 528 goog.fx.Dragger.EventType.END, this, e.clientX, e.clientY, e, x, y, |
525 dragCancelled)); | 529 dragCanceled)); |
526 } else { | 530 } else { |
527 this.dispatchEvent(goog.fx.Dragger.EventType.EARLY_CANCEL); | 531 this.dispatchEvent(goog.fx.Dragger.EventType.EARLY_CANCEL); |
528 } | 532 } |
529 | 533 |
530 // Call preventDefault to prevent mouseup from being raised if this is a | 534 // Call preventDefault to prevent mouseup from being raised if this is a |
531 // touchend event. | 535 // touchend event. |
532 if (e.type == goog.events.EventType.TOUCHEND || | 536 if (e.type == goog.events.EventType.TOUCHEND || |
533 e.type == goog.events.EventType.TOUCHCANCEL) { | 537 e.type == goog.events.EventType.TOUCHCANCEL) { |
534 e.preventDefault(); | 538 e.preventDefault(); |
535 } | 539 } |
(...skipping 24 matching lines...) Expand all Loading... |
560 this.clientX = e.clientX; | 564 this.clientX = e.clientX; |
561 this.clientY = e.clientY; | 565 this.clientY = e.clientY; |
562 this.screenX = e.screenX; | 566 this.screenX = e.screenX; |
563 this.screenY = e.screenY; | 567 this.screenY = e.screenY; |
564 | 568 |
565 if (!this.dragging_) { | 569 if (!this.dragging_) { |
566 var diffX = this.startX - this.clientX; | 570 var diffX = this.startX - this.clientX; |
567 var diffY = this.startY - this.clientY; | 571 var diffY = this.startY - this.clientY; |
568 var distance = diffX * diffX + diffY * diffY; | 572 var distance = diffX * diffX + diffY * diffY; |
569 if (distance > this.hysteresisDistanceSquared_) { | 573 if (distance > this.hysteresisDistanceSquared_) { |
570 this.initializeDrag_(e); | 574 if (this.fireDragStart_(e)) { |
571 if (!this.dragging_) { | 575 this.dragging_ = true; |
572 // If the start drag is cancelled, stop trying to drag. | 576 } else { |
573 this.endDrag(e); | 577 // DragListGroup disposes of the dragger if BEFOREDRAGSTART is |
| 578 // canceled. |
| 579 if (!this.isDisposed()) { |
| 580 this.endDrag(e); |
| 581 } |
574 return; | 582 return; |
575 } | 583 } |
576 } | 584 } |
577 } | 585 } |
578 | 586 |
579 var pos = this.calculatePosition_(dx, dy); | 587 var pos = this.calculatePosition_(dx, dy); |
580 var x = pos.x; | 588 var x = pos.x; |
581 var y = pos.y; | 589 var y = pos.y; |
582 | 590 |
583 if (this.dragging_) { | 591 if (this.dragging_) { |
584 | 592 |
585 var rv = this.dispatchEvent(new goog.fx.DragEvent( | 593 var rv = this.dispatchEvent(new goog.fx.DragEvent( |
586 goog.fx.Dragger.EventType.BEFOREDRAG, this, e.clientX, e.clientY, | 594 goog.fx.Dragger.EventType.BEFOREDRAG, this, e.clientX, e.clientY, |
587 e, x, y)); | 595 e, x, y)); |
588 | 596 |
589 // Only do the defaultAction and dispatch drag event if predrag didn't | 597 // Only do the defaultAction and dispatch drag event if predrag didn't |
590 // prevent default | 598 // prevent default |
591 if (rv !== false) { | 599 if (rv) { |
592 this.doDrag(e, x, y, false); | 600 this.doDrag(e, x, y, false); |
593 e.preventDefault(); | 601 e.preventDefault(); |
594 } | 602 } |
595 } | 603 } |
596 } | 604 } |
597 }; | 605 }; |
598 | 606 |
599 | 607 |
600 /** | 608 /** |
601 * Calculates the drag position. | 609 * Calculates the drag position. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 * is the number of pixels the target should be moved to from the right. | 699 * is the number of pixels the target should be moved to from the right. |
692 * @param {number} y Y-coordinate for target element. | 700 * @param {number} y Y-coordinate for target element. |
693 */ | 701 */ |
694 goog.fx.Dragger.prototype.defaultAction = function(x, y) { | 702 goog.fx.Dragger.prototype.defaultAction = function(x, y) { |
695 if (this.useRightPositioningForRtl_ && this.isRightToLeft_()) { | 703 if (this.useRightPositioningForRtl_ && this.isRightToLeft_()) { |
696 this.target.style.right = x + 'px'; | 704 this.target.style.right = x + 'px'; |
697 } else { | 705 } else { |
698 this.target.style.left = x + 'px'; | 706 this.target.style.left = x + 'px'; |
699 } | 707 } |
700 this.target.style.top = y + 'px'; | 708 this.target.style.top = y + 'px'; |
| 709 }; |
| 710 |
| 711 |
| 712 /** |
| 713 * @return {boolean} Whether the dragger is currently in the midst of a drag. |
| 714 */ |
| 715 goog.fx.Dragger.prototype.isDragging = function() { |
| 716 return this.dragging_; |
701 }; | 717 }; |
702 | 718 |
703 | 719 |
704 | 720 |
705 /** | 721 /** |
706 * Object representing a drag event | 722 * Object representing a drag event |
707 * @param {string} type Event type. | 723 * @param {string} type Event type. |
708 * @param {goog.fx.Dragger} dragobj Drag object initiating event. | 724 * @param {goog.fx.Dragger} dragobj Drag object initiating event. |
709 * @param {number} clientX X-coordinate relative to the viewport. | 725 * @param {number} clientX X-coordinate relative to the viewport. |
710 * @param {number} clientY Y-coordinate relative to the viewport. | 726 * @param {number} clientY Y-coordinate relative to the viewport. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 * event on FireFox when user drags the mouse out of the window, 2) with | 780 * event on FireFox when user drags the mouse out of the window, 2) with |
765 * drag END event on IE7 which is generated on MOUSEMOVE event when user | 781 * drag END event on IE7 which is generated on MOUSEMOVE event when user |
766 * moves the mouse into the document after the mouse button has been | 782 * moves the mouse into the document after the mouse button has been |
767 * released, 3) when TOUCHCANCEL is raised instead of TOUCHEND (on touch | 783 * released, 3) when TOUCHCANCEL is raised instead of TOUCHEND (on touch |
768 * events). | 784 * events). |
769 * @type {boolean} | 785 * @type {boolean} |
770 */ | 786 */ |
771 this.dragCanceled = !!opt_dragCanceled; | 787 this.dragCanceled = !!opt_dragCanceled; |
772 }; | 788 }; |
773 goog.inherits(goog.fx.DragEvent, goog.events.Event); | 789 goog.inherits(goog.fx.DragEvent, goog.events.Event); |
LEFT | RIGHT |