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