Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/labs/net/image.js
2868 views
1
// Copyright 2012 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 Simple image loader, used for preloading.
17
* @author [email protected] (Nathan Naze)
18
*/
19
20
goog.provide('goog.labs.net.image');
21
22
goog.require('goog.Promise');
23
goog.require('goog.events.EventHandler');
24
goog.require('goog.events.EventType');
25
goog.require('goog.net.EventType');
26
goog.require('goog.userAgent');
27
28
29
/**
30
* Loads a single image. Useful for preloading images.
31
*
32
* @param {string} uri URI of the image.
33
* @param {(!Image|function(): !Image)=} opt_image If present, instead of
34
* creating a new Image instance the function will use the passed Image
35
* instance or the result of calling the Image factory respectively. This
36
* can be used to control exactly how Image instances are created, for
37
* example if they should be created in a particular document element, or
38
* have fields that will trigger CORS image fetches.
39
* @return {!goog.Promise<!Image>} A Promise that will be resolved with the
40
* given image if the image successfully loads.
41
*/
42
goog.labs.net.image.load = function(uri, opt_image) {
43
return new goog.Promise(function(resolve, reject) {
44
var image;
45
if (!goog.isDef(opt_image)) {
46
image = new Image();
47
} else if (goog.isFunction(opt_image)) {
48
image = opt_image();
49
} else {
50
image = opt_image;
51
}
52
53
// IE's load event on images can be buggy. For older browsers, wait for
54
// readystatechange events and check if readyState is 'complete'.
55
// See:
56
// http://msdn.microsoft.com/en-us/library/ie/ms536957(v=vs.85).aspx
57
// http://msdn.microsoft.com/en-us/library/ie/ms534359(v=vs.85).aspx
58
//
59
// Starting with IE11, start using standard 'load' events.
60
// See:
61
// http://msdn.microsoft.com/en-us/library/ie/dn467845(v=vs.85).aspx
62
var loadEvent = (goog.userAgent.IE && goog.userAgent.VERSION < 11) ?
63
goog.net.EventType.READY_STATE_CHANGE :
64
goog.events.EventType.LOAD;
65
66
var handler = new goog.events.EventHandler();
67
handler.listen(
68
image, [loadEvent, goog.net.EventType.ABORT, goog.net.EventType.ERROR],
69
function(e) {
70
71
// We only registered listeners for READY_STATE_CHANGE for IE.
72
// If readyState is now COMPLETE, the image has loaded.
73
// See related comment above.
74
if (e.type == goog.net.EventType.READY_STATE_CHANGE &&
75
image.readyState != goog.net.EventType.COMPLETE) {
76
return;
77
}
78
79
// At this point, we know whether the image load was successful
80
// and no longer care about image events.
81
goog.dispose(handler);
82
83
// Whether the image successfully loaded.
84
if (e.type == loadEvent) {
85
resolve(image);
86
} else {
87
reject(null);
88
}
89
});
90
91
// Initiate the image request.
92
image.src = uri;
93
});
94
};
95
96