forked from sarkhanrajabov/saveStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsavestorage.js
133 lines (108 loc) · 4.17 KB
/
savestorage.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
/**
* javaScript saveStorage - 11.03.2020
* Version: 1.0.0
* Website: https://github.com/sarkhanrajabov/saveStorage-js
* Author: Sarkhan Rajabov
* Modified by Jeremy B. to use SessionsStorage
**/
function saveStorage(selector, options) {
'use strict';
if (typeof Storage !== "undefined") {
let form = document.querySelector(selector),
key = form.getAttribute('id') + '_saveStorage',
elements = form.querySelectorAll('input, textarea, select'),
defaults = {
exclude: [],
};
let extend = function (out) {
out = out || {};
for (let i = 1; i < arguments.length; i++) {
if (!arguments[i])
continue;
for (let key in arguments[i]) {
if (arguments[i].hasOwnProperty(key))
out[key] = arguments[i][key];
}
}
return out;
};
let opts = extend({}, defaults, options);
let excludeInputType = function () {
let inputType = '';
opts.exclude.forEach(function (type) {
inputType += ':not([type=' + type + '])';
});
return inputType;
};
let serializeArray = function () {
let serializeData = [];
elements.forEach(function (el) {
if (el.type !== 'radio' && el.type !== 'checkbox') {
serializeData.push({ name: el.name, value: el.value, type: el.type });
}
else if (el.hasAttribute("checked") || el.checked) {
serializeData.push({ name: el.name, value: el.value, type: el.type, checked: el.checked });
}
});
return serializeData;
};
let setSessionStorage = function () {
let formData = JSON.stringify(serializeArray());
console.log(formData);
sessionStorage.setItem(key, formData);
};
let initApp = function () {
if (sessionStorage.getItem(key) !== null) {
let data = JSON.parse(sessionStorage.getItem(key));
console.log(data)
data.forEach(function (v) {
if (v.type !== 'radio' && v.type !== 'checkbox') {
if (form.querySelector('[name=' + escapeBrackets(v.name) + ']')) {
let input = form.querySelector('[name=' + escapeBrackets(v.name) + ']' + excludeInputType());
if (input !== null) {
input.value = v.value;
}
}
}
else {
let input = form.querySelectorAll('[name=' + escapeBrackets(v.name) + ']');
input.forEach(function (el) {
if (el.name === v.name && el.value === v.value) {
el.checked = v.checked;
}
})
}
});
}
};
function escapeBrackets(value) {
const escapeCharacter = '\\';
let escaped = value;
escaped = escaped.replaceAll('[', escapeCharacter + '[');
escaped = escaped.replaceAll(']', escapeCharacter + ']');
return escaped
}
function unescapeBrackets(value) {
const escapeCharacter = '\\';
let unescaped = value;
unescaped = unescaped.replaceAll(escapeCharacter + '[', '[');
unescaped = unescaped.replaceAll(escapeCharacter + ']', ']');
return unescaped
}
form.addEventListener('change', function () {
setSessionStorage();
});
elements.forEach(function (el) {
el.addEventListener('keyup', function () {
setSessionStorage();
});
});
form.addEventListener('submit', function () {
sessionStorage.removeItem(key);
});
initApp();
}
else {
console.error('Sorry! No web storage support.');
}
}