ADD gestion du fragment de url, voir #2
[euphorik.git] / js / fragment.js
diff --git a/js/fragment.js b/js/fragment.js
new file mode 100644 (file)
index 0000000..835c05e
--- /dev/null
@@ -0,0 +1,69 @@
+// coding: utf-8
+// Copyright 2008 GrĂ©gory Burri
+//
+// This file is part of Euphorik.
+//
+// Euphorik is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Euphorik is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Euphorik.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+  *
+  */
+Fragment = function() {
+   var thisFragment = this;
+   this.fragments = {};
+   if (!window.location.hash) {
+       return;
+   }
+   try {
+       var fragmentsStr = window.location.hash.slice(1).split(";");
+       fragmentsStr.each(function(i, tuple) {
+          tuple = tuple.split("=");
+          thisFragment.fragments[tuple[0]] = JSON.parse(tuple[1]);
+       });
+   } catch(error) {
+      ;; console.log(error);
+   }
+};
+
+Fragment.prototype.setVal = function(name, val) {
+   this.fragments[name] = val;
+}
+
+Fragment.prototype.getVal = function(name) {
+   return this.fragments[name];
+}
+
+Fragment.prototype.delVal = function(name) {
+   delete this.fragments[name];
+}
+
+Fragment.prototype.eraseAllVal = function() {
+   this.fragments = [];
+}
+
+Fragment.prototype.each = function(fun) {
+    objectEach(this.fragments, function(name, val) {
+       fun(name, val);
+    });
+}
+
+Fragment.prototype.write = function() {
+   var fragmentsStr = "";
+   var first = true;
+   objectEach(this.fragments, function(name, val) {
+      fragmentsStr += (first ? "" : ";") + name + "=" + JSON.stringify(val);
+      first = false;
+   });
+   window.location.hash = fragmentsStr;
+}