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