Path: blob/trunk/third_party/closure/goog/labs/net/image.js
2868 views
// Copyright 2012 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 Simple image loader, used for preloading.16* @author [email protected] (Nathan Naze)17*/1819goog.provide('goog.labs.net.image');2021goog.require('goog.Promise');22goog.require('goog.events.EventHandler');23goog.require('goog.events.EventType');24goog.require('goog.net.EventType');25goog.require('goog.userAgent');262728/**29* Loads a single image. Useful for preloading images.30*31* @param {string} uri URI of the image.32* @param {(!Image|function(): !Image)=} opt_image If present, instead of33* creating a new Image instance the function will use the passed Image34* instance or the result of calling the Image factory respectively. This35* can be used to control exactly how Image instances are created, for36* example if they should be created in a particular document element, or37* have fields that will trigger CORS image fetches.38* @return {!goog.Promise<!Image>} A Promise that will be resolved with the39* given image if the image successfully loads.40*/41goog.labs.net.image.load = function(uri, opt_image) {42return new goog.Promise(function(resolve, reject) {43var image;44if (!goog.isDef(opt_image)) {45image = new Image();46} else if (goog.isFunction(opt_image)) {47image = opt_image();48} else {49image = opt_image;50}5152// IE's load event on images can be buggy. For older browsers, wait for53// readystatechange events and check if readyState is 'complete'.54// See:55// http://msdn.microsoft.com/en-us/library/ie/ms536957(v=vs.85).aspx56// http://msdn.microsoft.com/en-us/library/ie/ms534359(v=vs.85).aspx57//58// Starting with IE11, start using standard 'load' events.59// See:60// http://msdn.microsoft.com/en-us/library/ie/dn467845(v=vs.85).aspx61var loadEvent = (goog.userAgent.IE && goog.userAgent.VERSION < 11) ?62goog.net.EventType.READY_STATE_CHANGE :63goog.events.EventType.LOAD;6465var handler = new goog.events.EventHandler();66handler.listen(67image, [loadEvent, goog.net.EventType.ABORT, goog.net.EventType.ERROR],68function(e) {6970// We only registered listeners for READY_STATE_CHANGE for IE.71// If readyState is now COMPLETE, the image has loaded.72// See related comment above.73if (e.type == goog.net.EventType.READY_STATE_CHANGE &&74image.readyState != goog.net.EventType.COMPLETE) {75return;76}7778// At this point, we know whether the image load was successful79// and no longer care about image events.80goog.dispose(handler);8182// Whether the image successfully loaded.83if (e.type == loadEvent) {84resolve(image);85} else {86reject(null);87}88});8990// Initiate the image request.91image.src = uri;92});93};949596