-
Notifications
You must be signed in to change notification settings - Fork 11
/
readonly.js
132 lines (106 loc) · 3.2 KB
/
readonly.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
// Readonly.js $npm_package_version
// The MIT License © 2013 Arthur Corenzan
// More on https://github.com/corenzan/readonly.js
!function(root, undefined) {
const typesThatNeedSurrogate = [
'checkbox',
'range',
'radio',
];
const allowedNodeNames = [
'INPUT',
'SELECT',
'TEXTAREA',
];
const isCheckable = (target) => {
return target.type === 'checkbox' || target.type === 'radio';
};
const supportsReadOnly = (target) => {
return ('readOnly' in target && typesThatNeedSurrogate.indexOf(target.type) < 0);
};
const addSurrogate = (target) => {
const surrogate = document.createElement('input');
surrogate.type = 'hidden';
surrogate.sync = (e) => {
surrogate.name = target.name;
surrogate.value = target.value;
if (isCheckable(target)) {
surrogate.disabled = !target.checked;
}
};
surrogate.sync();
target.addEventListener('change', surrogate.sync);
target.surrogate = surrogate;
target.parentElement.insertBefore(surrogate, target.nextElementSibling);
};
const removeSurrogate = (target) => {
target.removeEventListener('change', target.surrogate.sync);
target.parentElement.removeChild(target.surrogate);
delete target.surrogate;
};
const setReadOnly = (target) => {
if (target.hasAttribute('readonly')) {
return;
}
if (supportsReadOnly(target)) {
target.readOnly = true;
return;
}
if (!target.parentElement) {
throw Error('readonly-js: control needs a surrogate but has not been inserted into a dom tree yet, i.e. has no parent element');
}
target.setAttribute('disabled', '');
target.setAttribute('readonly', '');
addSurrogate(target);
};
const unsetReadOnly = (target) => {
if (!target.hasAttribute('readonly')) {
return;
}
if (supportsReadOnly(target)) {
target.readOnly = false;
return;
}
if (!target.parentElement) {
throw Error('readonly-js: control needs a surrogate but has not been inserted into a dom tree yet, i.e. has no parent element');
}
target.removeAttribute('disabled');
target.removeAttribute('readonly');
removeSurrogate(target);
};
const toggleReadOnly = (target, value) => {
if (value === undefined) {
value = !target.hasAttribute('readonly');
}
if (value) {
setReadOnly(target);
} else {
unsetReadOnly(target);
}
};
const entryPoint = (mixed, value) => {
if (typeof mixed === 'string') {
mixed = document.querySelectorAll(mixed);
} else if (mixed instanceof NodeList) {
mixed = [].slice.call(mixed);
} else if (mixed instanceof HTMLElement) {
mixed = [mixed];
} else if (!mixed || !('forEach' in mixed)) {
throw Error('readonly-js: invalid argument');
}
mixed.forEach((target) => {
if (allowedNodeNames.indexOf(target.nodeName) < 0) {
throw Error('readonly-js: element ' + target.nodeName + ' is not allowed');
}
toggleReadOnly(target);
});
};
if ('jQuery' in root) {
root.jQuery.fn.readonly = function(value) {
return this.each(() => {
entryPoint(this, value);
});
};
}
root.readonly = entryPoint;
}(this);