PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/www/extras/yui-webgui/build/form/form.js

http://github.com/plainblack/webgui
JavaScript | 242 lines | 156 code | 36 blank | 50 comment | 55 complexity | dd2043295ffe1eafb126225d846f0ce5 MD5 | raw file
  1. // Initialize namespace
  2. if (typeof WebGUI == "undefined") {
  3. var WebGUI = {};
  4. }
  5. if (typeof WebGUI.Form == "undefined") {
  6. WebGUI.Form = {};
  7. }
  8. /**
  9. * This object contains generic form modification functions
  10. */
  11. /***********************************************************************************
  12. * @description This method assembles the form label and value pairs and
  13. * constructs an encoded string.
  14. * @method buildQueryString
  15. * @public
  16. * @static
  17. * @param {string || object} form id or name attribute, or form object.
  18. * @param {object} object containing array of form elements to exclude. { id:[], name:[], classNames:[], type:[] }
  19. * @return {string} string of the HTML form field name and value pairs.
  20. */
  21. WebGUI.Form.buildQueryString = function ( formId, excludes ) {
  22. var _isInArray = function ( value, array) {
  23. if(!array || !value) return 0;
  24. if(typeof array != 'object') return 0;
  25. for(var i = 0; i < array.length; i++) {
  26. if(array[i] == value) return 1;
  27. }
  28. return 0;
  29. };
  30. var oForm = (document.getElementById(formId) || document.forms[formId]);
  31. var oElement, oName, oValue, oDisabled;
  32. var sFormData = "";
  33. if(!excludes) {
  34. excludes = {};
  35. }
  36. // Iterate over the form elements collection to construct the label-value pairs.
  37. for (var i=0; i<oForm.elements.length; i++){
  38. oElement = oForm.elements[i];
  39. oDisabled = oElement.disabled;
  40. oName = oElement.name;
  41. oValue = oElement.value;
  42. oId = oElement.id;
  43. oClass = oElement.className;
  44. oType = oElement.type;
  45. // Do not submit fields that are disabled or do not have a name attribute value.
  46. if(oDisabled || oName == "") continue;
  47. //Filter any excludes passed in
  48. if(_isInArray(oClass,excludes.classNames)) continue;
  49. if(_isInArray(oId,excludes.id)) continue;
  50. if(_isInArray(oName,excludes.name)) continue;
  51. if(_isInArray(oType,excludes.type)) continue;
  52. switch(oType) {
  53. case 'select-one':
  54. case 'select-multiple':
  55. for(var j=0; j<oElement.options.length; j++){
  56. if(oElement.options[j].selected){
  57. if(window.ActiveXObject){
  58. sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oElement.options[j].attributes['value'].specified?oElement.options[j].value:oElement.options[j].text) + ";";
  59. }
  60. else{
  61. sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oElement.options[j].hasAttribute('value')?oElement.options[j].value:oElement.options[j].text) + ";";
  62. }
  63. }
  64. }
  65. break;
  66. case 'radio':
  67. case 'checkbox':
  68. if(oElement.checked){
  69. sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + ";";
  70. }
  71. break;
  72. case 'file':
  73. // stub case as XMLHttpRequest will only send the file path as a string.
  74. case undefined:
  75. // stub case for fieldset element which returns undefined.
  76. case 'reset':
  77. // stub case for input type reset button.
  78. default:
  79. sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + ";";
  80. }
  81. }
  82. sFormData = sFormData.substr(0, sFormData.length - 1);
  83. return sFormData;
  84. };
  85. /***********************************************************************************
  86. * @description This method clears all the values of the form. This is different than reset which will restore default values
  87. * @method clearForm
  88. * @public
  89. * @static
  90. * @param {string || object} id or object of the form element to clear values for.
  91. * @param {object} object containing array of form elements to exclude. { id:[], name:[], classNames:[], type:[] }
  92. */
  93. WebGUI.Form.clearForm = function ( oElement, excludes ) {
  94. var _isInArray = function ( value, array) {
  95. if(!array || !value) return 0;
  96. if(typeof array != 'object') return 0;
  97. for(var i = 0; i < array.length; i++) {
  98. if(array[i] == value) return 1;
  99. }
  100. return 0;
  101. };
  102. if(typeof oElement != 'object') oElement = document.getElementById(oElement);
  103. for (i = 0; i < oElement.length; i++) {
  104. var oType = oElement[i].type.toLowerCase();
  105. var oClass = oElement[i].className;
  106. var oName = oElement[i].name;
  107. var oId = oElement[i].id;
  108. if(_isInArray(oClass,excludes.classNames)) continue;
  109. if(_isInArray(oId,excludes.id)) continue;
  110. if(_isInArray(oName,excludes.name)) continue;
  111. if(_isInArray(oType,excludes.type)) continue;
  112. switch (oType) {
  113. case "text":
  114. case "password":
  115. case "textarea":
  116. case "hidden":
  117. oElement[i].value = "";
  118. break;
  119. case "radio":
  120. case "checkbox":
  121. if (oElement[i].checked) {
  122. oElement[i].checked = false;
  123. }
  124. break;
  125. case "select-one":
  126. case "select-multi":
  127. oElement[i].selectedIndex = -1;
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. return;
  134. };
  135. /***********************************************************************************
  136. * @description This method gets the proper value of the form element passed in
  137. * @method getFormValue
  138. * @public
  139. * @static
  140. * @param {string || object} id or object of the form element to get the value of.
  141. */
  142. WebGUI.Form.getFormValue = function ( oElement ) {
  143. if(typeof oElement != 'object') oElement = document.getElementById(oElement);
  144. var oType = oElement.type;
  145. var oValue = "";
  146. switch(oType) {
  147. case 'select-one':
  148. case 'select-multiple':
  149. for(var i=0; i<oElement.options.length; i++){
  150. if(oElement.options[i].selected){
  151. if(window.ActiveXObject){
  152. oValue = oElement.options[i].attributes['value'].specified?oElement.options[i].value:oElement.options[i].text;
  153. }
  154. else{
  155. oValue = oElement.options[i].hasAttribute('value')?oElement.options[i].value:oElement.options[i].text;
  156. }
  157. }
  158. }
  159. break;
  160. case 'radio':
  161. case 'checkbox':
  162. var form = oElement.form;
  163. var elem = null;
  164. for(var i = 0; i < form.length; i++) {
  165. if(form.elements[i].checked == true) {
  166. oValue = form.elements[i].value;
  167. }
  168. }
  169. break;
  170. default:
  171. oValue = oElement.value;
  172. }
  173. return oValue;
  174. };
  175. /****************************************************************************
  176. * WebGUI.Form.toggleAllCheckboxesInForm ( formElement [, checkboxesName] )
  177. * Toggles all the checkboxes in the form, optionally limited by name.
  178. * Will automatically set them all to "checked" the first time
  179. */
  180. WebGUI.Form.toggleAllCheckboxesInForm
  181. = function (formElement, checkboxesName) {
  182. // Get the state to set
  183. var oldState = WebGUI.Form.toggleAllCheckboxesState[formElement+checkboxesName]
  184. var state = oldState ? "" : "checked";
  185. for (var i = 0; i < formElement.elements.length; i++) {
  186. var input = formElement.elements[i];
  187. if (!/^check/.test(input.type))
  188. continue;
  189. if (checkboxesName && input.name != checkboxesName)
  190. continue;
  191. // Change the state
  192. input.checked = state;
  193. // Run the appropriate scripts
  194. if ( input.onchange )
  195. input.onchange();
  196. }
  197. // Update the saved state
  198. WebGUI.Form.toggleAllCheckboxesState[formElement+checkboxesName] = state;
  199. };
  200. /*
  201. * WebGUI.Form.toggleAllCheckboxesState
  202. * An object containing a hash of <formname>+<checkboxesName> : 0|1 to save
  203. * the state of the toggled checkboxes. You can use this to set what the
  204. * first run of toggleAllCheckboxesInForm will do.
  205. */
  206. WebGUI.Form.toggleAllCheckboxesState = {};