forked from nilbus/unserialize-to-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQuery.unserializeForm.js
145 lines (128 loc) · 6.45 KB
/
jQuery.unserializeForm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Unserialize (to) form plugin
// Version 1.0.5
// Copyright (C) 2010-2011 Christopher Thielen, others (see ChangeLog below)
// Dual-licensed under GPLv2 and the MIT open source licenses
// Usage:
// var s = $("form").serialize(); // save form settings
// $("form").unserializeForm(s); // restore form settings
// Notes:
// * Recurses fieldsets, p tags, etc.
// * Form elements must have a 'name' attribute.
// Alternate Usage:
// var s = $("form").serialize();
// $("form").unserializeForm(s, {
// 'callback' : function(el, val) { $(el).val(val); },
// 'override-values' : false
// });
//
// callback (optional):
// The above callback is given the element and value, allowing you to build
// dynamic forms via callback. If you return false, unserializeForm will
// try to find and set the DOM element, otherwise, (on true) it assumes you've
// handled that attribute and moves onto the next.
// override-values (optional, default is false):
// Controls whether elements already set (e.g. an input tag with a non-zero length value)
// will be touched by the unserializer. Does not apply to radio fields or checkboxes.
// If you have a use case for radio fields or checkboxes, please file an issue at
// https://github.com/cthielen/unserialize-to-form/issues/ . Also note this option
// does not apply to a callback, i.e. a callback would still have the opportunity
// to override a value even if this option is set to false. It is up to you as
// the callback author to enforce the behavior you wish.
// See ChangeLog at end of file for history.
(function($) {
var methods = {
_unserializeFormSetValue : function( el, _value, override ) {
if($(el).length > 1) {
// Assume multiple elements of the same name are radio buttons
$.each(el, function(i) {
if($(this).attr("value") == _value) {
// Check it
$(this).attr("checked", true);
} else {
// Uncheck it
$(this).attr("checked", false);
}
});
} else {
// Assume, if only a single element, it is not a radio button
if($(el).attr("type") == "checkbox") {
$(el).attr("checked", true);
} else {
if(override) {
$(el).val(_value);
} else {
if (!$(el).val()) {
$(el).val(_value);
}
}
}
}
}
};
// takes a GET-serialized string, e.g. first=5&second=3&a=b and sets input tags (e.g. input name="first") to their values (e.g. 5)
$.fn.unserializeForm = function( _values, _options ) {
// Set up defaults
var settings = $.extend( {
'callback' : undefined,
'override-values' : false
}, _options);
return this.each(function() {
// this small bit of unserializing borrowed from James Campbell's "JQuery Unserialize v1.0"
_values = _values.split("&");
_callback = settings["callback"];
_override_values = settings["override-values"];
if(_callback && typeof(_callback) !== "function") {
_callback = undefined; // whatever they gave us wasn't a function, act as though it wasn't given
}
var serialized_values = new Array();
$.each(_values, function() {
var properties = this.split("=");
if((typeof properties[0] != 'undefined') && (typeof properties[1] != 'undefined')) {
serialized_values[properties[0].replace(/\+/g, " ")] = decodeURI(properties[1].replace(/\+/g, " "));
}
});
// _values is now a proper array with values[hash_index] = associated_value
_values = serialized_values;
// Start with all checkboxes and radios unchecked, since an unchecked box will not show up in the serialized form
$(this).find(":checked").attr("checked", false);
// Iterate through each saved element and set the corresponding element
for(var key in _values) {
var el = $(this).add("input,select,textarea").find("[name=\"" + unescape(key) + "\"]");
var _value = unescape(_values[key]);
if(_callback == undefined) {
// No callback specified - assume DOM elements exist
methods._unserializeFormSetValue(el, _value, _override_values);
} else {
// Callback specified - don't assume DOM elements already exist
var result = _callback.call(this, unescape(key), _value);
// If they return true, it means they handled it. If not, we will handle it.
// Returning false then allows for DOM building without setting values.
if(result == false) {
// Try and find the element again as it may have just been created by the callback
var el = $(this).add("input,select,textarea").find("[name=\"" + unescape(key) + "\"]");
methods._unserializeFormSetValue(el, _value, _override_values);
}
}
}
})
}
})(jQuery);
// ChangeLog
// 2010-11-19: Version 1.0 release. Works on text, checkbox and select inputs.
// 2011-01-26: Version 1.0.1 release. Fixed regular expression search, thanks Anton.
// 2011-02-02: Version 1.0.2 release. Support for textareas & check for undefined values, thanks Brandon.
// 2011-10-19: Version 1.0.3 release:
// * Fixed unescaping issue for certain encoding elements (@)
// * Traverse saved elements instead of the form when unserializing
// * Provide optional callback for building dynamic forms
// * Fixed issue setting radio buttons
// 2011-11-11: Version 1.0.4 release:
// * Use .attr() instead of .prop() for jQuery 1.6 compatibility (Edward Anderson)
// * Restore unchecked checkboxes and radios (Edward Anderson)
// 2011-12-06: Version 1.0.5 release:
// * Fixed an issue with certain UTF characters (Chinese cited, thanks hoka!)
// * Implemented 'return this' chaining method as recommended in jQuery docs
// * Encapsulated internal method to keep namespaces clean
// * Changed second parameter to an options array to account for new option
// * Added new option 'override-values', default to true, to control whether fields
// with content should be touched by the unserializer