Created
March 25, 2012 06:28
-
-
Save pamelafox/2191822 to your computer and use it in GitHub Desktop.
MobileLogPage
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
MobileLogPage.prototype = MobilePage.prototype; | |
MobileLogPage.prototype.getForm = function() { | |
return ED.shared.logForms[this.id]; | |
}; | |
MobileLogPage.prototype.getErrorDOM = function() { | |
return $('#mobile-' + this.id + '-page .log-error'); | |
}; | |
MobileLogPage.prototype.getErrorMessage = function() { | |
return $('#mobile-' + this.id + '-page .log-error-message'); | |
}; | |
MobileLogPage.prototype.getSaveStatus = function() { | |
return $('#mobile-' + this.id + '-link .save-status'); | |
}; | |
MobileLogPage.prototype.getCacheKey = function() { | |
var date = ED.shared.getFormDate(CURRENT_DATE); | |
return LS_LOG_PAGE + date + this.id; | |
}; | |
// Show after error saving or when we realize a form is unsaved | |
MobileLogPage.prototype.showErrorStatus = function(errorText) { | |
this.getSaveStatus().hide(); | |
errorText = errorText || 'There was a problem saving. Please re-save.'; | |
this.getErrorMessage().html(errorText); | |
this.getErrorDOM().show(); | |
updateLinkIcons(); | |
}; | |
MobileLogPage.prototype.setupSave = function(formTarget, onSuccess, onCancel) { | |
var self = this; | |
var $formToSave = $('#mobile-log-' + this.id + '-form'); | |
var formActions = $formToSave.find('.log-buttons'); | |
var saveButton = formActions.find('.save-button'); | |
var cancelButton = formActions.find('.cancel-button'); | |
function showSuccessStatus(text) { | |
saveButton.html('Save'); | |
self.getSaveStatus().html('(Saved!)').show(); | |
window.setTimeout(function() { | |
self.getSaveStatus().hide(); | |
}, 6000); | |
updateLinkIcons(); | |
} | |
function showSavingStatus(text) { | |
self.isSaving = true; | |
self.getSaveStatus().html('(Saving...)').show(); | |
saveButton.html('<i>Saving...</i>'); | |
ED.util.disableElement(saveButton); | |
self.getErrorDOM().hide(); | |
updateLinkIcons(); | |
} | |
function resetButton() { | |
self.isSaving = false; | |
saveButton.html('Save'); | |
ED.util.enableElement(saveButton); | |
saveButton.removeAttr('disabled'); | |
} | |
function onSaveClick() { | |
showPage(pages.log); | |
showSavingStatus(); | |
self.cacheForm(); | |
self.markAsUnsaved(); | |
var MSG_SAVE_OFFLINE = 'Your phone seems to be offline right now. Please try later.'; | |
runIfOnline( | |
function() { | |
ED.shared.sendData(formTarget, $formToSave.serialize(), function(data) { | |
resetButton(); | |
if (data.status == 'success') { | |
self.markAsSaved(); | |
showSuccessStatus(); | |
if (onSuccess) onSuccess(data); | |
ED.SAVED_LOG = new Date(); | |
if (data.log) { | |
self.log = data.log; | |
onNewLog(data.log); | |
} | |
} else if (data.errors && data.errors == '403') { | |
doLogout(true); | |
} | |
}, function(error) { | |
resetButton(); | |
self.showErrorStatus('Sorry, there was a problem saving. Please try later.'); | |
}); | |
}, | |
MSG_SAVE_OFFLINE, | |
function() { | |
resetButton(); | |
self.showErrorStatus('We couldn\'t save since the phone was offline. Please try later.'); | |
} | |
); | |
} | |
function onCancelClick() { | |
if (onCancel) onCancel(); | |
showPage(pages.log); | |
} | |
ED.util.addTouchOrClickHandler(saveButton, onSaveClick); | |
ED.util.addTouchOrClickHandler(cancelButton, onCancelClick); | |
}; | |
MobileLogPage.prototype.setupPageAndForm = function() { | |
this.setupOnce(); | |
this.getForm().setup(); | |
}; | |
MobileLogPage.prototype.updateForm = function(log) { | |
if (this.hasUnsavedData()) { | |
this.restoreFormFromCache(); | |
this.showErrorStatus(); | |
} else if (log) { | |
this.getForm().setData(log); | |
} | |
}; | |
MobileLogPage.prototype.hasUnsavedData = function() { | |
if (this.isUnsaved || lscache.get(this.getCacheKey() + CLASS_UNSAVED)) { | |
return true; | |
} | |
return false; | |
}; | |
MobileLogPage.prototype.markAsUnsaved = function() { | |
this.isUnsaved = true; | |
lscache.set(this.getCacheKey() + CLASS_UNSAVED, true); | |
}; | |
MobileLogPage.prototype.markAsSaved = function() { | |
this.isUnsaved = false; | |
lscache.remove(this.getCacheKey()); | |
lscache.remove(this.getCacheKey() + CLASS_UNSAVED); | |
}; | |
MobileLogPage.prototype.cacheForm = function() { | |
lscache.set(this.getCacheKey(), this.getForm().getData()); | |
}; | |
MobileLogPage.prototype.restoreFormFromCache = function() { | |
var data = lscache.get(this.getCacheKey()); | |
if (data) { | |
this.getForm().setData(new ED.models.Log(data)); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment