2 // Copyright 2008 Grégory Burri
4 // This file is part of Euphorik.
6 // Euphorik is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
11 // Euphorik is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Euphorik. If not, see <http://www.gnu.org/licenses/>.
19 // Regroupe la partie communication JSON client -> serveur de euphorik.
20 // Voir : http://dev.euphorik.ch/wiki/euk/Protocole
23 * Les fonctions debutReq et finReq servent, par exemple, à afficher à l'utilisateur
24 * qu'une communication est en cours.
25 * @param funError un fonction executée lors d'un réponse 'error' de la part du serveur, peut être redéfinit pour une requête.
26 * @param funDebutReq fonction appelée au début d'une requête (facultatif)
27 * @param funFinReq fonction appelée à la fin d'une requête (facultatif)
29 euphorik
.Communication = function(funError
, funDebutReq
, funFinReq
) {
30 this.funError
= funError
;
31 this.funDebutReq
= funDebutReq
;
32 this.funFinReq
= funFinReq
;
36 * Charge un fichier depuis une url et retourne son contenu.
38 euphorik
.Communication
.prototype.load = function(url
) {
39 if (this.funDebutReq
) {
43 $.ajax({async: false, url: url
, success : function(page
) { contenu
+= page
; }});
51 * Effectue une requête JSON auprès du serveur.
52 * @param action une chaine spécifiant l'action, par exemple "put_message"
53 * @param json les données à envoyer associé à l'action, par exemple {"cookie" : "LKJDLAKSJBFLKASN", "nick" : "Paul", "content" : "Bonjour", "answer_to" : [] }
54 * @param funOk la fonction exécuté après réception des données du serveur
55 * @param funError la fonction exécuté si une erreur arrive (facultatif)
56 * @param asynchrone true pour une communication asychrone (facultatif, truepar défaut)
57 * @param paramsSupp un objet contenant des paramètres supplémentaire pour la fonction ajax de jQuery (facultatif)
59 euphorik
.Communication
.prototype.requete = function(action
, json
, funOk
, funError
, asynchrone
, paramsSupp
) {
60 var thisCommunication
= this;
61 if (asynchrone
=== undefined) {
62 asynchrone
= true; // asynchrone par défaut
65 var mess
= this.getBase(action
);
66 objectEach(json
, function(nom
, val
) {
70 if (this.funDebutReq
) {
79 data: { action : JSON
.stringify(mess
) },
82 if (thisCommunication
.funFinReq
) {
83 thisCommunication
.funFinReq();
85 if (data
.reply
=== "error") {
88 } else if (thisCommunication
.funError
) {
89 thisCommunication
.funError(data
);
97 if (thisCommunication
.funFinReq
) {
98 thisCommunication
.funFinReq();
104 objectEach(paramsSupp
, function(nom
, val
) {
105 paramsAjax
[nom
] = val
;
109 jQuery
.ajax(paramsAjax
);
112 euphorik
.Communication
.prototype.getBase = function(action
) {
114 "header" : { "action" : action
, "version" : euphorik
.conf
.versionProtocole
}