/** * Resets all the form controls in the given container. * Depending on the provided mode value, this will either 'reset' their values to those shown when the * page was first displayed, or 'clear' the values completely. * @param container {Group_Name} The HTML container whose form controls should be processed. * @param mode {string} (optional) Indicates which type of reset should apply. * Either 'reset' (default) or 'clear'. * 'reset' will put the previous values from when the page was loaded including defaults. * 'clear' will remove all the values. * * @author Hyfinity Limited */ hyf.util.resetContainer = function(container, mode) { if (typeof(container) == 'string') container = document.getElementById(container); if ((container == null) || (typeof(container) == 'undefined')) return; if ((typeof(mode) == 'undefined') || ((mode != 'reset') && (mode != 'clear'))) mode = 'reset'; var controls = new Array(); var inputs = container.getElementsByTagName('input'); for (var i = 0; i < inputs.length; ++i) controls[controls.length] = inputs[i]; inputs = container.getElementsByTagName('select'); for (var i = 0; i < inputs.length; ++i) controls[controls.length] = inputs[i]; inputs = container.getElementsByTagName('textarea'); for (var i = 0; i < inputs.length; ++i) controls[controls.length] = inputs[i]; //for radio and multicheck type controls, we will have multiple entries in the //controls array so store the values to set in this object to process at the end. //ach entry in this object will map control name to an array of values var objMultiPartControls = new Object(); for (var i = 0; i < controls.length; ++i) { //look at the type of input if (mode == 'clear') { switch (controls[i].type) { case 'image' : break; case 'button' : break; case 'reset' : break; case 'submit' : break; case 'file' : hyf.util.setFieldValue(controls[i].id, ""); break; case 'hidden' : break; case 'select-one': //want select one control to default to the first value? hyf.util.setFieldValue(controls[i].id, controls[i].options[0].value); break; case 'select-multiple': hyf.util.setFieldValue(controls[i].id, null); break; case 'radio' : case 'checkbox' : objMultiPartControls[controls[i].name] = [""]; break; default : hyf.util.setFieldValue(controls[i].id, ""); } } else { switch (controls[i].type) { case 'image' : break; case 'button' : break; case 'reset' : break; case 'submit' : break; case 'file' : hyf.util.setFieldValue(controls[i].id, ""); break; case 'hidden' : break; case 'select-one': var selected = controls[i].options[0].value; //reset to first entry if no other one was selected for (var op = 0; op < controls[i].options.length; ++op) { if (controls[i].options[op].defaultSelected) selected = controls[i].options[op].value; } hyf.util.setFieldValue(controls[i].id, selected); break; case 'select-multiple': //clear out all current selections hyf.util.setFieldValue(controls[i].id, null); //select any entries that were default selected for (var op = 0; op < controls[i].options.length; ++op) { if (controls[i].options[op].defaultSelected) hyf.util.setFieldValue(controls[i].id, controls[i].options[op].value); } break; case 'radio' : case 'checkbox' : if (!objMultiPartControls[controls[i].name]) objMultiPartControls[controls[i].name] = []; if (controls[i].defaultChecked) objMultiPartControls[controls[i].name].push(controls[i].value); break; default : hyf.util.setFieldValue(controls[i].id, controls[i].defaultValue); } } } //now handle any multiple input control (eg radio or multi check) for (var entry in objMultiPartControls) { //clear out all existing values hyf.util.setFieldValue(entry, null); //if resetting, then set the requiored values if (mode == 'reset') { for (var i = 0; i < objMultiPartControls[entry].length; ++i) { hyf.util.setFieldValue(entry, objMultiPartControls[entry][i]); } } } } /** * Attempts to set the current value for the given field. * This will attempt to determine the type of the given field name, * by checking dojo widigets, the form elements collection, and * display only containers with the given id. * If the field supports multiple selections, then the provided value will be * added in addition to any others. Use a value of null to clear all values for the control. * @param fieldName {Field_Name} The name of the field to set the value for. * @param fieldValue {Field_Value | string} The value to set the field with. * * @author Hyfinity Limited */ hyf.util.setFieldValue = function(fieldName, fieldValue) { //first check for a dojo widget if ((typeof(dijit) != 'undefined') && (typeof(dijit.byId) == 'function') && (dijit.byId(fieldName) != null)) { return dijit.byId(fieldName).set('value', fieldValue); } //check for form controls var nameMatches = dojo.query('*[name=' + fieldName + ']'); if (nameMatches.length > 0) { var controlFound = true; var valueToSet = hyf.util.processDateConstantsForField(fieldValue, nameMatches[0]); if (nameMatches.length == 1) { var entry = nameMatches[0]; if ((typeof(entry.type) == 'undefined') || (entry.type == '')) { //not actually a field control controlFound = false; } else { if (entry.type == 'select-multiple') { //for multi select we need to find the matching option to select for (var i = 0; i < entry.options.length; ++i) { if (entry.options[i].value == valueToSet) entry.options[i].selected = true; else if (valueToSet == null) entry.options[i].selected = false; } } else if (entry.type == 'checkbox') { //for single checkbox, we need to adjust the checked proeprty if (entry.value == valueToSet) entry.checked = true; else entry.checked = false; } else { if (entry.getAttribute('_type') == 'date') { //convert provided value to the correct display format if (entry.getAttribute('_display_date_format') && entry.getAttribute('_data_date_format') && (entry.getAttribute('_display_date_format') != entry.getAttribute('_data_date_format'))) { valueToSet = convertDate(valueToSet, entry.getAttribute('_data_date_format'), entry.getAttribute('_display_date_format')); } } entry.value = valueToSet; //check for any masking requirements hyf.validation.applyMask(entry); //if this is a rich text editor, then make sure the editor gets notified of the changes if ((entry.type == 'textarea') && hyf.richtext && hyf.richtext.isRichText(entry)) hyf.richtext.refreshEditorValue(entry); } hyf.fireEvent(entry, 'change'); } } else //is an array of controls, eg radio or multi check. { for (var i = 0; i < nameMatches.length; ++i) { var initialState = nameMatches[i].checked; if (nameMatches[i].value == valueToSet) { nameMatches[i].checked = true; if (initialState == false) { hyf.fireEvent(nameMatches[i], 'click'); hyf.fireEvent(nameMatches[i], 'change'); } } else if (valueToSet == null) { nameMatches[i].checked = false; if (initialState == true) { hyf.fireEvent(nameMatches[i], 'click'); hyf.fireEvent(nameMatches[i], 'change'); } } } } if (controlFound) return; } //finally check for an id if (document.getElementById(fieldName) != null) { //set the text contents of the container. var cont = document.getElementById(fieldName); var valueToSet = hyf.util.processDateConstantsForField(fieldValue, cont); //if the container has existing text node children then we just update the first one var textUpdated = false; var child = cont.firstChild; while (child != null) { if (child.nodeType == 3) //text node { if (!textUpdated) { child.nodeValue = valueToSet; textUpdated = true; child = child.nextSibling; } else { var oldchild = child; child = child.nextSibling; cont.removeChild(oldchild); } } else { child = child.nextSibling; } } //otherwise we set the contents of the element, removing any existing content if (!textUpdated) { if (typeof(cont.innerText) != 'undefined') cont.innerText = valueToSet; else if (typeof(cont.textContent) != 'undefined') cont.textContent = valueToSet; } //check for any masking requirements hyf.validation.applyMask(cont); } //final check in case this was a split date control if (document.getElementById(fieldName + '_container') != null) { var f = dojo.query('*[_originalFieldName=' + fieldName + ']', document.getElementById(fieldName + '_container'))[0]; if (f) { var time = getDateFromFormat(fieldValue, f.getAttribute('_data_date_format')); hyf.calendar.setSplitDateControlValue(fieldName, new Date(time)); } } }