diff options
Diffstat (limited to 'share/www/script/futon.js')
-rw-r--r-- | share/www/script/futon.js | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/share/www/script/futon.js b/share/www/script/futon.js index 1f51bdee..33c72318 100644 --- a/share/www/script/futon.js +++ b/share/www/script/futon.js @@ -12,6 +12,130 @@ (function($) { + function Session() { + + function doLogin(username, password, callback) { + $.couch.login({ + username : username, + password : password, + success : function() { + $.futon.session.sidebar(); + callback(); + }, + error : function(code, error, reason) { + $.futon.session.sidebar(); + callback({username : "Error logging in: "+reason}); + } + }); + }; + + function doSignup(username, password, callback, runLogin) { + $.couch.signup({ + username : username + }, password, { + success : function() { + if (runLogin) { + doLogin(username, password, callback); + } else { + callback(); + } + }, + error : function(status, error, reason) { + $.futon.session.sidebar(); + if (error = "conflict") { + callback({username : "Name '"+username+"' is taken"}); + } else { + callback({username : "Signup error: "+reason}); + } + } + }); + }; + + function validateUsernameAndPassword(data, callback) { + if (!data.username || data.username.length == 0) { + callback({username: "Please enter a username."}); + return false; + }; + if (!data.password || data.password.length == 0) { + callback({password: "Please enter a password."}); + return false; + }; + return true; + }; + + function createAdmin() { + $.showDialog("dialog/_create_admin.html", { + submit: function(data, callback) { + if (!validateUsernameAndPassword(data, callback)) return; + $.couch.config({ + success : function() { + callback(); + doLogin(data.username, data.password, callback); + doSignup(data.username, null, callback, false); + } + }, "admins", data.username, data.password); + } + }); + return false; + }; + + function login() { + $.showDialog("dialog/_login.html", { + submit: function(data, callback) { + if (!validateUsernameAndPassword(data, callback)) return; + doLogin(data.username, data.password, callback); + } + }); + return false; + }; + + function logout() { + $.couch.logout({ + success : function(resp) { + $.futon.session.sidebar(); + } + }) + }; + + function signup() { + $.showDialog("dialog/_signup.html", { + submit: function(data, callback) { + if (!validateUsernameAndPassword(data, callback)) return; + doSignup(data.username, data.password, callback, true); + } + }); + return false; + }; + + this.setupSidebar = function() { + $("#userCtx .login").click(login); + $("#userCtx .logout").click(logout); + $("#userCtx .signup").click(signup); + $("#userCtx .createadmin").click(createAdmin); + }; + + this.sidebar = function() { + // get users db info? + $("#userCtx span").hide(); + $.couch.session({ + success : function(userCtx) { + if (userCtx.name) { + $("#userCtx .username").text(userCtx.name).attr({href : "/_utils/document.html?users/org.couchdb.user%3A"+userCtx.name}); + if (userCtx.roles.indexOf("_admin") != -1) { + $("#userCtx .loggedinadmin").show(); + } else { + $("#userCtx .loggedin").show(); + } + } else if (userCtx.roles.indexOf("_admin") != -1) { + $("#userCtx .adminparty").show(); + } else { + $("#userCtx .loggedout").show(); + }; + } + }) + }; + }; + function Navigation() { var nav = this; this.loaded = false; @@ -233,6 +357,7 @@ $.futon = $.futon || {}; $.extend($.futon, { navigation: new Navigation(), + session : new Session(), storage: new Storage() }); @@ -309,12 +434,15 @@ $.futon.navigation.updateDatabases(); $.futon.navigation.updateSelection(); $.futon.navigation.ready(); + $.futon.session.setupSidebar(); + $.futon.session.sidebar(); $.couch.info({ success: function(info, status) { $("#version").text(info.version); } }); + }); }); |