Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Futon: Improve storage of session state by storing short-term setting…
…s in the window.name.

git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@889766 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cmlenz committed Dec 11, 2009
1 parent 3ffea6e commit 19e9b394717a7f6ee25e871ee1c8a06bedc40545
Showing 11 changed files with 151 additions and 98 deletions.
@@ -20,7 +20,6 @@
<link rel="stylesheet" href="style/layout.css?0.11.0" type="text/css">
<script src="script/json2.js"></script>
<script src="script/jquery.js?1.3.2"></script>
<script src="script/jquery.cookies.js?0.11.0"></script>
<script src="script/jquery.couch.js?0.11.0"></script>
<script src="script/jquery.editinline.js?0.11.0"></script>
<script src="script/futon.js?0.11.0"></script>
@@ -20,7 +20,6 @@
<link rel="stylesheet" href="style/layout.css?0.11.0" type="text/css">
<script src="script/json2.js"></script>
<script src="script/jquery.js?1.3.2"></script>
<script src="script/jquery.cookies.js?0.11.0"></script>
<script src="script/jquery.couch.js?0.11.0"></script>
<script src="script/couch.js?0.11.0"></script>
<script src="script/futon.js?0.11.0"></script>
@@ -20,7 +20,6 @@
<link rel="stylesheet" href="style/layout.css?0.11.0" type="text/css">
<script src="script/json2.js"></script>
<script src="script/jquery.js?1.3.2"></script>
<script src="script/jquery.cookies.js?0.11.0"></script>
<script src="script/jquery.couch.js?0.11.0"></script>
<script src="script/jquery.resizer.js?0.11.0"></script>
<script src="script/couch.js?0.11.0"></script>
@@ -20,7 +20,6 @@
<link rel="stylesheet" href="style/layout.css?0.11.0" type="text/css">
<script src="script/json2.js"></script>
<script src="script/jquery.js?1.3.2"></script>
<script src="script/jquery.cookies.js?0.11.0"></script>
<script src="script/jquery.couch.js?0.11.0"></script>
<script src="script/jquery.dialog.js?0.11.0"></script>
<script src="script/jquery.resizer.js?0.11.0"></script>
@@ -70,13 +69,10 @@
horizontal: true
});

// Restore preferences/state from cookies
var desc = $.cookies.get(page.db.name + ".desc");
if (desc) $("#documents thead th.key").addClass("desc");
var reduce = $.cookies.get(page.db.name + ".doreduce");
$("#reduce :checkbox")[0].checked = reduce;
var rowsPerPage = $.cookies.get(page.db.name + ".perpage");
if (rowsPerPage) $("#perpage").val(rowsPerPage);
// Restore preferences/state
$("#documents thead th.key").toggleClass("desc", $.futon.storage.get("desc"));
$("#reduce :checkbox")[0].checked = $.futon.storage.get("reduce");
$("#perpage").val($.futon.storage.get("per_page"));

page.populateViewsMenu();
page.populateViewEditor();
@@ -87,7 +83,7 @@

$("#switch select").change(function() {
var viewName = $(this).val();
if (!viewName) $.cookies.remove(page.db.name + ".view");
if (!viewName) $.futon.storage.del("view");
location.href = "?" + encodeURIComponent(page.db.name) +
(viewName ? "/" + viewName : "");
});
@@ -97,15 +93,11 @@
});
$("#reduce :checkbox").click(function() {
page.updateDocumentListing();
if (this.checked) {
$.cookies.set(page.db.name + ".doreduce", "1");
} else {
$.cookies.remove(page.db.name + ".doreduce");
}
$.futon.storage.set("reduce", this.checked);
});
$("#perpage").change(function() {
page.updateDocumentListing();
$.cookies.set(page.db.name + ".perpage", this.value);
$.futon.storage.set("per_page", this.value);
});
$("#toolbar button.add").click(page.newDocument);
$("#toolbar button.compact").click(page.compactDatabase);
@@ -20,7 +20,6 @@
<link rel="stylesheet" href="style/layout.css?0.11.0" type="text/css">
<script src="script/json2.js"></script>
<script src="script/jquery.js?1.3.2"></script>
<script src="script/jquery.cookies.js?0.11.0"></script>
<script src="script/jquery.couch.js?0.11.0"></script>
<script src="script/jquery.dialog.js?0.11.0"></script>
<script src="script/jquery.editinline.js?0.11.0"></script>
@@ -20,7 +20,6 @@
<link rel="stylesheet" href="style/layout.css?0.11.0" type="text/css">
<script src="script/json2.js"></script>
<script src="script/jquery.js?1.3.2"></script>
<script src="script/jquery.cookies.js?0.11.0"></script>
<script src="script/jquery.couch.js?0.11.0"></script>
<script src="script/jquery.dialog.js?0.11.0"></script>
<script src="script/futon.js?0.11.0"></script>
@@ -34,11 +33,11 @@
this.updateSelection(location.pathname + "index.html");
});
}
var dbsPerPage = $.cookies.get("perpage");
var dbsPerPage = $.futon.storage.get("per_page");
if (dbsPerPage) $("#perpage").val(dbsPerPage);
$("#perpage").change(function() {
page.updateDatabaseListing();
$.cookies.set("perpage", this.value);
$.futon.storage.set("per_page", this.value);
});

page.updateDatabaseListing();
@@ -20,7 +20,6 @@
<link rel="stylesheet" href="style/layout.css?0.11.0" type="text/css">
<script src="script/json2.js"></script>
<script src="script/jquery.js?1.3.2"></script>
<script src="script/jquery.cookies.js?0.11.0"></script>
<script src="script/jquery.couch.js?0.11.0"></script>
<script src="script/futon.js?0.11.0"></script>
<script>
@@ -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";
@@ -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);
}

0 comments on commit 19e9b39

Please sign in to comment.