aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/dom/bufferedviewportsizemonitor.js
blob: ac29f2670235d741585a7fd7d14574c395c58bd1 (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
// Copyright 2012 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 A viewport size monitor that buffers RESIZE events until the
 * window size has stopped changing, within a specified period of time.  For
 * every RESIZE event dispatched, this will dispatch up to two *additional*
 * events:
 * - {@link #EventType.RESIZE_WIDTH} if the viewport's width has changed since
 *   the last buffered dispatch.
 * - {@link #EventType.RESIZE_HEIGHT} if the viewport's height has changed since
 *   the last buffered dispatch.
 * You likely only need to listen to one of the three events.  But if you need
 * more, just be cautious of duplicating effort.
 *
 */

goog.provide('goog.dom.BufferedViewportSizeMonitor');

goog.require('goog.async.Delay');
goog.require('goog.events');
goog.require('goog.events.EventTarget');
goog.require('goog.events.EventType');
goog.require('goog.math.Size');



/**
 * Creates a new BufferedViewportSizeMonitor.
 * @param {!goog.dom.ViewportSizeMonitor} viewportSizeMonitor The
 *     underlying viewport size monitor.
 * @param {number=} opt_bufferMs The buffer time, in ms. If not specified, this
 *     value defaults to {@link #RESIZE_EVENT_DELAY_MS_}.
 * @constructor
 * @extends {goog.events.EventTarget}
 */
goog.dom.BufferedViewportSizeMonitor = function(
    viewportSizeMonitor, opt_bufferMs) {
  goog.base(this);

  /**
   * The underlying viewport size monitor.
   * @type {goog.dom.ViewportSizeMonitor}
   * @private
   */
  this.viewportSizeMonitor_ = viewportSizeMonitor;

  /**
   * The current size of the viewport.
   * @type {goog.math.Size}
   * @private
   */
  this.currentSize_ = this.viewportSizeMonitor_.getSize();

  /**
   * The previously recorded size of the viewport.
   * @type {goog.math.Size}
   * @private
   */
  this.previousSize_ = this.currentSize_;

  /**
   * The resize buffer time in ms.
   * @type {number}
   * @private
   */
  this.resizeBufferMs_ = opt_bufferMs ||
      goog.dom.BufferedViewportSizeMonitor.RESIZE_EVENT_DELAY_MS_;

  goog.events.listen(
      this.viewportSizeMonitor_,
      goog.events.EventType.RESIZE,
      this.handleResize_,
      false /* opt_capture */,
      this);
};
goog.inherits(goog.dom.BufferedViewportSizeMonitor, goog.events.EventTarget);


/**
 * Additional events to dispatch.
 * @enum {string}
 */
goog.dom.BufferedViewportSizeMonitor.EventType = {
  RESIZE_HEIGHT: goog.events.getUniqueId('rh'),
  RESIZE_WIDTH: goog.events.getUniqueId('rw')
};


/**
 * Delay for the resize event.
 * @type {goog.async.Delay}
 * @private
 */
goog.dom.BufferedViewportSizeMonitor.prototype.resizeDelay_;


/**
 * Whether the monitoring has been paused.
 * @type {boolean}
 * @private
 */
goog.dom.BufferedViewportSizeMonitor.prototype.paused_;


/**
 * Default number of milliseconds to wait after a resize event to relayout the
 * page.
 * @type {number}
 * @const
 * @private
 */
goog.dom.BufferedViewportSizeMonitor.RESIZE_EVENT_DELAY_MS_ = 100;


/** @override */
goog.dom.BufferedViewportSizeMonitor.prototype.disposeInternal =
    function() {
  goog.events.unlisten(
      this.viewportSizeMonitor_,
      goog.events.EventType.RESIZE,
      this.handleResize_,
      false /* opt_capture */,
      this);
  goog.base(this, 'disposeInternal');
};


/**
 * Handles resize events on the underlying ViewportMonitor.
 * @param {goog.events.Event} e The resize event.
 * @private
 */
goog.dom.BufferedViewportSizeMonitor.prototype.handleResize_ =
    function(e) {
  if (!this.resizeDelay_) {
    this.resizeDelay_ = new goog.async.Delay(
        goog.bind(this.onWindowResize_, this, e), this.resizeBufferMs_);
    this.registerDisposable(this.resizeDelay_);
  }
  this.resizeDelay_.start();
};


/**
 * Window resize callback that determines whether to reflow the view contents.
 * @param {goog.events.Event} e The resize event.
 * @private
 */
goog.dom.BufferedViewportSizeMonitor.prototype.onWindowResize_ =
    function(e) {
  if (this.viewportSizeMonitor_.isDisposed()) {
    return;
  }
  var viewportSize = this.viewportSizeMonitor_.getSize();
  if (!this.paused_ &&
      !goog.math.Size.equals(viewportSize, this.currentSize_)) {
    if (!this.previousSize_ ||
        viewportSize.width != this.previousSize_.width) {
      this.dispatchEvent(
          goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_WIDTH);
    }
    if (!this.previousSize_ ||
        viewportSize.height != this.previousSize_.height) {
      this.dispatchEvent(
          goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_HEIGHT);
    }
    this.previousSize_ = this.currentSize_;
    this.currentSize_ = viewportSize;
    this.dispatchEvent(e);
  }
};


/**
 * Returns the current size of the viewport.
 * @return {goog.math.Size?} The current viewport size.
 */
goog.dom.BufferedViewportSizeMonitor.prototype.getSize = function() {
  return this.currentSize_ ? this.currentSize_.clone() : null;
};


/**
 * Returns the previous size of the viewport.
 * @return {goog.math.Size?} The previous size of the viewport on the last
 *     RESIZE event.
 */
goog.dom.BufferedViewportSizeMonitor.prototype.getPreviousSize = function() {
  return this.previousSize_ ? this.previousSize_.clone() : null;
};


/**
 * Pauses and unpauses viewport size monitoring.
 * @param {boolean} paused True to pause.
 */
goog.dom.BufferedViewportSizeMonitor.prototype.setPaused = function(paused) {
  this.paused_ = paused;
};