Preparation à l'AJAXisation des communications

This commit is contained in:
xdrm-brackets 2015-12-14 08:35:20 +01:00
parent 9483937902
commit b145b13dea
8 changed files with 90 additions and 6 deletions

View File

@ -23,7 +23,8 @@ $answerType = (isset($_GET['type'])) ? $_GET['type'] : null;
<link rel='stylesheet' href='css/global.css'/> <link rel='stylesheet' href='css/global.css'/>
<link rel='stylesheet' href='css/responsive.css'/> <link rel='stylesheet' href='css/responsive.css'/>
<script type='text/javascript' src='js/adjust.js'></script> <script type='text/javascript' src='js/lib/adjust.js'></script>
<script type='text/javascript' src='js/lib/API.js'></script>
<script type='text/javascript' src='js/input-checker.js'></script> <script type='text/javascript' src='js/input-checker.js'></script>
</head> </head>
<body> <body>

View File

@ -18,7 +18,8 @@ if(!Authentification::checkUser(0)){
<link rel='stylesheet' href='css/global.css'/> <link rel='stylesheet' href='css/global.css'/>
<link rel='stylesheet' href='css/responsive.css'/> <link rel='stylesheet' href='css/responsive.css'/>
<script type='text/javascript' src='js/adjust.js'></script> <script type='text/javascript' src='js/lib/adjust.js'></script>
<script type='text/javascript' src='js/lib/API.js'></script>
<script type='text/javascript' src='js/input-checker.js'></script> <script type='text/javascript' src='js/input-checker.js'></script>
</head> </head>
<body> <body>

View File

@ -24,7 +24,8 @@ $answerType = (isset($_GET['type'])) ? $_GET['type'] : null;
<link rel='stylesheet' href='css/blue-green.theme.css'/> <link rel='stylesheet' href='css/blue-green.theme.css'/>
<link rel='stylesheet' href='css/responsive.css'/> <link rel='stylesheet' href='css/responsive.css'/>
<script type='text/javascript' src='js/adjust.js'></script> <script type='text/javascript' src='js/lib/adjust.js'></script>
<script type='text/javascript' src='js/lib/API.js'></script>
<script type='text/javascript' src='js/input-checker.js'></script> <script type='text/javascript' src='js/input-checker.js'></script>
</head> </head>
<body> <body>

View File

@ -24,7 +24,8 @@ $answerType = (isset($_GET['type'])) ? $_GET['type'] : null;
<link rel='stylesheet' href='css/purple.theme.css'/> <link rel='stylesheet' href='css/purple.theme.css'/>
<link rel='stylesheet' href='css/responsive.css'/> <link rel='stylesheet' href='css/responsive.css'/>
<script type='text/javascript' src='js/adjust.js'></script> <script type='text/javascript' src='js/lib/adjust.js'></script>
<script type='text/javascript' src='js/lib/API.js'></script>
<script type='text/javascript' src='js/input-checker.js'></script> <script type='text/javascript' src='js/input-checker.js'></script>
</head> </head>
<body> <body>

77
js/lib/API.js Normal file
View File

@ -0,0 +1,77 @@
/* classe API */
function APIClass(){};
APIClass.prototype = {
xhr: [], // tableau d'objets pour les requêtes ajax
/* transaction avec le serveur (managers)
*
* @param pRequest<Object> l'objet passé en JSON à API.php
* @param pHandler<Function> fonction qui s'éxécutera lors de la réponse (1 argument -> réponse<Object>)
*
* @return answer<Object> l'objet retourné par API.php via pHandler (1er argument)
*
***************************************************************************************************
*
* @usecase
* 1. var answerObject = sendRequest(
* 2. { var1: "exemple", var2: 198294 },
* 3. function(rep){ alert(rep); }
* 4. );
* @explain
* 1. on appelle la fonction <=> on créé la requête
* 2. on passe l'objet qui sera envoyé
* 3. on passe une fonction qui utilise un argument (sera la réponse de API.php) (sous forme d'objet)
*
*/
send: function(pRequest, pHandler){
// on efface les requêtes qui sont terminées (toutes celles de this.xhr)
for( var i = 0 ; i < this.xhr.length ; i++ ){
if( this.xhr[i].readyState == 4 ) // si terminée
this.xhr = this.xhr.slice(0,i-1).concat(this.xhr.slice(i,this.xhr.length-1)); // suppression entrée
}
// on créé une nouvelle entrée
this.xhr.push(null);
i = this.xhr.length-1;
// création de l'objet AJAX
if(window.XMLHttpRequest) // IE7+, Firefox, Chrome, Opera, Safari
this.xhr[i] = new XMLHttpRequest();
else // IE5, IE6
this.xhr[i] = new ActiveXObject('Microsoft.XMLHttpRequest');
console.log(pRequest);
var ptrAPI = this;
this.xhr[i].onreadystatechange = function(){
if( ptrAPI.xhr[i].readyState == 4 ){ // si la requête est terminée
/* DEBUG : affiche la réponse BRUTE de API.php */
console.log('managers/ => '+ptrAPI.xhr[i].responseText);
console.log( JSON.parse(ptrAPI.xhr[i].responseText) );
/* si success de requête */
if( [0,200].indexOf(ptrAPI.xhr[i].status) > -1 ){ // si fichier existe et reçu
try{ pHandler( JSON.parse(ptrAPI.xhr[i].responseText) ); } // si on peut parser, on envoie
catch(e){ pHandler({status:'corrupted'}); } // sinon on envoie obj.status = 'corrupted'
}
/* sinon retourne obj.status = 'unreachable' */
else
pHandler({status: 'unreachable'});
}
}
// on créé un formulaire POST (virtuel)
var form = new FormData();
form.append('json', JSON.stringify(pRequest) ); // on créé la variable $_POST['json']=>request
this.xhr[i].open('POST', 'managers/', true);
this.xhr[i].send( form );
}
};

View File

@ -66,6 +66,9 @@ sbCreer.addEventListener('click', function(e){
formElements[i].value = ''; // on vide formElements[i].value = ''; // on vide
}} }}
if( checker ) // si tout es ok uniquement, on submit() if( checker ){ // si tout es ok uniquement, on submit()
sbCreer.parentNode.submit(); sbCreer.parentNode.submit();
}else{ // sinon on affiche l'erreur
notif('error', 'Oups!', 'Certains champs sont requis ou incorrects.');
}
}, false); }, false);

View File

@ -50,7 +50,7 @@ class Manager{
public function dispatch($params){ public function dispatch($params){
if(($this->managerFound instanceof Authentification) or (Authentification::checkUser($this->commandFound['role'],$this->commandFound['strict']))){ if(($this->managerFound instanceof Authentification) or (Authentification::checkUser($this->commandFound['role'],$this->commandFound['strict']))){
if(method_exists($this->managerFound,$this->commandFound['method'])){ if(method_exists($this->managerFound,$this->commandFound['method'])){
$evalCommand = '$this->managerFound->'.$this->commandFound['method'].'($params)?>'; $evalCommand = '$this->managerFound->'.$this->commandFound['method'].'($params); ?>';
eval($evalCommand); eval($evalCommand);
}else{ }else{
Response::quickResponse(500,json_encode("La méthode: ".$this->commandFound['method'].' n\'est pas présente dans le manager')); Response::quickResponse(500,json_encode("La méthode: ".$this->commandFound['method'].' n\'est pas présente dans le manager'));