This commit is contained in:
xdrm-brackets 2016-10-05 10:49:25 +02:00
parent 6fcebb6d87
commit cc33a4ed4b
5 changed files with 76 additions and 12 deletions

View File

@ -1,3 +1,19 @@
var DOM = {
body: $('body')
};
var process = function(){
log('Image loaded');
};
/* [x] Chargement image
=========================================================*/
var iL = new ImageLoader( $('img'), function(){
this.load('front:male:1.jpg', process.bind(this));
});

View File

@ -1,7 +1,7 @@
/* CONSTRUCTEUR
*/
var ImageLoader = function(imageElement){
var ImageLoader = function(imageElement, callback){
/* [0] Initialisation + paramètres
=========================================================*/
this.wrapper = (imageElement instanceof HTMLImageElement) ? imageElement : null;
@ -9,12 +9,50 @@ var ImageLoader = function(imageElement){
if( !this.wrapper )
throw new Error('Param 1 expected to be an HTMLImageElement (<img>), but '+imageElement.constructor.name+' received');
/* [1] Chargement du tree d'images
/* [1] Chargement de la liste d'images
=========================================================*/
this.treeLoaded = false;
this.loaded = false;
AJAX.send('./pictures/index.php', (function(response){
this.treeLoaded = true;
console.log( JSON.parse(response) );
this.images = JSON.parse(response);
this.loaded = true;
callback.call(this);
}).bind(this), 'GET');
};
/* CHARGEMENT D'IMAGE
*
* @path<String> Chemin de l'image
* @callback<Function> Callback lancé quand l'image est chargée
*
*/
ImageLoader.prototype.load = function(path, callback){
if( !this.loaded )
throw new Error('image tree not loaded yet');
/* [0] Initialisation des paramètres
=========================================================*/
if( typeof path != 'string' )
throw new Error('Param 1 expected to be a <String>');
path = path.replace(/:/g, '/');
if( typeof callback != 'function' )
throw new Error('Param 2 expected to be a <Function>');
if( !~this.images.indexOf('/'+path) ){
console.warn('Resource not found!');
return;
}
/* [1] On charge l'image + callback
=========================================================*/
this.wrapper.addEventListener('load', callback, false);
this.wrapper.src = './pictures/' + path;
return true;
};

View File

@ -1 +1,2 @@
var ImageLoader=function(a){this.wrapper=a instanceof HTMLImageElement?a:null;if(!this.wrapper)throw Error("Param 1 expected to be an HTMLImageElement (<img>), but "+a.constructor.name+" received");this.treeLoaded=!1;AJAX.send("./pictures/index.php",function(a){this.treeLoaded=!0;console.log(JSON.parse(a))}.bind(this),"GET")};
var ImageLoader=function(a,b){this.wrapper=a instanceof HTMLImageElement?a:null;if(!this.wrapper)throw Error("Param 1 expected to be an HTMLImageElement (<img>), but "+a.constructor.name+" received");this.loaded=!1;AJAX.send("./pictures/index.php",function(a){this.images=JSON.parse(a);this.loaded=!0;b.call(this)}.bind(this),"GET")};
ImageLoader.prototype.load=function(a,b){if(!this.loaded)throw Error("image tree not loaded yet");if("string"!=typeof a)throw Error("Param 1 expected to be a <String>");a=a.replace(/:/g,"/");if("function"!=typeof b)throw Error("Param 2 expected to be a <Function>");if(~this.images.indexOf("/"+a))return this.wrapper.addEventListener("load",b,!1),this.wrapper.src="./pictures/"+a,!0;console.warn("Resource not found!")};

View File

@ -1 +1 @@
var DOM={body:$("body")};
var DOM={body:$("body")},process=function(){log("Image loaded")},iL=new ImageLoader($("img"),function(){this.load("front:male:1.jpg",process.bind(this))});

View File

@ -6,10 +6,6 @@
/* (1) Catch 'pictures' dir */
$dir = dirname(__FILE__);
/* (2) Defining authorized extensions */
$ext = ['jpg', 'png', 'jpeg'];
$checker = '/\.('. implode('|', $ext) .')$/i';
/* [2] Listing all pictures recursivly
=========================================================*/
@ -48,10 +44,23 @@
}
// TRANSFORME UN ARBRE EN LISTE
function toFlaggedImage($folder, $name){
$flagged = [];
foreach($folder as $e=>$element)
if( !is_array($element) )
$flagged[] = "$name/$element";
else
$flagged = array_merge($flagged, toFlaggedImage($element, "$name/$e") );
return $flagged;
}
/* [2] On affiche le résultat
=========================================================*/
$fetched = toFlaggedImage( getImages( $dir ) );
header('Content-Type: application/json');
echo json_encode( getImages($dir) );
echo json_encode( $fetched );