formpost.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Utility for making the browser submit a hidden form, which can
  16. * be used to effect a POST from JavaScript.
  17. *
  18. * @author dpb@google.com (David P. Baker)
  19. */
  20. goog.provide('goog.ui.FormPost');
  21. goog.require('goog.array');
  22. goog.require('goog.dom.InputType');
  23. goog.require('goog.dom.TagName');
  24. goog.require('goog.dom.safe');
  25. goog.require('goog.html.SafeHtml');
  26. goog.require('goog.ui.Component');
  27. /**
  28. * Creates a formpost object.
  29. * @constructor
  30. * @extends {goog.ui.Component}
  31. * @param {goog.dom.DomHelper=} opt_dom The DOM helper.
  32. * @final
  33. */
  34. goog.ui.FormPost = function(opt_dom) {
  35. goog.ui.Component.call(this, opt_dom);
  36. };
  37. goog.inherits(goog.ui.FormPost, goog.ui.Component);
  38. /** @override */
  39. goog.ui.FormPost.prototype.createDom = function() {
  40. this.setElementInternal(
  41. this.getDomHelper().createDom(
  42. goog.dom.TagName.FORM, {'method': 'POST', 'style': 'display:none'}));
  43. };
  44. /**
  45. * Constructs a POST request and directs the browser as if a form were
  46. * submitted.
  47. * @param {Object} parameters Object with parameter values. Values can be
  48. * strings, numbers, or arrays of strings or numbers.
  49. * @param {string=} opt_url The destination URL. If not specified, uses the
  50. * current URL for window for the DOM specified in the constructor.
  51. * @param {string=} opt_target An optional name of a window in which to open the
  52. * URL. If not specified, uses the window for the DOM specified in the
  53. * constructor.
  54. */
  55. goog.ui.FormPost.prototype.post = function(parameters, opt_url, opt_target) {
  56. var form = this.getElement();
  57. if (!form) {
  58. this.render();
  59. form = this.getElement();
  60. }
  61. form.action = opt_url || '';
  62. form.target = opt_target || '';
  63. this.setParameters_(form, parameters);
  64. form.submit();
  65. };
  66. /**
  67. * Creates hidden inputs in a form to match parameters.
  68. * @param {!Element} form The form element.
  69. * @param {Object} parameters Object with parameter values. Values can be
  70. * strings, numbers, or arrays of strings or numbers.
  71. * @private
  72. */
  73. goog.ui.FormPost.prototype.setParameters_ = function(form, parameters) {
  74. var name, value, html = [];
  75. for (name in parameters) {
  76. value = parameters[name];
  77. if (goog.isArrayLike(value)) {
  78. goog.array.forEach(value, goog.bind(function(innerValue) {
  79. html.push(this.createInput_(name, String(innerValue)));
  80. }, this));
  81. } else {
  82. html.push(this.createInput_(name, String(value)));
  83. }
  84. }
  85. goog.dom.safe.setInnerHtml(form, goog.html.SafeHtml.concat(html));
  86. };
  87. /**
  88. * Creates a hidden <input> tag.
  89. * @param {string} name The name of the input.
  90. * @param {string} value The value of the input.
  91. * @return {!goog.html.SafeHtml}
  92. * @private
  93. */
  94. goog.ui.FormPost.prototype.createInput_ = function(name, value) {
  95. return goog.html.SafeHtml.create(
  96. 'input',
  97. {'type': goog.dom.InputType.HIDDEN, 'name': name, 'value': value});
  98. };