cd1ade642e2b2f151ec84c4bfde67b51241df8fb
[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() {
26 this.pageCourante = undefined;
27 this.pages = {};
28 };
29
30 /**
31 * Accepte soit un objet soit un string.
32 * un string correspond au nom de la page, par exemple : "page" -> "page.html"
33 */
34 euphorik.Pages.prototype.ajouterPage = function(page) {
35 if (typeof page === "string") {
36 this.pages[page] = page;
37 } else {
38 page.pages = this; // la magie des langages dynamiques : le foutoire
39 this.pages[page.nom] = page;
40 }
41 };
42
43 euphorik.Pages.prototype.afficherPage = function(nomPage, forcerChargement) {
44 forcerChargement = forcerChargement || false;
45
46 var page = this.pages[nomPage];
47 if (!page || (!forcerChargement && page === this.pageCourante)) {
48 return;
49 }
50
51 if (this.pageCourante && this.pageCourante.decharger) {
52 this.pageCourante.decharger();
53 }
54
55 $("#menu li").removeClass("courante");
56 $("#menu li." + nomPage).addClass("courante");
57
58 this.pageCourante = page;
59 var contenu = "";
60 if (typeof page === "string") {
61 $.ajax({async: false, url: "pages/" + page + ".html", success : function(page) { contenu += page; }});
62 } else {
63 contenu += this.pageCourante.contenu();
64 }
65
66 $("#page").html(contenu).removeClass().addClass(
67 this.pageCourante.nom +
68 (this.pageCourante.classes ? " " + this.pageCourante.classes() : "") // l'objet peut fournire des classes css supplémentaires sous la forme d'un string
69 );
70
71 if (this.pageCourante.charger) {
72 this.pageCourante.charger();
73 }
74 };