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.
|
2010-03-31 14:39:12 +00:00
|
|
|
// NB must be done after the elements have been loaded.
|
|
|
|
|
2010-03-30 01:03:57 +00:00
|
|
|
this.form = $('#content form'),
|
|
|
|
this.button = $('#submit'),
|
2010-03-31 14:39:12 +00:00
|
|
|
this.requiredLegend = $('#requiredLegend'),
|
|
|
|
|
|
|
|
// These may need to be changed to classes rather than ids, if there are
|
|
|
|
// multiple sets of divs to show/hide during the workflow.
|
2010-03-30 01:03:57 +00:00
|
|
|
this.addNewLink = $('#addNewLink'),
|
|
|
|
this.existing = $('#existing'),
|
|
|
|
this.addNew = $('#new'),
|
|
|
|
this.entry = $('#entry'),
|
2010-03-31 14:39:12 +00:00
|
|
|
this.existingOrNew = $('#existingOrNew'),
|
|
|
|
|
2010-03-30 18:16:40 +00:00
|
|
|
this.cancel = this.form.find('.cancel'),
|
2010-03-31 14:39:12 +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-31 14:39:12 +00:00
|
|
|
|
|
|
|
this.adjustForJs();
|
|
|
|
|
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-31 14:39:12 +00:00
|
|
|
// On page load, make changes to the non-Javascript version for the Javascript version.
|
|
|
|
// These are features that will not change in the Javascript version.
|
|
|
|
adjustForJs: function() {
|
|
|
|
this.existingOrNew.hide();
|
|
|
|
|
|
|
|
var selectExistingLabel = $('#existing label');
|
|
|
|
selectExistingLabel.html(selectExistingLabel.html().replace('Select Existing ', ''));
|
|
|
|
|
|
|
|
},
|
|
|
|
|
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-31 14:39:12 +00:00
|
|
|
|
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 21:42:13 +00:00
|
|
|
this.addNewLink.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
|
|
|
|
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();
|
2010-03-31 14:39:12 +00:00
|
|
|
this.entry.hide();
|
2010-03-30 01:03:57 +00:00
|
|
|
this.requiredLegend.hide();
|
|
|
|
this.button.val('Continue');
|
2010-03-29 15:41:45 +00:00
|
|
|
|
2010-03-31 14:39:12 +00:00
|
|
|
// Assign event listeners
|
|
|
|
|
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;
|
|
|
|
});
|
2010-03-31 14:39:12 +00:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
customForm.button.val('Create ' + customForm.entryType + ' & ' + customForm.newType);
|
|
|
|
customForm.button.unbind('click');
|
|
|
|
|
|
|
|
customForm.doCancel();
|
|
|
|
});
|
2010-03-30 01:03:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
initEditForm: function() {
|
2010-03-30 19:33:54 +00:00
|
|
|
|
|
|
|
this.showFields(this.existing);
|
|
|
|
this.addNewLink.show();
|
|
|
|
this.addNew.hide();
|
2010-03-30 21:42:13 +00:00
|
|
|
this.entry.show();
|
2010-03-30 19:33:54 +00:00
|
|
|
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
|
|
|
|
2010-03-31 14:39:12 +00:00
|
|
|
// Add required hints to required fields in a list of elements.
|
2010-03-30 01:03:57 +00:00
|
|
|
// Use when the non-Javascript version should not show the required hint,
|
2010-03-31 14:39:12 +00:00
|
|
|
// because the field is not required in that version (e.g., it's one of two
|
|
|
|
// fields, where one of the two must be filled in but neither one is required).
|
2010-03-30 18:16:40 +00:00
|
|
|
// Arguments: action = 'add' or 'remove'
|
|
|
|
// Varargs: element(s)
|
|
|
|
toggleRequiredHints: function(action /* elements */) {
|
2010-03-31 14:39:12 +00:00
|
|
|
|
2010-03-30 18:16:40 +00:00
|
|
|
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();
|
|
|
|
});
|