-
Notifications
You must be signed in to change notification settings - Fork 5
/
jquery.serialize-list.js
97 lines (97 loc) · 4.32 KB
/
jquery.serialize-list.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
/**
* jQuery Serialize List
* Copyright (c) 2009 Mike Botsko, Botsko.net LLC
* Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* Copyright notice and license must remain intact for legal use
* Version .2
*
* Serialize an unordered or ordered list item. Optional ability
* to determine which attributes are included. The serialization
* will be read by PHP as a multidimensional array which you may
* use for saving state.
*
* http://github.com/botskonet/jquery.serialize-list
*/
(function($){
$.fn.serializelist = function(options) {
// Extend the configuration options with user-provided
var defaults = {
attributes: ['id', 'class'], // which html attributes should be sent?
text: true,
allow_nest: true, // allow nested elements to be included
prepend: 'ul', // which query string param name should be used?
att_regex: false, // replacement regex to run on attr values
is_child: false // determine if we're serializing a child list
};
var opts = $.extend(defaults, options);
var serialStr = '';
if(!opts.is_child){
opts.prepend = '&'+opts.prepend;
}
// Begin the core plugin
this.each(function() {
var li_count = 0;
$(this).children().each(function(index, li){
if(opts.allow_nest || opts.attributes.length > 1){
for(att in opts.attributes){
val = att_rep(opts.attributes[att], $(this).attr(opts.attributes[att]));
if (val != undefined) serialStr += opts.prepend+'['+li_count+']['+opts.attributes[att]+']='+val;
if ($(li).children().size() > 1) {
serialStr += opts.prepend+'['+li_count+'][text]='+$(li)[0].firstChild.textContent;
}
else {
if (opts.text) {
if (opts.attributes[att] == 'id') {
serialStr += opts.prepend+'['+li_count+'][text]='+$(this).text();
}
}
}
}
} else {
val = att_rep(opts.attributes[0], $(this).attr(opts.attributes[0]));
if (val != undefined) serialStr += opts.prepend+'['+li_count+']='+val;
if ($(li).children().size() > 1) {
if (opts.text) {
if (opts.attributes[att] == 'id') {
serialStr += opts.prepend+'['+li_count+'][text]='+$(li)[0].firstChild.textContent;
}
}
}
else {
if (opts.text) {
if (opts.attributes[att] == 'id') {
serialStr += opts.prepend+'['+li_count+'][text]='+$(this).text();
}
}
}
}
// append any children elements
if(opts.allow_nest){
var child_base = opts.prepend+'['+li_count+'][children]';
$(this).children().each(function(){
if(this.tagName == 'UL' || this.tagName == 'OL'){
serialStr += $(this).serializelist({
'prepend': child_base,
'is_child': true,
'attributes':opts.attributes
});
}
});
}
li_count++;
});
function att_rep (att, val){
if(opts.att_regex){
for(x in opts.att_regex){
if(opts.att_regex[x].att == att){
return val.replace(opts.att_regex[x].regex, '');
}
}
} else {
return val;
}
}
});
return(serialStr);
};
})(jQuery);