| 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 |
/** |
| 16 |
* @fileoverview Utility for making the browser submit a hidden form, which can |
| 17 |
* be used to effect a POST from JavaScript. |
| 18 |
* |
| 19 |
* @author [email protected] (David P. Baker) |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.ui.FormPost'); |
| 23 |
|
| 24 |
goog.require('goog.array'); |
| 25 |
goog.require('goog.dom.InputType'); |
| 26 |
goog.require('goog.dom.TagName'); |
| 27 |
goog.require('goog.dom.safe'); |
| 28 |
goog.require('goog.html.SafeHtml'); |
| 29 |
goog.require('goog.ui.Component'); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Creates a formpost object. |
| 35 |
* @constructor |
| 36 |
* @extends {goog.ui.Component} |
| 37 |
* @param {goog.dom.DomHelper=} opt_dom The DOM helper. |
| 38 |
* @final |
| 39 |
*/ |
| 40 |
goog.ui.FormPost = function(opt_dom) { |
| 41 |
goog.ui.Component.call(this, opt_dom); |
| 42 |
}; |
| 43 |
goog.inherits(goog.ui.FormPost, goog.ui.Component); |
| 44 |
|
| 45 |
|
| 46 |
/** @override */ |
| 47 |
goog.ui.FormPost.prototype.createDom = function() { |
| 48 |
this.setElementInternal( |
| 49 |
this.getDomHelper().createDom( |
| 50 |
goog.dom.TagName.FORM, {'method': 'POST', 'style': 'display:none'})); |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Constructs a POST request and directs the browser as if a form were |
| 56 |
* submitted. |
| 57 |
* @param {Object} parameters Object with parameter values. Values can be |
| 58 |
* strings, numbers, or arrays of strings or numbers. |
| 59 |
* @param {string=} opt_url The destination URL. If not specified, uses the |
| 60 |
* current URL for window for the DOM specified in the constructor. |
| 61 |
* @param {string=} opt_target An optional name of a window in which to open the |
| 62 |
* URL. If not specified, uses the window for the DOM specified in the |
| 63 |
* constructor. |
| 64 |
*/ |
| 65 |
goog.ui.FormPost.prototype.post = function(parameters, opt_url, opt_target) { |
| 66 |
var form = this.getElement(); |
| 67 |
if (!form) { |
| 68 |
this.render(); |
| 69 |
form = this.getElement(); |
| 70 |
} |
| 71 |
form.action = opt_url || ''; |
| 72 |
form.target = opt_target || ''; |
| 73 |
this.setParameters_(form, parameters); |
| 74 |
form.submit(); |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Creates hidden inputs in a form to match parameters. |
| 80 |
* @param {!Element} form The form element. |
| 81 |
* @param {Object} parameters Object with parameter values. Values can be |
| 82 |
* strings, numbers, or arrays of strings or numbers. |
| 83 |
* @private |
| 84 |
*/ |
| 85 |
goog.ui.FormPost.prototype.setParameters_ = function(form, parameters) { |
| 86 |
var name, value, html = []; |
| 87 |
for (name in parameters) { |
| 88 |
value = parameters[name]; |
| 89 |
if (goog.isArrayLike(value)) { |
| 90 |
goog.array.forEach(value, goog.bind(function(innerValue) { |
| 91 |
html.push(this.createInput_(name, String(innerValue))); |
| 92 |
}, this)); |
| 93 |
} else { |
| 94 |
html.push(this.createInput_(name, String(value))); |
| 95 |
} |
| 96 |
} |
| 97 |
goog.dom.safe.setInnerHtml(form, goog.html.SafeHtml.concat(html)); |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Creates a hidden <input> tag. |
| 103 |
* @param {string} name The name of the input. |
| 104 |
* @param {string} value The value of the input. |
| 105 |
* @return {!goog.html.SafeHtml} |
| 106 |
* @private |
| 107 |
*/ |
| 108 |
goog.ui.FormPost.prototype.createInput_ = function(name, value) { |
| 109 |
return goog.html.SafeHtml.create( |
| 110 |
'input', |
| 111 |
{'type': goog.dom.InputType.HIDDEN, 'name': name, 'value': value}); |
| 112 |
}; |