summaryrefslogtreecommitdiff
path: root/share/www/script
diff options
context:
space:
mode:
Diffstat (limited to 'share/www/script')
-rw-r--r--share/www/script/futon.browse.js39
-rw-r--r--share/www/script/futon.js121
-rw-r--r--share/www/script/jquery.cookies.js50
3 files changed, 138 insertions, 72 deletions
diff --git a/share/www/script/futon.browse.js b/share/www/script/futon.browse.js
index 76084d64..37f4ef08 100644
--- a/share/www/script/futon.browse.js
+++ b/share/www/script/futon.browse.js
@@ -18,6 +18,8 @@
CouchIndexPage: function() {
page = this;
+ $.futon.storage.declare("per_page", {defaultValue: 10});
+
this.addDatabase = function() {
$.showDialog("dialog/_create_database.html", {
submit: function(data, callback) {
@@ -95,11 +97,22 @@
CouchDatabasePage: function() {
var urlParts = location.search.substr(1).split("/");
var dbName = decodeURIComponent(urlParts.shift());
+
+ $.futon.storage.declareWithPrefix(dbName + ".", {
+ desc: {},
+ language: {defaultValue: "javascript"},
+ map_fun: {},
+ reduce_fun: {},
+ reduce: {},
+ per_page: {defaultValue: 10},
+ view: {defaultValue: ""}
+ });
+
var viewName = (urlParts.length > 0) ? urlParts.join("/") : null;
if (viewName) {
- $.cookies.set(dbName + ".view", viewName);
+ $.futon.storage.set("view", viewName);
} else {
- viewName = $.cookies.get(dbName + ".view", "");
+ viewName = $.futon.storage.get("view");
if (viewName) {
this.redirecting = true;
location.href = "database.html?" + encodeURIComponent(dbName) +
@@ -204,10 +217,10 @@
});
$("#grouptruenotice").show();
} else if (viewName == "_temp_view") {
- page.viewLanguage = $.cookies.get(db.name + ".language", page.viewLanguage);
+ page.viewLanguage = $.futon.storage.get("language");
page.updateViewEditor(
- $.cookies.get(db.name + ".map", templates[page.viewLanguage]),
- $.cookies.get(db.name + ".reduce", "")
+ $.futon.storage.get("map", templates[page.viewLanguage]),
+ $.futon.storage.get("reduce")
);
$("#grouptruenotice").show();
} else {
@@ -312,13 +325,13 @@
db.openDoc("_design/" + designDocId, {
error: function(status, error, reason) {
if (status == 404) {
- $.cookies.remove(dbName + ".view");
+ $.futon.storage.del("view");
location.href = "database.html?" + encodeURIComponent(db.name);
}
},
success: function(resp) {
if(!resp.views || !resp.views[localViewName]) {
- $.cookies.remove(dbName + ".view");
+ $.futon.storage.del("view");
location.href = "database.html?" + encodeURIComponent(db.name);
}
var viewCode = resp.views[localViewName];
@@ -512,10 +525,10 @@
if ($("#documents thead th.key").is(".desc")) {
if (typeof options.descending == 'undefined') options.descending = true;
var descend = true;
- $.cookies.set(dbName + ".desc", "1");
+ $.futon.storage.set("desc", "1");
} else {
var descend = false;
- $.cookies.remove(dbName + ".desc");
+ $.futon.storage.del("desc");
}
$("#paging a").unbind();
$("#documents").find("tbody.content").empty().end().show();
@@ -641,19 +654,19 @@
if (viewName == "_temp_view") {
$("#viewcode").show().removeClass("collapsed");
var mapFun = $("#viewcode_map").val();
- $.cookies.set(db.name + ".map", mapFun);
+ $.futon.storage.set("map_fun", mapFun);
var reduceFun = $.trim($("#viewcode_reduce").val()) || null;
if (reduceFun) {
- $.cookies.set(db.name + ".reduce", reduceFun);
+ $.futon.storage.set("reduce_fun", reduceFun);
if ($("#reduce :checked").length) {
options.group = true;
} else {
options.reduce = false;
}
} else {
- $.cookies.remove(db.name + ".reduce");
+ $.futon.storage.del("reduce");
}
- $.cookies.set(db.name + ".language", page.viewLanguage);
+ $.futon.storage.set("language", page.viewLanguage);
db.query(mapFun, reduceFun, page.viewLanguage, options);
} else if (viewName == "_design_docs") {
options.startkey = options.descending ? "_design0" : "_design";
diff --git a/share/www/script/futon.js b/share/www/script/futon.js
index 31f04a90..f5a81444 100644
--- a/share/www/script/futon.js
+++ b/share/www/script/futon.js
@@ -35,22 +35,22 @@
}
this.addDatabase = function(name) {
- var recentDbs = $.cookies.get("recent", "").split(",");
+ var recentDbs = $.futon.storage.get("recent", "").split(",");
if ($.inArray(name, recentDbs) == -1) {
recentDbs.unshift(name);
if (recentDbs.length > 10) recentDbs.length = 10;
- $.cookies.set("recent", recentDbs.join(","));
+ $.futon.storage.set("recent", recentDbs.join(","));
this.updateDatabases();
}
}
this.removeDatabase = function(name) {
// remove database from recent databases list
- var recentDbs = $.cookies.get("recent", "").split(",");
+ var recentDbs = $.futon.storage.get("recent").split(",");
var recentIdx = $.inArray(name, recentDbs);
if (recentIdx >= 0) {
recentDbs.splice(recentIdx, 1);
- $.cookies.set("recent", recentDbs.join(","));
+ $.futon.storage.set("recent", recentDbs.join(","));
this.updateDatabases();
}
}
@@ -61,7 +61,7 @@
selection = [this.pathname, this.search];
});
$("#dbs").empty();
- var recentDbs = $.cookies.get("recent", "").split(",");
+ var recentDbs = $.futon.storage.get("recent").split(",");
recentDbs.sort();
$.each(recentDbs, function(idx, name) {
if (name) {
@@ -123,13 +123,113 @@
$("#sidebar-toggle")
.attr("title", hidden ? "Show Sidebar" : "Hide Sidebar");
- $.cookies.set("sidebar", hidden ? "hidden" : "show");
+ $.futon.storage.set("sidebar", hidden ? "hidden" : "show");
};
}
+ function Storage() {
+ var storage = this;
+ this.decls = {};
+
+ this.declare = function(name, options) {
+ this.decls[name] = $.extend({}, {
+ scope: "window",
+ defaultValue: null,
+ prefix: ""
+ }, options || {});
+ }
+
+ this.declareWithPrefix = function(prefix, decls) {
+ for (var name in decls) {
+ var options = decls[name];
+ options.prefix = prefix;
+ storage.declare(name, options);
+ }
+ }
+
+ this.del = function(name) {
+ lookup(name, function(decl) {
+ handlers[decl.scope].del(decl.prefix + name);
+ });
+ }
+
+ this.get = function(name, defaultValue) {
+ return lookup(name, function(decl) {
+ var value = handlers[decl.scope].get(decl.prefix + name);
+ if (value !== undefined) {
+ return value;
+ }
+ if (defaultValue !== undefined) {
+ return defaultValue;
+ }
+ return decl.defaultValue;
+ });
+ }
+
+ this.set = function(name, value) {
+ lookup(name, function(decl) {
+ handlers[decl.scope].set(decl.prefix + name, value);
+ });
+ }
+
+ function lookup(name, callback) {
+ var decl = storage.decls[name];
+ if (decl === undefined) {
+ return decl;
+ }
+ return callback(decl);
+ }
+
+ var handlers = {
+
+ "cookie": {
+ get: function(name) {
+ var nameEq = name + "=";
+ var parts = document.cookie.split(';');
+ for (var i = 0; i < parts.length; i++) {
+ var part = parts[i].replace(/^\s+/, "");
+ if (part.indexOf(nameEq) == 0) {
+ return unescape(part.substring(nameEq.length, part.length));
+ }
+ }
+ },
+ set: function(name, value) {
+ var date = new Date();
+ date.setTime(date.getTime() + 14*24*60*60*1000); // two weeks
+ document.cookie = name + "=" + escape(value) + "; expires=" +
+ date.toGMTString();
+ },
+ del: function(name) {
+ var date = new Date();
+ date.setTime(date.getTime() - 24*60*60*1000); // yesterday
+ document.cookie = name + "=; expires=" + date.toGMTString();
+ }
+ },
+
+ "window": {
+ get: function(name) {
+ return JSON.parse(window.name || "{}")[name];
+ },
+ set: function(name, value) {
+ var obj = JSON.parse(window.name || "{}");
+ obj[name] = value || null;
+ window.name = JSON.stringify(obj);
+ },
+ del: function(name) {
+ var obj = JSON.parse(window.name || "{}");
+ delete obj[name];
+ window.name = JSON.stringify(obj);
+ }
+ }
+
+ };
+
+ }
+
$.futon = $.futon || {};
$.extend($.futon, {
- navigation: new Navigation()
+ navigation: new Navigation(),
+ storage: new Storage()
});
$.fn.addPlaceholder = function(text) {
@@ -183,9 +283,12 @@
.ajaxStart(function() { $(this.body).addClass("loading"); })
.ajaxStop(function() { $(this.body).removeClass("loading"); });
+ $.futon.storage.declare("sidebar", {scope: "cookie", defaultValue: "show"});
+ $.futon.storage.declare("recent", {scope: "cookie", defaultValue: ""});
+
$(function() {
document.title = "Apache CouchDB - Futon: " + document.title;
- if ($.cookies.get("sidebar") == "hidden") {
+ if ($.futon.storage.get("sidebar") == "hidden") {
// doing this as early as possible prevents flickering
$(document.body).addClass("fullwidth");
}
@@ -195,7 +298,7 @@
$.futon.navigation.toggle(e.shiftKey ? 2500 : 500);
return false;
});
- if ($.cookies.get("sidebar") == "hidden") {
+ if ($.futon.storage.get("sidebar") == "hidden") {
$.futon.navigation.toggle(0);
}
diff --git a/share/www/script/jquery.cookies.js b/share/www/script/jquery.cookies.js
deleted file mode 100644
index 642f1792..00000000
--- a/share/www/script/jquery.cookies.js
+++ /dev/null
@@ -1,50 +0,0 @@
-// Licensed under the Apache License, Version 2.0 (the "License"); you may not
-// use this file except in compliance with the License. You may obtain a copy of
-// the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
-// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
-// License for the specific language governing permissions and limitations under
-// the License.
-
-(function($) {
- $.cookies = $.cookies || {}
- $.extend($.cookies, {
-
- /* Return the value of a cookie. */
- get: function(name, defaultValue) {
- var nameEq = name + "=";
- var parts = document.cookie.split(';');
- for (var i = 0; i < parts.length; i++) {
- var part = parts[i].replace(/^\s+/, "");
- if (part.indexOf(nameEq) == 0) {
- return unescape(part.substring(nameEq.length, part.length));
- }
- }
- return defaultValue !== undefined ? defaultValue : null;
- },
-
- /* Create or update a cookie. */
- set: function(name, value, path, days) {
- var params = [];
- if (path) {
- params.push("; path=" + path);
- }
- if (days) {
- var date = new Date();
- date.setTime(date.getTime() + (days * 24*60*60*1000));
- params.push("; expires=" + date.toGMTString());
- }
- document.cookie = name + "=" + escape(value) + params.join();
- },
-
- /* Remove a cookie. */
- remove: function(name, path) {
- $.cookies.set(name, "", path, -1);
- }
-
- });
-})(jQuery);