2010-03-29 15:41:45 +00:00
|
|
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
|
|
|
|
|
|
|
var customForm = {
|
2010-03-30 01:03:57 +00:00
|
|
|
|
2010-03-29 15:41:45 +00:00
|
|
|
onLoad: function() {
|
2010-03-30 01:03:57 +00:00
|
|
|
|
|
|
|
// Create references to form elements.
|
|
|
|
// NB must be done after document has loaded, else the elements aren't present
|
|
|
|
// in the DOM yet.
|
|
|
|
this.form = $('#content form'),
|
|
|
|
this.button = $('#submit'),
|
|
|
|
this.addNewLink = $('#addNewLink'),
|
|
|
|
this.existing = $('#existing'),
|
|
|
|
this.addNew = $('#new'),
|
|
|
|
this.entry = $('#entry'),
|
2010-03-30 18:16:40 +00:00
|
|
|
this.cancel = this.form.find('.cancel'),
|
2010-03-30 01:03:57 +00:00
|
|
|
this.requiredLegend = $('#requiredLegend'),
|
2010-03-30 18:16:40 +00:00
|
|
|
this.requiredHints = this.form.find('.requiredHint'),
|
2010-03-29 15:41:45 +00:00
|
|
|
|
2010-03-30 01:03:57 +00:00
|
|
|
// Read values used to control display
|
|
|
|
this.editType = $("input[name='editType']").val(),
|
|
|
|
this.entryType = $("input[name='entryType']").val().capitalize(),
|
|
|
|
this.newType = $("input[name='newType']").val().capitalize();
|
2010-03-29 15:41:45 +00:00
|
|
|
|
2010-03-30 01:03:57 +00:00
|
|
|
if (this.editType == 'add') { //adding a new entry
|
2010-03-30 19:33:54 +00:00
|
|
|
this.initAddForm();
|
|
|
|
|
2010-03-29 15:41:45 +00:00
|
|
|
} else { // editing existing entry
|
2010-03-30 01:03:57 +00:00
|
|
|
this.initEditForm();
|
2010-03-30 19:33:54 +00:00
|
|
|
}
|
2010-03-30 01:03:57 +00:00
|
|
|
},
|
|
|
|
|
2010-03-30 19:33:54 +00:00
|
|
|
// Reset add form to initial state (step 1)
|
|
|
|
// Should only be needed after returning to step 1 from step 2,
|
|
|
|
// but sometimes seems to be needed on page load as well, so call it from initAddForm()
|
|
|
|
resetAddForm: function() {
|
2010-03-30 01:03:57 +00:00
|
|
|
// Clear all form data and error messages
|
|
|
|
$('input:text').val('');
|
|
|
|
$('.error').text('');
|
2010-03-30 14:13:23 +00:00
|
|
|
|
2010-03-30 01:03:57 +00:00
|
|
|
// Remove previously bound event handlers
|
2010-03-30 14:13:23 +00:00
|
|
|
this.cancel.unbind('click');
|
2010-03-30 19:33:54 +00:00
|
|
|
this.button.unbind('click');
|
2010-03-30 14:13:23 +00:00
|
|
|
|
2010-03-30 19:33:54 +00:00
|
|
|
this.toggleRequiredHints('remove', this.addNew, this.existing);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Set up add form on page load, or when returning to initial state
|
|
|
|
// (The latter is not yet implemented, but we are preparing for it. Note
|
|
|
|
// that initializations to occur ONLY on page load are done in the onLoad() method.)
|
|
|
|
// RY *** SOME of this will be shared with the edit form - figure out which
|
|
|
|
initAddForm: function() {
|
|
|
|
|
|
|
|
this.resetAddForm();
|
2010-03-29 15:41:45 +00:00
|
|
|
|
2010-03-30 01:03:57 +00:00
|
|
|
// Step 1 of the form
|
|
|
|
this.addNewLink.show();
|
|
|
|
this.existing.show();
|
|
|
|
this.addNew.hide();
|
|
|
|
this.entry.hide();
|
|
|
|
this.requiredLegend.hide();
|
|
|
|
this.button.val('Continue');
|
2010-03-29 15:41:45 +00:00
|
|
|
|
2010-03-30 01:03:57 +00:00
|
|
|
// Assign event listeners
|
|
|
|
// add new link => step 2b
|
|
|
|
this.addNewLink.bind('click', function() {
|
|
|
|
$(this).hide();
|
|
|
|
customForm.existing.hide();
|
|
|
|
customForm.showFields(customForm.addNew);
|
|
|
|
customForm.entry.show();
|
|
|
|
customForm.requiredLegend.show();
|
2010-03-30 18:16:40 +00:00
|
|
|
|
2010-03-30 01:03:57 +00:00
|
|
|
customForm.button.val('Create ' + customForm.entryType + ' & ' + customForm.newType);
|
|
|
|
customForm.button.unbind('click');
|
|
|
|
|
2010-03-30 18:16:40 +00:00
|
|
|
customForm.doCancel();
|
2010-03-30 01:03:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// button => step 2a
|
|
|
|
this.button.bind('click', function() {
|
|
|
|
customForm.entry.show();
|
|
|
|
customForm.showFields(customForm.existing);
|
|
|
|
customForm.addNewLink.hide();
|
|
|
|
customForm.requiredLegend.show();
|
2010-03-30 18:16:40 +00:00
|
|
|
|
2010-03-30 01:03:57 +00:00
|
|
|
$(this).val('Create ' + customForm.entryType);
|
|
|
|
$(this).unbind('click');
|
|
|
|
|
2010-03-30 18:16:40 +00:00
|
|
|
customForm.doCancel();
|
2010-03-30 01:03:57 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
initEditForm: function() {
|
2010-03-30 19:33:54 +00:00
|
|
|
|
|
|
|
this.showFields(this.existing);
|
|
|
|
this.addNewLink.show();
|
|
|
|
this.addNew.hide();
|
|
|
|
this.showFields(this.entry);
|
|
|
|
this.requiredLegend.show();
|
|
|
|
this.button.val('Save Changes');
|
|
|
|
|
|
|
|
this.addNewLink.bind('click', function() {
|
|
|
|
$(this).hide();
|
|
|
|
customForm.existing.hide();
|
|
|
|
customForm.showFields(customForm.addNew);
|
|
|
|
|
|
|
|
customForm.button.val('Create ' + customForm.newType + ' & Save Changes');
|
|
|
|
});
|
|
|
|
|
2010-03-30 01:03:57 +00:00
|
|
|
},
|
2010-03-30 18:16:40 +00:00
|
|
|
|
|
|
|
// Add required hints to required fields in element array elArray.
|
2010-03-30 01:03:57 +00:00
|
|
|
// Use when the non-Javascript version should not show the required hint,
|
2010-03-30 18:16:40 +00:00
|
|
|
// because the field is not required in that version.
|
|
|
|
// Arguments: action = 'add' or 'remove'
|
|
|
|
// Varargs: element(s)
|
|
|
|
toggleRequiredHints: function(action /* elements */) {
|
|
|
|
var labelText,
|
|
|
|
newLabelText,
|
|
|
|
requiredHintText = '<span class="requiredHint"> *</span>';
|
2010-03-30 01:03:57 +00:00
|
|
|
|
2010-03-30 18:16:40 +00:00
|
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
|
|
arguments[i].find('label.required').each(function() {
|
|
|
|
labelText = $(this).html();
|
|
|
|
newLabelText = action == 'add' ? labelText + requiredHintText :
|
|
|
|
labelText.replace(requiredHintText, '');
|
|
|
|
$(this).html(newLabelText);
|
|
|
|
});
|
|
|
|
}
|
2010-03-30 01:03:57 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
showFields: function(el) {
|
|
|
|
el.show();
|
2010-03-30 18:16:40 +00:00
|
|
|
this.toggleRequiredHints('add', el);
|
2010-03-30 01:03:57 +00:00
|
|
|
},
|
2010-03-30 18:16:40 +00:00
|
|
|
|
|
|
|
// Add event listener to the cancel link in step 2
|
|
|
|
doCancel: function() {
|
|
|
|
this.cancel.unbind('click');
|
|
|
|
this.cancel.bind('click', function() {
|
|
|
|
customForm.initAddForm();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
2010-03-30 01:03:57 +00:00
|
|
|
|
2010-03-29 15:41:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$(document).ready(function(){
|
|
|
|
customForm.onLoad();
|
|
|
|
});
|