alert()
@example Galleria.log("hello", document.body, [1,2,3]);
*/
Galleria.log = function() {
try {
window.console.log.apply( window.console, Utils.array(arguments) );
} catch( e ) {
try {
opera.postError.apply( opera, arguments );
} catch( er ) {
alert( Utils.array(arguments).split(', ') );
}
}
};
/**
Method for raising errors
@param {String} msg The message to throw
@param {Boolean} fatal Set this to true to override debug settings and display a fatal error
*/
Galleria.raise = function( msg, fatal ) {
if ( DEBUG || fatal ) {
var type = fatal ? 'Fatal error' : 'Error';
throw new Error( type + ': ' + msg );
}
};
/**
Adds preload, cache, scale and crop functionality
@constructor
@requires jQuery
@param {Number} id Optional id to keep track of instances
*/
Galleria.Picture = function( id ) {
// save the id
this.id = id || null;
// the image should be null until loaded
this.image = null;
// Create a new container
this.container = Utils.create('galleria-image');
// add container styles
$( this.container ).css({
overflow: 'hidden',
position: 'relative' // for IE Standards mode
});
// saves the original meassurements
this.original = {
width: 0,
height: 0
};
// flag when the image is ready
this.ready = false;
// flag when the image is loaded
this.loaded = false;
};
Galleria.Picture.prototype = {
// the inherited cache object
cache: {},
// creates a new image and adds it to cache when loaded
add: function( src ) {
var self = this;
// create the image
var image = new Image();
// force a block display
$( image ).css( 'display', 'block');
if ( self.cache[ src ] ) {
// no need to onload if the image is cached
image.src = src;
self.loaded = true;
self.original = {
height: image.height,
width: image.width
};
return image;
}
// begin preload and insert in cache when done
image.onload = function() {
self.original = {
height: this.height,
width: this.width
};
self.cache[ src ] = src; // will override old cache
self.loaded = true;
};
image.src = src;
return image;
},
// show the image on stage
show: function() {
Utils.show( this.image );
},
// hide the image
hide: function() {
Utils.moveOut( this.image );
},
clear: function() {
this.image = null;
},
/**
Checks if an image is in cache
@param {String} src The image source path, ex '/path/to/img.jpg'
@returns {Boolean}
*/
isCached: function( src ) {
return !!this.cache[src];
},
/**
Loads an image and call the callback when ready.
Will also add the image to cache.
@param {String} src The image source path, ex '/path/to/img.jpg'
@param {Function} callback The function to be executed when the image is loaded & scaled
@returns {jQuery} The image container object
*/
load: function(src, callback) {
// save the instance
var self = this;
$( this.container ).empty(true);
// add the image to cache and hide it
this.image = this.add( src );
Utils.hide( this.image );
// append the image into the container
$( this.container ).append( this.image );
// check for loaded image using a timeout
Utils.wait({
until: function() {
// TODO this should be properly tested in Opera
return self.loaded && self.image.complete && self.image.width;
},
success: function() {
// call success
window.setTimeout(function() { callback.call( self, self ); }, 50 );
},
error: function() {
window.setTimeout(function() { callback.call( self, self ); }, 50 );
Galleria.raise('image not loaded in 10 seconds: '+ src);
},
timeout: 10000
});
// return the container
return this.container;
},
/**
Scales and crops the image
@param {Object} options The method takes an object with a number of options:
alert()
@example Galleria.log("hello", document.body, [1,2,3]);
*/
Galleria.log = function() {
try {
window.console.log.apply( window.console, Utils.array(arguments) );
} catch( e ) {
try {
opera.postError.apply( opera, arguments );
} catch( er ) {
alert( Utils.array(arguments).split(', ') );
}
}
};
/**
Method for raising errors
@param {String} msg The message to throw
@param {Boolean} fatal Set this to true to override debug settings and display a fatal error
*/
Galleria.raise = function( msg, fatal ) {
if ( DEBUG || fatal ) {
var type = fatal ? 'Fatal error' : 'Error';
throw new Error( type + ': ' + msg );
}
};
/**
Adds preload, cache, scale and crop functionality
@constructor
@requires jQuery
@param {Number} id Optional id to keep track of instances
*/
Galleria.Picture = function( id ) {
// save the id
this.id = id || null;
// the image should be null until loaded
this.image = null;
// Create a new container
this.container = Utils.create('galleria-image');
// add container styles
$( this.container ).css({
overflow: 'hidden',
position: 'relative' // for IE Standards mode
});
// saves the original meassurements
this.original = {
width: 0,
height: 0
};
// flag when the image is ready
this.ready = false;
// flag when the image is loaded
this.loaded = false;
};
Galleria.Picture.prototype = {
// the inherited cache object
cache: {},
// creates a new image and adds it to cache when loaded
add: function( src ) {
var self = this;
// create the image
var image = new Image();
// force a block display
$( image ).css( 'display', 'block');
if ( self.cache[ src ] ) {
// no need to onload if the image is cached
image.src = src;
self.loaded = true;
self.original = {
height: image.height,
width: image.width
};
return image;
}
// begin preload and insert in cache when done
image.onload = function() {
self.original = {
height: this.height,
width: this.width
};
self.cache[ src ] = src; // will override old cache
self.loaded = true;
};
image.src = src;
return image;
},
// show the image on stage
show: function() {
Utils.show( this.image );
},
// hide the image
hide: function() {
Utils.moveOut( this.image );
},
clear: function() {
this.image = null;
},
/**
Checks if an image is in cache
@param {String} src The image source path, ex '/path/to/img.jpg'
@returns {Boolean}
*/
isCached: function( src ) {
return !!this.cache[src];
},
/**
Loads an image and call the callback when ready.
Will also add the image to cache.
@param {String} src The image source path, ex '/path/to/img.jpg'
@param {Function} callback The function to be executed when the image is loaded & scaled
@returns {jQuery} The image container object
*/
load: function(src, callback) {
// save the instance
var self = this;
$( this.container ).empty(true);
// add the image to cache and hide it
this.image = this.add( src );
Utils.hide( this.image );
// append the image into the container
$( this.container ).append( this.image );
// check for loaded image using a timeout
Utils.wait({
until: function() {
// TODO this should be properly tested in Opera
return self.loaded && self.image.complete && self.image.width;
},
success: function() {
// call success
window.setTimeout(function() { callback.call( self, self ); }, 50 );
},
error: function() {
window.setTimeout(function() { callback.call( self, self ); }, 50 );
Galleria.raise('image not loaded in 10 seconds: '+ src);
},
timeout: 10000
});
// return the container
return this.container;
},
/**
Scales and crops the image
@param {Object} options The method takes an object with a number of options: