2
Requires foswikiCSS.js and foswikiString.js
5
var foswiki; if (foswiki == undefined) foswiki = {};
9
Original js filename: formdata2querystring.js
11
Copyright 2005 Matthew Eernisse (mde@fleegix.org)
13
Licensed under the Apache License, Version 2.0 (the "License");
14
http://www.apache.org/licenses/LICENSE-2.0
16
Original code by Matthew Eernisse (mde@fleegix.org), March 2005
17
Additional bugfixes by Mark Pruett (mark.pruett@comcast.net), 12th July 2005
18
Multi-select added by Craig Anderson (craig@sitepoint.com), 24th August 2006
23
Added KEYVALUEPAIR_DELIMITER and documentation by Arthur Clemens, 2006
26
KEYVALUEPAIR_DELIMITER : ";",
29
Serializes the data from all the inputs in a Web form
30
into a query-string style string.
31
@param inForm : (HTMLElement) Reference to a DOM node of the form element
32
@param inFormatOptions : (Object) value object of options for how to format the return string. Supported options:
33
collapseMulti: (Boolean) take values from elements that can return multiple values (multi-select, checkbox groups) and collapse into a single, comma-delimited value (e.g., thisVar=asdf,qwer,zxcv)
34
@returns Query-string formatted String of variable-value pairs
37
var queryString = foswiki.Form.formData2QueryString(
38
document.getElementById('myForm'),
43
formData2QueryString:function (inForm, inFormatOptions) {
44
if (!inForm) return null;
45
var opts = inFormatOptions || {};
48
var lastElemName = '';
50
for (i = 0; i < inForm.elements.length; i++) {
51
formElem = inForm.elements[i];
53
switch (formElem.type) {
54
// Text fields, hidden form elements
62
+ encodeURI(formElem.value)
63
+ foswiki.Form.KEYVALUEPAIR_DELIMITER;
66
// Multi-option select
67
case 'select-multiple':
69
for(var j = 0; j < formElem.options.length; j++) {
70
var currOpt = formElem.options[j];
71
if(currOpt.selected) {
72
if (opts.collapseMulti) {
75
+ encodeURI(currOpt.text);
79
+ encodeURI(currOpt.text);
85
+ encodeURI(currOpt.text)
86
+ foswiki.Form.KEYVALUEPAIR_DELIMITER;
90
if (opts.collapseMulti) {
91
str += foswiki.Form.KEYVALUEPAIR_DELIMITER;
97
if (formElem.checked) {
100
+ encodeURI(formElem.value)
101
+ foswiki.Form.KEYVALUEPAIR_DELIMITER;
107
if (formElem.checked) {
108
// Collapse multi-select into comma-separated list
109
if (opts.collapseMulti && (formElem.name == lastElemName)) {
110
// Strip of end ampersand if there is one
111
if (str.lastIndexOf('&') == str.length-1) {
112
str = str.substr(0, str.length - 1);
114
// Append value as comma-delimited string
116
+ encodeURI(formElem.value);
121
+ encodeURI(formElem.value);
123
str += foswiki.Form.KEYVALUEPAIR_DELIMITER;
124
lastElemName = formElem.name;
130
// Remove trailing separator
131
str = str.substr(0, str.length - 1);
136
Makes form field values safe to insert in a Foswiki table. Any table-breaking characters are replaced.
137
@param inForm: (String) the form to make safe
139
makeSafeForTableEntry:function(inForm) {
140
if (!inForm) return null;
143
for (i = 0; i < inForm.elements.length; i++) {
144
formElem = inForm.elements[i];
145
switch (formElem.type) {
146
// Text fields, hidden form elements
150
formElem.value = foswiki.String.makeTextSafeForTableEntry(formElem.value);
157
Finds the form element.
158
@param inFormName : (String) name of the form
159
@param inElementName : (String) name of the form element
162
getFormElement:function(inFormName, inElementName) {
163
return document[inFormName][inElementName];
167
Sets input focus to input element. Note: only one field on a page can have focus.
168
@param inFormName : (String) name of the form
169
@param inInputFieldName : (String) name of the input field that will get focus
171
setFocus:function(inFormName, inInputFieldName) {
173
var el = foswiki.Form.getFormElement(inFormName, inInputFieldName);
179
Sets the default text of an input field (for instance the text 'Enter keyword or product number' in a search box) that is cleared when the field gets focus. The field is styled with CSS class 'foswikiInputFieldBeforeFocus'.
180
@param el : (HTMLElement) the input field to receive default text
181
@param inText : (String) the default text
183
initBeforeFocusText:function(el, inText) {
184
el.FP_defaultValue = inText;
185
if (!el.value || el.value == inText) {
186
foswiki.Form._setDefaultStyle(el);
191
Clears the default input field text. The CSS styling 'foswikiInputFieldBeforeFocus' is removed. Call this function at 'onfocus'.
192
@param el : (HTMLElement) the input field that has default text
194
clearBeforeFocusText:function(el) {
195
if (!el.FP_defaultValue) {
196
el.FP_defaultValue = el.value;
198
if (el.FP_defaultValue == el.value) {
201
foswiki.CSS.addClass(el, "foswikiInputFieldFocus");
202
foswiki.CSS.removeClass(el, "foswikiInputFieldBeforeFocus");
206
Restores the default text when the input field is empty. Call this function at 'onblur'.
207
@param el : (HTMLElement) the input field to clear
209
restoreBeforeFocusText:function(el) {
210
if (!el.value && el.FP_defaultValue) {
211
foswiki.Form._setDefaultStyle(el);
213
foswiki.CSS.removeClass(el, "foswikiInputFieldFocus");
217
Sets the value and style of unfocussed or empty text field.
218
@param el : (HTMLElement) the input field that has default text
220
_setDefaultStyle:function(el) {
221
el.value = el.FP_defaultValue;
222
foswiki.CSS.addClass(el, "foswikiInputFieldBeforeFocus");