5265f28657295d91035c156552d73590ff5e5078
[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
46 // tout un tas d'améliorations de JavaScript ;)
47 /**
48 * Pour chaque propriété de l'objet execute f(p, v) ou p est le nom de la propriété et v sa valeur.
49 * Ne parcours pas les propriétés des prototypes.
50 * FIXME : Normalement : Object.prototype.each = function(f) mais non supporté par jquery
51 */
52 //Object.prototype.each = function(f) {
53 var objectEach = function(o, f) {
54 for (var k in o) {
55 if (o.hasOwnProperty(k)) {
56 f(k, o[k]);
57 }
58 }
59 };
60
61 var objectMemberCount = function(o) {
62 var nb = 0;
63 for (var k in o) {
64 if (o.hasOwnProperty(k)) {
65 nb += 1;
66 }
67 }
68 return nb;
69 };
70
71 Array.prototype.each = function(f) {
72 for (var i = 0; i < this.length; i++) {
73 f(i, this[i]);
74 }
75 };
76
77 Array.prototype.map = function(f) {
78 for (var i = 0; i < this.length; i++) {
79 this[i] = f(this[i])
80 }
81 };
82
83 String.prototype.trim = function() {
84 return jQuery.trim(this) // anciennement : this.replace(/^\s+|\s+$/g, "");
85 }
86
87 String.prototype.ltrim = function() {
88 return this.replace(/^\s+/, "");
89 }
90
91 String.prototype.rtrim = function() {
92 return this.replace(/\s+$/, "");
93 }
94
95
96 /**
97 * Voir : http://www.coolpage.com/developer/javascript/Correct%20OOP%20for%20Javascript.html
98 *
99 * Exemple :
100 *
101 * function Mammal(name) {
102 * this.name = name;
103 * }
104 *
105 * Cat.Inherits(Mammal);
106 * function Cat(name) {
107 * this.Super(Mammal, name);
108 * }
109 */
110 /*Object.prototype.Super = function(parent) {
111 if(arguments.length > 1) {
112 parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
113 } else {
114 parent.call( this );
115 }
116 }
117 Function.prototype.Inherits = function(parent) {
118 this.prototype = new parent();
119 this.prototype.constructor = this;
120 }*/
121
122
123
124 ///////////////////////////////////////////////////////////////////////////////////////////////////
125
126
127 // le main
128 $(document).ready(
129 function()
130 {
131 var formateur = new euphorik.Formateur()
132 var util = new euphorik.Util(formateur)
133 var client = new euphorik.Client(util)
134 var pages = new euphorik.Pages()
135
136
137 // connexion vers le serveur (utilise un cookie qui traine)
138 client.connexionCookie()
139
140 $("#menuCss").change(function(){ client.setCss("styles/" + $("option:selected", this).attr("value") + "/euphorik.css")})
141
142 // FIXME : ne fonctionne pas sous opera
143 // voir : http://dev.jquery.com/ticket/2892#preview
144 $(window).unload(function(){client.flush()})
145
146 $("#menu .minichat").click(function(){ pages.afficherPage("minichat") })
147 $("#menu .admin").click(function(){ pages.afficherPage("admin") })
148 $("#menu .profile").click(function(){ pages.afficherPage("profile") })
149 $("#menu .logout").click(function(){
150 util.messageDialogue("Êtes-vous sur de vouloir vous délogger ?", euphorik.Util.messageType.question,
151 {"Oui" : function()
152 {
153 client.deconnexion();
154 pages.afficherPage("minichat", true)
155 },
156 "Non" : function(){}
157 }
158 )
159 })
160 $("#menu .register").click(function(){ pages.afficherPage("register") })
161 $("#menu .about").click(function(){ pages.afficherPage("about") })
162
163 // TODO : simplifier et pouvoir créer des liens par exemple : <span class="lien" href="conditions">Conditions d'utilisation</span>
164 $("#footer .conditions").click(function(){ pages.afficherPage("conditions_utilisation") })
165
166 pages.ajouterPage(new euphorik.PageMinichat(client, formateur, util))
167 pages.ajouterPage(new euphorik.PageAdmin(client, formateur, util))
168 pages.ajouterPage(new euphorik.PageProfile(client, formateur, util))
169 pages.ajouterPage(new euphorik.PageRegister(client, formateur, util))
170 pages.ajouterPage(new euphorik.PageAbout(client, formateur, util))
171 pages.ajouterPage("conditions_utilisation")
172
173 pages.afficherPage("minichat")
174 }
175 )