Update to the new library 'json2'
[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 * Pages manager.
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 * Add a new page, must be done one time per page after the website is loaded.
35 * Accept a string or a Page object.
36 * If a string is given then a static object page is automatically created.
37 * A string is the name of the page without the 'html' extension, for example
38 * "page" -> "page.html"
39 * @defaultPage If true then the page will be flaged as 'default'. It must be only one default page.
40 */
41 euphorik.Pages.prototype.addPage = function(page, defaultPage) {
42 if (typeof page === "string") {
43 page = new euphorik.PageStatique(page, this.communication);
44 }
45
46 page.pages = this; // la magie des langages dynamiques : le foutoire
47 page.fragment = this.fragment;
48
49 this.pages[page.name] = page;
50
51 if (defaultPage) {
52 this.pageDefaut = page;
53 }
54 };
55
56 /**
57 * Display a given page.
58 * If no page name given the the default page will be loaded.
59 * @pageName [optional] The page name to load.
60 * @forceToLoad [optional] If true then the page will be reloaded even it already displayed.
61 */
62 euphorik.Pages.prototype.displayPage = function(pageName, forceToLoad) {
63 forceToLoad = forceToLoad || false;
64
65 // si le nom de la page n'est pas donné on le prend du fragment
66 if (!pageName) {
67 pageName = this.fragment.getVal("page");
68 }
69
70 var page = this.pages[pageName];
71 if (!page) {
72 page = this.pageDefaut;
73 }
74
75 if (!page || (!forceToLoad && page === this.pageCourante)) {
76 return;
77 }
78
79 if (this.pageCourante && this.pageCourante.decharger) {
80 this.pageCourante.decharger();
81 }
82
83 $("#menu li").removeClass("courante");
84 $("#menu li." + page.name).addClass("courante");
85
86 this.pageCourante = page;
87 var contenu = this.pageCourante.contenu();
88
89 $("#page").html(contenu).removeClass().addClass(
90 this.pageCourante.name +
91 // A page can bring some additionnals CSS classes
92 (this.pageCourante.classes ? " " + this.pageCourante.classes() : "")
93 );
94
95 if (this.pageCourante.charger) {
96 this.pageCourante.charger();
97 }
98
99 this.fragment.setVal("page", this.pageCourante.name);
100 this.fragment.write();
101 };