REPORT de la branche 1.1 : 477->494
[euphorik.git] / js / communication.js
1 // coding: utf-8
2 // Copyright 2008 Grégory Burri
3 //
4 // This file is part of Euphorik.
5 //
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.
10 //
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.
15 //
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/>.
18
19 // Regroupe la partie communication JSON client -> serveur de euphorik.
20 // Voir : http://dev.euphorik.ch/wiki/euk/Protocole
21
22 /**
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)
28 */
29 euphorik.Communication = function(funError, funDebutReq, funFinReq) {
30 this.funError = funError;
31 this.funDebutReq = funDebutReq;
32 this.funFinReq = funFinReq;
33 };
34
35 /**
36 * Charge un fichier depuis une url et retourne son contenu.
37 */
38 euphorik.Communication.prototype.load = function(url) {
39 if (this.funDebutReq) {
40 this.funDebutReq();
41 }
42 var contenu = "";
43 $.ajax({async: false, url: url, success : function(page) { contenu += page; }});
44 if (this.funFinReq) {
45 this.funFinReq();
46 }
47 return contenu;
48 };
49
50 /**
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)
58 */
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
63 }
64
65 var mess = this.getBase(action);
66 objectEach(json, function(nom, val) {
67 mess[nom] = val;
68 });
69
70 if (this.funDebutReq) {
71 this.funDebutReq();
72 }
73
74 paramsAjax = {
75 async: asynchrone,
76 type: "POST",
77 url: "request",
78 dataType: "json",
79 data: { action : JSON.stringify(mess) },
80 success:
81 function(data) {
82 if (thisCommunication.funFinReq) {
83 thisCommunication.funFinReq();
84 }
85 if (data.reply === "error") {
86 if (funError) {
87 funError(data);
88 } else if (thisCommunication.funError) {
89 thisCommunication.funError(data);
90 }
91 } else if (funOk) {
92 funOk(data);
93 }
94 },
95 error:
96 function(data) {
97 if (thisCommunication.funFinReq) {
98 thisCommunication.funFinReq();
99 }
100 }
101 };
102
103 if (paramsSupp) {
104 objectEach(paramsSupp, function(nom, val) {
105 paramsAjax[nom] = val;
106 });
107 }
108
109 jQuery.ajax(paramsAjax);
110 };
111
112 euphorik.Communication.prototype.getBase = function(action) {
113 return {
114 "header" : { "action" : action, "version" : euphorik.conf.versionProtocole }
115 };
116 };