Skip to content

Commit

Permalink
* Added img! plugin and test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
asilvas committed Dec 10, 2012
1 parent 83f445b commit 0f1c953
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/curl/plugin/img.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* plugin loads and returns an IMG once available.
*/

define(/*=='img',==*/['domReady!'], function () {
return {
'load': function (absId, require, loaded, config) {
// init
var img = document.createElement('img');
img.style.display = "none"; // should not be visible during load
var done = false; // state

// wireup events
img.onload = callback;
img.onreadystatechange = callback;
img.onerror = error;

function callback() {
//console.log("img!callback.done:", done, ", readyState: ", img.readyState);
if (done === true)
return; // ignore dups

if (typeof img.readyState === 'string') { // hello, IE (and Opera)
if (img.readyState === 'complete') { // done
done = true; // ignore dups
success();
}
return;
}

done = true;

success();
}

function success() {
img.style.display = "block"; // must show before calculating dimensions
var o = { dom: img, width: img.width, height: img.height }; // calc dimensions before removing from DOM (some browsers will calc improperly)
document.body.removeChild(img); // remove from DOM before returning result
loaded(o);
}

function error() {
//console.log("img!callback.ERR:", done, ", readyState: ", img.readyState);
loaded.error("IMG load error: " + absId);
}

document.body.appendChild(img); // requires domReady!
img.src = absId; // set source after adding to body

if (done === false && img.complete === true) { // IE
callback();
}
}
};
});
63 changes: 63 additions & 0 deletions test/img.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE HTML>
<html>
<head>
<title>curl loading test for img! plugin</title>

<script>

curl = {
debug: true,
paths: {
curl: '../src/curl'
},
pluginPath: 'curl/plugin'
};

</script>
<script src="../src/curl.js" type="text/javascript"></script>


<script type="text/javascript">

curl(
[
'img!img/circle.png',
'img!img/rect.png',
'domReady!'
]
).then(
function (img1, img2) {
write('SUCCESS: images loaded, img1: ' + (img1.width + 'x' + img1.height) + ', img2: ' + (img2.width + 'x' + img2.height));
document.body.appendChild(img1.dom);
document.body.appendChild(img2.dom);
},
function (ex) {
write('FAILED: ' + ex.message)
}
);

curl([ 'img!img/does_not_exist.png', 'domReady!' ])
.then(
function (img3) {
write("FAILED: img3 does not exist, and returned success anyway, which is BAD.");
},
function (ex) {
write("SUCCESS: img3 does not exist, and failed to load, as desired.");
}
);

function write (msg) {
curl(['domReady!'], function () {
document.body.appendChild(document.createElement('div')).innerHTML = msg;
});
}

</script>

</head>
<body>

<p>We should know the dimensions of both images, and they should be hidden in the root of the body until loaded.</p>

</body>
</html>
Binary file added test/img/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/img/rect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0f1c953

Please sign in to comment.