Path: blob/trunk/third_party/closure/goog/fx/dragger.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Drag Utilities.16*17* Provides extensible functionality for drag & drop behaviour.18*19* @see ../demos/drag.html20* @see ../demos/dragger.html21*/222324goog.provide('goog.fx.DragEvent');25goog.provide('goog.fx.Dragger');26goog.provide('goog.fx.Dragger.EventType');2728goog.require('goog.dom');29goog.require('goog.dom.TagName');30goog.require('goog.events');31goog.require('goog.events.Event');32goog.require('goog.events.EventHandler');33goog.require('goog.events.EventTarget');34goog.require('goog.events.EventType');35goog.require('goog.math.Coordinate');36goog.require('goog.math.Rect');37goog.require('goog.style');38goog.require('goog.style.bidi');39goog.require('goog.userAgent');40414243/**44* A class that allows mouse or touch-based dragging (moving) of an element45*46* @param {Element} target The element that will be dragged.47* @param {Element=} opt_handle An optional handle to control the drag, if null48* the target is used.49* @param {goog.math.Rect=} opt_limits Object containing left, top, width,50* and height.51*52* @extends {goog.events.EventTarget}53* @constructor54* @struct55*/56goog.fx.Dragger = function(target, opt_handle, opt_limits) {57goog.fx.Dragger.base(this, 'constructor');5859/**60* Reference to drag target element.61* @type {?Element}62*/63this.target = target;6465/**66* Reference to the handler that initiates the drag.67* @type {?Element}68*/69this.handle = opt_handle || target;7071/**72* Object representing the limits of the drag region.73* @type {goog.math.Rect}74*/75this.limits = opt_limits || new goog.math.Rect(NaN, NaN, NaN, NaN);7677/**78* Reference to a document object to use for the events.79* @private {Document}80*/81this.document_ = goog.dom.getOwnerDocument(target);8283/** @private {!goog.events.EventHandler} */84this.eventHandler_ = new goog.events.EventHandler(this);85this.registerDisposable(this.eventHandler_);8687/**88* Whether the element is rendered right-to-left. We initialize this lazily.89* @private {boolean|undefined}}90*/91this.rightToLeft_;9293/**94* Current x position of mouse or touch relative to viewport.95* @type {number}96*/97this.clientX = 0;9899/**100* Current y position of mouse or touch relative to viewport.101* @type {number}102*/103this.clientY = 0;104105/**106* Current x position of mouse or touch relative to screen. Deprecated because107* it doesn't take into affect zoom level or pixel density.108* @type {number}109* @deprecated Consider switching to clientX instead.110*/111this.screenX = 0;112113/**114* Current y position of mouse or touch relative to screen. Deprecated because115* it doesn't take into affect zoom level or pixel density.116* @type {number}117* @deprecated Consider switching to clientY instead.118*/119this.screenY = 0;120121/**122* The x position where the first mousedown or touchstart occurred.123* @type {number}124*/125this.startX = 0;126127/**128* The y position where the first mousedown or touchstart occurred.129* @type {number}130*/131this.startY = 0;132133/**134* Current x position of drag relative to target's parent.135* @type {number}136*/137this.deltaX = 0;138139/**140* Current y position of drag relative to target's parent.141* @type {number}142*/143this.deltaY = 0;144145/**146* The current page scroll value.147* @type {?goog.math.Coordinate}148*/149this.pageScroll;150151/**152* Whether dragging is currently enabled.153* @private {boolean}154*/155this.enabled_ = true;156157/**158* Whether object is currently being dragged.159* @private {boolean}160*/161this.dragging_ = false;162163/**164* Whether mousedown should be default prevented.165* @private {boolean}166**/167this.preventMouseDown_ = true;168169/**170* The amount of distance, in pixels, after which a mousedown or touchstart is171* considered a drag.172* @private {number}173*/174this.hysteresisDistanceSquared_ = 0;175176/**177* The SCROLL event target used to make drag element follow scrolling.178* @private {?EventTarget}179*/180this.scrollTarget_;181182/**183* Whether IE drag events cancelling is on.184* @private {boolean}185*/186this.ieDragStartCancellingOn_ = false;187188/**189* Whether the dragger implements the changes described in http://b/6324964,190* making it truly RTL. This is a temporary flag to allow clients to191* transition to the new behavior at their convenience. At some point it will192* be the default.193* @private {boolean}194*/195this.useRightPositioningForRtl_ = false;196197// Add listener. Do not use the event handler here since the event handler is198// used for listeners added and removed during the drag operation.199goog.events.listen(200this.handle,201[goog.events.EventType.TOUCHSTART, goog.events.EventType.MOUSEDOWN],202this.startDrag, false, this);203};204goog.inherits(goog.fx.Dragger, goog.events.EventTarget);205// Dragger is meant to be extended, but defines most properties on its206// prototype, thus making it unsuitable for sealing.207goog.tagUnsealableClass(goog.fx.Dragger);208209210/**211* Whether setCapture is supported by the browser.212* IE and Gecko after 1.9.3 have setCapture. MS Edge and WebKit213* (https://bugs.webkit.org/show_bug.cgi?id=27330) don't.214* @type {boolean}215* @private216*/217goog.fx.Dragger.HAS_SET_CAPTURE_ = goog.global.document &&218goog.global.document.documentElement &&219!!goog.global.document.documentElement.setCapture &&220!!goog.global.document.releaseCapture;221222223/**224* Creates copy of node being dragged. This is a utility function to be used225* wherever it is inappropriate for the original source to follow the mouse226* cursor itself.227*228* @param {Element} sourceEl Element to copy.229* @return {!Element} The clone of {@code sourceEl}.230*/231goog.fx.Dragger.cloneNode = function(sourceEl) {232var clonedEl = sourceEl.cloneNode(true),233origTexts =234goog.dom.getElementsByTagName(goog.dom.TagName.TEXTAREA, sourceEl),235dragTexts =236goog.dom.getElementsByTagName(goog.dom.TagName.TEXTAREA, clonedEl);237// Cloning does not copy the current value of textarea elements, so correct238// this manually.239for (var i = 0; i < origTexts.length; i++) {240dragTexts[i].value = origTexts[i].value;241}242switch (sourceEl.tagName) {243case String(goog.dom.TagName.TR):244return goog.dom.createDom(245goog.dom.TagName.TABLE, null,246goog.dom.createDom(goog.dom.TagName.TBODY, null, clonedEl));247case String(goog.dom.TagName.TD):248case String(goog.dom.TagName.TH):249return goog.dom.createDom(250goog.dom.TagName.TABLE, null,251goog.dom.createDom(252goog.dom.TagName.TBODY, null,253goog.dom.createDom(goog.dom.TagName.TR, null, clonedEl)));254case String(goog.dom.TagName.TEXTAREA):255clonedEl.value = sourceEl.value;256default:257return clonedEl;258}259};260261262/**263* Constants for event names.264* @enum {string}265*/266goog.fx.Dragger.EventType = {267// The drag action was canceled before the START event. Possible reasons:268// disabled dragger, dragging with the right mouse button or releasing the269// button before reaching the hysteresis distance.270EARLY_CANCEL: 'earlycancel',271START: 'start',272BEFOREDRAG: 'beforedrag',273DRAG: 'drag',274END: 'end'275};276277278/**279* Turns on/off true RTL behavior. This should be called immediately after280* construction. This is a temporary flag to allow clients to transition281* to the new component at their convenience. At some point true will be the282* default.283* @param {boolean} useRightPositioningForRtl True if "right" should be used for284* positioning, false if "left" should be used for positioning.285*/286goog.fx.Dragger.prototype.enableRightPositioningForRtl = function(287useRightPositioningForRtl) {288this.useRightPositioningForRtl_ = useRightPositioningForRtl;289};290291292/**293* Returns the event handler, intended for subclass use.294* @return {!goog.events.EventHandler<T>} The event handler.295* @this {T}296* @template T297*/298goog.fx.Dragger.prototype.getHandler = function() {299// TODO(user): templated "this" values currently result in "this" being300// "unknown" in the body of the function.301var self = /** @type {goog.fx.Dragger} */ (this);302return self.eventHandler_;303};304305306/**307* Sets (or reset) the Drag limits after a Dragger is created.308* @param {goog.math.Rect?} limits Object containing left, top, width,309* height for new Dragger limits. If target is right-to-left and310* enableRightPositioningForRtl(true) is called, then rect is interpreted as311* right, top, width, and height.312*/313goog.fx.Dragger.prototype.setLimits = function(limits) {314this.limits = limits || new goog.math.Rect(NaN, NaN, NaN, NaN);315};316317318/**319* Sets the distance the user has to drag the element before a drag operation is320* started.321* @param {number} distance The number of pixels after which a mousedown and322* move is considered a drag.323*/324goog.fx.Dragger.prototype.setHysteresis = function(distance) {325this.hysteresisDistanceSquared_ = Math.pow(distance, 2);326};327328329/**330* Gets the distance the user has to drag the element before a drag operation is331* started.332* @return {number} distance The number of pixels after which a mousedown and333* move is considered a drag.334*/335goog.fx.Dragger.prototype.getHysteresis = function() {336return Math.sqrt(this.hysteresisDistanceSquared_);337};338339340/**341* Sets the SCROLL event target to make drag element follow scrolling.342*343* @param {EventTarget} scrollTarget The event target that dispatches SCROLL344* events.345*/346goog.fx.Dragger.prototype.setScrollTarget = function(scrollTarget) {347this.scrollTarget_ = scrollTarget;348};349350351/**352* Enables cancelling of built-in IE drag events.353* @param {boolean} cancelIeDragStart Whether to enable cancelling of IE354* dragstart event.355*/356goog.fx.Dragger.prototype.setCancelIeDragStart = function(cancelIeDragStart) {357this.ieDragStartCancellingOn_ = cancelIeDragStart;358};359360361/**362* @return {boolean} Whether the dragger is enabled.363*/364goog.fx.Dragger.prototype.getEnabled = function() {365return this.enabled_;366};367368369/**370* Set whether dragger is enabled371* @param {boolean} enabled Whether dragger is enabled.372*/373goog.fx.Dragger.prototype.setEnabled = function(enabled) {374this.enabled_ = enabled;375};376377378/**379* Set whether mousedown should be default prevented.380* @param {boolean} preventMouseDown Whether mousedown should be default381* prevented.382*/383goog.fx.Dragger.prototype.setPreventMouseDown = function(preventMouseDown) {384this.preventMouseDown_ = preventMouseDown;385};386387388/** @override */389goog.fx.Dragger.prototype.disposeInternal = function() {390goog.fx.Dragger.superClass_.disposeInternal.call(this);391goog.events.unlisten(392this.handle,393[goog.events.EventType.TOUCHSTART, goog.events.EventType.MOUSEDOWN],394this.startDrag, false, this);395this.cleanUpAfterDragging_();396397this.target = null;398this.handle = null;399};400401402/**403* Whether the DOM element being manipulated is rendered right-to-left.404* @return {boolean} True if the DOM element is rendered right-to-left, false405* otherwise.406* @private407*/408goog.fx.Dragger.prototype.isRightToLeft_ = function() {409if (!goog.isDef(this.rightToLeft_)) {410this.rightToLeft_ = goog.style.isRightToLeft(this.target);411}412return this.rightToLeft_;413};414415416/**417* Event handler that is used to start the drag418* @param {goog.events.BrowserEvent} e Event object.419*/420goog.fx.Dragger.prototype.startDrag = function(e) {421var isMouseDown = e.type == goog.events.EventType.MOUSEDOWN;422423// Dragger.startDrag() can be called by AbstractDragDrop with a mousemove424// event and IE does not report pressed mouse buttons on mousemove. Also,425// it does not make sense to check for the button if the user is already426// dragging.427428if (this.enabled_ && !this.dragging_ &&429(!isMouseDown || e.isMouseActionButton())) {430if (this.hysteresisDistanceSquared_ == 0) {431if (this.fireDragStart_(e)) {432this.dragging_ = true;433if (this.preventMouseDown_ && isMouseDown) {434e.preventDefault();435}436} else {437// If the start drag is cancelled, don't setup for a drag.438return;439}440} else if (this.preventMouseDown_ && isMouseDown) {441// Need to preventDefault for hysteresis to prevent page getting selected.442e.preventDefault();443}444this.setupDragHandlers();445446this.clientX = this.startX = e.clientX;447this.clientY = this.startY = e.clientY;448this.screenX = e.screenX;449this.screenY = e.screenY;450this.computeInitialPosition();451this.pageScroll = goog.dom.getDomHelper(this.document_).getDocumentScroll();452} else {453this.dispatchEvent(goog.fx.Dragger.EventType.EARLY_CANCEL);454}455};456457458/**459* Sets up event handlers when dragging starts.460* @protected461*/462goog.fx.Dragger.prototype.setupDragHandlers = function() {463var doc = this.document_;464var docEl = doc.documentElement;465// Use bubbling when we have setCapture since we got reports that IE has466// problems with the capturing events in combination with setCapture.467var useCapture = !goog.fx.Dragger.HAS_SET_CAPTURE_;468469this.eventHandler_.listen(470doc, [goog.events.EventType.TOUCHMOVE, goog.events.EventType.MOUSEMOVE],471this.handleMove_, useCapture);472this.eventHandler_.listen(473doc, [goog.events.EventType.TOUCHEND, goog.events.EventType.MOUSEUP],474this.endDrag, useCapture);475476if (goog.fx.Dragger.HAS_SET_CAPTURE_) {477docEl.setCapture(false);478this.eventHandler_.listen(479docEl, goog.events.EventType.LOSECAPTURE, this.endDrag);480} else {481// Make sure we stop the dragging if the window loses focus.482// Don't use capture in this listener because we only want to end the drag483// if the actual window loses focus. Since blur events do not bubble we use484// a bubbling listener on the window.485this.eventHandler_.listen(486goog.dom.getWindow(doc), goog.events.EventType.BLUR, this.endDrag);487}488489if (goog.userAgent.IE && this.ieDragStartCancellingOn_) {490// Cancel IE's 'ondragstart' event.491this.eventHandler_.listen(492doc, goog.events.EventType.DRAGSTART, goog.events.Event.preventDefault);493}494495if (this.scrollTarget_) {496this.eventHandler_.listen(497this.scrollTarget_, goog.events.EventType.SCROLL, this.onScroll_,498useCapture);499}500};501502503/**504* Fires a goog.fx.Dragger.EventType.START event.505* @param {goog.events.BrowserEvent} e Browser event that triggered the drag.506* @return {boolean} False iff preventDefault was called on the DragEvent.507* @private508*/509goog.fx.Dragger.prototype.fireDragStart_ = function(e) {510return this.dispatchEvent(511new goog.fx.DragEvent(512goog.fx.Dragger.EventType.START, this, e.clientX, e.clientY, e));513};514515516/**517* Unregisters the event handlers that are only active during dragging, and518* releases mouse capture.519* @private520*/521goog.fx.Dragger.prototype.cleanUpAfterDragging_ = function() {522this.eventHandler_.removeAll();523if (goog.fx.Dragger.HAS_SET_CAPTURE_) {524this.document_.releaseCapture();525}526};527528529/**530* Event handler that is used to end the drag.531* @param {goog.events.BrowserEvent} e Event object.532* @param {boolean=} opt_dragCanceled Whether the drag has been canceled.533*/534goog.fx.Dragger.prototype.endDrag = function(e, opt_dragCanceled) {535this.cleanUpAfterDragging_();536537if (this.dragging_) {538this.dragging_ = false;539540var x = this.limitX(this.deltaX);541var y = this.limitY(this.deltaY);542var dragCanceled =543opt_dragCanceled || e.type == goog.events.EventType.TOUCHCANCEL;544this.dispatchEvent(545new goog.fx.DragEvent(546goog.fx.Dragger.EventType.END, this, e.clientX, e.clientY, e, x, y,547dragCanceled));548} else {549this.dispatchEvent(goog.fx.Dragger.EventType.EARLY_CANCEL);550}551};552553554/**555* Event handler that is used to end the drag by cancelling it.556* @param {goog.events.BrowserEvent} e Event object.557*/558goog.fx.Dragger.prototype.endDragCancel = function(e) {559this.endDrag(e, true);560};561562563/**564* Event handler that is used on mouse / touch move to update the drag565* @param {goog.events.BrowserEvent} e Event object.566* @private567*/568goog.fx.Dragger.prototype.handleMove_ = function(e) {569if (this.enabled_) {570// dx in right-to-left cases is relative to the right.571var sign =572this.useRightPositioningForRtl_ && this.isRightToLeft_() ? -1 : 1;573var dx = sign * (e.clientX - this.clientX);574var dy = e.clientY - this.clientY;575this.clientX = e.clientX;576this.clientY = e.clientY;577this.screenX = e.screenX;578this.screenY = e.screenY;579580if (!this.dragging_) {581var diffX = this.startX - this.clientX;582var diffY = this.startY - this.clientY;583var distance = diffX * diffX + diffY * diffY;584if (distance > this.hysteresisDistanceSquared_) {585if (this.fireDragStart_(e)) {586this.dragging_ = true;587} else {588// DragListGroup disposes of the dragger if BEFOREDRAGSTART is589// canceled.590if (!this.isDisposed()) {591this.endDrag(e);592}593return;594}595}596}597598var pos = this.calculatePosition_(dx, dy);599var x = pos.x;600var y = pos.y;601602if (this.dragging_) {603var rv = this.dispatchEvent(604new goog.fx.DragEvent(605goog.fx.Dragger.EventType.BEFOREDRAG, this, e.clientX, e.clientY,606e, x, y));607608// Only do the defaultAction and dispatch drag event if predrag didn't609// prevent default610if (rv) {611this.doDrag(e, x, y, false);612e.preventDefault();613}614}615}616};617618619/**620* Calculates the drag position.621*622* @param {number} dx The horizontal movement delta.623* @param {number} dy The vertical movement delta.624* @return {!goog.math.Coordinate} The newly calculated drag element position.625* @private626*/627goog.fx.Dragger.prototype.calculatePosition_ = function(dx, dy) {628// Update the position for any change in body scrolling629var pageScroll = goog.dom.getDomHelper(this.document_).getDocumentScroll();630dx += pageScroll.x - this.pageScroll.x;631dy += pageScroll.y - this.pageScroll.y;632this.pageScroll = pageScroll;633634this.deltaX += dx;635this.deltaY += dy;636637var x = this.limitX(this.deltaX);638var y = this.limitY(this.deltaY);639return new goog.math.Coordinate(x, y);640};641642643/**644* Event handler for scroll target scrolling.645* @param {goog.events.BrowserEvent} e The event.646* @private647*/648goog.fx.Dragger.prototype.onScroll_ = function(e) {649var pos = this.calculatePosition_(0, 0);650e.clientX = this.clientX;651e.clientY = this.clientY;652this.doDrag(e, pos.x, pos.y, true);653};654655656/**657* @param {goog.events.BrowserEvent} e The closure object658* representing the browser event that caused a drag event.659* @param {number} x The new horizontal position for the drag element.660* @param {number} y The new vertical position for the drag element.661* @param {boolean} dragFromScroll Whether dragging was caused by scrolling662* the associated scroll target.663* @protected664*/665goog.fx.Dragger.prototype.doDrag = function(e, x, y, dragFromScroll) {666this.defaultAction(x, y);667this.dispatchEvent(668new goog.fx.DragEvent(669goog.fx.Dragger.EventType.DRAG, this, e.clientX, e.clientY, e, x, y));670};671672673/**674* Returns the 'real' x after limits are applied (allows for some675* limits to be undefined).676* @param {number} x X-coordinate to limit.677* @return {number} The 'real' X-coordinate after limits are applied.678*/679goog.fx.Dragger.prototype.limitX = function(x) {680var rect = this.limits;681var left = !isNaN(rect.left) ? rect.left : null;682var width = !isNaN(rect.width) ? rect.width : 0;683var maxX = left != null ? left + width : Infinity;684var minX = left != null ? left : -Infinity;685return Math.min(maxX, Math.max(minX, x));686};687688689/**690* Returns the 'real' y after limits are applied (allows for some691* limits to be undefined).692* @param {number} y Y-coordinate to limit.693* @return {number} The 'real' Y-coordinate after limits are applied.694*/695goog.fx.Dragger.prototype.limitY = function(y) {696var rect = this.limits;697var top = !isNaN(rect.top) ? rect.top : null;698var height = !isNaN(rect.height) ? rect.height : 0;699var maxY = top != null ? top + height : Infinity;700var minY = top != null ? top : -Infinity;701return Math.min(maxY, Math.max(minY, y));702};703704705/**706* Overridable function for computing the initial position of the target707* before dragging begins.708* @protected709*/710goog.fx.Dragger.prototype.computeInitialPosition = function() {711this.deltaX = this.useRightPositioningForRtl_ ?712goog.style.bidi.getOffsetStart(this.target) :713/** @type {!HTMLElement} */ (this.target).offsetLeft;714this.deltaY = /** @type {!HTMLElement} */ (this.target).offsetTop;715};716717718/**719* Overridable function for handling the default action of the drag behaviour.720* Normally this is simply moving the element to x,y though in some cases it721* might be used to resize the layer. This is basically a shortcut to722* implementing a default ondrag event handler.723* @param {number} x X-coordinate for target element. In right-to-left, x this724* is the number of pixels the target should be moved to from the right.725* @param {number} y Y-coordinate for target element.726*/727goog.fx.Dragger.prototype.defaultAction = function(x, y) {728if (this.useRightPositioningForRtl_ && this.isRightToLeft_()) {729this.target.style.right = x + 'px';730} else {731this.target.style.left = x + 'px';732}733this.target.style.top = y + 'px';734};735736737/**738* @return {boolean} Whether the dragger is currently in the midst of a drag.739*/740goog.fx.Dragger.prototype.isDragging = function() {741return this.dragging_;742};743744745746/**747* Object representing a drag event748* @param {string} type Event type.749* @param {goog.fx.Dragger} dragobj Drag object initiating event.750* @param {number} clientX X-coordinate relative to the viewport.751* @param {number} clientY Y-coordinate relative to the viewport.752* @param {goog.events.BrowserEvent} browserEvent The closure object753* representing the browser event that caused this drag event.754* @param {number=} opt_actX Optional actual x for drag if it has been limited.755* @param {number=} opt_actY Optional actual y for drag if it has been limited.756* @param {boolean=} opt_dragCanceled Whether the drag has been canceled.757* @constructor758* @struct759* @extends {goog.events.Event}760*/761goog.fx.DragEvent = function(762type, dragobj, clientX, clientY, browserEvent, opt_actX, opt_actY,763opt_dragCanceled) {764goog.events.Event.call(this, type);765766/**767* X-coordinate relative to the viewport768* @type {number}769*/770this.clientX = clientX;771772/**773* Y-coordinate relative to the viewport774* @type {number}775*/776this.clientY = clientY;777778/**779* The closure object representing the browser event that caused this drag780* event.781* @type {goog.events.BrowserEvent}782*/783this.browserEvent = browserEvent;784785/**786* The real x-position of the drag if it has been limited787* @type {number}788*/789this.left = goog.isDef(opt_actX) ? opt_actX : dragobj.deltaX;790791/**792* The real y-position of the drag if it has been limited793* @type {number}794*/795this.top = goog.isDef(opt_actY) ? opt_actY : dragobj.deltaY;796797/**798* Reference to the drag object for this event799* @type {goog.fx.Dragger}800*/801this.dragger = dragobj;802803/**804* Whether drag was canceled with this event. Used to differentiate between805* a legitimate drag END that can result in an action and a drag END which is806* a result of a drag cancelation. For now it can happen 1) with drag END807* event on FireFox when user drags the mouse out of the window, 2) with808* drag END event on IE7 which is generated on MOUSEMOVE event when user809* moves the mouse into the document after the mouse button has been810* released, 3) when TOUCHCANCEL is raised instead of TOUCHEND (on touch811* events).812* @type {boolean}813*/814this.dragCanceled = !!opt_dragCanceled;815};816goog.inherits(goog.fx.DragEvent, goog.events.Event);817818819