forked from DevExpress/testcafe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select-element.js
308 lines (227 loc) · 10.4 KB
/
select-element.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//NOTE: we can't manipulate (open/close option list) with a native select element during test running, so we
// draw our custom option list to emulate this.
import hammerhead from './deps/hammerhead';
import testCafeCore from './deps/testcafe-core';
import uiRoot from './ui-root';
const shadowUI = hammerhead.shadowUI;
const browserUtils = hammerhead.utils.browser;
const featureDetection = hammerhead.utils.featureDetection;
const nativeMethods = hammerhead.nativeMethods;
const eventSimulator = hammerhead.eventSandbox.eventSimulator;
const listeners = hammerhead.eventSandbox.listeners;
const positionUtils = testCafeCore.positionUtils;
const domUtils = testCafeCore.domUtils;
const styleUtils = testCafeCore.styleUtils;
const eventUtils = testCafeCore.eventUtils;
const arrayUtils = testCafeCore.arrayUtils;
const OPTION_LIST_CLASS = 'tcOptionList';
const OPTION_GROUP_CLASS = 'tcOptionGroup';
const OPTION_CLASS = 'tcOption';
const DISABLED_CLASS = 'disabled';
const MAX_OPTION_LIST_LENGTH = browserUtils.isIE ? 30 : 20;
let curSelectEl = null;
let optionList = null;
let groups = [];
let options = [];
function onDocumentMouseDown (e) {
const target = nativeMethods.eventTargetGetter.call(e);
//NOTE: only in Mozilla 'mousedown' raises for option
if ((target || e.srcElement) !== curSelectEl && !domUtils.containsElement(curSelectEl, target) &&
!domUtils.containsElement(optionList, target))
collapseOptionList();
}
function onWindowClick (e, dispatched, preventDefault) {
const target = nativeMethods.eventTargetGetter.call(e);
const optionIndex = arrayUtils.indexOf(options, target);
if (optionIndex < 0)
return;
preventDefault();
const isDisabled = target.className.indexOf(DISABLED_CLASS) > -1;
if (isDisabled && browserUtils.isWebKit)
return;
clickOnOption(optionIndex, isDisabled);
}
function clickOnOption (optionIndex, isOptionDisabled) {
const curSelectIndex = curSelectEl.selectedIndex;
const realOption = curSelectEl.getElementsByTagName('option')[optionIndex];
const clickLeadChanges = !isOptionDisabled && optionIndex !== curSelectIndex;
if (clickLeadChanges && !browserUtils.isIE)
curSelectEl.selectedIndex = optionIndex;
if (!browserUtils.isFirefox && !browserUtils.isIE && clickLeadChanges) {
eventSimulator.input(curSelectEl);
eventSimulator.change(curSelectEl);
}
if (browserUtils.isFirefox || browserUtils.isIE)
eventSimulator.mousedown(browserUtils.isFirefox ? realOption : curSelectEl);
if (!featureDetection.isTouchDevice)
eventSimulator.mouseup(browserUtils.isFirefox ? realOption : curSelectEl);
if ((browserUtils.isFirefox || browserUtils.isIE) && clickLeadChanges) {
if (browserUtils.isIE)
curSelectEl.selectedIndex = optionIndex;
if (!browserUtils.isIE)
eventSimulator.input(curSelectEl);
eventSimulator.change(curSelectEl);
}
if (!featureDetection.isTouchDevice)
eventSimulator.click(browserUtils.isFirefox || browserUtils.isIE ? realOption : curSelectEl);
if (!isOptionDisabled)
collapseOptionList();
}
function createOption (realOption, parent) {
const option = document.createElement('div');
const isOptionDisabled = realOption.disabled || domUtils.getTagName(realOption.parentElement) === 'optgroup' &&
realOption.parentElement.disabled;
// eslint-disable-next-line no-restricted-properties
nativeMethods.nodeTextContentSetter.call(option, realOption.text);
parent.appendChild(option);
shadowUI.addClass(option, OPTION_CLASS);
if (isOptionDisabled) {
shadowUI.addClass(option, DISABLED_CLASS);
styleUtils.set(option, 'color', styleUtils.get(realOption, 'color'));
}
options.push(option);
}
function createGroup (realGroup, parent) {
const group = document.createElement('div');
nativeMethods.nodeTextContentSetter.call(group, realGroup.label || ' ');
parent.appendChild(group);
shadowUI.addClass(group, OPTION_GROUP_CLASS);
if (group.disabled) {
shadowUI.addClass(group, DISABLED_CLASS);
styleUtils.set(group, 'color', styleUtils.get(realGroup, 'color'));
}
createChildren(realGroup.children, group);
groups.push(group);
}
function createChildren (children, parent) {
const childrenLength = domUtils.getChildrenLength(children);
for (let i = 0; i < childrenLength; i++) {
if (domUtils.isOptionElement(children[i]))
createOption(children[i], parent);
else if (domUtils.getTagName(children[i]) === 'optgroup')
createGroup(children[i], parent);
}
}
export function expandOptionList (select) {
const selectChildren = select.children;
if (!selectChildren.length || select.disabled)
return;
//NOTE: check is option list expanded
if (curSelectEl) {
const isSelectExpanded = select === curSelectEl;
collapseOptionList();
if (isSelectExpanded)
return;
}
curSelectEl = select;
optionList = document.createElement('div');
uiRoot.element().appendChild(optionList);
shadowUI.addClass(optionList, OPTION_LIST_CLASS);
createChildren(selectChildren, optionList);
listeners.addInternalEventBeforeListener(window, [ 'click' ], onWindowClick);
nativeMethods.setTimeout.call(window, () => {
eventUtils.bind(document, 'mousedown', onDocumentMouseDown);
}, 0);
styleUtils.set(optionList, {
position: 'absolute',
fontSize: styleUtils.get(curSelectEl, 'fontSize'),
fontFamily: styleUtils.get(curSelectEl, 'fontFamily'),
minWidth: styleUtils.getWidth(curSelectEl) + 'px',
left: positionUtils.getOffsetPosition(curSelectEl).left + 'px',
height: domUtils.getSelectVisibleChildren(select).length > MAX_OPTION_LIST_LENGTH ?
styleUtils.getOptionHeight(select) * MAX_OPTION_LIST_LENGTH : '',
});
const selectTopPosition = positionUtils.getOffsetPosition(curSelectEl).top;
const optionListHeight = styleUtils.getHeight(optionList);
let optionListTopPosition = selectTopPosition + styleUtils.getHeight(curSelectEl) + 2;
if (optionListTopPosition + optionListHeight > styleUtils.getScrollTop(window) + styleUtils.getHeight(window)) {
const topPositionAboveSelect = selectTopPosition - 3 - optionListHeight;
if (topPositionAboveSelect >= styleUtils.getScrollTop(window))
optionListTopPosition = topPositionAboveSelect;
}
styleUtils.set(optionList, 'top', optionListTopPosition + 'px');
}
export function collapseOptionList () {
domUtils.remove(optionList);
eventUtils.unbind(document, 'mousedown', onDocumentMouseDown);
optionList = null;
curSelectEl = null;
options = [];
groups = [];
}
export function isOptionListExpanded (select) {
return select ? select === curSelectEl : !!curSelectEl;
}
export function getEmulatedChildElement (element) {
const isGroup = domUtils.getTagName(element) === 'optgroup';
const elementIndex = isGroup ? domUtils.getElementIndexInParent(curSelectEl, element) :
domUtils.getElementIndexInParent(curSelectEl, element);
if (!isGroup)
return options[elementIndex];
return groups[elementIndex];
}
export function scrollOptionListByChild (child) {
const select = domUtils.getSelectParent(child);
if (!select)
return;
const realSizeValue = styleUtils.getSelectElementSize(select);
const optionHeight = styleUtils.getOptionHeight(select);
let scrollIndent = 0;
const topVisibleIndex = Math.max(styleUtils.getScrollTop(select) / optionHeight, 0);
const bottomVisibleIndex = topVisibleIndex + realSizeValue - 1;
const childIndex = domUtils.getChildVisibleIndex(select, child);
if (childIndex < topVisibleIndex) {
scrollIndent = optionHeight * (topVisibleIndex - childIndex);
styleUtils.setScrollTop(select, Math.max(styleUtils.getScrollTop(select) - scrollIndent, 0));
}
else if (childIndex > bottomVisibleIndex) {
scrollIndent = optionHeight * (childIndex - bottomVisibleIndex);
styleUtils.setScrollTop(select, styleUtils.getScrollTop(select) + scrollIndent);
}
}
export function getSelectChildCenter (child) {
const select = domUtils.getSelectParent(child);
if (!select) {
return {
x: 0,
y: 0,
};
}
const optionHeight = styleUtils.getOptionHeight(select);
const childRectangle = positionUtils.getElementRectangle(child);
return {
x: Math.round(childRectangle.left + childRectangle.width / 2),
y: Math.round(childRectangle.top + optionHeight / 2),
};
}
export function switchOptionsByKeys (element, command) {
const selectSize = styleUtils.getSelectElementSize(element);
const optionListHidden = !styleUtils.hasDimensions(shadowUI.select('.' + OPTION_LIST_CLASS)[0]);
if (/down|up/.test(command) ||
!browserUtils.isIE && (selectSize <= 1 || browserUtils.isFirefox) &&
(optionListHidden || browserUtils.isFirefox) && /left|right/.test(command)) {
const realOptions = element.querySelectorAll('option');
const enabledOptions = [];
for (let i = 0; i < realOptions.length; i++) {
const parent = realOptions[i].parentElement;
if (!realOptions[i].disabled && !(domUtils.getTagName(parent) === 'optgroup' && parent.disabled))
enabledOptions.push(realOptions[i]);
}
const curSelectedOptionIndex = arrayUtils.indexOf(enabledOptions, realOptions[element.selectedIndex]);
const nextIndex = curSelectedOptionIndex + (/down|right/.test(command) ? 1 : -1);
if (nextIndex >= 0 && nextIndex < enabledOptions.length) {
element.selectedIndex = arrayUtils.indexOf(realOptions, enabledOptions[nextIndex]);
if (!browserUtils.isIE)
eventSimulator.input(element);
eventSimulator.change(element);
}
}
}
export function isOptionElementVisible (el) {
const parentSelect = domUtils.getSelectParent(el);
if (!parentSelect)
return true;
const expanded = isOptionListExpanded(parentSelect);
const selectSizeValue = styleUtils.getSelectElementSize(parentSelect);
return expanded || selectSizeValue > 1;
}