From b756e5ac630b7ff6bdf472f756187b2fa025557f Mon Sep 17 00:00:00 2001 From: Vijay Rudraraju Date: Mon, 2 Jan 2012 19:23:10 -0800 Subject: [PATCH] autosaving --- _attachments/index.html | 10 +-- _attachments/js/main.js | 11 +-- _attachments/js/util.js | 27 ++++++ vendor/couchapp/evently/profile/loggedIn.js | 92 ++++++++++++++------- 4 files changed, 100 insertions(+), 40 deletions(-) diff --git a/_attachments/index.html b/_attachments/index.html index ab5de0e..80d0934 100644 --- a/_attachments/index.html +++ b/_attachments/index.html @@ -1,7 +1,7 @@ - toiweb + werdmawb @@ -18,7 +18,7 @@ -
+
@@ -39,7 +39,7 @@
-
+
@@ -68,7 +68,7 @@
-
+

login

@@ -85,7 +85,7 @@

login

-
+

signup

diff --git a/_attachments/js/main.js b/_attachments/js/main.js index 37764fd..183635e 100644 --- a/_attachments/js/main.js +++ b/_attachments/js/main.js @@ -7,11 +7,11 @@ $(document).ready(function() { console.log('$document.ready'); $.couch.app(function(app) { $("#account1").evently("account", app); - $("#account2").evently("account", app); + //$("#account2").evently("account", app); $("#profile1").evently("profile", app); - $("#profile2").evently("profile", app); + //$("#profile2").evently("profile", app); $.evently.connect("#account1","#profile1", ["loggedIn","loggedOut"]); - $.evently.connect("#account2","#profile2", ["loggedIn","loggedOut"]); + //$.evently.connect("#account2","#profile2", ["loggedIn","loggedOut"]); //$('#account').trigger('create'); }); @@ -109,8 +109,8 @@ $(document).ready(function() { $(this).data('timeIndex',{}); $(this).data('assocIndex',{}); - $(this).html('apple apartment application apart\nhello'); - $(this).trigger('keyup'); + //$(this).html('apple apartment application apart\nhello'); + //$(this).trigger('keyup'); }, keyup: function(e) { var newTime = $.now(); @@ -143,6 +143,7 @@ $(document).ready(function() { // end word index $(this).data('oldTime',newTime); + startAutoSaver(); } }); diff --git a/_attachments/js/util.js b/_attachments/js/util.js index b6ac18a..ac43f05 100644 --- a/_attachments/js/util.js +++ b/_attachments/js/util.js @@ -370,3 +370,30 @@ function calcLevenshteinDistance(firstWord,secondWord) { return distMatrix[matrixWidth-1][matrixHeight-1]; } + +function startAutoSaver() { + $('#profile1').data('autosave',true); + autoSaver(); +} +function stopAutoSaver() { + $('#profile1').data('autosave',false); +} +function autoSaver() { + if ($('#profile1').data('autosave')) { + console.log('autosaving'); + $('#profile1').html('

Saving...

'); + var mainDb = $.couch.db('werdmawb'); + var newDoc = $('#profile1').data('mainDoc'); + newDoc['wordIndex'] = $('#textarea1').data('wordIndex'); + newDoc['phraseIndex'] = $('#textarea1').data('phraseIndex'); + mainDb.saveDoc(newDoc, + { + success: function(data) { + $('#profile1').html('

Saved!

'); + stopAutoSaver(); + var t = setTimeout('autoSaver()',10000); + } + } + ); + } +} diff --git a/vendor/couchapp/evently/profile/loggedIn.js b/vendor/couchapp/evently/profile/loggedIn.js index dc7368c..d7a3194 100644 --- a/vendor/couchapp/evently/profile/loggedIn.js +++ b/vendor/couchapp/evently/profile/loggedIn.js @@ -2,41 +2,73 @@ function(e, r) { var userCtx = r.userCtx; var widget = $(this); // load the profile from the user doc - var db = $.couch.db(r.info.authentication_db); + var userDb = $.couch.db(r.info.authentication_db); + var mainDb = $.couch.db('werdmawb'); var userDocId = "org.couchdb.user:"+userCtx.name; - console.log('profile, loggedin'); - db.openDoc(userDocId, { + + console.log(userCtx.name+' profile, loggedin'); + + userDb.openDoc(userDocId, { success : function(userDoc) { console.log('open userdoc'); - var profile = userDoc["couch.app.profile"]; - if (profile) { - // we copy the name to the profile so it can be used later - // without publishing the entire userdoc (roles, pass, etc) - profile.name = userDoc.name; - $$(widget).profile = profile; - //widget.trigger("profileReady", [profile]); - } else { - //widget.trigger("noProfile", [userCtx]); - var newProfile = {}; - // store the user profile on the user account document - $.couch.userDb(function(db) { - var userDocId = "org.couchdb.user:"+userDoc.name; - db.openDoc(userDocId, { - success : function(userDoc) { - console.log('open userdoc'); - /* - userDoc["couch.app.profile"] = newProfile; - db.saveDoc(userDoc, { - success : function() { - newProfile.name = userDoc.name; - $$(widget).profile = newProfile; - } - }); - */ + var profile = userDoc['couch.app.profile']; + if (profile['main']) { + // we copy the name to the profile so it can be used later + // without publishing the entire userdoc (roles, pass, etc) + profile.name = userDoc.name; + $('#profile1').data('profile', profile); + mainDb.openDoc(profile.main, { + success : function(mainDoc) { + $('#profile1').data('mainDoc',mainDoc); + $('#profile1').data('wordIndex',mainDoc.wordIndex); + $('#profile1').data('phraseIndex',mainDoc.phraseIndex); + $('#textarea1').data('wordIndex',mainDoc.wordIndex); + $('#textarea1').data('phraseIndex',mainDoc.phraseIndex); + startAutoSaver(); } }); - }); - } + //widget.trigger("profileReady", [profile]); + } else { + //widget.trigger("noProfile", [userCtx]); + console.log('reinitializing user'); + var newProfile = {}; + var newId = ''; + mainDb.saveDoc( + { + wordIndex:$('#textarea1').data('wordIndex'), + phraseIndex:$('#textarea1').data('phraseIndex') + }, + { + success: function(data) { + console.log('maindoc saved'); + console.log(data); + newId = data.id; + console.log('open maindoc'); + newProfile.main = newId; + newProfile.name = userDoc.name; + userDoc['couch.app.profile'] = newProfile; + // store the user profile on the user account document + userDb.saveDoc(userDoc, { + success : function() { + console.log('save userdoc'); + $('#profile1').data('profile', newProfile); + startAutoSaver(); + } + }); + mainDb.openDoc(newProfile.main, { + success : function(mainDoc) { + $('#profile1').data('mainDoc',mainDoc); + $('#profile1').data('wordIndex',mainDoc.wordIndex); + $('#profile1').data('phraseIndex',mainDoc.phraseIndex); + $('#textarea1').data('wordIndex',mainDoc.wordIndex); + $('#textarea1').data('phraseIndex',mainDoc.phraseIndex); + startAutoSaver(); + } + }); + } + } + ); + } } }); }