Path: blob/trunk/third_party/closure/goog/net/multiiframeloadmonitor.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Class that can be used to determine when multiple iframes have16* been loaded. Refactored from static APIs in IframeLoadMonitor.17*/18goog.provide('goog.net.MultiIframeLoadMonitor');1920goog.require('goog.events');21goog.require('goog.net.IframeLoadMonitor');22232425/**26* Provides a wrapper around IframeLoadMonitor, to allow the caller to wait for27* multiple iframes to load.28*29* @param {Array<HTMLIFrameElement>} iframes Array of iframe elements to30* wait until they are loaded.31* @param {function():void} callback The callback to invoke once the frames have32* loaded.33* @param {boolean=} opt_hasContent true if the monitor should wait until the34* iframes have content (body.firstChild != null).35* @constructor36* @final37*/38goog.net.MultiIframeLoadMonitor = function(iframes, callback, opt_hasContent) {39/**40* Array of IframeLoadMonitors we use to track the loaded status of any41* currently unloaded iframes.42* @type {Array<goog.net.IframeLoadMonitor>}43* @private44*/45this.pendingIframeLoadMonitors_ = [];4647/**48* Callback which is invoked when all of the iframes are loaded.49* @type {function():void}50* @private51*/52this.callback_ = callback;5354for (var i = 0; i < iframes.length; i++) {55var iframeLoadMonitor =56new goog.net.IframeLoadMonitor(iframes[i], opt_hasContent);57if (iframeLoadMonitor.isLoaded()) {58// Already loaded - don't need to wait59iframeLoadMonitor.dispose();60} else {61// Iframe isn't loaded yet - register to be notified when it is62// loaded, and track this monitor so we can dispose later as63// required.64this.pendingIframeLoadMonitors_.push(iframeLoadMonitor);65goog.events.listen(66iframeLoadMonitor, goog.net.IframeLoadMonitor.LOAD_EVENT, this);67}68}69if (!this.pendingIframeLoadMonitors_.length) {70// All frames were already loaded71this.callback_();72}73};747576/**77* Handles a pending iframe load monitor load event.78* @param {goog.events.Event} e The goog.net.IframeLoadMonitor.LOAD_EVENT event.79*/80goog.net.MultiIframeLoadMonitor.prototype.handleEvent = function(e) {81var iframeLoadMonitor = e.target;82// iframeLoadMonitor is now loaded, remove it from the array of83// pending iframe load monitors.84for (var i = 0; i < this.pendingIframeLoadMonitors_.length; i++) {85if (this.pendingIframeLoadMonitors_[i] == iframeLoadMonitor) {86this.pendingIframeLoadMonitors_.splice(i, 1);87break;88}89}9091// Disposes of the iframe load monitor. We created this iframe load monitor92// and installed the single listener on it, so it is safe to dispose it93// in the middle of this event handler.94iframeLoadMonitor.dispose();9596// If there are no more pending iframe load monitors, all the iframes97// have loaded, and so we invoke the callback.98if (!this.pendingIframeLoadMonitors_.length) {99this.callback_();100}101};102103104/**105* Stops monitoring the iframes, cleaning up any associated resources. In106* general, the object cleans up its own resources before invoking the107* callback, so this API should only be used if the caller wants to stop the108* monitoring before the iframes are loaded (for example, if the caller is109* implementing a timeout).110*/111goog.net.MultiIframeLoadMonitor.prototype.stopMonitoring = function() {112for (var i = 0; i < this.pendingIframeLoadMonitors_.length; i++) {113this.pendingIframeLoadMonitors_[i].dispose();114}115this.pendingIframeLoadMonitors_.length = 0;116};117118119