From cc33a4ed4b71d5fb1938625008c6b86a08f33252 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 5 Oct 2016 10:49:25 +0200 Subject: [PATCH] minmod --- public_html/js/action-script.js | 16 +++++++++ public_html/js/lib/image-loader.js | 48 +++++++++++++++++++++++--- public_html/js/lib/min/image-loader.js | 3 +- public_html/js/min/action-script.js | 2 +- public_html/pictures/index.php | 19 +++++++--- 5 files changed, 76 insertions(+), 12 deletions(-) diff --git a/public_html/js/action-script.js b/public_html/js/action-script.js index f9f25f4..22f06ec 100644 --- a/public_html/js/action-script.js +++ b/public_html/js/action-script.js @@ -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)); + +}); diff --git a/public_html/js/lib/image-loader.js b/public_html/js/lib/image-loader.js index caeaf56..602a0a3 100644 --- a/public_html/js/lib/image-loader.js +++ b/public_html/js/lib/image-loader.js @@ -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 (), 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 Chemin de l'image +* @callback 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 '); + + path = path.replace(/:/g, '/'); + + if( typeof callback != 'function' ) + throw new Error('Param 2 expected to be a '); + + 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; +}; diff --git a/public_html/js/lib/min/image-loader.js b/public_html/js/lib/min/image-loader.js index 546f2f8..e8d0c5f 100644 --- a/public_html/js/lib/min/image-loader.js +++ b/public_html/js/lib/min/image-loader.js @@ -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 (), 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 (), 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 ");a=a.replace(/:/g,"/");if("function"!=typeof b)throw Error("Param 2 expected to be a ");if(~this.images.indexOf("/"+a))return this.wrapper.addEventListener("load",b,!1),this.wrapper.src="./pictures/"+a,!0;console.warn("Resource not found!")}; diff --git a/public_html/js/min/action-script.js b/public_html/js/min/action-script.js index b7d8d22..895e84e 100644 --- a/public_html/js/min/action-script.js +++ b/public_html/js/min/action-script.js @@ -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))}); diff --git a/public_html/pictures/index.php b/public_html/pictures/index.php index d28c196..30336d3 100755 --- a/public_html/pictures/index.php +++ b/public_html/pictures/index.php @@ -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 );