2016-10-05 08:47:05 +00:00
|
|
|
|
|
|
|
/* CONSTRUCTEUR
|
2016-10-05 10:13:48 +00:00
|
|
|
*
|
|
|
|
* @imageElement<HTMLImageElement> Image cible
|
|
|
|
* @callback<Function> Fonction à appliquer après initialisation
|
|
|
|
* @src<String> Chemin de la resource
|
|
|
|
* @process<Function> Fonction à appeller après chargement image
|
|
|
|
*
|
|
|
|
*
|
2016-10-05 08:47:05 +00:00
|
|
|
*/
|
2016-10-05 08:49:25 +00:00
|
|
|
var ImageLoader = function(imageElement, callback){
|
2016-10-05 08:47:05 +00:00
|
|
|
/* [0] Initialisation + paramètres
|
|
|
|
=========================================================*/
|
2016-10-05 10:13:48 +00:00
|
|
|
this._wrapper = (imageElement instanceof HTMLImageElement) ? imageElement : null;
|
|
|
|
this.src = null;
|
|
|
|
this._src = null;
|
|
|
|
this._callback = typeof callback != 'function' ? function(){} : callback;
|
|
|
|
this._process = typeof process != 'function' ? function(){} : process;
|
|
|
|
this._loaded = false;
|
2016-10-05 08:47:05 +00:00
|
|
|
|
2016-10-05 10:13:48 +00:00
|
|
|
if( !this._wrapper )
|
2016-10-05 08:47:05 +00:00
|
|
|
throw new Error('Param 1 expected to be an HTMLImageElement (<img>), but '+imageElement.constructor.name+' received');
|
|
|
|
|
2016-10-05 10:13:48 +00:00
|
|
|
/* [1] Définition du setter
|
2016-10-05 08:47:05 +00:00
|
|
|
=========================================================*/
|
2016-10-05 10:13:48 +00:00
|
|
|
this.__defineSetter__('src', function(v){
|
|
|
|
log('src update', '[ImageLoader]');
|
|
|
|
this._src = v;
|
|
|
|
this._load();
|
|
|
|
});
|
2016-10-05 08:47:05 +00:00
|
|
|
|
2016-10-05 10:13:48 +00:00
|
|
|
this.__defineGetter__('src', function(){
|
|
|
|
return this._src;
|
|
|
|
});
|
|
|
|
|
|
|
|
/* [2] Chargement de la liste d'images
|
|
|
|
=========================================================*/
|
2016-10-05 08:47:05 +00:00
|
|
|
AJAX.send('./pictures/index.php', (function(response){
|
2016-10-05 10:13:48 +00:00
|
|
|
this._images = JSON.parse(response);
|
|
|
|
this._loaded = true;
|
|
|
|
log('initialized', '[ImageLoader]');
|
2016-10-05 08:49:25 +00:00
|
|
|
|
2016-10-05 10:13:48 +00:00
|
|
|
this._callback.call(this);
|
2016-10-05 08:47:05 +00:00
|
|
|
}).bind(this), 'GET');
|
|
|
|
};
|
2016-10-05 08:49:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-10-05 10:13:48 +00:00
|
|
|
|
|
|
|
|
2016-10-05 08:49:25 +00:00
|
|
|
/* CHARGEMENT D'IMAGE
|
|
|
|
*
|
|
|
|
*/
|
2016-10-05 10:13:48 +00:00
|
|
|
ImageLoader.prototype._load = function(){
|
|
|
|
if( !this._loaded )
|
2016-10-05 08:49:25 +00:00
|
|
|
throw new Error('image tree not loaded yet');
|
|
|
|
|
|
|
|
/* [0] Initialisation des paramètres
|
|
|
|
=========================================================*/
|
2016-10-05 10:13:48 +00:00
|
|
|
if( typeof this.src != 'string' )
|
|
|
|
throw new Error('ImageLoader.src expected to be a <String>');
|
2016-10-05 08:49:25 +00:00
|
|
|
|
2016-10-05 10:13:48 +00:00
|
|
|
if( typeof this._callback != 'function' )
|
|
|
|
throw new Error('ImageLoader.callback expected to be a <Function>');
|
2016-10-05 08:49:25 +00:00
|
|
|
|
2016-10-05 10:13:48 +00:00
|
|
|
if( !~this._images.indexOf(this.src) ){
|
|
|
|
console.warn('Resource '+this.src+' not found!');
|
2016-10-05 08:49:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [1] On charge l'image + callback
|
|
|
|
=========================================================*/
|
2016-10-05 13:04:48 +00:00
|
|
|
|
|
|
|
this._wrapper.addEventListener('load', this._process.bind(this._wrapper), false);
|
2016-11-02 18:15:20 +00:00
|
|
|
this._wrapper.src = './pictures/' + this.src;
|
2016-10-05 08:49:25 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|