Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/net/multiiframeloadmonitor.js
2868 views
1
// Copyright 2008 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 Class that can be used to determine when multiple iframes have
17
* been loaded. Refactored from static APIs in IframeLoadMonitor.
18
*/
19
goog.provide('goog.net.MultiIframeLoadMonitor');
20
21
goog.require('goog.events');
22
goog.require('goog.net.IframeLoadMonitor');
23
24
25
26
/**
27
* Provides a wrapper around IframeLoadMonitor, to allow the caller to wait for
28
* multiple iframes to load.
29
*
30
* @param {Array<HTMLIFrameElement>} iframes Array of iframe elements to
31
* wait until they are loaded.
32
* @param {function():void} callback The callback to invoke once the frames have
33
* loaded.
34
* @param {boolean=} opt_hasContent true if the monitor should wait until the
35
* iframes have content (body.firstChild != null).
36
* @constructor
37
* @final
38
*/
39
goog.net.MultiIframeLoadMonitor = function(iframes, callback, opt_hasContent) {
40
/**
41
* Array of IframeLoadMonitors we use to track the loaded status of any
42
* currently unloaded iframes.
43
* @type {Array<goog.net.IframeLoadMonitor>}
44
* @private
45
*/
46
this.pendingIframeLoadMonitors_ = [];
47
48
/**
49
* Callback which is invoked when all of the iframes are loaded.
50
* @type {function():void}
51
* @private
52
*/
53
this.callback_ = callback;
54
55
for (var i = 0; i < iframes.length; i++) {
56
var iframeLoadMonitor =
57
new goog.net.IframeLoadMonitor(iframes[i], opt_hasContent);
58
if (iframeLoadMonitor.isLoaded()) {
59
// Already loaded - don't need to wait
60
iframeLoadMonitor.dispose();
61
} else {
62
// Iframe isn't loaded yet - register to be notified when it is
63
// loaded, and track this monitor so we can dispose later as
64
// required.
65
this.pendingIframeLoadMonitors_.push(iframeLoadMonitor);
66
goog.events.listen(
67
iframeLoadMonitor, goog.net.IframeLoadMonitor.LOAD_EVENT, this);
68
}
69
}
70
if (!this.pendingIframeLoadMonitors_.length) {
71
// All frames were already loaded
72
this.callback_();
73
}
74
};
75
76
77
/**
78
* Handles a pending iframe load monitor load event.
79
* @param {goog.events.Event} e The goog.net.IframeLoadMonitor.LOAD_EVENT event.
80
*/
81
goog.net.MultiIframeLoadMonitor.prototype.handleEvent = function(e) {
82
var iframeLoadMonitor = e.target;
83
// iframeLoadMonitor is now loaded, remove it from the array of
84
// pending iframe load monitors.
85
for (var i = 0; i < this.pendingIframeLoadMonitors_.length; i++) {
86
if (this.pendingIframeLoadMonitors_[i] == iframeLoadMonitor) {
87
this.pendingIframeLoadMonitors_.splice(i, 1);
88
break;
89
}
90
}
91
92
// Disposes of the iframe load monitor. We created this iframe load monitor
93
// and installed the single listener on it, so it is safe to dispose it
94
// in the middle of this event handler.
95
iframeLoadMonitor.dispose();
96
97
// If there are no more pending iframe load monitors, all the iframes
98
// have loaded, and so we invoke the callback.
99
if (!this.pendingIframeLoadMonitors_.length) {
100
this.callback_();
101
}
102
};
103
104
105
/**
106
* Stops monitoring the iframes, cleaning up any associated resources. In
107
* general, the object cleans up its own resources before invoking the
108
* callback, so this API should only be used if the caller wants to stop the
109
* monitoring before the iframes are loaded (for example, if the caller is
110
* implementing a timeout).
111
*/
112
goog.net.MultiIframeLoadMonitor.prototype.stopMonitoring = function() {
113
for (var i = 0; i < this.pendingIframeLoadMonitors_.length; i++) {
114
this.pendingIframeLoadMonitors_[i].dispose();
115
}
116
this.pendingIframeLoadMonitors_.length = 0;
117
};
118
119