MOD avancement dans le module 'communication'
[euphorik.git] / js / pages.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 /*jslint laxbreak:true */
20
21
22 /**
23 * Gestion des pages.
24 */
25 euphorik.Pages = function(fragment) {
26 this.fragment = fragment;
27 this.pageCourante = undefined;
28 this.pageDefaut = undefined;
29 this.pages = {};
30 };
31
32 /**
33 * Accepte soit un objet soit un string.
34 * un string correspond au nom de la page, par exemple : "page" -> "page.html"
35 * @defaut si vrai alors la page est la page par défaut
36 */
37 euphorik.Pages.prototype.ajouterPage = function(page, defaut) {
38 if (typeof page === "string") {
39 page = new euphorik.PageStatique(page);
40 }
41
42 page.pages = this; // la magie des langages dynamiques : le foutoire
43 page.fragment = this.fragment;
44
45 this.pages[page.nom] = page;
46
47 if (defaut) {
48 this.pageDefaut = page;
49 }
50 };
51
52 euphorik.Pages.prototype.afficherPage = function(nomPage, forcerChargement) {
53 forcerChargement = forcerChargement || false;
54
55 // si le nom de la page n'est pas donné on le prend du fragment
56 if (!nomPage) {
57 nomPage = this.fragment.getVal("page");
58 }
59
60 var page = this.pages[nomPage];
61 if (!page) {
62 page = this.pageDefaut;
63 }
64
65 if (!page || (!forcerChargement && page === this.pageCourante)) {
66 return;
67 }
68
69 if (this.pageCourante && this.pageCourante.decharger) {
70 this.pageCourante.decharger();
71 }
72
73 $("#menu li").removeClass("courante");
74 $("#menu li." + page.nom).addClass("courante");
75
76 this.pageCourante = page;
77 var contenu = this.pageCourante.contenu();
78
79 $("#page").html(contenu).removeClass().addClass(
80 this.pageCourante.nom +
81 (this.pageCourante.classes ? " " + this.pageCourante.classes() : "") // l'objet peut fournire des classes css supplémentaires sous la forme d'un string
82 );
83
84 if (this.pageCourante.charger) {
85 this.pageCourante.charger();
86 }
87
88 this.fragment.setVal("page", this.pageCourante.nom);
89 this.fragment.write();
90 };