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