Debian Codegraph @

closure-library/closure/goog/dom/bufferedviewportsizemonitor.js

raw
1 // Copyright 2012 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 A viewport size monitor that buffers RESIZE events until the
17 * window size has stopped changing, within a specified period of time. For
18 * every RESIZE event dispatched, this will dispatch up to two *additional*
19 * events:
20 * - {@link #EventType.RESIZE_WIDTH} if the viewport's width has changed since
21 * the last buffered dispatch.
22 * - {@link #EventType.RESIZE_HEIGHT} if the viewport's height has changed since
23 * the last buffered dispatch.
24 * You likely only need to listen to one of the three events. But if you need
25 * more, just be cautious of duplicating effort.
26 *
27 */
28
29 goog.provide('goog.dom.BufferedViewportSizeMonitor');
30
31 goog.require('goog.asserts');
32 goog.require('goog.async.Delay');
33 goog.require('goog.events');
34 goog.require('goog.events.EventTarget');
35 goog.require('goog.events.EventType');
36
37
38
39 /**
40 * Creates a new BufferedViewportSizeMonitor.
41 * @param {!goog.dom.ViewportSizeMonitor} viewportSizeMonitor The
42 * underlying viewport size monitor.
43 * @param {number=} opt_bufferMs The buffer time, in ms. If not specified, this
44 * value defaults to {@link #RESIZE_EVENT_DELAY_MS_}.
45 * @constructor
46 * @extends {goog.events.EventTarget}
47 * @final
48 */
49 goog.dom.BufferedViewportSizeMonitor = function(
50 viewportSizeMonitor, opt_bufferMs) {
51 goog.dom.BufferedViewportSizeMonitor.base(this, 'constructor');
52
53 /**
54 * Delay for the resize event.
55 * @private {goog.async.Delay}
56 */
57 this.resizeDelay_;
58
59 /**
60 * The underlying viewport size monitor.
61 * @type {goog.dom.ViewportSizeMonitor}
62 * @private
63 */
64 this.viewportSizeMonitor_ = viewportSizeMonitor;
65
66 /**
67 * The current size of the viewport.
68 * @type {goog.math.Size}
69 * @private
70 */
71 this.currentSize_ = this.viewportSizeMonitor_.getSize();
72
73 /**
74 * The resize buffer time in ms.
75 * @type {number}
76 * @private
77 */
78 this.resizeBufferMs_ = opt_bufferMs ||
79 goog.dom.BufferedViewportSizeMonitor.RESIZE_EVENT_DELAY_MS_;
80
81 /**
82 * Listener key for the viewport size monitor.
83 * @type {goog.events.Key}
84 * @private
85 */
86 this.listenerKey_ = goog.events.listen(
87 viewportSizeMonitor, goog.events.EventType.RESIZE, this.handleResize_,
88 false, this);
89 };
90 goog.inherits(goog.dom.BufferedViewportSizeMonitor, goog.events.EventTarget);
91
92
93 /**
94 * Additional events to dispatch.
95 * @enum {string}
96 */
97 goog.dom.BufferedViewportSizeMonitor.EventType = {
98 RESIZE_HEIGHT: goog.events.getUniqueId('resizeheight'),
99 RESIZE_WIDTH: goog.events.getUniqueId('resizewidth')
100 };
101
102
103 /**
104 * Default number of milliseconds to wait after a resize event to relayout the
105 * page.
106 * @type {number}
107 * @const
108 * @private
109 */
110 goog.dom.BufferedViewportSizeMonitor.RESIZE_EVENT_DELAY_MS_ = 100;
111
112
113 /** @override */
114 goog.dom.BufferedViewportSizeMonitor.prototype.disposeInternal = function() {
115 goog.events.unlistenByKey(this.listenerKey_);
116 goog.dom.BufferedViewportSizeMonitor.base(this, 'disposeInternal');
117 };
118
119
120 /**
121 * Handles resize events on the underlying ViewportMonitor.
122 * @private
123 */
124 goog.dom.BufferedViewportSizeMonitor.prototype.handleResize_ = function() {
125 // Lazily create when needed.
126 if (!this.resizeDelay_) {
127 this.resizeDelay_ =
128 new goog.async.Delay(this.onWindowResize_, this.resizeBufferMs_, this);
129 this.registerDisposable(this.resizeDelay_);
130 }
131 this.resizeDelay_.start();
132 };
133
134
135 /**
136 * Window resize callback that determines whether to reflow the view contents.
137 * @private
138 */
139 goog.dom.BufferedViewportSizeMonitor.prototype.onWindowResize_ = function() {
140 if (this.viewportSizeMonitor_.isDisposed()) {
141 return;
142 }
143
144 var previousSize = this.currentSize_;
145 var currentSize = this.viewportSizeMonitor_.getSize();
146
147 goog.asserts.assert(currentSize, 'Viewport size should be set at this point');
148
149 this.currentSize_ = currentSize;
150
151 if (previousSize) {
152 var resized = false;
153
154 // Width has changed
155 if (previousSize.width != currentSize.width) {
156 this.dispatchEvent(
157 goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_WIDTH);
158 resized = true;
159 }
160
161 // Height has changed
162 if (previousSize.height != currentSize.height) {
163 this.dispatchEvent(
164 goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_HEIGHT);
165 resized = true;
166 }
167
168 // If either has changed, this is a resize event.
169 if (resized) {
170 this.dispatchEvent(goog.events.EventType.RESIZE);
171 }
172
173 } else {
174 // If we didn't have a previous size, we consider all events to have
175 // changed.
176 this.dispatchEvent(
177 goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_HEIGHT);
178 this.dispatchEvent(
179 goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_WIDTH);
180 this.dispatchEvent(goog.events.EventType.RESIZE);
181 }
182 };
183
184
185 /**
186 * Returns the current size of the viewport.
187 * @return {goog.math.Size?} The current viewport size.
188 */
189 goog.dom.BufferedViewportSizeMonitor.prototype.getSize = function() {
190 return this.currentSize_ ? this.currentSize_.clone() : null;
191 };