-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-persist.js
215 lines (192 loc) · 6.77 KB
/
jquery-persist.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
jquery-persist 201203*pike
persist form values in localstorage or sessionstorage
example usage:
$('input,select,textarea').persist(options);
$('input,select,textarea').unpersist(options);
options
context : 'def', // a context or namespace for each field
replace : true, // replace existing field contents if any
basename : 'jqpersist', // variable basename
session : false // use sessionstorage instead of localstorage
*/
jQuery.fn.persist = function(options) {
options = jQuery.extend({}, jQuery.persist.defaults, options);
return jQuery(this).each(function() {
var name = $(this).attr('name');
var val =jQuery.persistedValue(name,options);
if(val) {
switch(this.tagName.toLowerCase()) {
case 'input':
switch($(this).attr('type')) {
case 'radio':
// if we can replace anything or there are no checked radio buttons
if (options['replace']||$(this).parents('form').eq(0).find('input[name="'+name+'"]:checked').size()==0) {
$(this).parents('form').eq(0)
.find('input[name="'+name+'"]').each(function() {
this.checked = ($(this).val()==val);
});
}
break;
case 'checkbox':
var vals = val.split(jQuery.persist.arrsep);
$(this).parents('form').eq(0)
.find('input[name="'+name+'"]').each(function() {
// if we can replace this value or it was checked by itself
this.checked = ((jQuery.inArray($(this).val(),vals)!=-1)||(this.checked&&!options['replace']));
});
break;
default:
// if we can replace it or it is empty or 0
if (options['replace']||!$(this).val()) {
$(this).val(val);
}
}
break;
case 'select':
if ($(this).attr('multiple')) {
var vals = val.split(jQuery.persist.arrsep);
$(this).children('option').each(function() {
// if we can replace this value or it was selected by itself
this.selected = ((jQuery.inArray($(this).val(),vals)!=-1)||(this.selected&&!options['replace']));
});
} else {
// if we can replace it or it is empty or 0
if (options['replace']||!$(this).val()) {
$(this).val(val);
}
}
break;
default:
// if we can replace it or it is empty or 0
if (options['replace']||!$(this).val()) {
$(this).val(val);
}
}
}
}).on('change.persist', function(){
var name = $(this).attr('name');
switch(this.tagName.toLowerCase()) {
case 'input':
switch($(this).attr('type')) {
case "checkbox":
var vals = [];
$(this).parents('form').eq(0)
.find('input[name="'+name+'"]').each(function() {
if (this.checked) vals.push($(this).val());
});
jQuery.persistValue(name,vals.join(jQuery.persist.arrsep),options);
break;
default:
jQuery.persistValue(name, $(this).val(), options);
}
break;
case "select":
if ($(this).attr('multiple')) {
var vals = [];
$(this).children('option').each(function() {
if (this.selected) vals.push($(this).val());
});
jQuery.persistValue(name,vals.join(jQuery.persist.arrsep),options);
} else {
jQuery.persistValue(name, $(this).val(), options);
}
break;
default:
jQuery.persistValue(name, $(this).val(), options);
}
});
}
jQuery.fn.unpersist = function(options) {
options = jQuery.extend({}, jQuery.persist.defaults, options);
$(this).each(function() {
var name = $(this).attr('name');
jQuery.persistValue(name,null,options);
}).off('change.persist');
return $(this);
}
jQuery.persistValue = function (key, value, options) {
options = jQuery.extend({}, jQuery.persist.defaults, options);
var ctx = options['context'];
if (!jQuery.persist.keys.length) {
if (!jQuery.persistInit(options)) return false;
}
var idx = jQuery.inArray(ctx+jQuery.persist.ctxsep+key,jQuery.persist.keys);
if (idx!=-1) {
if (value === null || value === undefined) {
// remove value
if (jQuery.persist.debug) console.log('unpersist '+key);
jQuery.persist.keys.splice(idx,1);
jQuery.persist.vals.splice(idx,1);
} else {
if (jQuery.persist.debug) console.log('persist '+key+':'+value);
jQuery.persist.vals[idx]=value;
}
} else {
if (!(value === null || value === undefined)) {
if (jQuery.persist.debug) console.log('add persist '+key+':'+value);
jQuery.persist.keys.push(ctx+jQuery.persist.ctxsep+key);
jQuery.persist.vals.push(value);
}
}
if (jQuery.persist.keys.length) {
// store keys/vals
jQuery.persist.storage.setItem(options.basename+'_keys',jQuery.persist.keys.join(jQuery.persist.elmsep));
jQuery.persist.storage.setItem(options.basename+'_vals',jQuery.persist.vals.join(jQuery.persist.elmsep));
} else {
// remove the whole cookie
jQuery.persist.storage.removeItem(options.basename+'_keys');
jQuery.persist.storage.removeItem(options.basename+'_vals');
}
}
jQuery.persistedValue = function(key,options) {
options = jQuery.extend({}, jQuery.persist.defaults, options);
var ctx = options['context'];
if (!jQuery.persist.keys.length) {
if (!jQuery.persistInit(options)) return false;
}
var idx = jQuery.inArray(ctx+jQuery.persist.ctxsep+key,jQuery.persist.keys);
if (idx!=-1) {
if (jQuery.persist.debug) console.log('persisted '+key+':'+ jQuery.persist.vals[idx]);
return jQuery.persist.vals[idx];
} else {
if (jQuery.persist.debug) console.log('persisted '+key+': nop');
return null; //undefined
}
}
jQuery.persistInit = function(options) {
if (jQuery.persist.debug) console.log('persist init ');
options = jQuery.extend({}, jQuery.persist.defaults, options);
jQuery.persist.storage = (options.session)?window.sessionStorage:window.localStorage;
var skeys = jQuery.persist.storage.getItem(options.basename+'_keys') || '';
var svals = jQuery.persist.storage.getItem(options.basename+'_vals') || '';
jQuery.persist.keys = skeys.split(jQuery.persist.elmsep);
jQuery.persist.vals = svals.split(jQuery.persist.elmsep);
if (jQuery.persist.keys.length!=jQuery.persist.vals.length) {
// this should never happen
alert('persist error - erasing');
jQuery.persist.storage.removeItem(options.basename+'_keys',null,options);
jQuery.persist.storage.removeItem(options.basename+'_vals',null,options);
jQuery.persist.keys = [];
jQuery.persist.vals = [];
return false;
}
if (jQuery.persist.debug) console.log(jQuery.persist.keys);
if (jQuery.persist.debug) console.log(jQuery.persist.vals);
return true;
}
jQuery.persist = {
debug : false,
storage : window.localStorage,
defaults: {
context : 'def', // a context or namespace for each field
replace : true, // replace existing field contents if any
basename : 'jqpersist', // localstorage basename
session : false // use sessionstorage, not localstorage
},
elmsep : '##',
ctxsep : '::',
arrsep : '//',
keys : [],
vals : []
};