forked from petersooley/form-save-and-restore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave.js
128 lines (126 loc) · 3.41 KB
/
save.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
// Generated by CoffeeScript 1.6.2
(function() {
$(function() {
var data, foundInputs, foundOne, foundSelects, foundTexts, i, input, inputs, key, name, savedData, select, selects, textarea, texts, type, url, val, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _m, _n, _ref, _ref1, _ref2, _ref3, _ref4, _ref5;
inputs = {};
_ref = $('input');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
input = _ref[_i];
if (!$(input).is(':visible')) {
continue;
}
name = $(input).attr('name');
type = $(input).attr('type');
if (name in inputs) {
continue;
}
foundInputs = {};
i = 0;
foundOne = false;
_ref1 = $('input[name="' + name + '"]');
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
input = _ref1[_j];
++i;
if (!$(input).is(':visible')) {
continue;
}
switch (type) {
case 'checkbox':
case 'radio':
foundInputs[i] = $(input).is(':checked') ? 'checked' : '';
foundOne = true;
break;
case 'text':
case 'password':
case 'date':
case 'datetime':
case 'datetime-local':
case 'email':
case 'color':
case 'month':
case 'number':
case 'range':
case 'tel':
case 'url':
case 'week':
foundInputs[i] = $(input).val();
foundOne = true;
}
}
if (foundOne) {
inputs[name] = foundInputs;
}
}
texts = {};
_ref2 = $('textarea');
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
textarea = _ref2[_k];
if (!$('textarea').is(':visible')) {
continue;
}
name = $(textarea).attr('name');
if (name in texts) {
continue;
}
foundTexts = {};
i = 0;
foundOne = false;
_ref3 = $('textarea[name="' + name + '"]');
for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
textarea = _ref3[_l];
++i;
if (!$(textarea).is(':visible')) {
continue;
}
foundTexts[i] = $(textarea).val();
foundOne = true;
}
if (foundOne) {
texts[name] = foundTexts;
}
}
selects = {};
_ref4 = $('select');
for (_m = 0, _len4 = _ref4.length; _m < _len4; _m++) {
select = _ref4[_m];
if (!$(select).is(':visible')) {
continue;
}
name = $(select).attr('name');
if (name in selects) {
continue;
}
foundSelects = {};
i = 0;
foundOne = false;
_ref5 = $('select[name="' + name + '"]');
for (_n = 0, _len5 = _ref5.length; _n < _len5; _n++) {
select = _ref5[_n];
++i;
if (!$(select).is(':visible')) {
continue;
}
val = $(select).val();
foundOne = true;
if (val instanceof Array) {
foundSelects[i] = val;
} else {
foundSelects[i] = [val];
}
}
if (foundOne) {
selects[name] = foundSelects;
}
}
url = window.location.pathname;
key = 'formSaveAndRestore';
data = localStorage.getItem(key);
savedData = data ? JSON.parse(data) : {};
savedData[url] = {
inputs: inputs,
texts: texts,
selects: selects
};
return localStorage.setItem(key, JSON.stringify(savedData));
});
}).call(this);