forked from PatrickJS/angular-hmr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperimental.ts
176 lines (155 loc) · 6.36 KB
/
experimental.ts
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
// Experimental API below
/**
* get input values
*
* Extended by: Gabriel Schuster <[email protected]>
* Now gets values of inputs (including "checked" status radios, checkboxes), textareas and selects (including multiselects)
* Tries to identify the elements as exact as possible, falls back to numeric index when identification fails
* WIP refactor by: PatrickJS
*/
export function __getInputValues() {
const _inputs = document.querySelectorAll('input, textarea, select');
const inputs = Array.prototype.slice.call(_inputs);
return inputs.map(function(input: any) {
const inputTagName = input.tagName.toLowerCase();
const inputType = input.type;
const inputId = (input.id && typeof input.id === 'string') ? input.id : null;
const inputName = (input.name && typeof input.name === 'string') ? input.name : null;
const inputValue = (input.value && typeof input.value === 'string') ? input.value : null;
const inputChildNodes = input.childNodes;
const inputSelected = Boolean(input.selected);
type InputOption = {value: string, selected: boolean};
type HmrStore = {
tag: string;
type: string;
id: string;
name: string;
value: string;
checked: boolean;
options: Array<InputOption>;
};
let elementStore: HmrStore = {
'tag': inputTagName,
'type': null,
'id': inputId,
'name': inputName,
'value': '',
'checked': false,
'options': []
};
if ('input' === inputTagName || 'textarea' === inputTagName) {
elementStore['type'] = inputType;
if ('input' !== inputTagName) {
elementStore['value'] = inputValue;
return elementStore;
}
switch (inputType) {
case 'checkbox':
case 'radio':
elementStore['checked'] = inputSelected;
elementStore['value'] = inputValue;
return elementStore;
case 'image':
case 'button':
case 'submit':
case 'reset':
default:
// These types don't need any config and thus need no update, they only were stored because they match "input"
return elementStore;
}
} else if ('select' === inputTagName) {
const childNodes = Array.prototype.slice.call(inputChildNodes);
const options: Array<InputOption> = childNodes.map((option: any, i: number) => {
return { value: option['value'], selected: Boolean(option['selected']) };
});
elementStore['options'] = options;
return elementStore;
}
return elementStore;
});
}
/**
* set input values
*
* Extended by: Gabriel Schuster <[email protected]>
* WIP refactor by: PatrickJS
*/
export function __setInputValues($inputs: any) {
const inputs: any = document.querySelectorAll('input, textarea');
$inputs.forEach((store: any, i: number) => {
if ('input' === store.tag || 'textarea' === store.tag) {
if ('input' === store.tag && ('checkbox' === store.type || 'radio' === store.type)) {
let selector = 'input' + (
null !== store.id ? '#' + store.id : ''
) + '[type="' + store.type + '"]' + (null !== store.name ? '[name="' + store.name + '"]' : '') +
'[value="' + store.value + '"]';
let element: any = document.body.querySelector(selector);
if (element && Boolean(store['checked'])) {
element['checked'] = 'checked';
element.dispatchEvent(new CustomEvent('input', {detail: element['checked']}));
}
} else if ('input' === store.tagName.toLowerCase() &&
('image' === store.type || 'button' === store.type || 'submit' === store.type || 'reset' === store.type)) {
// These types don't need any config and thus need no update, they only were stored because they match "input"
} else {
if (null === store.id && null === store.name) {
if (store.value.length &&
inputs[i] &&
inputs[i].tagName.toLowerCase() === store.tag &&
('textarea' === store.tag || inputs[i].getAttribute('type') === store.type) &&
('string' !== typeof inputs[i].id || !inputs[i].id.length) &&
('string' !== typeof inputs[i].getAttribute('name') ||
!inputs[i].getAttribute('name').length)) {
inputs[i]['value'] = store.value;
inputs[i].dispatchEvent(new CustomEvent('input', {detail: inputs[i]['value']}));
}
} else {
let selector = 'input' +
(null !== store.id ? '#' + store.id : '') + ('input' === store.tag ? '[type="' + store.type + '"]' : '') +
(null !== store.name ? '[name="' + store.name + '"]' : '');
let element: any = document.body.querySelector(selector);
if (element && store.value.length) {
element['value'] = store.value;
element.dispatchEvent(new CustomEvent('input', {detail: element['value']}));
}
}
}
} else if ('select' === store.tag) {
let select: any = null;
if (null === store.id && null === store.name) {
if (inputs[i] && inputs[i].tagName.toLowerCase() === store.tag && ('string' !== typeof inputs[i].id || !inputs[i].id.length) &&
('string' !== typeof inputs[i].getAttribute('name') || !inputs[i].getAttribute('name').length)) {
select = inputs[i];
}
} else {
let selector = 'select' + (null !== store.id ? '#' + store.id : '') + (null !== store.name ? '[name="' + store.name + '"]' : '');
let element: any = document.body.querySelector(selector);
if (element) {
select = element;
}
}
if (select) {
store.options.forEach((storedOption: any, j: number) => {
let option: any = select.querySelector('option[value="' + storedOption.value + '"]');
if (
!option &&
select.childNodes[j] &&
('string' !== typeof select.childNodes[j]['value'] || !select.childNodes[j]['value'].length)
) {
option = select.childNodes[j];
}
if (option && !!storedOption.selected) {
option['selected'] = 'selected';
option.dispatchEvent(new CustomEvent('input', {detail: option['selected']}));
}
});
}
}
});
}
export function __createInputTransfer() {
const $inputs = __getInputValues();
return function restoreInputValues() {
return __setInputValues($inputs);
};
}