-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_preloader.js
More file actions
144 lines (123 loc) · 3.88 KB
/
input_preloader.js
File metadata and controls
144 lines (123 loc) · 3.88 KB
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
/*
* InputPreloader 2.0.1
* Copyright © 2019 Zaim Ramlan
*/
class InputPreloader {
/**
* Creates an instance of InputPreloader.
*
* @constructor
* @author: Zaim Ramlan
* @param {array} inputs The array of inputs with their corresponding expected input type, that
* are desired to be preloaded.
*/
constructor(inputs) {
this._inputs = this._toObject(inputs);
}
/**
* Configures to save the given inputs before moving away from
* the page, and preloads them (if their data exists).
*/
configure() {
this._setToSaveInputsBeforeUnloading();
this._preloadInputsIfAvailable();
}
/**
* Deconfigures to not save the given inputs before moving away from
* the page, and remove all stored inputs (if their data exists).
*/
deconfigure() {
this._unsetToSaveInputsBeforeUnloading();
this._removeAllStoredInputs();
}
/**
* Breaks down the inputs from string, into a object of inputs.
*
* @param {array} inputs The array of inputs with their corresponding expected input type, that
* are desired to be preloaded.
* @return {array} An array of input objects of each inputs passed.
*/
_toObject(inputs) {
inputsArray = [];
inputs.forEach((input) => {
inputDetails = input.split(":");
inputHash = { id: inputDetails[0], type: inputDetails[1], value: null };
inputsArray.push(inputHash);
});
return inputsArray;
}
/**
* Sets to save inputs' values in the session storage for preloading,
* before unloading the page.
*/
_setToSaveInputsBeforeUnloading() {
var self = this;
window.onbeforeunload = function() {
self._inputs.forEach((input) => {
var element = document.getElementById(input.id);
if (element !== null) {
var isRadioButton = input.type === "radio";
var value = isRadioButton ? element.checked : element.value;
sessionStorage.setItem(input.id, value);
}
});
}
}
/**
* Preloads inputs' values, if they are available.
*/
_preloadInputsIfAvailable() {
var self = this;
this._inputs.forEach((input) => {
var value = sessionStorage.getItem(input.id);
if (value !== null) {
input.value = value;
self._preload(input);
}
});
}
/**
* Preloads the element with the `input.id`, to the `input.value`
* of `input.type`.
*
* @param {object} input The input object that has the input's `id`, `type` and `value`.
*/
_preload(input) {
if (input.value !== "") {
switch(input.type) {
case "radio":
input.value = input.value === "true";
break;
case "float":
input.value = parseFloat(input.value);
break;
case "integer":
input.value = parseInt(input.value);
break;
case "string":
// do nothing with `input.value` as it's already a string
break;
}
}
var element = document.getElementById(input.id);
if (element !== null) {
var isRadioButton = input.type === "radio";
isRadioButton ? element.checked = input.value : element.value = input.value;
}
}
/**
* Remove functions to be executed on `window.onbeforeunload`.
*/
_unsetToSaveInputsBeforeUnloading() {
window.onbeforeunload = null;
}
/**
* Removes all stored inputs.
*/
_removeAllStoredInputs() {
this._inputs.forEach((input) => {
sessionStorage.removeItem(input.id);
});
}
}
module.exports = InputPreloader;