aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/fx/dragscrollsupport.js
blob: a1e20980290fa678f7455aeef7ee4dd37c4d51b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Class to support scrollable containers for drag and drop.
 *
 */

goog.provide('goog.fx.DragScrollSupport');

goog.require('goog.Disposable');
goog.require('goog.Timer');
goog.require('goog.dom');
goog.require('goog.events.EventHandler');
goog.require('goog.events.EventType');
goog.require('goog.math.Coordinate');
goog.require('goog.style');



/**
 * A scroll support class. Currently this class will automatically scroll
 * a scrollable container node and scroll it by a fixed amount at a timed
 * interval when the mouse is moved above or below the container or in vertical
 * margin areas. Intended for use in drag and drop. This could potentially be
 * made more general and could support horizontal scrolling.
 *
 * @param {Element} containerNode A container that can be scrolled.
 * @param {number=} opt_margin Optional margin to use while scrolling.
 * @param {boolean=} opt_externalMouseMoveTracking Whether mouse move events
 *     are tracked externally by the client object which calls the mouse move
 *     event handler, useful when events are generated for more than one source
 *     element and/or are not real mousemove events.
 * @constructor
 * @extends {goog.Disposable}
 * @see ../demos/dragscrollsupport.html
 */
goog.fx.DragScrollSupport = function(containerNode, opt_margin,
                                     opt_externalMouseMoveTracking) {
  goog.Disposable.call(this);

  /**
   * The container to be scrolled.
   * @type {Element}
   * @private
   */
  this.containerNode_ = containerNode;

  /**
   * Scroll timer that will scroll the container until it is stopped.
   * It will scroll when the mouse is outside the scrolling area of the
   * container.
   *
   * @type {goog.Timer}
   * @private
   */
  this.scrollTimer_ = new goog.Timer(goog.fx.DragScrollSupport.TIMER_STEP_);

  /**
   * EventHandler used to set up and tear down listeners.
   * @type {goog.events.EventHandler}
   * @private
   */
  this.eventHandler_ = new goog.events.EventHandler(this);

  /**
   * The current scroll delta.
   * @type {goog.math.Coordinate}
   * @private
   */
  this.scrollDelta_ = new goog.math.Coordinate();

  /**
   * The container bounds.
   * @type {goog.math.Rect}
   * @private
   */
  this.containerBounds_ = goog.style.getBounds(containerNode);

  /**
   * The margin for triggering a scroll.
   * @type {number}
   * @private
   */
  this.margin_ = opt_margin || 0;

  /**
   * The bounding rectangle which if left triggers scrolling.
   * @type {goog.math.Rect}
   * @private
   */
  this.scrollBounds_ = opt_margin ?
      this.constrainBounds_(this.containerBounds_.clone()) :
      this.containerBounds_;

  this.setupListeners_(!!opt_externalMouseMoveTracking);
};
goog.inherits(goog.fx.DragScrollSupport, goog.Disposable);


/**
 * The scroll timer step in ms.
 * @type {number}
 * @private
 */
goog.fx.DragScrollSupport.TIMER_STEP_ = 50;


/**
 * The scroll step in pixels.
 * @type {number}
 * @private
 */
goog.fx.DragScrollSupport.SCROLL_STEP_ = 8;


/**
 * The suggested scrolling margin.
 * @type {number}
 */
goog.fx.DragScrollSupport.MARGIN = 32;


/**
 * Whether scrolling should be constrained to happen only when the cursor is
 * inside the container node.
 * @type {boolean}
 * @private
 */
goog.fx.DragScrollSupport.prototype.constrainScroll_ = false;


/**
 * Whether horizontal scrolling is allowed.
 * @type {boolean}
 * @private
 */
goog.fx.DragScrollSupport.prototype.horizontalScrolling_ = true;


/**
 * Sets whether scrolling should be constrained to happen only when the cursor
 * is inside the container node.
 * NOTE: If a margin is not set, then it does not make sense to
 * contain the scroll, because in that case scroll will never be triggered.
 * @param {boolean} constrain Whether scrolling should be constrained to happen
 *     only when the cursor is inside the container node.
 */
goog.fx.DragScrollSupport.prototype.setConstrainScroll = function(constrain) {
  this.constrainScroll_ = !!this.margin_ && constrain;
};


/**
 * Sets whether horizontal scrolling is allowed.
 * @param {boolean} scrolling Whether horizontal scrolling is allowed.
 */
goog.fx.DragScrollSupport.prototype.setHorizontalScrolling =
    function(scrolling) {
  this.horizontalScrolling_ = scrolling;
};


