multiiframeloadmonitor.js 4.0 KB

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