| 1 |
// Copyright 2007 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 Utility class that monitors viewport size changes. |
| 17 |
* |
| 18 |
* @author [email protected] (Attila Bodis) |
| 19 |
* @see ../demos/viewportsizemonitor.html |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.dom.ViewportSizeMonitor'); |
| 23 |
|
| 24 |
goog.require('goog.dom'); |
| 25 |
goog.require('goog.events'); |
| 26 |
goog.require('goog.events.EventTarget'); |
| 27 |
goog.require('goog.events.EventType'); |
| 28 |
goog.require('goog.math.Size'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* This class can be used to monitor changes in the viewport size. Instances |
| 34 |
* dispatch a {@link goog.events.EventType.RESIZE} event when the viewport size |
| 35 |
* changes. Handlers can call {@link goog.dom.ViewportSizeMonitor#getSize} to |
| 36 |
* get the new viewport size. |
| 37 |
* |
| 38 |
* Use this class if you want to execute resize/reflow logic each time the |
| 39 |
* user resizes the browser window. This class is guaranteed to only dispatch |
| 40 |
* {@code RESIZE} events when the pixel dimensions of the viewport change. |
| 41 |
* (Internet Explorer fires resize events if any element on the page is resized, |
| 42 |
* even if the viewport dimensions are unchanged, which can lead to infinite |
| 43 |
* resize loops.) |
| 44 |
* |
| 45 |
* Example usage: |
| 46 |
* <pre> |
| 47 |
* var vsm = new goog.dom.ViewportSizeMonitor(); |
| 48 |
* goog.events.listen(vsm, goog.events.EventType.RESIZE, function(e) { |
| 49 |
* alert('Viewport size changed to ' + vsm.getSize()); |
| 50 |
* }); |
| 51 |
* </pre> |
| 52 |
* |
| 53 |
* Manually verified on IE6, IE7, FF2, Opera 11, Safari 4 and Chrome. |
| 54 |
* |
| 55 |
* @param {Window=} opt_window The window to monitor; defaults to the window in |
| 56 |
* which this code is executing. |
| 57 |
* @constructor |
| 58 |
* @extends {goog.events.EventTarget} |
| 59 |
*/ |
| 60 |
goog.dom.ViewportSizeMonitor = function(opt_window) { |
| 61 |
goog.dom.ViewportSizeMonitor.base(this, 'constructor'); |
| 62 |
|
| 63 |
/** |
| 64 |
* The window to monitor. Defaults to the window in which the code is running. |
| 65 |
* @private {Window} |
| 66 |
*/ |
| 67 |
this.window_ = opt_window || window; |
| 68 |
|
| 69 |
/** |
| 70 |
* Event listener key for window the window resize handler, as returned by |
| 71 |
* {@link goog.events.listen}. |
| 72 |
* @private {goog.events.Key} |
| 73 |
*/ |
| 74 |
this.listenerKey_ = goog.events.listen( |
| 75 |
this.window_, goog.events.EventType.RESIZE, this.handleResize_, false, |
| 76 |
this); |
| 77 |
|
| 78 |
/** |
| 79 |
* The most recently recorded size of the viewport, in pixels. |
| 80 |
* @private {goog.math.Size} |
| 81 |
*/ |
| 82 |
this.size_ = goog.dom.getViewportSize(this.window_); |
| 83 |
}; |
| 84 |
goog.inherits(goog.dom.ViewportSizeMonitor, goog.events.EventTarget); |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Returns a viewport size monitor for the given window. A new one is created |
| 89 |
* if it doesn't exist already. This prevents the unnecessary creation of |
| 90 |
* multiple spooling monitors for a window. |
| 91 |
* @param {Window=} opt_window The window to monitor; defaults to the window in |
| 92 |
* which this code is executing. |
| 93 |
* @return {!goog.dom.ViewportSizeMonitor} Monitor for the given window. |
| 94 |
*/ |
| 95 |
goog.dom.ViewportSizeMonitor.getInstanceForWindow = function(opt_window) { |
| 96 |
var currentWindow = opt_window || window; |
| 97 |
var uid = goog.getUid(currentWindow); |
| 98 |
|
| 99 |
return goog.dom.ViewportSizeMonitor.windowInstanceMap_[uid] = |
| 100 |
goog.dom.ViewportSizeMonitor.windowInstanceMap_[uid] || |
| 101 |
new goog.dom.ViewportSizeMonitor(currentWindow); |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Removes and disposes a viewport size monitor for the given window if one |
| 107 |
* exists. |
| 108 |
* @param {Window=} opt_window The window whose monitor should be removed; |
| 109 |
* defaults to the window in which this code is executing. |
| 110 |
*/ |
| 111 |
goog.dom.ViewportSizeMonitor.removeInstanceForWindow = function(opt_window) { |
| 112 |
var uid = goog.getUid(opt_window || window); |
| 113 |
|
| 114 |
goog.dispose(goog.dom.ViewportSizeMonitor.windowInstanceMap_[uid]); |
| 115 |
delete goog.dom.ViewportSizeMonitor.windowInstanceMap_[uid]; |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Map of window hash code to viewport size monitor for that window, if |
| 121 |
* created. |
| 122 |
* @type {Object<number,goog.dom.ViewportSizeMonitor>} |
| 123 |
* @private |
| 124 |
*/ |
| 125 |
goog.dom.ViewportSizeMonitor.windowInstanceMap_ = {}; |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* Returns the most recently recorded size of the viewport, in pixels. May |
| 130 |
* return null if no window resize event has been handled yet. |
| 131 |
* @return {goog.math.Size} The viewport dimensions, in pixels. |
| 132 |
*/ |
| 133 |
goog.dom.ViewportSizeMonitor.prototype.getSize = function() { |
| 134 |
// Return a clone instead of the original to preserve encapsulation. |
| 135 |
return this.size_ ? this.size_.clone() : null; |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
/** @override */ |
| 140 |
goog.dom.ViewportSizeMonitor.prototype.disposeInternal = function() { |
| 141 |
goog.dom.ViewportSizeMonitor.superClass_.disposeInternal.call(this); |
| 142 |
|
| 143 |
if (this.listenerKey_) { |
| 144 |
goog.events.unlistenByKey(this.listenerKey_); |
| 145 |
this.listenerKey_ = null; |
| 146 |
} |
| 147 |
|
| 148 |
this.window_ = null; |
| 149 |
this.size_ = null; |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
/** |
| 154 |
* Handles window resize events by measuring the dimensions of the |
| 155 |
* viewport and dispatching a {@link goog.events.EventType.RESIZE} event if the |
| 156 |
* current dimensions are different from the previous ones. |
| 157 |
* @param {goog.events.Event} event The window resize event to handle. |
| 158 |
* @private |
| 159 |
*/ |
| 160 |
goog.dom.ViewportSizeMonitor.prototype.handleResize_ = function(event) { |
| 161 |
var size = goog.dom.getViewportSize(this.window_); |
| 162 |
if (!goog.math.Size.equals(size, this.size_)) { |
| 163 |
this.size_ = size; |
| 164 |
this.dispatchEvent(goog.events.EventType.RESIZE); |
| 165 |
} |
| 166 |
}; |