imageloader.js revision c365f6f0f0482e0486a2be7782b96b7aefb89602
919N/A * A group for images. A group can have one time limit and a series of triggers. Thus the images belonging to this group must share these constraints
919N/A Y.ImgLoadGroup = function() {
1349N/A * Distance below the fold for which images are loaded. Images are not loaded until they are at most this distance away from (or above) the fold
577N/A * This check is performed at page load and after any window scroll or window resize event (until all images are loaded)
970N/A * Class name that will identify images belonging to the group. This class name will be removed from each element in order to fetch images
577N/A var groupProto = {
577N/A * Elements are stored during the _foldCheck function and reused later during any subsequent _foldCheck calls - gives a slight performance improvement when the page fold is repeatedly checked
970N/A this._classImageEls = null;
this._className = null;
* Boolean tracking whether the window scroll and window resize triggers have been set for fold groups
this._areFoldTriggersSet = false;
// TODO can't get this to work. 'load' for window doesn't even fire, and 'domready' won't fire anything if the dom is already ready
* @param {String|HTMLElement} context The HTML element id or reference to assign the trigger event to // TODO type
/* Need to wrap the fetch function. Event Util can't distinguish prototyped functions of different instantiations
* Leads to this scenario: groupA and groupZ both have window-scroll triggers. groupZ also has a 2-sec timeout (groupA has no timeout).
* groupZ's timeout fires; we remove the triggers. The detach call finds the first window-scroll event with Y.ILG.p.fetch, which is groupA's.
var wrappedFetch = function() {
this.fetch();
* @param {Object} obj The object on which to attach the event. obj is optional - by default the event is attached to the Y instance
if (! name) {
var wrappedFetch = function() {
this.fetch();
* Sets the window scroll and window resize triggers for any group that is fold-conditional (i.e., has a fold distance set)
_setFoldTriggers: function() {
if (this._areFoldTriggersSet) {
var wrappedFoldCheck = function() {
this._foldCheck();
this._areFoldTriggersSet = true;
_onloadTasks: function() {
this._foldCheck();
_getFetchTimeout: function() {
var self = this;
registerImage: function() {
if (! domId) {
* This method is called when a trigger fires or the time limit expires; it shouldn't be called externally, but is not private in the event that it needs to be called immediately
fetch: function() {
this._clearTriggers();
this._fetchByClass();
_clearTriggers: function() {
* Checks the position of each image in the group. If any part of the image is within the specified distance of the client viewport, the image is fetched immediately
_foldCheck: function() {
var allFetched = true;
if (this._className) {
if (this._classImageEls === null) {
this._classImageEls = [];
classedEls.each( function(node) { this._classImageEls.push( { el: node, y: node.getY(), fetched: false } ); }, this);
allFetched = false;
if (allFetched) {
this._clearTriggers();
* Finds all elements in the Dom with the class name specified in the group. Removes the class from the element in order to let the style definitions trigger the image fetching
_fetchByClass: function() {
if (! this._className) {
Y.ImgLoadImgObj = function() {
this._init();
domId: {
value: null,
writeOnce: true
bgUrl: {
value: null
srcUrl: {
value: null
* Pixel width of the image. Will be set as a "width" attribute on the DOM element after the image is fetched
width: {
value: null
* Pixel height of the image. Will be set as a "height" attribute on the DOM element after the image is fetched
height: {
value: null
setVisible: {
value: false
* PNG images get special treatment in that the URL is specified through AlphaImageLoader for IE, versions 6 and earlier
isPng: {
value: false
sizingMethod: {
enabled: {
var imgProto = {
_init: function() {
this._fetched = false;
this._imgEl = null;
* The Y position is checked only for images registered with fold-conditional groups. The position is checked first at page load
* and this caching enhancement assumes that the image's vertical position won't change after that first check
this._yPos = null;
* This method shouldn't be called externally, but is not private in the event that it needs to be called immediately
* @param {Int} withinY The pixel distance from the top of the page, for which if the image lies within this distance, it will be fetched. Undefined indicates that no check should be made, and the image should always be fetched
if (this._fetched) {
if (! el) {
if (withinY) {
el.setStyle('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + this.get('url') + '", sizingMethod="' + this.get('sizingMethod') + '", enabled="' + this.get('enabled') + '")');
this._fetched = true;
_getImgEl: function() {
if (this._imgEl === null) {
return this._imgEl;
_getYPos: function() {
if (this._yPos === null) {
return this._yPos;