Created
January 18, 2009 11:08
-
-
Save nikosd/48596 to your computer and use it in GitHub Desktop.
Some sample code from my thesis about creating offline web applications (http://nikosd.com/work.html). iris.js is partially the "core" of the app and database.js is a simple object mapping of the database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var DB = { | |
init: function(){ | |
dojox.sql("CREATE TABLE IF NOT EXISTS received_messages (" | |
+ "id INTEGER PRIMARY KEY, " | |
+ "title TEXT NOT NULL, " | |
+ "content TEXT, " | |
+ "sender_name TEXT NOT NULL, " | |
+ "sender_id TEXT NOT NULL) "); | |
dojox.sql("CREATE TABLE IF NOT EXISTS local_draft_messages (" | |
+ "id INTEGER PRIMARY KEY, " | |
+ "title TEXT NOT NULL, " | |
+ "content TEXT, " | |
+ "recepient_name TEXT) "); | |
dojox.sql("CREATE TABLE IF NOT EXISTS remote_draft_messages (" | |
+ "id INTEGER PRIMARY KEY, " | |
+ "title TEXT NOT NULL, " | |
+ "content TEXT, " | |
+ "recepient_name TEXT, " | |
+ "recepient_id TEXT, " | |
+ "lock_version INTEGER) "); | |
dojox.sql("CREATE TABLE IF NOT EXISTS sent_messages (" | |
+ "id INTEGER PRIMARY KEY, " | |
+ "title TEXT NOT NULL, " | |
+ "content TEXT, " | |
+ "recepient_name TEXT NOT NULL, " | |
+ "recepient_id TEXT NOT NULL) "); | |
dojox.sql("CREATE TABLE IF NOT EXISTS queued_actions (" | |
+ "id INTEGER PRIMARY KEY, " | |
+ "action TEXT NOT NULL, " | |
+ "data TEXT NOT NULL) "); | |
dojox.sql("CREATE TABLE IF NOT EXISTS old_remote_draft_messages (" | |
+ "id INTEGER PRIMARY KEY, " | |
+ "title TEXT NOT NULL, " | |
+ "content TEXT, " | |
+ "recepient_name TEXT, " | |
+ "recepient_id TEXT, " | |
+ "lock_version INTEGER) "); | |
}, | |
getReceived: function(){ | |
return dojox.sql("SELECT * FROM received_messages ORDER BY id DESC"); | |
}, | |
setReceived: function(received){ | |
dojox.sql("DELETE FROM received_messages"); | |
for (i in received){ | |
dojox.sql("INSERT INTO received_messages (id, title, content, sender_name, sender_id) " | |
+ "VALUES (?,?,?,?,?)", received[i].id, | |
received[i].title, | |
received[i].content, | |
received[i].sender_name, | |
received[i].sender_id); | |
} | |
}, | |
getSent: function(){ | |
return dojox.sql("SELECT * FROM sent_messages ORDER BY id DESC"); | |
}, | |
setSent: function(sent){ | |
dojox.sql("DELETE FROM sent_messages"); | |
for (i in sent){ | |
dojox.sql("INSERT INTO sent_messages (id, title, content, recepient_name, recepient_id) " | |
+ "VALUES (?,?,?,?,?)", sent[i].id, | |
sent[i].title, | |
sent[i].content, | |
sent[i].recepient_name, | |
sent[i].recepient_id); | |
} | |
}, | |
getLocalDrafts: function(){ | |
return dojox.sql("SELECT * FROM local_draft_messages ORDER BY id DESC"); | |
}, | |
setRemoteDrafts: function(drafts){ | |
dojox.sql("DELETE FROM local_draft_messages"); | |
for (i in drafts){ | |
dojox.sql("INSERT INTO local_draft_messages (id, title, content, recepient_name) " | |
+ "VALUES (?,?,?,?)", drafts[i].id, | |
drafts[i].title, | |
drafts[i].content, | |
drafts[i].recepient_name); | |
} | |
}, | |
getRemoteDrafts: function(){ | |
return dojox.sql("SELECT * FROM remote_draft_messages ORDER BY id DESC"); | |
}, | |
setRemoteDrafts: function(drafts){ | |
dojox.sql("DELETE FROM remote_draft_messages"); | |
for (i in drafts){ | |
dojox.sql("INSERT INTO remote_draft_messages (id, title, content, recepient_id, recepient_name, lock_version) " | |
+ "VALUES (?,?,?,?,?,?)", drafts[i].id, | |
drafts[i].title, | |
drafts[i].content, | |
drafts[i].recepient_id, | |
drafts[i].recepient_name, | |
drafts[i].lock_version); | |
} | |
}, | |
// Single rows by id | |
getLocalDraftMessage: function(msgID){ | |
return dojox.sql("SELECT * FROM local_draft_messages WHERE id = ?", msgID)[0]; | |
}, | |
updateLocalDraftMessage: function(msgID, attributes){ | |
var title = attributes.title || ''; | |
var content = attributes.content || ''; | |
var recepient_name = attributes.recepient_name || ''; | |
dojox.sql("UPDATE local_draft_messages " | |
+ "SET title = ?, content = ?, recepient_name = ? " | |
+ "WHERE id = ?", | |
title, content, recepient_name, | |
msgID); | |
}, | |
deleteLocalDraftMessage: function(msgID){ | |
dojox.sql("DELETE FROM local_draft_messages WHERE id = ?", msgID); | |
}, | |
getRemoteDraftMessage: function(msgID){ | |
return dojox.sql("SELECT * FROM remote_draft_messages WHERE id = ?", msgID)[0]; | |
}, | |
createRemoteDraftMessage: function(msg){ | |
var title = msg.title || ''; | |
var content = msg.content || ''; | |
var recepient_name = msg.recepient_name || ''; | |
var recepient_id = msg.recepient_id || ''; | |
var lock_version = msg.lock_version || '0'; | |
dojox.sql("INSERT INTO remote_draft_messages (id, title, content, recepient_id, recepient_name, lock_version) " | |
+ "VALUES (?,?,?,?,?,?)", msg.id, title, content, recepient_id, recepient_name, lock_version); | |
}, | |
updateRemoteDraftMessage: function(msg){ | |
var title = msg.title || ''; | |
var content = msg.content || ''; | |
var recepient_name = msg.recepient_name || ''; | |
var recepient_id = msg.recepient_id || ''; | |
var lock_version = msg.lock_version || '0'; | |
dojox.sql("UPDATE remote_draft_messages " | |
+ "SET title = ?, content = ?, recepient_name = ?, recepient_id = ?, lock_version = ? " | |
+ "WHERE id = ?", | |
title, content, recepient_name, recepient_id, lock_version, | |
msg.id); | |
}, | |
deleteRemoteDraftMessage: function(msgID){ | |
dojox.sql("DELETE FROM remote_draft_messages WHERE id = ?", msgID); | |
}, | |
getOldRemoteDraftMessage: function(msgID){ | |
return dojox.sql("SELECT * FROM old_remote_draft_messages WHERE id = ?", msgID)[0]; | |
}, | |
createOldRemoteDraftMessage: function(msg){ | |
if(!this.getOldRemoteDraftMessage(msg.id)){ | |
var title = msg.title || ''; | |
var content = msg.content || ''; | |
var recepient_name = msg.recepient_name || ''; | |
var recepient_id = msg.recepient_id || ''; | |
var lock_version = msg.lock_version || '0'; | |
dojox.sql("INSERT INTO old_remote_draft_messages (id, title, content, recepient_id, recepient_name, lock_version) " | |
+ "VALUES (?,?,?,?,?,?)", msg.id, title, content, recepient_id, recepient_name, lock_version); | |
return true; | |
}else{ | |
return false; | |
} | |
}, | |
deleteOldRemoteDraftMessage: function(msgID){ | |
dojox.sql("DELETE FROM old_remote_draft_messages WHERE id = ?", msgID); | |
}, | |
createSentMessage: function(msg){ | |
var title = msg.title || ''; | |
var content = msg.content || ''; | |
var recepient_name = msg.recepient_name || ''; | |
var recepient_id = msg.recepient_id || ''; | |
dojox.sql("INSERT INTO sent_messages (id, title, content, recepient_name, recepient_id) " | |
+ "VALUES (?,?,?,?,?)", msg.id, title, content, recepient_name, recepient_id); | |
}, | |
deleteSentMessage: function(msgID){ | |
dojox.sql("DELETE FROM sent_messages WHERE id = ?", msgID); | |
}, | |
deleteReceivedMessage: function(msgID){ | |
dojox.sql("DELETE FROM received_messages WHERE id = ?", msgID); | |
}, | |
// Queued actions | |
isReceivedMessageQueued: function(msgID){ | |
if (dojox.sql("SELECT id FROM queued_actions WHERE data = ? AND action = ?", msgID, "deleteReceivedMessageFromInbox").length) { | |
return true; | |
}else{ | |
return false; | |
} | |
}, | |
isLocalDraftQueuedForCreation: function(msgID){ | |
if (dojox.sql("SELECT id FROM queued_actions WHERE data = ? AND action = ?", msgID, "createNewDraft").length){ | |
return true; | |
}else{ | |
return false; | |
} | |
}, | |
isRemoteDraftQueuedForSend: function(msgID){ | |
if (dojox.sql("SELECT id FROM queued_actions WHERE data = ? AND action = ?", msgID, "sendRemoteDraft").length){ | |
return true; | |
}else{ | |
return false; | |
} | |
}, | |
isRemoteDraftQueuedForEdit: function(msgID){ | |
if (dojox.sql("SELECT id FROM queued_actions WHERE data = ? AND action = ?", msgID, "editRemoteDraft").length){ | |
return true; | |
}else{ | |
return false; | |
} | |
}, | |
isRemoteDraftQueuedForDeletion: function(msgID){ | |
if (dojox.sql("SELECT id FROM queued_actions WHERE data = ? AND action = ?", msgID, "deleteRemoteDraft").length){ | |
return true; | |
}else{ | |
return false; | |
} | |
}, | |
deleteQueuedAction: function(actionID){ | |
dojox.sql("DELETE FROM queued_actions WHERE id = ?", actionID); | |
}, | |
deleteQueuedActionByTypeAndData: function(actionType, actionData){ | |
dojox.sql("DELETE FROM queued_actions WHERE action = ? AND data = ?", actionType, actionData); | |
}, | |
addActionToQueue: function(actionMsg, actionID){ | |
dojox.sql("INSERT INTO queued_actions (action, data) VALUES (?,?)", actionMsg, actionID); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dojox.off.ui.appName = "The message memory demo take 2"; | |
dojox.off.files.slurp(); | |
var Iris = { | |
_localDraftMessages: [], | |
_remoteDraftMessages: [], | |
_sentMessages: [], | |
_receivedMessages: [], | |
_nextID: 1, | |
initialize: function(){ | |
DB.init(); | |
this._refreshAllMessages(); | |
dojo.byId('sendMessageButton').disabled = true; | |
if (last = this._localDraftMessages[this._localDraftMessages.length - 1]){ | |
this._nextID = last.id + 1; | |
} | |
this._addOnSyncHandlers(); | |
this._addOnNetworkHandlers(); | |
}, | |
/******* Event Handlers ********/ | |
_addOnSyncHandlers: function(){ | |
dojo.connect(dojox.off.sync, "onSync", this, function(type){ | |
switch (type){ | |
case "start": | |
dojox.off.sync.error = false; | |
dojox.off.sync.successful = true; | |
UploadSync.finished = false; | |
UploadSync.halted = false; | |
DownloadSync.finished = false; | |
DownloadSync.halted = false; | |
dojo.byId('dot-sync-details').style.display = 'none'; | |
break; | |
case "upload": | |
UploadSync.start(); | |
break; | |
case "download": | |
dojox.off.sync.details = []; | |
if (UploadSync.finished == true && UploadSync.halted == false){ | |
DownloadSync.start(); | |
if (DownloadSync.finished == true && DownloadSync.halted == false){ | |
dojox.off.sync.finishedDownloading(); | |
}else{ | |
dojox.off.sync.finishedDownloading(false, DownloadSync.error); | |
} | |
}else{ | |
dojox.off.sync.finishedDownloading(false, UploadSync.error); | |
} | |
break; | |
case "finished": | |
this._refreshAllMessages(); | |
dojo.byId('sendMessageButton').disabled = false; | |
configSyncDetailsLink(); | |
break; | |
} | |
}); | |
}, | |
_addOnNetworkHandlers: function(){ | |
dojo.connect(dojox.off, "onNetwork", function(status){ | |
sendMessageButton = dojo.byId('sendMessageButton'); | |
if(status == "online"){ | |
sendMessageButton.disabled = false; | |
}else if(status == "offline"){ | |
sendMessageButton.disabled = true; | |
} | |
}); | |
}, | |
/**** End of Event Handlers ****/ | |
/************ HELPERS! ************/ | |
_refreshAllMessages: function(){ | |
this._refreshReceivedMessages(); | |
this._refreshSentMessages(); | |
this._refreshDraftMessages(); | |
}, | |
_refreshReceivedMessages: function(){ | |
this._receivedMessages = DB.getReceived(); | |
this._printReceivedMessages(); | |
}, | |
_refreshSentMessages: function(){ | |
this._sentMessages = DB.getSent(); | |
this._printSentMessages(); | |
}, | |
_refreshDraftMessages: function(){ | |
this._localDraftMessages = DB.getLocalDrafts(); | |
this._remoteDraftMessages = DB.getRemoteDrafts(); | |
this._printDraftMessages(); | |
}, | |
/******** END OF HELPERS! *********/ | |
/******** GUI ********/ | |
_printAllMessages: function(){ | |
this._printReceivedMessages(); | |
this._printDraftMessages(); | |
this._printSentMessages(); | |
}, | |
_printReceivedMessages: function(){ | |
var outputElem = dojo.byId("receivedMessagesData"); | |
var msgOutput = ""; | |
if(!this._receivedMessages.length){ | |
msgOutput = '<p>No received messages available</p>'; | |
}else{ | |
msgOutput += '<table border="0" cellspacing="5" cellpadding="5">' | |
+ '<tr><th>ID</th><th>Message</th><th>Sender</th><th></th></tr>'; | |
for (m in this._receivedMessages){ | |
var msg = this._receivedMessages[m]; | |
var link = ''; | |
if (DB.isReceivedMessageQueued(msg.id)){ | |
link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.cancelRemoveReceivedMessageFromInboxRequest(' + msg.id + ');">' | |
+ 'Cancel delete' | |
+ '</a>'; | |
}else{ | |
link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.removeReceivedMessageFromInbox(' + msg.id + ');">' | |
+ 'Delete' | |
+ '</a>'; | |
} | |
msgOutput += '<tr>' | |
+ '<td>' + msg.id + '</td>' | |
+ '<td>' + msg.title + '</td>' | |
+ '<td>' + msg.sender_name + '</td>' | |
+ '<td>' + link + '</td>' | |
+ '</tr>' | |
+ '<tr id="sentMsg_' + msg.id + '_Content">' | |
+ '<td></td>' | |
+ '<td colspan="2">' + msg.content + '</td>' | |
+ '</tr>'; | |
} | |
msgOutput += '</table>'; | |
} | |
outputElem.innerHTML = msgOutput; | |
}, | |
_printDraftMessages: function(){ | |
var outputElem = dojo.byId("draftMessagesData"); | |
var msgOutput = ""; | |
if (this._remoteDraftMessages.length == 0 && this._localDraftMessages.length == 0){ | |
msgOutput = '<p>No draft messages available</p>'; | |
}else{ | |
msgOutput += '<table border="0" cellspacing="5" cellpadding="5">' | |
+ '<tr><th>ID</th><th>Message title</th><th>Recepient</th><th></th><th></th><th></th></tr>'; | |
// Local Drafts: | |
for (m in this._localDraftMessages){ | |
var msg = this._localDraftMessages[m]; | |
var edit_link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.showEditFormForLocalDraft(' + msg.id + ');">' | |
+ 'Edit' | |
+ '</a>'; | |
var del_link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.deleteLocalDraftMessage(' + msg.id + ');">' | |
+ 'Delete' | |
+ '</a>'; | |
msgOutput += '<tr>' | |
+ '<td>' + msg.id + '</td>' | |
+ '<td>' + msg.title + '</td>' | |
+ '<td>' + msg.recepient_name + '</td>' | |
+ '<td>' + del_link + '</td>' | |
+ '<td>' + edit_link + '</td>' | |
+ '<td> </td>' | |
+ '</tr>' | |
+ '<tr id="storedMsg_' + msg.id + '_Content">' | |
+ '<td></td>' | |
+ '<td colspan="2">' + msg.content + '</td>' | |
+ '</tr>'; | |
} | |
// Remote Drafts: | |
for (m in this._remoteDraftMessages){ | |
var msg = this._remoteDraftMessages[m]; | |
var del_link = ''; | |
var edit_link = ''; | |
var send_link = ''; | |
if (DB.isRemoteDraftQueuedForSend(msg.id)){ | |
send_link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.cancelSendRemoteDraftMessageRequest(' + msg.id + ');">' | |
+ 'Cancel send' | |
+ '</a>'; | |
}else if (DB.isRemoteDraftQueuedForEdit(msg.id)){ | |
edit_link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.cancelEditRemoteDraftMessage(' + msg.id + ');">' | |
+ 'Revert' | |
+ '</a>'; | |
}else if(DB.isRemoteDraftQueuedForDeletion(msg.id)){ | |
del_link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.cancelDeleteRemoteDraftMessageRequest(' + msg.id + ');">' | |
+ 'Cancel Delete' | |
+ '</a>'; | |
}else{ | |
del_link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.deleteRemoteDraftMessage(' + msg.id + ');">' | |
+ 'Delete' | |
+ '</a>'; | |
edit_link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.showEditFormForRemoteDraft(' + msg.id + ');">' | |
+ 'Edit' | |
+ '</a>'; | |
if (msg.recepient_name || msg.recepient_id){ | |
send_link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.sendRemoteDraftMessage(' + msg.id + ');">' | |
+ 'Send' | |
+ '</a>'; | |
} | |
} | |
msgOutput += '<tr>' | |
+ '<td>' + msg.id + '</td>' | |
+ '<td>' + msg.title + '</td>' | |
+ '<td>' + msg.recepient_name + '</td>' | |
+ '<td>' + del_link + '</td>' | |
+ '<td>' + edit_link + '</td>' | |
+ '<td>' + send_link + '</td>' | |
+ '</tr>' | |
+ '<tr id="storedMsg_' + msg.id + '_Content">' | |
+ '<td></td>' | |
+ '<td colspan="2">' + msg.content + '</td>' | |
+ '</tr>'; | |
}//End of for{} for remote drafts | |
msgOutput += '</table>'; | |
} | |
outputElem.innerHTML = msgOutput; | |
}, | |
_printSentMessages: function(){ | |
var outputElem = dojo.byId("sentMessagesData"); | |
var msgOutput = ""; | |
if(!this._sentMessages.length){ | |
msgOutput = '<p>No sent messages available</p>'; | |
}else{ | |
msgOutput += '<table border="0" cellspacing="5" cellpadding="5">' | |
+ '<tr><th>ID</th><th>Message</th><th>Recepient</th><th></th></tr>'; | |
for (m in this._sentMessages){ | |
var link = ''; | |
queued = dojox.sql("SELECT id FROM queued_actions WHERE data = ? AND action = ?", | |
this._sentMessages[m].id, "deleteSentMessageFromSentbox").length; | |
if ( queued ){ | |
link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.cancelRemoveSentMessageFromSentboxRequest(' + this._sentMessages[m].id + ');">' | |
+ 'Cancel delete' | |
+ '</a>'; | |
}else{ | |
link = '<a class="directActionLink" ' | |
+ 'onclick="Iris.removeSentMessageFromSentbox(' + this._sentMessages[m].id + ');">' | |
+ 'Delete' | |
+ '</a>'; | |
} | |
msgOutput += '<tr>' | |
+ '<td>' + this._sentMessages[m].id + '</td>' | |
+ '<td>' + this._sentMessages[m].title + '</td>' | |
+ '<td>' + this._sentMessages[m].recepient_name + '</td>' | |
+ '<td>' + link + '</td>' | |
+ '</tr>' | |
+ '<tr id="sentMsg_' + this._sentMessages[m].id + '_Content">' | |
+ '<td></td>' | |
+ '<td colspan="2">' + this._sentMessages[m].content + '</td>' | |
+ '</tr>'; | |
} | |
msgOutput += '</table>'; | |
} | |
outputElem.innerHTML = msgOutput; | |
return true; | |
}, | |
/***** End of GUI *****/ | |
/***** Front-end actions ******/ | |
// => Remove from inbox | |
removeReceivedMessageFromInbox: function(msgID){ | |
if(dojox.off.isOnline){ | |
self = this; | |
dojo.xhrPut({ | |
url: "/messages/" + msgID + ".js", // MessagesController#update | |
headers: { "X-Requested-With": "XMLHttpRequest" }, | |
content: { 'message[in_inbox]': false}, | |
handleAs: "text", | |
timeout: 5000, | |
load: function(response, ioArgs){ | |
serverResponse = eval('(' + response + ')'); | |
switch (serverResponse.status){ | |
case 'success': | |
DB.deleteReceivedMessage(msgID); | |
self._refreshReceivedMessages(); | |
break; | |
case 'error': | |
DB.addActionToQueue('deleteReceivedMessageFromInbox',msgID); | |
self._refreshReceivedMessages(); | |
alert("Some error occured!\n Action queued."); | |
break; | |
case 'unauthorized': | |
alert('Unauthorized. Please login first.'); | |
break; | |
default: | |
alert('Boom-boom!'); | |
} | |
return response; | |
}, | |
error: function(response, ioArgs){ | |
DB.addActionToQueue('deleteReceivedMessageFromInbox', msgID); | |
self._refreshReceivedMessages(); | |
console.error("HTTP status code: ", ioArgs.xhr.status); | |
alert("Some error occured!\n Action queued."); | |
return response; | |
} | |
}); | |
}else{ | |
DB.addActionToQueue('deleteReceivedMessageFromInbox', msgID); | |
this._refreshReceivedMessages(); | |
} | |
}, | |
cancelRemoveReceivedMessageFromInboxRequest: function(msgID){ | |
DB.deleteQueuedActionByTypeAndData('deleteReceivedMessageFromInbox', msgID); | |
this._refreshReceivedMessages(); | |
}, | |
// => Remove from sentbox | |
removeSentMessageFromSentbox: function(msgID){ | |
if(dojox.off.isOnline){ | |
self = this; | |
dojo.xhrPut({ | |
url: "/messages/" + msgID + ".js", // MessagesController#update | |
headers: { "X-Requested-With": "XMLHttpRequest" }, | |
content: { 'message[in_sentbox]': false}, | |
handleAs: "text", | |
timeout: 5000, | |
load: function(response, ioArgs){ | |
serverResponse = eval('(' + response + ')'); | |
switch (serverResponse.status){ | |
case 'success': | |
DB.deleteSentMessage(msgID); | |
self._refreshSentMessages(); | |
break; | |
case 'error': | |
DB.addActionToQueue('deleteSentMessageFromSentbox', msgID); | |
self._refreshSentMessages(); | |
alert("Some error occured!\n Action queued."); | |
break; | |
case 'unauthorized': | |
alert('Unauthorized. Please login first.'); | |
break; | |
default: | |
alert('Boom-boom!'); | |
} | |
return response; | |
}, | |
error: function(response, ioArgs){ | |
DB.addActionToQueue('deleteSentMessageFromSentbox', msgID); | |
self._refreshSentMessages(); | |
console.error("HTTP status code: ", ioArgs.xhr.status); | |
alert("Some error occured!\n Action queued."); | |
return response; | |
} | |
}); | |
}else{ | |
DB.addActionToQueue('deleteSentMessageFromSentbox', msgID); | |
this._refreshSentMessages(); | |
} | |
}, | |
cancelRemoveSentMessageFromSentboxRequest: function(msgID){ | |
DB.deleteQueuedActionByTypeAndData('deleteSentMessageFromSentbox',msgID); | |
this._refreshSentMessages(); | |
}, | |
// => Send remote draft message | |
sendRemoteDraftMessage: function(msgID){ | |
if(dojox.off.isOnline){ | |
self = this; | |
msg = DB.getRemoteDraftMessage(msgID); | |
dojo.xhrPost({ | |
url: "/messages/" + msgID + "/send_it.js", | |
headers: { "X-Requested-With": "XMLHttpRequest" }, | |
content: { | |
'id': msgID, | |
'message[lock_version]': msg.lock_version }, | |
handleAs: "text", | |
timeout: 5000, | |
load: function(response, ioArgs){ | |
serverResponse = eval('(' + response + ')'); | |
switch (serverResponse.status){ | |
case 'success': | |
DB.deleteRemoteDraftMessage(msgID); | |
DB.createSentMessage(serverResponse.data.message); | |
self._refreshDraftMessages(); | |
self._refreshSentMessages(); | |
break; | |
case 'conflict': | |
DB.updateRemoteDraftMessage(serverResponse.data.message); | |
self._refreshDraftMessages(); | |
alert('The message with title: ' + msg.title + ' was not sent because a newer version existed on server.\n' | |
+ 'Local copy was updated.'); | |
break; | |
case 'error': | |
DB.addActionToQueue('sendRemoteDraft',msgID); | |
self._refreshDraftMessages(); | |
alert("Some error occured!\n Action queued."); | |
break; | |
case 'unauthorized': | |
alert('Unauthorized. Please login first.'); | |
break; | |
default: | |
alert('Boom-boom!'); | |
} | |
return response; | |
}, | |
error: function(response, ioArgs){ | |
DB.addActionToQueue('sendRemoteDraft', msgID); | |
self._refreshDraftMessages(); | |
console.error("HTTP status code: ", ioArgs.xhr.status); | |
alert("Some error occured!\n Action queued."); | |
return response; | |
} | |
}); | |
}else{ | |
DB.addActionToQueue('sendRemoteDraft', msgID); | |
this._refreshDraftMessages(); | |
} | |
}, | |
cancelSendRemoteDraftMessageRequest: function(msgID){ | |
DB.deleteQueuedActionByTypeAndData('sendRemoteDraft', msgID); | |
this._refreshDraftMessages(); | |
}, | |
// => Delete draft | |
deleteLocalDraftMessage: function(msgID){ | |
DB.deleteQueuedActionByTypeAndData('createNewDraft', msgID); | |
DB.deleteLocalDraftMessage(msgID); | |
this._refreshDraftMessages(); | |
}, | |
deleteRemoteDraftMessage: function(msgID){ | |
if (dojox.off.isOnline){ | |
self = this; | |
dojo.xhrDelete({ | |
url: "/messages/" + msgID + ".js", // MessagesController#destroy | |
headers: { "X-Requested-With": "XMLHttpRequest" }, | |
handleAs: "text", | |
timeout: 5000, | |
load: function(response, ioArgs){ | |
serverResponse = eval('(' + response + ')'); | |
switch (serverResponse.status){ | |
case 'success': | |
DB.deleteRemoteDraftMessage(msgID); | |
msg = serverResponse.data.message; | |
if (msg.id){ // Server found it as a sent message (so it was draft and became sent before we delete it localy) | |
DB.createSentMessage(msg); | |
self._refreshSentMessages(); | |
} | |
self._refreshDraftMessages(); | |
break; | |
case 'unauthorized': | |
alert('Unauthorized. Please login first.'); | |
break; | |
default: | |
alert('Boom-boom!'); | |
} | |
return response; | |
}, | |
error: function(response, ioArgs){ | |
DB.addActionToQueue('deleteRemoteDraft', msgID); | |
self._refreshDraftMessages(); | |
console.error("HTTP status code: ", ioArgs.xhr.status); | |
alert("Some error occured!\n Action queued."); | |
return response; | |
} | |
}); | |
}else{ | |
DB.addActionToQueue('deleteRemoteDraft', msgID); | |
this._refreshDraftMessages(); | |
} | |
}, | |
cancelDeleteRemoteDraftMessageRequest: function(msgID){ | |
DB.deleteQueuedActionByTypeAndData('deleteRemoteDraft', msgID); | |
this._refreshDraftMessages(); | |
}, | |
// => Edit local or remote draft(commons) | |
closeEditFormForDraft: function(draftType){ | |
clearInputForm(draftType); | |
showBox(DRAFTS_ID); | |
}, | |
// => Edit local draft | |
showEditFormForLocalDraft: function(msgID){ | |
var msg = DB.getLocalDraftMessage(msgID); | |
showEditFormForDraft('local', msg); | |
}, | |
doEditLocalDraftMessage: function(){ | |
input = getFormContents('localDraft'); | |
DB.updateLocalDraftMessage(input.id, input); | |
clearInputForm('localDraft'); | |
if(dojox.off.isOnline && DB.isLocalDraftQueuedForCreation(input.id)){ | |
showBox(DRAFTS_ID); | |
dojox.off.sync.start(); | |
}else{ | |
this._refreshDraftMessages(); | |
showBox(DRAFTS_ID); | |
} | |
}, | |
// => Edit remote draft | |
showEditFormForRemoteDraft: function(msgID){ | |
msg = DB.getRemoteDraftMessage(msgID); | |
showEditFormForDraft('remote', msg); | |
}, | |
doEditRemoteDraftMessage: function(){ | |
input = getFormContents('remoteDraft'); | |
self = this; | |
if(dojox.off.isOnline){ | |
dojo.xhrPut({ | |
url: "/messages/" + input.id + ".js", // MessagesController#update | |
headers: { "X-Requested-With": "XMLHttpRequest" }, | |
content: { | |
'message[title]': input.title, | |
'message[content]': input.content, | |
'message[lock_version]': input.lock_version, | |
'recepient_name': input.recepient_name | |
}, | |
handleAs: "text", | |
timeout: 5000, | |
load: function(response, ioArgs){ | |
serverResponse = eval('(' + response + ')'); | |
switch (serverResponse.status){ | |
case 'success': | |
msg = serverResponse.data.message; | |
DB.updateRemoteDraftMessage(msg); | |
clearInputForm('remoteDraft'); | |
self._refreshDraftMessages(); | |
showBox(DRAFTS_ID); | |
break; | |
case 'unauthorized': | |
alert('Unauthorized. Please login first.'); | |
break; | |
case 'error': | |
if (serverResponse.data.message == 'Recepient not found'){ | |
alert('Invalid recepient.\nPlease specify a correct one and try again.'); | |
}else{ | |
var error_msg = Inflections[serverResponse.data.message] || serverResponse.data.message; | |
if (details = serverResponse.data.errors){ | |
error_msg += ": " + details; | |
} | |
alert('Error:\n' + error_msg); | |
} | |
break; | |
default: | |
alert('Boom-boom!'); | |
break; | |
} | |
return response; | |
}, | |
error: function(response, ioArgs){ | |
console.error("HTTP status code: ", ioArgs.xhr.status); | |
alert("Some error occured!\n Please try again in a moment."); | |
return response; | |
} | |
}); | |
}else{ | |
DB.createOldRemoteDraftMessage(DB.getRemoteDraftMessage(input.id)); | |
DB.updateRemoteDraftMessage(input); | |
DB.addActionToQueue('editRemoteDraft', input.id); | |
this._refreshDraftMessages(); | |
showBox(DRAFTS_ID); | |
} | |
}, | |
cancelEditRemoteDraftMessage: function(msgID){ | |
DB.deleteQueuedActionByTypeAndData("editRemoteDraft", msgID); | |
var old = DB.getOldRemoteDraftMessage(msgID); | |
DB.updateRemoteDraftMessage(old); | |
DB.deleteOldRemoteDraftMessage(old.id); | |
this._refreshDraftMessages(); | |
}, | |
// => Handle new messages | |
storeNewMessage: function(){ | |
input = getFormContents('new'); | |
self = this; | |
if(dojox.off.isOnline){ | |
dojo.xhrPost({ | |
url: "/messages.js", | |
headers: { "X-Requested-With": "XMLHttpRequest" }, | |
content: { | |
'message[title]': input.title, | |
'message[content]': input.content, | |
'recepient_name': input.recepient_name, | |
}, | |
handleAs: "text", | |
timeout: 5000, | |
load: function(response, ioArgs){ | |
serverResponse = eval('(' + response + ')'); | |
switch (serverResponse.status){ | |
case 'success': | |
msg = serverResponse.data.message; | |
dojox.sql("INSERT INTO remote_draft_messages (id, title, content, recepient_name, recepient_id, lock_version) " | |
+ "VALUES (?,?,?,?,?,?)", msg.id, | |
msg.title, | |
msg.content, | |
msg.recepient_name, | |
msg.recepient_id, | |
msg.lock_version); | |
self._refreshDraftMessages(); | |
clearInputForm('new'); | |
showBox(DRAFTS_ID); | |
return response; | |
break; | |
case 'unauthorized': | |
console.debug('Unauthorized. Please login first.'); | |
return response; | |
break; | |
case 'error': | |
if (serverResponse.data.message == 'Recepient not found'){ | |
alert('Invalid recepient.\nPlease specify a correct one and try again.'); | |
}else{ | |
alert('Server returned error:\n' + serverResponse.data.message); | |
} | |
return response; | |
break; | |
default: | |
console.debug('Boom-boom!'); | |
return response; | |
break; | |
} | |
}, | |
error: function(response, ioArgs){ | |
console.error("HTTP status code: ", ioArgs.xhr.status); | |
return response; | |
} | |
}); | |
}else{ | |
dojox.sql("INSERT INTO local_draft_messages (id, title, content, recepient_name) " | |
+ "VALUES (?,?,?,?)", this._nextID, | |
input.title, | |
input.content, | |
input.recepient_name); | |
DB.addActionToQueue('createNewDraft', this._nextID); | |
this._nextID = this._nextID + 1; | |
this._refreshDraftMessages(); | |
clearInputForm('new'); | |
showBox(DRAFTS_ID); | |
} | |
}, | |
sendNewMessage: function(){ | |
input = getFormContents('new'); | |
self = this; | |
valid = true; | |
if (!input.title){ | |
alert('Title is not given'); | |
valid = false; | |
} | |
if (!input.recepient_name){ | |
alert('Recepient is not given'); | |
valid = false; | |
} | |
if (valid){ | |
if(dojox.off.isOnline){ | |
dojo.xhrPost({ | |
url: "/messages.js", | |
headers: { "X-Requested-With": "XMLHttpRequest" }, | |
content: { | |
'message[title]': input.title, | |
'message[content]': input.content, | |
'recepient_name': input.recepient_name, | |
'sent': 'true' | |
}, | |
handleAs: "text", | |
timeout: 5000, | |
load: function(response, ioArgs){ | |
serverResponse = eval('(' + response + ')'); | |
switch (serverResponse.status){ | |
case 'success': | |
msg = serverResponse.data.message; | |
dojox.sql("INSERT INTO sent_messages (id, title, content, recepient_name, recepient_id) " | |
+ "VALUES (?,?,?,?,?)", msg.id, | |
msg.title, | |
msg.content, | |
msg.recepient_name, | |
msg.recepient_id); | |
self._refreshDraftMessages(); | |
self._refreshSentMessages(); | |
clearInputForm('new'); | |
showBox(SENTBOX_ID); | |
return response; | |
break; | |
case 'unauthorized': | |
alert('Unauthorized. Please login first.'); | |
return response; | |
break; | |
case 'error': | |
if (serverResponse.data.message == 'Recepient not found'){ | |
alert('Invalid recepient.\nPlease specify a correct one and try again.'); | |
}else{ | |
alert('Server returned error:\n' + serverResponse.data.message); | |
} | |
return response; | |
break; | |
default: | |
console.debug('Boom-boom!'); | |
return response; | |
break; | |
} | |
}, | |
error: function(response, ioArgs){ | |
console.error("HTTP status code: ", ioArgs.xhr.status); | |
alert("Some error occured!\n Message is going to be stored as draft."); | |
this.storeNewMessage(); | |
return response; | |
} | |
}); | |
}else{ | |
alert("Not online!\n Message is going to be stored as draft."); | |
this.storeNewMessage(); | |
} | |
} | |
} | |
} | |
dojo.connect(dojox.off.ui, "onLoad", Iris, Iris.initialize); | |
dojox.off.initialize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment