| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (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 |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Drag Utilities. |
| 17 |
* |
| 18 |
* Provides extensible functionality for drag & drop behaviour. |
| 19 |
* |
| 20 |
* @see ../demos/drag.html |
| 21 |
* @see ../demos/dragger.html |
| 22 |
*/ |
| 23 |
|
| 24 |
|
| 25 |
goog.provide('goog.fx.DragEvent'); |
| 26 |
goog.provide('goog.fx.Dragger'); |
| 27 |
goog.provide('goog.fx.Dragger.EventType'); |
| 28 |
|
| 29 |
goog.require('goog.dom'); |
| 30 |
goog.require('goog.dom.TagName'); |
| 31 |
goog.require('goog.events'); |
| 32 |
goog.require('goog.events.Event'); |
| 33 |
goog.require('goog.events.EventHandler'); |
| 34 |
goog.require('goog.events.EventTarget'); |
| 35 |
goog.require('goog.events.EventType'); |
| 36 |
goog.require('goog.math.Coordinate'); |
| 37 |
goog.require('goog.math.Rect'); |
| 38 |
goog.require('goog.style'); |
| 39 |
goog.require('goog.style.bidi'); |
| 40 |
goog.require('goog.userAgent'); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* A class that allows mouse or touch-based dragging (moving) of an element |
| 46 |
* |
| 47 |
* @param {Element} target The element that will be dragged. |
| 48 |
* @param {Element=} opt_handle An optional handle to control the drag, if null |
| 49 |
* the target is used. |
| 50 |
* @param {goog.math.Rect=} opt_limits Object containing left, top, width, |
| 51 |
* and height. |
| 52 |
* |
| 53 |
* @extends {goog.events.EventTarget} |
| 54 |
* @constructor |
| 55 |
* @struct |
| 56 |
*/ |
| 57 |
goog.fx.Dragger = function(target, opt_handle, opt_limits) { |
| 58 |
goog.fx.Dragger.base(this, 'constructor'); |
| 59 |
|
| 60 |
/** |
| 61 |
* Reference to drag target element. |
| 62 |
* @type {?Element} |
| 63 |
*/ |
| 64 |
this.target = target; |
| 65 |
|
| 66 |
/** |
| 67 |
* Reference to the handler that initiates the drag. |
| 68 |
* @type {?Element} |
| 69 |
*/ |
| 70 |
this.handle = opt_handle || target; |
| 71 |
|
| 72 |
/** |
| 73 |
* Object representing the limits of the drag region. |
| 74 |
* @type {goog.math.Rect} |
| 75 |
*/ |
| 76 |
this.limits = opt_limits || new goog.math.Rect(NaN, NaN, NaN, NaN); |
| 77 |
|
| 78 |
/** |
| 79 |
* Reference to a document object to use for the events. |
| 80 |
* @private {Document} |
| 81 |
*/ |
| 82 |
this.document_ = goog.dom.getOwnerDocument(target); |
| 83 |
|
| 84 |
/** @private {!goog.events.EventHandler} */ |
| 85 |
this.eventHandler_ = new goog.events.EventHandler(this); |
| 86 |
this.registerDisposable(this.eventHandler_); |
| 87 |
|
| 88 |
/** |
| 89 |
* Whether the element is rendered right-to-left. We initialize this lazily. |
| 90 |
* @private {boolean|undefined}} |
| 91 |
*/ |
| 92 |
this.rightToLeft_; |
| 93 |
|
| 94 |
/** |
| 95 |
* Current x position of mouse or touch relative to viewport. |
| 96 |
* @type {number} |
| 97 |
*/ |
| 98 |
this.clientX = 0; |
| 99 |
|
| 100 |
/** |
| 101 |
* Current y position of mouse or touch relative to viewport. |
| 102 |
* @type {number} |
| 103 |
*/ |
| 104 |
this.clientY = 0; |
| 105 |
|
| 106 |
/** |
| 107 |
* Current x position of mouse or touch relative to screen. Deprecated because |
| 108 |
* it doesn't take into affect zoom level or pixel density. |
| 109 |
* @type {number} |
| 110 |
* @deprecated Consider switching to clientX instead. |
| 111 |
*/ |
| 112 |
this.screenX = 0; |
| 113 |
|
| 114 |
/** |
| 115 |
* Current y position of mouse or touch relative to screen. Deprecated because |
| 116 |
* it doesn't take into affect zoom level or pixel density. |
| 117 |
* @type {number} |
| 118 |
* @deprecated Consider switching to clientY instead. |
| 119 |
*/ |
| 120 |
this.screenY = 0; |
| 121 |
|
| 122 |
/** |
| 123 |
* The x position where the first mousedown or touchstart occurred. |
| 124 |
* @type {number} |
| 125 |
*/ |
| 126 |
this.startX = 0; |
| 127 |
|
| 128 |
/** |
| 129 |
* The y position where the first mousedown or touchstart occurred. |
| 130 |
* @type {number} |
| 131 |
*/ |
| 132 |
this.startY = 0; |
| 133 |
|
| 134 |
/** |
| 135 |
* Current x position of drag relative to target's parent. |
| 136 |
* @type {number} |
| 137 |
*/ |
| 138 |
this.deltaX = 0; |
| 139 |
|
| 140 |
/** |
| 141 |
* Current y position of drag relative to target's parent. |
| 142 |
* @type {number} |
| 143 |
*/ |
| 144 |
this.deltaY = 0; |
| 145 |
|
| 146 |
/** |
| 147 |
* The current page scroll value. |
| 148 |
* @type {?goog.math.Coordinate} |
| 149 |
*/ |
| 150 |
this.pageScroll; |
| 151 |
|
| 152 |
/** |
| 153 |
* Whether dragging is currently enabled. |
| 154 |
* @private {boolean} |
| 155 |
*/ |
| 156 |
this.enabled_ = true; |
| 157 |
|
| 158 |
/** |
| 159 |
* Whether object is currently being dragged. |
| 160 |
* @private {boolean} |
| 161 |
*/ |
| 162 |
this.dragging_ = false; |
| 163 |
|
| 164 |
/** |
| 165 |
* Whether mousedown should be default prevented. |
| 166 |
* @private {boolean} |
| 167 |
**/ |
| 168 |
this.preventMouseDown_ = true; |
| 169 |
|
| 170 |
/** |
| 171 |
* The amount of distance, in pixels, after which a mousedown or touchstart is |
| 172 |
* considered a drag. |
| 173 |
* @private {number} |
| 174 |
*/ |
| 175 |
this.hysteresisDistanceSquared_ = 0; |
| 176 |
|
| 177 |
/** |
| 178 |
* The SCROLL event target used to make drag element follow scrolling. |
| 179 |
* @private {?EventTarget} |
| 180 |
*/ |
| 181 |
this.scrollTarget_; |
| 182 |
|
| 183 |
/** |
| 184 |
* Whether IE drag events cancelling is on. |
| 185 |
* @private {boolean} |
| 186 |
*/ |
| 187 |
this.ieDragStartCancellingOn_ = false; |
| 188 |
|
| 189 |
/** |
| 190 |
* Whether the dragger implements the changes described in http://b/6324964, |
| 191 |
* making it truly RTL. This is a temporary flag to allow clients to |
| 192 |
* transition to the new behavior at their convenience. At some point it will |
| 193 |
* be the default. |
| 194 |
* @private {boolean} |
| 195 |
*/ |
| 196 |
this.useRightPositioningForRtl_ = false; |
| 197 |
|
| 198 |
// Add listener. Do not use the event handler here since the event handler is |
| 199 |
// used for listeners added and removed during the drag operation. |
| 200 |
goog.events.listen( |
| 201 |
this.handle, |
| 202 |
[goog.events.EventType.TOUCHSTART, goog.events.EventType.MOUSEDOWN], |
| 203 |
this.startDrag, false, this); |
| 204 |
|
| 205 |
/** @private {boolean} Avoids setCapture() calls to fix click handlers. */ |
| 206 |
this.useSetCapture_ = goog.fx.Dragger.HAS_SET_CAPTURE_; |
| 207 |
}; |
| 208 |
goog.inherits(goog.fx.Dragger, goog.events.EventTarget); |
| 209 |
// Dragger is meant to be extended, but defines most properties on its |
| 210 |
// prototype, thus making it unsuitable for sealing. |
| 211 |
goog.tagUnsealableClass(goog.fx.Dragger); |
| 212 |
|
| 213 |
|
| 214 |
/** |
| 215 |
* Whether setCapture is supported by the browser. |
| 216 |
* IE and Gecko after 1.9.3 have setCapture. MS Edge and WebKit |
| 217 |
* (https://bugs.webkit.org/show_bug.cgi?id=27330) don't. |
| 218 |
* @type {boolean} |
| 219 |
* @private |
| 220 |
*/ |
| 221 |
goog.fx.Dragger.HAS_SET_CAPTURE_ = goog.global.document && |
| 222 |
goog.global.document.documentElement && |
| 223 |
!!goog.global.document.documentElement.setCapture && |
| 224 |
!!goog.global.document.releaseCapture; |
| 225 |
|
| 226 |
|
| 227 |
/** |
| 228 |
* Creates copy of node being dragged. This is a utility function to be used |
| 229 |
* wherever it is inappropriate for the original source to follow the mouse |
| 230 |
* cursor itself. |
| 231 |
* |
| 232 |
* @param {Element} sourceEl Element to copy. |
| 233 |
* @return {!Element} The clone of {@code sourceEl}. |
| 234 |
*/ |
| 235 |
goog.fx.Dragger.cloneNode = function(sourceEl) { |
| 236 |
var clonedEl = sourceEl.cloneNode(true), |
| 237 |
origTexts = |
| 238 |
goog.dom.getElementsByTagName(goog.dom.TagName.TEXTAREA, sourceEl), |
| 239 |
dragTexts = |
| 240 |
goog.dom.getElementsByTagName(goog.dom.TagName.TEXTAREA, clonedEl); |
| 241 |
// Cloning does not copy the current value of textarea elements, so correct |
| 242 |
// this manually. |
| 243 |
for (var i = 0; i < origTexts.length; i++) { |
| 244 |
dragTexts[i].value = origTexts[i].value; |
| 245 |
} |
| 246 |
switch (sourceEl.tagName) { |
| 247 |
case String(goog.dom.TagName.TR): |
| 248 |
return goog.dom.createDom( |
| 249 |
goog.dom.TagName.TABLE, null, |
| 250 |
goog.dom.createDom(goog.dom.TagName.TBODY, null, clonedEl)); |
| 251 |
case String(goog.dom.TagName.TD): |
| 252 |
case String(goog.dom.TagName.TH): |
| 253 |
return goog.dom.createDom( |
| 254 |
goog.dom.TagName.TABLE, null, |
| 255 |
goog.dom.createDom( |
| 256 |
goog.dom.TagName.TBODY, null, |
| 257 |
goog.dom.createDom(goog.dom.TagName.TR, null, clonedEl))); |
| 258 |
case String(goog.dom.TagName.TEXTAREA): |
| 259 |
clonedEl.value = sourceEl.value; |
| 260 |
default: |
| 261 |
return clonedEl; |
| 262 |
} |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
/** |
| 267 |
* Constants for event names. |
| 268 |
* @enum {string} |
| 269 |
*/ |
| 270 |
goog.fx.Dragger.EventType = { |
| 271 |
// The drag action was canceled before the START event. Possible reasons: |
| 272 |
// disabled dragger, dragging with the right mouse button or releasing the |
| 273 |
// button before reaching the hysteresis distance. |
| 274 |
EARLY_CANCEL: 'earlycancel', |
| 275 |
START: 'start', |
| 276 |
BEFOREDRAG: 'beforedrag', |
| 277 |
DRAG: 'drag', |
| 278 |
END: 'end' |
| 279 |
}; |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* Prevents the dragger from calling setCapture(), even in browsers that support |
| 284 |
* it. If the draggable item has click handlers, setCapture() can break them. |
| 285 |
* @param {boolean} allow True to use setCapture if the browser supports it. |
| 286 |
*/ |
| 287 |
goog.fx.Dragger.prototype.setAllowSetCapture = function(allow) { |
| 288 |
this.useSetCapture_ = allow && goog.fx.Dragger.HAS_SET_CAPTURE_; |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
/** |
| 293 |
* Turns on/off true RTL behavior. This should be called immediately after |
| 294 |
* construction. This is a temporary flag to allow clients to transition |
| 295 |
* to the new component at their convenience. At some point true will be the |
| 296 |
* default. |
| 297 |
* @param {boolean} useRightPositioningForRtl True if "right" should be used for |
| 298 |
* positioning, false if "left" should be used for positioning. |
| 299 |
*/ |
| 300 |
goog.fx.Dragger.prototype.enableRightPositioningForRtl = function( |
| 301 |
useRightPositioningForRtl) { |
| 302 |
this.useRightPositioningForRtl_ = useRightPositioningForRtl; |
| 303 |
}; |
| 304 |
|
| 305 |
|
| 306 |
/** |
| 307 |
* Returns the event handler, intended for subclass use. |
| 308 |
* @return {!goog.events.EventHandler<T>} The event handler. |
| 309 |
* @this {T} |
| 310 |
* @template T |
| 311 |
*/ |
| 312 |
goog.fx.Dragger.prototype.getHandler = function() { |
| 313 |
// TODO(user): templated "this" values currently result in "this" being |
| 314 |
// "unknown" in the body of the function. |
| 315 |
var self = /** @type {goog.fx.Dragger} */ (this); |
| 316 |
return self.eventHandler_; |
| 317 |
}; |
| 318 |
|
| 319 |
|
| 320 |
/** |
| 321 |
* Sets (or reset) the Drag limits after a Dragger is created. |
| 322 |
* @param {goog.math.Rect?} limits Object containing left, top, width, |
| 323 |
* height for new Dragger limits. If target is right-to-left and |
| 324 |
* enableRightPositioningForRtl(true) is called, then rect is interpreted as |
| 325 |
* right, top, width, and height. |
| 326 |
*/ |
| 327 |
goog.fx.Dragger.prototype.setLimits = function(limits) { |
| 328 |
this.limits = limits || new goog.math.Rect(NaN, NaN, NaN, NaN); |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
/** |
| 333 |
* Sets the distance the user has to drag the element before a drag operation is |
| 334 |
* started. |
| 335 |
* @param {number} distance The number of pixels after which a mousedown and |
| 336 |
* move is considered a drag. |
| 337 |
*/ |
| 338 |
goog.fx.Dragger.prototype.setHysteresis = function(distance) { |
| 339 |
this.hysteresisDistanceSquared_ = Math.pow(distance, 2); |
| 340 |
}; |
| 341 |
|
| 342 |
|
| 343 |
/** |
| 344 |
* Gets the distance the user has to drag the element before a drag operation is |
| 345 |
* started. |
| 346 |
* @return {number} distance The number of pixels after which a mousedown and |
| 347 |
* move is considered a drag. |
| 348 |
*/ |
| 349 |
goog.fx.Dragger.prototype.getHysteresis = function() { |
| 350 |
return Math.sqrt(this.hysteresisDistanceSquared_); |
| 351 |
}; |
| 352 |
|
| 353 |
|
| 354 |
/** |
| 355 |
* Sets the SCROLL event target to make drag element follow scrolling. |
| 356 |
* |
| 357 |
* @param {EventTarget} scrollTarget The event target that dispatches SCROLL |
| 358 |
* events. |
| 359 |
*/ |
| 360 |
goog.fx.Dragger.prototype.setScrollTarget = function(scrollTarget) { |
| 361 |
this.scrollTarget_ = scrollTarget; |
| 362 |
}; |
| 363 |
|
| 364 |
|
| 365 |
/** |
| 366 |
* Enables cancelling of built-in IE drag events. |
| 367 |
* @param {boolean} cancelIeDragStart Whether to enable cancelling of IE |
| 368 |
* dragstart event. |
| 369 |
*/ |
| 370 |
goog.fx.Dragger.prototype.setCancelIeDragStart = function(cancelIeDragStart) { |
| 371 |
this.ieDragStartCancellingOn_ = cancelIeDragStart; |
| 372 |
}; |
| 373 |
|
| 374 |
|
| 375 |
/** |
| 376 |
* @return {boolean} Whether the dragger is enabled. |
| 377 |
*/ |
| 378 |
goog.fx.Dragger.prototype.getEnabled = function() { |
| 379 |
return this.enabled_; |
| 380 |
}; |
| 381 |
|
| 382 |
|
| 383 |
/** |
| 384 |
* Set whether dragger is enabled |
| 385 |
* @param {boolean} enabled Whether dragger is enabled. |
| 386 |
*/ |
| 387 |
goog.fx.Dragger.prototype.setEnabled = function(enabled) { |
| 388 |
this.enabled_ = enabled; |
| 389 |
}; |
| 390 |
|
| 391 |
|
| 392 |
/** |
| 393 |
* Set whether mousedown should be default prevented. |
| 394 |
* @param {boolean} preventMouseDown Whether mousedown should be default |
| 395 |
* prevented. |
| 396 |
*/ |
| 397 |
goog.fx.Dragger.prototype.setPreventMouseDown = function(preventMouseDown) { |
| 398 |
this.preventMouseDown_ = preventMouseDown; |
| 399 |
}; |
| 400 |
|
| 401 |
|
| 402 |
/** @override */ |
| 403 |
goog.fx.Dragger.prototype.disposeInternal = function() { |
| 404 |
goog.fx.Dragger.superClass_.disposeInternal.call(this); |
| 405 |
goog.events.unlisten( |
| 406 |
this.handle, |
| 407 |
[goog.events.EventType.TOUCHSTART, goog.events.EventType.MOUSEDOWN], |
| 408 |
this.startDrag, false, this); |
| 409 |
this.cleanUpAfterDragging_(); |
| 410 |
|
| 411 |
this.target = null; |
| 412 |
this.handle = null; |
| 413 |
}; |
| 414 |
|
| 415 |
|
| 416 |
/** |
| 417 |
* Whether the DOM element being manipulated is rendered right-to-left. |
| 418 |
* @return {boolean} True if the DOM element is rendered right-to-left, false |
| 419 |
* otherwise. |
| 420 |
* @private |
| 421 |
*/ |
| 422 |
goog.fx.Dragger.prototype.isRightToLeft_ = function() { |
| 423 |
if (!goog.isDef(this.rightToLeft_)) { |
| 424 |
this.rightToLeft_ = goog.style.isRightToLeft(this.target); |
| 425 |
} |
| 426 |
return this.rightToLeft_; |
| 427 |
}; |
| 428 |
|
| 429 |
|
| 430 |
/** |
| 431 |
* Event handler that is used to start the drag |
| 432 |
* @param {goog.events.BrowserEvent} e Event object. |
| 433 |
*/ |
| 434 |
goog.fx.Dragger.prototype.startDrag = function(e) { |
| 435 |
var isMouseDown = e.type == goog.events.EventType.MOUSEDOWN; |
| 436 |
|
| 437 |
// Dragger.startDrag() can be called by AbstractDragDrop with a mousemove |
| 438 |
// event and IE does not report pressed mouse buttons on mousemove. Also, |
| 439 |
// it does not make sense to check for the button if the user is already |
| 440 |
// dragging. |
| 441 |
|
| 442 |
if (this.enabled_ && !this.dragging_ && |
| 443 |
(!isMouseDown || e.isMouseActionButton())) { |
| 444 |
if (this.hysteresisDistanceSquared_ == 0) { |
| 445 |
if (this.fireDragStart_(e)) { |
| 446 |
this.dragging_ = true; |
| 447 |
if (this.preventMouseDown_ && isMouseDown) { |
| 448 |
e.preventDefault(); |
| 449 |
} |
| 450 |
} else { |
| 451 |
// If the start drag is cancelled, don't setup for a drag. |
| 452 |
return; |
| 453 |
} |
| 454 |
} else if (this.preventMouseDown_ && isMouseDown) { |
| 455 |
// Need to preventDefault for hysteresis to prevent page getting selected. |
| 456 |
e.preventDefault(); |
| 457 |
} |
| 458 |
this.setupDragHandlers(); |
| 459 |
|
| 460 |
this.clientX = this.startX = e.clientX; |
| 461 |
this.clientY = this.startY = e.clientY; |
| 462 |
this.screenX = e.screenX; |
| 463 |
this.screenY = e.screenY; |
| 464 |
this.computeInitialPosition(); |
| 465 |
this.pageScroll = goog.dom.getDomHelper(this.document_).getDocumentScroll(); |
| 466 |
} else { |
| 467 |
this.dispatchEvent(goog.fx.Dragger.EventType.EARLY_CANCEL); |
| 468 |
} |
| 469 |
}; |
| 470 |
|
| 471 |
|
| 472 |
/** |
| 473 |
* Sets up event handlers when dragging starts. |
| 474 |
* @protected |
| 475 |
*/ |
| 476 |
goog.fx.Dragger.prototype.setupDragHandlers = function() { |
| 477 |
var doc = this.document_; |
| 478 |
var docEl = doc.documentElement; |
| 479 |
// Use bubbling when we have setCapture since we got reports that IE has |
| 480 |
// problems with the capturing events in combination with setCapture. |
| 481 |
var useCapture = !this.useSetCapture_; |
| 482 |
|
| 483 |
this.eventHandler_.listen( |
| 484 |
doc, [goog.events.EventType.TOUCHMOVE, goog.events.EventType.MOUSEMOVE], |
| 485 |
this.handleMove_, {capture: useCapture, passive: false}); |
| 486 |
this.eventHandler_.listen( |
| 487 |
doc, [goog.events.EventType.TOUCHEND, goog.events.EventType.MOUSEUP], |
| 488 |
this.endDrag, useCapture); |
| 489 |
|
| 490 |
if (this.useSetCapture_) { |
| 491 |
docEl.setCapture(false); |
| 492 |
this.eventHandler_.listen( |
| 493 |
docEl, goog.events.EventType.LOSECAPTURE, this.endDrag); |
| 494 |
} else { |
| 495 |
// Make sure we stop the dragging if the window loses focus. |
| 496 |
// Don't use capture in this listener because we only want to end the drag |
| 497 |
// if the actual window loses focus. Since blur events do not bubble we use |
| 498 |
// a bubbling listener on the window. |
| 499 |
this.eventHandler_.listen( |
| 500 |
goog.dom.getWindow(doc), goog.events.EventType.BLUR, this.endDrag); |
| 501 |
} |
| 502 |
|
| 503 |
if (goog.userAgent.IE && this.ieDragStartCancellingOn_) { |
| 504 |
// Cancel IE's 'ondragstart' event. |
| 505 |
this.eventHandler_.listen( |
| 506 |
doc, goog.events.EventType.DRAGSTART, goog.events.Event.preventDefault); |
| 507 |
} |
| 508 |
|
| 509 |
if (this.scrollTarget_) { |
| 510 |
this.eventHandler_.listen( |
| 511 |
this.scrollTarget_, goog.events.EventType.SCROLL, this.onScroll_, |
| 512 |
useCapture); |
| 513 |
} |
| 514 |
}; |
| 515 |
|
| 516 |
|
| 517 |
/** |
| 518 |
* Fires a goog.fx.Dragger.EventType.START event. |
| 519 |
* @param {goog.events.BrowserEvent} e Browser event that triggered the drag. |
| 520 |
* @return {boolean} False iff preventDefault was called on the DragEvent. |
| 521 |
* @private |
| 522 |
*/ |
| 523 |
goog.fx.Dragger.prototype.fireDragStart_ = function(e) { |
| 524 |
return this.dispatchEvent( |
| 525 |
new goog.fx.DragEvent( |
| 526 |
goog.fx.Dragger.EventType.START, this, e.clientX, e.clientY, e)); |
| 527 |
}; |
| 528 |
|
| 529 |
|
| 530 |
/** |
| 531 |
* Unregisters the event handlers that are only active during dragging, and |
| 532 |
* releases mouse capture. |
| 533 |
* @private |
| 534 |
*/ |
| 535 |
goog.fx.Dragger.prototype.cleanUpAfterDragging_ = function() { |
| 536 |
this.eventHandler_.removeAll(); |
| 537 |
if (this.useSetCapture_) { |
| 538 |
this.document_.releaseCapture(); |
| 539 |
} |
| 540 |
}; |
| 541 |
|
| 542 |
|
| 543 |
/** |
| 544 |
* Event handler that is used to end the drag. |
| 545 |
* @param {goog.events.BrowserEvent} e Event object. |
| 546 |
* @param {boolean=} opt_dragCanceled Whether the drag has been canceled. |
| 547 |
*/ |
| 548 |
goog.fx.Dragger.prototype.endDrag = function(e, opt_dragCanceled) { |
| 549 |
this.cleanUpAfterDragging_(); |
| 550 |
|
| 551 |
if (this.dragging_) { |
| 552 |
this.dragging_ = false; |
| 553 |
|
| 554 |
var x = this.limitX(this.deltaX); |
| 555 |
var y = this.limitY(this.deltaY); |
| 556 |
var dragCanceled = |
| 557 |
opt_dragCanceled || e.type == goog.events.EventType.TOUCHCANCEL; |
| 558 |
this.dispatchEvent( |
| 559 |
new goog.fx.DragEvent( |
| 560 |
goog.fx.Dragger.EventType.END, this, e.clientX, e.clientY, e, x, y, |
| 561 |
dragCanceled)); |
| 562 |
} else { |
| 563 |
this.dispatchEvent(goog.fx.Dragger.EventType.EARLY_CANCEL); |
| 564 |
} |
| 565 |
}; |
| 566 |
|
| 567 |
|
| 568 |
/** |
| 569 |
* Event handler that is used to end the drag by cancelling it. |
| 570 |
* @param {goog.events.BrowserEvent} e Event object. |
| 571 |
*/ |
| 572 |
goog.fx.Dragger.prototype.endDragCancel = function(e) { |
| 573 |
this.endDrag(e, true); |
| 574 |
}; |
| 575 |
|
| 576 |
|
| 577 |
/** |
| 578 |
* Event handler that is used on mouse / touch move to update the drag |
| 579 |
* @param {goog.events.BrowserEvent} e Event object. |
| 580 |
* @private |
| 581 |
*/ |
| 582 |
goog.fx.Dragger.prototype.handleMove_ = function(e) { |
| 583 |
if (this.enabled_) { |
| 584 |
// dx in right-to-left cases is relative to the right. |
| 585 |
var sign = |
| 586 |
this.useRightPositioningForRtl_ && this.isRightToLeft_() ? -1 : 1; |
| 587 |
var dx = sign * (e.clientX - this.clientX); |
| 588 |
var dy = e.clientY - this.clientY; |
| 589 |
this.clientX = e.clientX; |
| 590 |
this.clientY = e.clientY; |
| 591 |
this.screenX = e.screenX; |
| 592 |
this.screenY = e.screenY; |
| 593 |
|
| 594 |
if (!this.dragging_) { |
| 595 |
var diffX = this.startX - this.clientX; |
| 596 |
var diffY = this.startY - this.clientY; |
| 597 |
var distance = diffX * diffX + diffY * diffY; |
| 598 |
if (distance > this.hysteresisDistanceSquared_) { |
| 599 |
if (this.fireDragStart_(e)) { |
| 600 |
this.dragging_ = true; |
| 601 |
} else { |
| 602 |
// DragListGroup disposes of the dragger if BEFOREDRAGSTART is |
| 603 |
// canceled. |
| 604 |
if (!this.isDisposed()) { |
| 605 |
this.endDrag(e); |
| 606 |
} |
| 607 |
return; |
| 608 |
} |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
var pos = this.calculatePosition_(dx, dy); |
| 613 |
var x = pos.x; |
| 614 |
var y = pos.y; |
| 615 |
|
| 616 |
if (this.dragging_) { |
| 617 |
var rv = this.dispatchEvent( |
| 618 |
new goog.fx.DragEvent( |
| 619 |
goog.fx.Dragger.EventType.BEFOREDRAG, this, e.clientX, e.clientY, |
| 620 |
e, x, y)); |
| 621 |
|
| 622 |
// Only do the defaultAction and dispatch drag event if predrag didn't |
| 623 |
// prevent default |
| 624 |
if (rv) { |
| 625 |
this.doDrag(e, x, y, false); |
| 626 |
e.preventDefault(); |
| 627 |
} |
| 628 |
} |
| 629 |
} |
| 630 |
}; |
| 631 |
|
| 632 |
|
| 633 |
/** |
| 634 |
* Calculates the drag position. |
| 635 |
* |
| 636 |
* @param {number} dx The horizontal movement delta. |
| 637 |
* @param {number} dy The vertical movement delta. |
| 638 |
* @return {!goog.math.Coordinate} The newly calculated drag element position. |
| 639 |
* @private |
| 640 |
*/ |
| 641 |
goog.fx.Dragger.prototype.calculatePosition_ = function(dx, dy) { |
| 642 |
// Update the position for any change in body scrolling |
| 643 |
var pageScroll = goog.dom.getDomHelper(this.document_).getDocumentScroll(); |
| 644 |
dx += pageScroll.x - this.pageScroll.x; |
| 645 |
dy += pageScroll.y - this.pageScroll.y; |
| 646 |
this.pageScroll = pageScroll; |
| 647 |
|
| 648 |
this.deltaX += dx; |
| 649 |
this.deltaY += dy; |
| 650 |
|
| 651 |
var x = this.limitX(this.deltaX); |
| 652 |
var y = this.limitY(this.deltaY); |
| 653 |
return new goog.math.Coordinate(x, y); |
| 654 |
}; |
| 655 |
|
| 656 |
|
| 657 |
/** |
| 658 |
* Event handler for scroll target scrolling. |
| 659 |
* @param {goog.events.BrowserEvent} e The event. |
| 660 |
* @private |
| 661 |
*/ |
| 662 |
goog.fx.Dragger.prototype.onScroll_ = function(e) { |
| 663 |
var pos = this.calculatePosition_(0, 0); |
| 664 |
e.clientX = this.clientX; |
| 665 |
e.clientY = this.clientY; |
| 666 |
this.doDrag(e, pos.x, pos.y, true); |
| 667 |
}; |
| 668 |
|
| 669 |
|
| 670 |
/** |
| 671 |
* @param {goog.events.BrowserEvent} e The closure object |
| 672 |
* representing the browser event that caused a drag event. |
| 673 |
* @param {number} x The new horizontal position for the drag element. |
| 674 |
* @param {number} y The new vertical position for the drag element. |
| 675 |
* @param {boolean} dragFromScroll Whether dragging was caused by scrolling |
| 676 |
* the associated scroll target. |
| 677 |
* @protected |
| 678 |
*/ |
| 679 |
goog.fx.Dragger.prototype.doDrag = function(e, x, y, dragFromScroll) { |
| 680 |
this.defaultAction(x, y); |
| 681 |
this.dispatchEvent( |
| 682 |
new goog.fx.DragEvent( |
| 683 |
goog.fx.Dragger.EventType.DRAG, this, e.clientX, e.clientY, e, x, y)); |
| 684 |
}; |
| 685 |
|
| 686 |
|
| 687 |
/** |
| 688 |
* Returns the 'real' x after limits are applied (allows for some |
| 689 |
* limits to be undefined). |
| 690 |
* @param {number} x X-coordinate to limit. |
| 691 |
* @return {number} The 'real' X-coordinate after limits are applied. |
| 692 |
*/ |
| 693 |
goog.fx.Dragger.prototype.limitX = function(x) { |
| 694 |
var rect = this.limits; |
| 695 |
var left = !isNaN(rect.left) ? rect.left : null; |
| 696 |
var width = !isNaN(rect.width) ? rect.width : 0; |
| 697 |
var maxX = left != null ? left + width : Infinity; |
| 698 |
var minX = left != null ? left : -Infinity; |
| 699 |
return Math.min(maxX, Math.max(minX, x)); |
| 700 |
}; |
| 701 |
|
| 702 |
|
| 703 |
/** |
| 704 |
* Returns the 'real' y after limits are applied (allows for some |
| 705 |
* limits to be undefined). |
| 706 |
* @param {number} y Y-coordinate to limit. |
| 707 |
* @return {number} The 'real' Y-coordinate after limits are applied. |
| 708 |
*/ |
| 709 |
goog.fx.Dragger.prototype.limitY = function(y) { |
| 710 |
var rect = this.limits; |
| 711 |
var top = !isNaN(rect.top) ? rect.top : null; |
| 712 |
var height = !isNaN(rect.height) ? rect.height : 0; |
| 713 |
var maxY = top != null ? top + height : Infinity; |
| 714 |
var minY = top != null ? top : -Infinity; |
| 715 |
return Math.min(maxY, Math.max(minY, y)); |
| 716 |
}; |
| 717 |
|
| 718 |
|
| 719 |
/** |
| 720 |
* Overridable function for computing the initial position of the target |
| 721 |
* before dragging begins. |
| 722 |
* @protected |
| 723 |
*/ |
| 724 |
goog.fx.Dragger.prototype.computeInitialPosition = function() { |
| 725 |
this.deltaX = this.useRightPositioningForRtl_ ? |
| 726 |
goog.style.bidi.getOffsetStart(this.target) : |
| 727 |
/** @type {!HTMLElement} */ (this.target).offsetLeft; |
| 728 |
this.deltaY = /** @type {!HTMLElement} */ (this.target).offsetTop; |
| 729 |
}; |
| 730 |
|
| 731 |
|
| 732 |
/** |
| 733 |
* Overridable function for handling the default action of the drag behaviour. |
| 734 |
* Normally this is simply moving the element to x,y though in some cases it |
| 735 |
* might be used to resize the layer. This is basically a shortcut to |
| 736 |
* implementing a default ondrag event handler. |
| 737 |
* @param {number} x X-coordinate for target element. In right-to-left, x this |
| 738 |
* is the number of pixels the target should be moved to from the right. |
| 739 |
* @param {number} y Y-coordinate for target element. |
| 740 |
*/ |
| 741 |
goog.fx.Dragger.prototype.defaultAction = function(x, y) { |
| 742 |
if (this.useRightPositioningForRtl_ && this.isRightToLeft_()) { |
| 743 |
this.target.style.right = x + 'px'; |
| 744 |
} else { |
| 745 |
this.target.style.left = x + 'px'; |
| 746 |
} |
| 747 |
this.target.style.top = y + 'px'; |
| 748 |
}; |
| 749 |
|
| 750 |
|
| 751 |
/** |
| 752 |
* @return {boolean} Whether the dragger is currently in the midst of a drag. |
| 753 |
*/ |
| 754 |
goog.fx.Dragger.prototype.isDragging = function() { |
| 755 |
return this.dragging_; |
| 756 |
}; |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
/** |
| 761 |
* Object representing a drag event |
| 762 |
* @param {string} type Event type. |
| 763 |
* @param {goog.fx.Dragger} dragobj Drag object initiating event. |
| 764 |
* @param {number} clientX X-coordinate relative to the viewport. |
| 765 |
* @param {number} clientY Y-coordinate relative to the viewport. |
| 766 |
* @param {goog.events.BrowserEvent} browserEvent The closure object |
| 767 |
* representing the browser event that caused this drag event. |
| 768 |
* @param {number=} opt_actX Optional actual x for drag if it has been limited. |
| 769 |
* @param {number=} opt_actY Optional actual y for drag if it has been limited. |
| 770 |
* @param {boolean=} opt_dragCanceled Whether the drag has been canceled. |
| 771 |
* @constructor |
| 772 |
* @struct |
| 773 |
* @extends {goog.events.Event} |
| 774 |
*/ |
| 775 |
goog.fx.DragEvent = function( |
| 776 |
type, dragobj, clientX, clientY, browserEvent, opt_actX, opt_actY, |
| 777 |
opt_dragCanceled) { |
| 778 |
goog.events.Event.call(this, type); |
| 779 |
|
| 780 |
/** |
| 781 |
* X-coordinate relative to the viewport |
| 782 |
* @type {number} |
| 783 |
*/ |
| 784 |
this.clientX = clientX; |
| 785 |
|
| 786 |
/** |
| 787 |
* Y-coordinate relative to the viewport |
| 788 |
* @type {number} |
| 789 |
*/ |
| 790 |
this.clientY = clientY; |
| 791 |
|
| 792 |
/** |
| 793 |
* The closure object representing the browser event that caused this drag |
| 794 |
* event. |
| 795 |
* @type {goog.events.BrowserEvent} |
| 796 |
*/ |
| 797 |
this.browserEvent = browserEvent; |
| 798 |
|
| 799 |
/** |
| 800 |
* The real x-position of the drag if it has been limited |
| 801 |
* @type {number} |
| 802 |
*/ |
| 803 |
this.left = goog.isDef(opt_actX) ? opt_actX : dragobj.deltaX; |
| 804 |
|
| 805 |
/** |
| 806 |
* The real y-position of the drag if it has been limited |
| 807 |
* @type {number} |
| 808 |
*/ |
| 809 |
this.top = goog.isDef(opt_actY) ? opt_actY : dragobj.deltaY; |
| 810 |
|
| 811 |
/** |
| 812 |
* Reference to the drag object for this event |
| 813 |
* @type {goog.fx.Dragger} |
| 814 |
*/ |
| 815 |
this.dragger = dragobj; |
| 816 |
|
| 817 |
/** |
| 818 |
* Whether drag was canceled with this event. Used to differentiate between |
| 819 |
* a legitimate drag END that can result in an action and a drag END which is |
| 820 |
* a result of a drag cancelation. For now it can happen 1) with drag END |
| 821 |
* event on FireFox when user drags the mouse out of the window, 2) with |
| 822 |
* drag END event on IE7 which is generated on MOUSEMOVE event when user |
| 823 |
* moves the mouse into the document after the mouse button has been |
| 824 |
* released, 3) when TOUCHCANCEL is raised instead of TOUCHEND (on touch |
| 825 |
* events). |
| 826 |
* @type {boolean} |
| 827 |
*/ |
| 828 |
this.dragCanceled = !!opt_dragCanceled; |
| 829 |
}; |
| 830 |
goog.inherits(goog.fx.DragEvent, goog.events.Event); |