-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathcomponent.js
187 lines (156 loc) · 5.63 KB
/
component.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
/* eslint-disable ember/classic-decorator-no-classic-methods, ember/no-classic-components, ember/no-computed-properties-in-native-classes, prettier/prettier */
import Component from '@ember/component';
import template from './template';
import { tagName, layout } from '@ember-decorators/component';
import { computed, action } from '@ember/object';
import { next, scheduleOnce } from '@ember/runloop';
import { nextTick } from 'ember-css-transitions/utils/transition-utils';
import { ESCAPE, LEFT_ARROW, UP_ARROW, RIGHT_ARROW, DOWN_ARROW, ENTER } from 'ember-paper/utils/key-constants';
import { advanceSelectableOption } from 'ember-power-select/utils/group-utils';
import { getOwner } from '@ember/application';
import ebdGetParent from 'ember-paper/utils/ebd-get-parent';
function waitForAnimations(element, callback) {
let computedStyle = window.getComputedStyle(element);
if (computedStyle.transitionDuration && computedStyle.transitionDuration !== '0s') {
let eventCallback = function() {
element.removeEventListener('transitionend', eventCallback);
callback();
};
element.addEventListener('transitionend', eventCallback);
} else if (computedStyle.animationName !== 'none' && computedStyle.animationPlayState === 'running') {
let eventCallback = function() {
element.removeEventListener('animationend', eventCallback);
callback();
};
element.addEventListener('animationend', eventCallback);
} else {
callback();
}
}
@tagName('')
@layout(template)
class PaperSelectEbdContent extends Component {
@computed('otherStyles', 'isActive')
get customStyles() {
if (this.isActive) {
return {};
} else {
return this.otherStyles;
}
}
@computed('destination')
get destinationElement() {
return document.getElementById(this.destination);
}
@action
animateIn(dropdownElement) {
next(() => {
scheduleOnce('afterRender', this, this.afterAnimateIn, dropdownElement);
});
}
afterAnimateIn(dropdownElement) {
this.dropdown.actions.reposition();
this.set('isActive', true);
this.focusItem(dropdownElement);
}
@action
async animateOut(dropdownElement) {
let parentElement = this.renderInPlace ? dropdownElement.parentElement.parentElement : dropdownElement.parentElement;
// workaround for https://github.com/adopted-ember-addons/ember-paper/issues/1166
if (!parentElement) {
parentElement = ebdGetParent(getOwner(this));
}
let clone = dropdownElement.cloneNode(true);
clone.id = `${clone.id}--clone`;
parentElement.appendChild(clone);
clone.children[0].children[0].scrollTop = dropdownElement.children[0].children[0].scrollTop;
await nextTick();
if (!this.isDestroyed) {
this.set('isActive', false);
clone.classList.add('md-leave');
waitForAnimations(clone, function() {
clone.classList.remove('md-active');
parentElement.removeChild(clone);
});
} else {
parentElement.removeChild(clone);
}
}
@action
focusItem(element) {
let focusTarget = element.querySelector('md-option[aria-selected="true"]');
// default to first not disabled option
if (!focusTarget) {
focusTarget = element.querySelector('md-option:not([aria-disabled="true"])');
}
if (focusTarget) {
focusTarget.focus();
}
}
@action
handleKeyDown(ev) {
switch (ev.which) {
case ESCAPE: {
this.dropdown.actions.close();
break;
}
case LEFT_ARROW:
case UP_ARROW: {
ev.preventDefault();
let newHighlighted = advanceSelectableOption(this.select.results, this.select.highlighted, -1);
this.select.actions.highlight(newHighlighted, ev);
this.select.actions.scrollTo(newHighlighted);
this.focusOption(ev, -1);
break;
}
case RIGHT_ARROW:
case DOWN_ARROW: {
ev.preventDefault();
let newHighlighted = advanceSelectableOption(this.select.results, this.select.highlighted, 1);
this.select.actions.highlight(newHighlighted, ev);
this.select.actions.scrollTo(newHighlighted);
this.focusOption(ev, 1);
break;
}
case ENTER: {
ev.preventDefault();
this.select.actions.choose(this.select.highlighted);
break;
}
}
}
focusOption(e, direction) {
let focusTarget = e.target.closest('md-option');
do {
if (direction > 0) {
focusTarget = focusTarget.nextElementSibling;
} else {
focusTarget = focusTarget.previousElementSibling;
}
} while (focusTarget && !isFocusable(focusTarget));
if (focusTarget) {
focusTarget.focus();
}
}
shouldReposition(mutations) {
let shouldReposition = false;
shouldReposition = Array.prototype.slice.call(mutations[0].addedNodes).some((node) => {
if (node.classList) {
return !node.classList.contains('md-ripple') && (node.nodeName !== '#comment') && !(node.nodeName === '#text' && node.nodeValue === '');
}
return false;
});
shouldReposition = shouldReposition || Array.prototype.slice.call(mutations[0].removedNodes).some((node) => {
if (node.classList) {
return !node.classList.contains('md-ripple') && (node.nodeName !== '#comment') && !(node.nodeName === '#text' && node.nodeValue === '');
}
return false;
});
return shouldReposition;
}
}
function isFocusable(el) {
// is a menu-item, doesn't have tabindex -1 and is not disabled
return el && el.tagName === 'MD-OPTION' && el.getAttribute('tabindex') !== -1 && el.getAttribute('disabled') === null && el.getAttribute('aria-disabled') !== true;
}
export default PaperSelectEbdContent;