| 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 |
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
| import {
inject,
bindable,
DOM,
ElementEvents,
PLATFORM,
ViewCompiler,
ViewResources,
customElement,
Disposable,
BindingEngine,
processAttributes,
} from 'aurelia-framework';
import {
PaperRipple
} from '@aurelia-ux/core';
import { getAuViewModel } from './util';
export interface UxOptionElement extends HTMLElement {
disabled: boolean;
focused: boolean;
nodeName: 'UX-OPTION';
selected: boolean;
ripple: PaperRipple;
value: any;
wave(): void;
}
export interface UxOptionSelectEvent extends Event {
target: UxOptionElement;
}
@inject(DOM.Element, BindingEngine)
@customElement('ux-option')
@processAttributes(convertTextToAttr)
export class UxOption {
private selected: boolean = false;
private subscriptions: Disposable[];
public disabled: boolean;
public parentDisabled: boolean;
public isDisabled: boolean;
public focused: boolean = false;
public isMultiple: boolean;
// populated by aurelia
public readonly textEl: HTMLElement;
@bindable()
public text: string;
@bindable()
public value: any;
constructor(
public readonly element: UxOptionElement,
private bindingEngine: BindingEngine
) {
Object.setPrototypeOf(element, UxOptionElementProto);
}
public created() {
const element = this.element;
if (element.hasAttribute('value')) {
this.value = element.getAttribute('value');
}
this.setDisabled(element.hasAttribute('disabled'));
element.removeAttribute('disabled');
element.removeAttribute('text');
}
public bind() {
if (this.value === undefined) {
this.value = this.text;
}
}
public attached() {
const optGroup = this.optGroup = this.getOptGroup();
const uxSelect = this.uxSelect = this.getUxSelect();
const bindingEngine = this.bindingEngine;
this.setParentDisabled(optGroup ? optGroup.isDisabled : uxSelect.isDisabled);
this.isMultiple = uxSelect.isMultiple;
this.subscriptions = [
bindingEngine.propertyObserver(uxSelect, 'isMultiple').subscribe(this.uxMultipleChanged.bind(this)),
optGroup
// ux-opt group will also subscribe to ux-select to know if it's disabled
? bindingEngine.propertyObserver(optGroup, 'isDisabled').subscribe(this.setParentDisabled.bind(this))
// If ux-option is not a member of a group, then subscribe to disabled state of ux-select
: bindingEngine.propertyObserver(uxSelect, 'isDisabled').subscribe(this.setParentDisabled.bind(this))
];
}
public detached() {
for (const s of this.subscriptions) {
s.dispose();
}
this.subscriptions.length = 0;
}
private getOptGroup() {
let el: HTMLElement | null = this.element;
while (el) {
if (el.tagName === 'UX-OPTGROUP') {
return getAuViewModel<UxOption['optGroup']>(el);
}
el = el.parentElement;
}
return null;
}
private getUxSelect() {
let el: HTMLElement | null = this.element;
while (el) {
if (el.tagName === 'UX-SELECT') {
return getAuViewModel<UxOption['uxSelect']>(el);
}
el = el.parentElement;
}
throw new Error('Ux option has no "ux-select" parent');
}
private uxMultipleChanged(useSelect: boolean) {
this.isMultiple = useSelect;
}
private setParentDisabled(disabled: boolean) {
this.parentDisabled = !!disabled;
this.isDisabled = this.disabled || this.parentDisabled;
}
private notify() {
this.element.dispatchEvent(DOM.createCustomEvent('select', { bubbles: true, detail: this.element }));
}
public getFocused() {
return this.focused;
}
public setFocused(focused: boolean) {
this.focused = !!focused;
}
public getSelected() {
return this.selected;
}
public setSelected(selected: boolean) {
const oldValue = this.selected;
const newValue = !!selected;
if (newValue !== oldValue) {
this.selected = newValue;
this.notify();
}
}
public getDisabled() {
return this.disabled || this.parentDisabled;
}
public setDisabled(disabled: boolean) {
this.disabled = !!disabled;
this.isDisabled = this.disabled || this.parentDisabled;
}
public onClick() {
if (!this.disabled) {
if (this.isMultiple) {
this.setSelected(!this.selected);
} else {
this.selected = true;
this.notify();
}
}
}
public onMouseDown(e: MouseEvent) {
this.addWave(e);
return true;
}
/**
* @param autoEnd Internal flag to distinguish between keyboard navigation and mouse
*/
public addWave(e?: MouseEvent | null, autoEnd?: boolean) {
const target = this.element;
if (target.classList.contains('ripple')) {
if (target.ripple === null || target.ripple === undefined) {
target.ripple = new PaperRipple();
target.appendChild(target.ripple.$);
}
target.ripple.downAction(e!);
if (autoEnd) {
setTimeout(removeWave, 125, target);
} else {
new ElementEvents(PLATFORM.global).subscribeOnce('mouseup', () => {
target.ripple.upAction();
}, true);
}
}
}
}
function removeWave(el: UxOptionElement) {
el.ripple.upAction();
}
function convertTextToAttr(_: ViewCompiler, __: ViewResources, node: UxOptionElement, attributes: NamedNodeMap) {
const ii = attributes.length;
for (let i = 0; ii > i; ++i) {
const attr = attributes[i];
if (attr.nodeName === 'text') {
return;
}
const parts = attr.nodeName.split('.');
if (parts[0] === 'text') {
return;
}
}
node.setAttribute('text', node.textContent || '');
node.textContent = '';
}
const UxOptionElementProto = Object.create(HTMLElement.prototype, {
disabled: {
get() {
return getAuViewModel<UxOption>(this).getDisabled();
},
set(disabled: boolean) {
getAuViewModel<UxOption>(this).setDisabled(disabled);
}
},
focused: {
get() {
return getAuViewModel<UxOption>(this).getFocused();
},
set(focused: boolean) {
getAuViewModel<UxOption>(this).setFocused(focused);
}
},
selected: {
get() {
return getAuViewModel<UxOption>(this).getSelected();
},
set(selected: boolean) {
getAuViewModel<UxOption>(this).setSelected(selected);
}
},
value: {
get() {
return getAuViewModel<UxOption>(this).value;
},
set(value: any) {
getAuViewModel<UxOption>(this).value = value;
}
},
wave: {
value(): void {
getAuViewModel<UxOption>(this).addWave(null, true);
}
}
});
|