-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputTypes.mjs
131 lines (106 loc) · 3.84 KB
/
inputTypes.mjs
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
import { UNDEFINED } from "./core.mjs";
import { onKeyDown } from "./core.mjs";
import { onMouseUp } from "./core.mjs";
import { onContextMenu } from "./core.mjs";
import { TRUE } from "./core.mjs";
import { onChange } from "./core.mjs";
import { toBoolean } from "./core.mjs";
import { FALSE } from "./core.mjs";
import { setAttribute } from "./core.mjs";
import { onInput } from "./core.mjs";
import { onDrop } from "./core.mjs";
import { onSelect } from "./core.mjs";
import { onMouseDown } from "./core.mjs";
import { onKeyUp } from "./core.mjs";
import { SoftEvent } from "./core.mjs";
const textInputUserChangeEventsList =
[onInput, onKeyDown, onKeyUp, onMouseDown, onMouseUp, onSelect, onContextMenu, onDrop];
export const assignTextInputUserChangeEvents = (element, callback) => {
let oldValue, oldSelectionStart, oldSelectionEnd;
const eventProcessor = (evt) => {
const target = evt.target;
const value = target.value;
if (value !== oldValue) {
if (callback(value, oldValue, evt)) {
oldSelectionStart = target.selectionStart;
oldSelectionEnd = target.selectionEnd;
} else if (oldValue !== UNDEFINED) {
target.value = oldValue;
target.setSelectionRange(oldSelectionStart, oldSelectionEnd);
}
oldValue = value;
}
}
for (let eventElement of textInputUserChangeEventsList) {
eventElement(eventProcessor, element);
}
}
const assignOnValueChange = (element) => {
if (element.onValueChange) return;
element.onValueChange = SoftEvent(element);
assignTextInputUserChangeEvents(element, (v, old) => {
element.onValueChange.raise(v, old, element);
return TRUE;
});
}
export const autogrowSimple = element => {
assignOnValueChange(element);
element.onValueChange.add( ()=> element.size = Math.max(element.value.length, 1))
}
// --=======================================================================================--
export const typeText = (element) => {
element.s = (v = '') => {
const old = element.value;
// if (v.length > lengthMax) v = v.substr(0, lengthMax);
if (v !== old) {
element.value = v;
element.onValueChange.raise(v, old, element);
}
}
element.g = () => element.value;
assignOnValueChange(element);
}
export const typeCheckbox = (element) => {
setAttribute(element, 'type', 'checkbox');
element.onValueChange = SoftEvent(element);
let internalValue = FALSE;
const setElementValue = () => {
if (internalValue === UNDEFINED) {
element.checked = FALSE;
element.indeterminate = TRUE;
} else {
element.checked = internalValue;
element.indeterminate = FALSE;
}
}
element.s = (v) => {
const internalValuePrevious = internalValue;
internalValue = (v === UNDEFINED) ? UNDEFINED : toBoolean(v);
if (internalValue !== internalValuePrevious) {
setElementValue();
element.onValueChange.raise(internalValue, internalValuePrevious, element);
}
}
element.g = () => internalValue;
const toggle = () => {
if (!element.readOnly) {
if (internalValue === UNDEFINED) {
internalValue = TRUE;
} else if (internalValue === TRUE) {
internalValue = FALSE
} else if (element.isTristate) {
internalValue = UNDEFINED
} else {
internalValue = TRUE;
}
setElementValue();
}
};
onChange(() => {
const internalValuePrevious = internalValue;
toggle();
element.onValueChange.raise(internalValue, internalValuePrevious, element);
}, element);
}
export const typeNumber = element => {
}