ADD début du regroupement de la communication avec le serveur dans protocole.js
[euphorik.git] / js / fragment.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 * Gestion du fragment d'url, permet de le modifier en direct.
21 */
22 Fragment = function() {
23 var thisFragment = this;
24 this.fragments = {};
25 if (!window.location.hash) {
26 return;
27 }
28 try {
29 var fragmentsStr = window.location.hash.slice(1).split(";");
30 fragmentsStr.each(function(i, tuple) {
31 tuple = tuple.split("=");
32 thisFragment.fragments[tuple[0]] = JSON.parse(tuple[1]);
33 });
34 } catch(error) {
35 ;; console.log(error);
36 }
37 };
38
39 Fragment.prototype.setVal = function(name, val) {
40 this.fragments[name] = val;
41 };
42
43 Fragment.prototype.getVal = function(name) {
44 return this.fragments[name];
45 };
46
47 Fragment.prototype.delVal = function(name) {
48 delete this.fragments[name];
49 };
50
51 Fragment.prototype.eraseAllVal = function() {
52 this.fragments = [];
53 };
54
55 Fragment.prototype.each = function(fun) {
56 objectEach(this.fragments, function(name, val) {
57 fun(name, val);
58 });
59 };
60
61 Fragment.prototype.write = function() {
62 var fragmentsStr = "";
63 var first = true;
64 objectEach(this.fragments, function(name, val) {
65 fragmentsStr += (first ? "" : ";") + name + "=" + JSON.stringify(val);
66 first = false;
67 });
68 window.location.hash = fragmentsStr;
69 };