forked from simsalabim/sisyphus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sisyphus.js
257 lines (233 loc) · 7.48 KB
/
sisyphus.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Plugin developed to save html forms data to LocalStorage to restore them after browser crashes, tabs closings
* and other disasters.
*
* @author Alexander Kaupanin <[email protected]>
*/
(function($) {
$.fn.sisyphus = function(options) {
options = $.extend($.fn.sisyphus.defaults, options);
protect(this, options);
};
$.fn.sisyphus.defaults = {
excludeFields: null,
customKeyPrefix: '',
timeout: 0,
onSaveCallback: function() {},
onRestoreCallback: function() {},
onReleaseDataCallback: function() {}
}
/**
* Protect specified forms, store it's fields data to local storage and restore them on page load
*
* @param [Object] targets forms object(s), result of jQuery selector
* @param Object options plugin options
*
* @return void
*/
function protect(targets, options) {
if (! isLocalStorageAvailable()) {
return false;
}
console.log(options);
var href = window.location.hostname + window.location.pathname + window.location.search;
$(targets).each(function(){
var target = $(this);
var protectedFields = target.find(':input');
bindSaveAndRestoreData(protectedFields, target, href, options);
bindReleaseDataOnSubmitOrReset(protectedFields, target, href, options);
});
}
/**
* Restore form fields data and bind saving it's instant condition to local storage
*
* @param Object protectedFields jQuery object contains form fields to protect
* @param Object target jQuery target form object
* @param String href current window location
* @param Object options plugin options
*
* @return void
*/
function bindSaveAndRestoreData(protectedFields, target, href, options) {
var restored = false;
protectedFields.each(function(){
var prefix = href + target[0].id + this.name + options.customKeyPrefix;
var resque = localStorage.getItem(prefix);
if (resque) {
restoreData(this, resque, options);
restored = true;
}
bindSaveData(this, prefix, options);
});
if (restored && typeof options.onRestoreCallback == 'function') {
options.onRestoreCallback.call();
}
}
/**
* Restore form field data from local storage
*
* @param HTMLDomElement elem form element object
* @param String resque previously stored fields data
* @param Object options plugin options
*
* @return void
*/
function restoreData(elem, resque, options) {
if (elem.type == 'checkbox' && resque != 'false' && elem.name.indexOf('[') == -1) {
elem.checked = true;
} else if (elem.type == 'radio') {
if (elem.value == resque) {
$(elem).attr('checked', 'checked');
}
} else if (elem.name.indexOf('[') == -1) {
elem.value = resque;
} else {
resque = resque.split(',');
$(elem).val(resque);
}
}
/**
* Bind saving field data to local storage when user fills it
*
* @param HTMLDomElement elem form element object
* @param String prefix prefix used as key to store data in local storage
* @param Object options plugin options
*
* @return void
*/
function bindSaveData(elem, prefix, options) {
if ($(elem).is(':text') || $(elem).is('textarea')) {
if (! options.timeout) {
bindSaveDataImmediately(elem, prefix, options);
} else {
bindSaveDataByTimeout(elem, prefix, options);
}
} else {
bindSaveDataOnChange(elem, prefix, options);
}
}
/**
* Bind immediate saving (on typing/checking/changing) field data to local storage when user fills it
*
* @param HTMLDomElement elem form element object
* @param String prefix prefix used as key to store data in local storage
* @param Object options plugin options
*
* @return void
*/
function bindSaveDataImmediately(elem, prefix, options) {
elem.oninput = function() {
try {
localStorage.setItem(prefix, elem.value);
} catch (e) { /*QUOTA_EXCEEDED_ERR*/ }
if (typeof options.onSaveCallback == 'function') {
options.onSaveCallback.call();
}
}
}
/**
* Bind saving (by timeout) field data to local storage when user fills it
*
* @param HTMLDomElement elem form element object
* @param String prefix prefix used as key to store data in local storage
* @param Object options plugin options
*
* @return void
*/
function bindSaveDataByTimeout(elem, prefix, options) {
setTimeout((function(elem){
function timeout(){
try {
localStorage.setItem(prefix, elem.value);
} catch (e) { }
if (typeof options.onSaveCallback == 'function') {
options.onSaveCallback.call();
}
setTimeout(timeout, options.timeout * 1000);
}
return timeout;
})(elem), options.timeout * 1000);
}
/**
* Bind saving field data on change
*
* @param HTMLDomElement elem form element object
* @param String prefix prefix used as key to store data in local storage
* @param Object options plugin options
*
* @return void
*/
function bindSaveDataOnChange(elem, prefix, options) {
elem.onchange = function() {
var value = $(elem).val();
if (elem.type == 'checkbox') {
if (elem.name.indexOf('[') != -1) {
value = [];
$('[name="' + elem.name + '"]:checked').each(function(){ value.push(this.value) });
} else {
value = $(elem).is(':checked');
}
}
if (elem.type == 'radio') {
value = elem.value;
}
try {
localStorage.setItem(prefix, value);
} catch (e) { /*QUOTA_EXCEEDED_ERR*/ }
if (typeof options.onSaveCallback == 'function') {
options.onSaveCallback.call();
}
}
}
/**
* Bind release form fields data from local storage on submit/resett form
*
* @param Object protectedFields jQuery object contains form fields to protect
* @param Object target jQuery target form object
* @param String href current window location
* @param Object options plugin options
*
* @return void
*/
function bindReleaseDataOnSubmitOrReset(protectedFields, target, href, options) {
target.submit(function(){
releaseData(protectedFields, target, href, options);
});
target.bind('reset', function(){
releaseData(protectedFields, target, href, options);
});
}
/**
* Bind release form fields data from local storage on submit/resett form
*
* @param Object protectedFields jQuery object contains form fields to protect
* @param Object target jQuery target form object
* @param String href current window location
* @param Object options plugin options
*
* @return void
*/
function releaseData(protectedFields, target, href, options) {
var released = false;
protectedFields.each(function(){
var prefix = href + target[0].id + this.name;
localStorage.removeItem(prefix);
released = true;
});
if (released && typeof options.onReleaseDataCallback == 'function') {
options.onReleaseDataCallback.call();
}
}
/**
* Check if local storage is available
*
* @return Boolean
*/
function isLocalStorageAvailable() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
})(jQuery);