/**
 * Constrains the container bounds with respect to the margin.
 *
 * @param {goog.math.Rect} bounds The container element.
 * @return {goog.math.Rect} The bounding rectangle used to calculate scrolling
 *     direction.
 * @private
 */
goog.fx.DragScrollSupport.prototype.constrainBounds_ = function(bounds) {
  var margin = this.margin_;
  if (margin) {
    var quarterHeight = bounds.height * 0.25;
    var yMargin = Math.min(margin, quarterHeight);
    bounds.top += yMargin;
    bounds.height -= 2 * yMargin;

    var quarterWidth = bounds.width * 0.25;
    var xMargin = Math.min(margin, quarterWidth);
    bounds.top += xMargin;
    bounds.height -= 2 * xMargin;
  }
  return bounds;
};


/**
 * Attaches listeners and activates automatic scrolling.
 * @param {boolean} externalMouseMoveTracking Whether to enable internal
 *     mouse move event handling.
 * @private
 */
goog.fx.DragScrollSupport.prototype.setupListeners_ = function(
    externalMouseMoveTracking) {
  if (!externalMouseMoveTracking) {
    // Track mouse pointer position to determine scroll direction.
    this.eventHandler_.listen(goog.dom.getOwnerDocument(this.containerNode_),
        goog.events.EventType.MOUSEMOVE, this.onMouseMove);
  }

  // Scroll with a constant speed.
  this.eventHandler_.listen(this.scrollTimer_, goog.Timer.TICK, this.onTick_);
};


/**
 * Handler for timer tick event, scrolls the container by one scroll step if
 * needed.
 * @param {goog.events.Event} event Timer tick event.
 * @private
 */
goog.fx.DragScrollSupport.prototype.onTick_ = function(event) {
  this.containerNode_.scrollTop += this.scrollDelta_.y;
  this.containerNode_.scrollLeft += this.scrollDelta_.x;
};


/**
 * Handler for mouse moves events.
 * @param {goog.events.Event} event Mouse move event.
 */
goog.fx.DragScrollSupport.prototype.onMouseMove = function(event) {
  var deltaX = this.horizontalScrolling_ ? this.calculateScrollDelta(
      event.clientX, this.scrollBounds_.left, this.scrollBounds_.width) : 0;
  var deltaY = this.calculateScrollDelta(event.clientY,
      this.scrollBounds_.top, this.scrollBounds_.height);
  this.scrollDelta_.x = deltaX;
  this.scrollDelta_.y = deltaY;

  // If the scroll data is 0 or the event fired outside of the
  // bounds of the container node.
  if ((!deltaX && !deltaY) ||
      (this.constrainScroll_ &&
       !this.isInContainerBounds_(event.clientX, event.clientY))) {
    this.scrollTimer_.stop();
  } else if (!this.scrollTimer_.enabled) {
    this.scrollTimer_.start();
  }
};


/**
 * Gets whether the input coordinate is in the container bounds.
 * @param {number} x The x coordinate.
 * @param {number} y The y coordinate.
 * @return {boolean} Whether the input coordinate is in the container bounds.
 * @private
 */
goog.fx.DragScrollSupport.prototype.isInContainerBounds_ = function(x, y) {
  var containerBounds = this.containerBounds_;
  return containerBounds.left <= x &&
         containerBounds.left + containerBounds.width >= x &&
         containerBounds.top <= y &&
         containerBounds.top + containerBounds.height >= y;
};


/**
 * Calculates scroll delta.
 *
 * @param {number} coordinate Current mouse pointer coordinate.
 * @param {number} min The coordinate value below which scrolling up should be
 *     started.
 * @param {number} rangeLength The length of the range in which scrolling should
 *     be disabled and above which scrolling down should be started.
 * @return {number} The calculated scroll delta.
 * @protected
 */
goog.fx.DragScrollSupport.prototype.calculateScrollDelta = function(
    coordinate, min, rangeLength) {
  var delta = 0;
  if (coordinate < min) {
    delta = -goog.fx.DragScrollSupport.SCROLL_STEP_;
  } else if (coordinate > min + rangeLength) {
    delta = goog.fx.DragScrollSupport.SCROLL_STEP_;
  }
  return delta;
};


/** @override */
goog.fx.DragScrollSupport.prototype.disposeInternal = function() {
  goog.fx.DragScrollSupport.superClass_.disposeInternal.call(this);
  this.eventHandler_.dispose();
  this.scrollTimer_.dispose();
};