MOD avancement dans la Grande Restructuration
[euphorik.git] / js / euphorik.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 /**
20 * Contient la base javascript pour le site euphorik.ch.
21 * Chaque page possède son propre fichier js nommé "page<nom de la page>.js".
22 * Auteur : GBurri
23 * Date : 6.11.2007
24 */
25
26 // tout euphorik est contenu dans cet objet
27 var euphorik = {}
28 // ;; euphorik.include =
29 //;; euphorik.include = function(f) { var s = document.createElement('script'); s.type = 'text/javascript'; s.src = "js/" + f + ".js"; document.getElementsByTagName('head')[0].appendChild(s); }
30
31
32 // version jQuery : function(f) { jQuery.ajax({async : false, url : "js/" + f + ".js", dataType : "script"}) }
33 // mais comme il n'est pas encore chargé...
34 ;; euphorik.include = function(f) {
35 ;; var req, url = 'js/' + f + '.js'
36 ;; if (window.XMLHttpRequest) {
37 ;; req = new XMLHttpRequest(); req.open("GET", url, false); req.send(null);
38 ;; } else if (window.ActiveXObject) {
39 ;; req = new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') != -1) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP");
40 ;; if (req) { req.open("GET", url, false); req.send(); }
41 ;; }
42 ;; if (req!==false) { if (req.status==200) { window.eval(req.responseText); } else if (req.status==404) { alert("erreur de chargement (404) de : " + url) } }
43 ;; };
44
45 ;; euphorik.include("jquery");
46 ;; euphorik.include("jquery.lightbox");
47 ;; euphorik.include("md5");
48 ;; euphorik.include("json2");
49
50 ;; euphorik.include("conf");
51 ;; euphorik.include("util");
52 ;; euphorik.include("formateur");
53 ;; euphorik.include("pages");
54 ;; euphorik.include("client");
55 ;; euphorik.include("pageEvent");
56
57 ;; euphorik.include("pageMinichat/pageMinichat");
58 ;; euphorik.include("pageMinichat/conversation");
59 ;; euphorik.include("pageMinichat/conversations");
60 ;; euphorik.include("pageMinichat/message");
61 ;; euphorik.include("pageAdmin");
62 ;; euphorik.include("pageProfile");
63 ;; euphorik.include("pageRegister");
64 ;; euphorik.include("pageAbout");
65
66 // tout un tas d'améliorations de JavaScript ;)
67 /**
68 * Pour chaque propriété de l'objet execute f(p, v) ou p est le nom de la propriété et v sa valeur.
69 * Ne parcours pas les propriétés des prototypes.
70 * FIXME : Normalement : Object.prototype.each = function(f) mais non supporté par jquery
71 */
72 //Object.prototype.each = function(f) {
73 var objectEach = function(o, f) {
74 for (var k in o) {
75 if (o.hasOwnProperty(k)) {
76 f(k, o[k]);
77 }
78 }
79 };
80
81 Array.prototype.each = function(f) {
82 for (var i = 0; i < this.length; i++) {
83 f(i, this[i]);
84 }
85 };
86
87 Array.prototype.map = function(f) {
88 for (var i = 0; i < this.length; i++) {
89 this[i] = f(this[i])
90 }
91 };
92
93 String.prototype.trim = function() {
94 return jQuery.trim(this) // anciennement : this.replace(/^\s+|\s+$/g, "");
95 }
96
97 String.prototype.ltrim = function() {
98 return this.replace(/^\s+/, "");
99 }
100
101 String.prototype.rtrim = function() {
102 return this.replace(/\s+$/, "");
103 }
104
105
106 /**
107 * Voir : http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html
108 *
109 * Exemple :
110 *
111 * function Mammal(name) {
112 * this.name = name;
113 * }
114 *
115 * Cat.Inherits(Mammal);
116 * function Cat(name) {
117 * this.Super(Mammal, name);
118 * }
119 */
120 /*Object.prototype.Super = function(parent) {
121 if(arguments.length > 1) {
122 parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
123 } else {
124 parent.call( this );
125 }
126 }
127 Function.prototype.Inherits = function(parent) {
128 this.prototype = new parent();
129 this.prototype.constructor = this;
130 }*/
131
132
133
134 ///////////////////////////////////////////////////////////////////////////////////////////////////
135
136
137 // le main
138 $(document).ready(
139 function()
140 {
141 var formateur = new euphorik.Formateur()
142 var util = new euphorik.Util(formateur)
143 var client = new euphorik.Client(util)
144 var pages = new euphorik.Pages()
145
146
147 // connexion vers le serveur (utilise un cookie qui traine)
148 client.connexionCookie()
149
150 $("#menuCss").change(function(){ client.setCss("styles/" + $("option:selected", this).attr("value") + "/euphorik.css")})
151
152 // FIXME : ne fonctionne pas sous opera
153 // voir : http://dev.jquery.com/ticket/2892#preview
154 $(window).unload(function(){client.flush()})
155
156 $("#menu .minichat").click(function(){ pages.afficherPage("minichat") })
157 $("#menu .admin").click(function(){ pages.afficherPage("admin") })
158 $("#menu .profile").click(function(){ pages.afficherPage("profile") })
159 $("#menu .logout").click(function(){
160 util.messageDialogue("Êtes-vous sur de vouloir vous délogger ?", euphorik.Util.messageType.question,
161 {"Oui" : function()
162 {
163 client.deconnexion();
164 pages.afficherPage("minichat", true)
165 },
166 "Non" : function(){}
167 }
168 )
169 })
170 $("#menu .register").click(function(){ pages.afficherPage("register") })
171 $("#menu .about").click(function(){ pages.afficherPage("about") })
172
173 // TODO : simplifier et pouvoir créer des liens par exemple : <span class="lien" href="conditions">Conditions d'utilisation</span>
174 $("#footer .conditions").click(function(){ pages.afficherPage("conditions_utilisation") })
175
176 pages.ajouterPage(new PageMinichat(client, formateur, util))
177 pages.ajouterPage(new euphorik.PageAdmin(client, formateur, util))
178 pages.ajouterPage(new euphorik.PageProfile(client, formateur, util))
179 pages.ajouterPage(new euphorik.PageRegister(client, formateur, util))
180 pages.ajouterPage(new euphorik.PageAbout(client, formateur, util))
181 pages.ajouterPage("conditions_utilisation")
182
183 pages.afficherPage("minichat")
184 }
185 )