var DataHandlerJSRevision="$Revision: 70 $".split(' ')[1] function changedEntryDataHandler(response) { var allEntries=response.Entries; var maxModified=0; if (allEntries != null) { db.transaction(["Entry"]).then( // transaction has three callbacks in the .then function: function(){ // Transaction Complete if (maxModified > 0) { db.objectStore("maxModified").put({objectId:"Entry",value:maxModified}); } $("#lstEntries").listview("refresh"); console.log("Transaction complete [ changedEntryDataHandler ]" ); check4Changes(); }, function(){ // Transaction Aborted $("#lstEntries").listview("refresh"); console.log("Transaction aborted! [ changedEntryDataHandler ]" ); }, function(t){ // Transaction in Progress for (i=0;i Number(maxModified)) { maxModified=theEntry.Modified; } console.log("[ changedEntryDataHandler ] Added Entry from Server : " , theEntry); }).fail( function (result, event){ console.log("Failed Inserting List Item: " , result , event ) }); } else { console.log("[ changedEntryDataHandler ] Entry from Server : " , theEntry, " didn't exist on the client, therefore doing nothing"); if (Number(theEntry.Modified) > Number(maxModified)) { maxModified=theEntry.Modified; } } } else { if (theEntry.Status=="d") { t.objectStore("Entry").delete(result).done(function (result,event){ if (Number(theEntry.Modified) > Number(maxModified)) { maxModified=theEntry.Modified; } }); deleteEntryFromUi(result); console.log("Deleted Entry:" , theEntry.Description , " Key:" , result) } else { // shop updated t.objectStore("Entry").put({"Description":theEntry.Description, "shopGlobalId":theEntry.ShopGlobalId }).done( function(result,event){ if (Number(theEntry.Modified) > Number(maxModified)) { maxModified=theEntry.Modified; } console.log("[ changedEntryDataHandler ] changed Entry from Server : " , theEntry ); }).fail( function (result, event){ console.log("Failed changing Entry List Item: " , result , event ) }); } } }); } singleEntryInsert(theEntry); } } ); } else { deleteQitem(response); console.log("[ changedEntryDataHandler ] - No New Entries." ); } } function brandBrowser(sShoppingListName) { sGShoppingListName=sShoppingListName; sGBrowserId=generateUUID(); addToQ("BrandBrowser",sGShoppingListName); db.objectStore("Config").add({Id:"ShoppingListName",value:sGShoppingListName}); db.objectStore("Config").add({Id:"BrowserId",value:sGBrowserId}); db.objectStore("maxModified").add({objectId:"Entry",value:0}); db.objectStore("maxModified").add({objectId:"Shop",value:0}); // Actually - that's not it: it's only for new shopping lists and not if a member is added... //db.objectStore("Shop").add({ShopName:"ShopName(Click to change)"}); $("#pnlBranding").hide(); setMode("Entries"); loadAndShowShops(); window.setTimeout(check4Changes,1000); } function unbrandBrowser() { db.deleteDatabase("Shoppinglist").done(function(){ window.location.reload() }); } function addNewEntry(sEntry){ var shopGlobalId=getActiveShopGlobalId(); if (shopGlobalId== null) { shopGlobalId=generateUUID(); shopChangeHandler(shopGlobalId,"Default"); } db.objectStore("Entry").add({"Description":sEntry,"shopGlobalId":shopGlobalId }).done( function(result,event){ addToQ("SendEntry",result); addEntryToUi(result,sEntry); $("#lstEntries").listview("refresh"); }).fail( function (result, event){ console.log("Failed Inserting List Item: " , result, event ) }); } var lastDeletedEntry = null; function deleteEntry(sKey) { db.objectStore("Entry").get(Number(sKey)).done( function(result,event){ lastDeletedEntry = result; lastDeletedEntry.key = Number(sKey); addToQ("DeleteEntry",result); db.objectStore("Entry").delete(Number(sKey)).done( function(result,event){ deleteEntryFromUi(sKey); showUndoButton(); }).fail( function (result, event){ console.log("[deleteEntry] Failed deleting List Item: " , result , event ) }); }).fail( function (result, event){ console.log("[deleteEntry] Failed retrieving List Item Details: " , result , event ) }); } function undoDeleteEntry() { if (lastDeletedEntry) { db.objectStore("Entry").add({"Description":lastDeletedEntry.Description,"shopGlobalId":lastDeletedEntry.shopGlobalId }).done( function(result,event){ addToQ("SendEntry",result); addEntryToUi(result,lastDeletedEntry.Description); $("#lstEntries").listview("refresh"); lastDeletedEntry = null; hideUndoButton(); }).fail( function (result, event){ console.log("Failed Inserting List Item: " , result, event ) }); } } function shopDeletion(shopGlobalId){ db.objectStore("Shop").delete(shopGlobalId) .done(function(){ addToQ("ShopDeletion",shopGlobalId); $("#pnlShops>ul>li>a.ui-btn-active").remove(); loadAndShowShops(null).done(function(){ setMode("Entries"); }); }); } function shopChangeHandler(shopGlobalId,shopName){ db.objectStore("Shop").put({"GlobalId":shopGlobalId, "Description":shopName}) .done(function(result){ console.log("[shopChangeHandler] - created new Shop with UUID " , shopGlobalId , " and Description " , shopName , " resulting in key " , result ); addToQ("SendShop",shopGlobalId); loadAndShowShops(shopGlobalId).done(function(){ setMode("Entries"); }); }) .fail(function (result,event){ console.log("[shopChangeHandler] - error creating new Shop: " , result , event ) }); } function check4Changes(){ var ojAllModi=new Object(); ojAllModi.maxModified=new Object(); db.transaction(["maxModified"]).then( // transaction has three callbacks in the .then function: function() { // transaction complete callServerFunction("SendChangedData",ojAllModi,changedDataTypeDispatcher, function(){ // on error wait 5 sec. window.setTimeout(check4Changes,5000); } ); }, function(){ // Transaction Aborted console.log("Transaction aborted! [ check4Changes ]" ); }, function(t){ // Transaction in Progress t.objectStore("maxModified").each(function(result){ if (result != null) { console.log("[Check4Changes] - maxModified " , result); ojAllModi.maxModified[result.key]=result.value.value; } }); } ); } function changedDataTypeDispatcher(response){ console.log("[changedDataTypeDispatcher] Detected type: " , response.ChangeType); if (response.ChangeType=="Entry") { changedEntryDataHandler(response) } if (response.ChangeType=="Shop") { changedShopDataHandler(response) } if (response.ChangeType=="None"){ check4Changes(); } } function changedShopDataHandler(response){ var allShops=response.Shops; var maxModified=0; if (allShops != null) { db.transaction(["Shop"]).then( // transaction has three callbacks in the .then function: function(){ // Transaction Complete if (maxModified > 0) { db.objectStore("maxModified").put({objectId:"Shop",value:maxModified}) .done(function(result){ console.log("updated modified to " + maxModified + " [ changedShopDataHandler ]" ); }) .fail(function(result,event){ console.log("[changeShopDataHandler] Failed to update modified: " , result ,event ); }); } loadAndShowShops(); console.log("[changeShopDataHandler] Transaction complete" ); check4Changes(); }, function(){ // Transaction Aborted console.log("Transaction aborted! [ changedShopDataHandler ]" ); }, function(t){ // Transaction in Progress for (i=0;i Number(maxModified)) { maxModified=theShop.Modified; } console.log("[ changedShopDataHandler ] Added Shop from Server : " . theShop); }).fail( function (result, event){ console.log("Failed Inserting List Item: " , result , event ) }); } else { // yes, looks a bit doubled but has to be as the other 2 are transaction success dependant... if (Number(theShop.Modified) > Number(maxModified)) { maxModified=theShop.Modified; } } } else { if (theShop.Status=="d") { t.objectStore("Shop").delete(result.GlobalId).done(function (result,event){ if (Number(theShop.Modified) > Number(maxModified)) { maxModified=theShop.Modified; } }); console.log("Deleted Shop:" , theShop ) } else { // shop updated t.objectStore("Shop").put({"Description":theShop.Description, "GlobalId":theShop.GlobalId }).done( function(result,event){ if (Number(theShop.Modified) > Number(maxModified)) { maxModified=theShop.Modified; } console.log("[ changedShopDataHandler ] changed Shop from Server : " , theShop ); }).fail( function (result, event){ console.log("[ changedShopDataHandler ] Failed changing Shop Item: " , result,event ); }); } } }); } singleShopInsert(theShop); } } ); } else { deleteQitem(response); console.log("[ changedShopDataHandler ] - No New Shops." ); } }