PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/cfObjective2008/coldfusion/lib/SpryUtils.js

http://andrewpowell.googlecode.com/
JavaScript | 159 lines | 101 code | 20 blank | 38 comment | 37 complexity | 1473f1f6298cf921deb3b10b270b97ee MD5 | raw file
Possible License(s): Apache-2.0, MIT
  1. // SpryUtils.js - version 0.3 - Spry Pre-Release 1.6.1
  2. //
  3. // Copyright (c) 2007. Adobe Systems Incorporated.
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. // * Neither the name of Adobe Systems Incorporated nor the names of its
  15. // contributors may be used to endorse or promote products derived from this
  16. // software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. // POSSIBILITY OF SUCH DAMAGE.
  29. var Spry; if (!Spry) Spry = {}; if (!Spry.Utils) Spry.Utils = {};
  30. Spry.Utils.submitForm = function(form, callback, opts)
  31. {
  32. if (!form)
  33. return true;
  34. if ( typeof form == 'string' )
  35. form = Spry.$(form) || document.forms[form];
  36. var frmOpts = {};
  37. frmOpts.method = form.getAttribute('method');
  38. frmOpts.url = form.getAttribute('action') || document.location.href;
  39. frmOpts.enctype = form.getAttribute('enctype');
  40. Spry.Utils.setOptions(frmOpts, opts);
  41. var submitData = Spry.Utils.extractParamsFromForm(form, frmOpts.elements);
  42. if (frmOpts.additionalData)
  43. submitData += "&" + frmOpts.additionalData;
  44. if (!frmOpts.enctype || frmOpts.enctype.toLowerCase() != 'multipart/form-data')
  45. {
  46. // Ajax submission of a form doesn't work for multipart/form-data!
  47. frmOpts.method = (frmOpts.method && frmOpts.method.toLowerCase() == "post") ? 'POST' : 'GET';
  48. if (frmOpts.method == "GET")
  49. {
  50. // Data will be submitted in the url.
  51. if (frmOpts.url.indexOf('?') == -1)
  52. frmOpts.url += '?';
  53. else
  54. frmOpts.url += '&';
  55. frmOpts.url += submitData;
  56. }
  57. else
  58. {
  59. // Send Content-Type header.
  60. if (!frmOpts.headers) frmOpts.headers = {};
  61. if (!frmOpts.headers['Content-Type'] || frmOpts.headers['Content-Type'].indexOf("application/x-www-form-urlencoded") ==-1 )
  62. frmOpts.headers['Content-Type'] = 'application/x-www-form-urlencoded';
  63. // Set the postData
  64. frmOpts.postData = submitData;
  65. }
  66. Spry.Utils.loadURL(frmOpts.method, frmOpts.url, true, callback, frmOpts);
  67. return false;
  68. }
  69. // Native submission when 'multipart/form-data' is used.
  70. return true;
  71. };
  72. Spry.Utils.extractParamsFromForm = function (form, elements)
  73. {
  74. if (!form)
  75. return '';
  76. if ( typeof form == 'string' )
  77. form = document.getElementById(form) || document.forms[form];
  78. var formElements;
  79. if (elements)
  80. formElements = ',' + elements.join(',') + ',';
  81. var compStack = new Array(); // key=value pairs
  82. var el;
  83. for (var i = 0; i < form.elements.length; i++ )
  84. {
  85. el = form.elements[i];
  86. if (el.disabled || !el.name)
  87. {
  88. // Don't submit disabled elements.
  89. // Don't submit elements without name.
  90. continue;
  91. }
  92. if (!el.type)
  93. {
  94. // It seems that this element doesn't have a type set,
  95. // so skip it.
  96. continue;
  97. }
  98. if (formElements && formElements.indexOf(',' + el.name + ',')==-1)
  99. continue;
  100. switch(el.type.toLowerCase())
  101. {
  102. case 'text':
  103. case 'password':
  104. case 'textarea':
  105. case 'hidden':
  106. case 'submit':
  107. compStack.push(encodeURIComponent(el.name) + '=' + encodeURIComponent(el.value));
  108. break;
  109. case 'select-one':
  110. var value = '';
  111. var opt;
  112. if (el.selectedIndex >= 0) {
  113. opt = el.options[el.selectedIndex];
  114. value = opt.value || opt.text;
  115. }
  116. compStack.push(encodeURIComponent(el.name) + '=' + encodeURIComponent(value));
  117. break;
  118. case 'select-multiple':
  119. for (var j = 0; j < el.length; j++)
  120. {
  121. if (el.options[j].selected)
  122. {
  123. value = el.options[j].value || el.options[j].text;
  124. compStack.push(encodeURIComponent(el.name) + '=' + encodeURIComponent(value));
  125. }
  126. }
  127. break;
  128. case 'checkbox':
  129. case 'radio':
  130. if (el.checked)
  131. compStack.push(encodeURIComponent(el.name) + '=' + encodeURIComponent(el.value));
  132. break;
  133. default:
  134. // file, button, reset
  135. break;
  136. }
  137. }
  138. return compStack.join('&');
  139. };