/
select-value-observer.js
172 lines (153 loc) · 4.52 KB
/
select-value-observer.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
import {subscriberCollection} from './subscriber-collection';
import {DOM} from 'aurelia-pal';
const selectArrayContext = 'SelectValueObserver:array'
@subscriberCollection()
export class SelectValueObserver {
constructor(element, handler, observerLocator){
this.element = element;
this.handler = handler;
this.observerLocator = observerLocator;
}
getValue() {
return this.value;
}
setValue(newValue) {
if (newValue !== null && newValue !== undefined && this.element.multiple && !Array.isArray(newValue)) {
throw new Error('Only null or Array instances can be bound to a multi-select.')
}
if (this.value === newValue) {
return;
}
// unsubscribe from old array.
if (this.arrayObserver) {
this.arrayObserver.unsubscribe(selectArrayContext, this);
this.arrayObserver = null;
}
// subscribe to new array.
if (Array.isArray(newValue)) {
this.arrayObserver = this.observerLocator.getArrayObserver(newValue);
this.arrayObserver.subscribe(selectArrayContext, this);
}
// assign and sync element.
this.oldValue = this.value;
this.value = newValue;
this.synchronizeOptions();
this.notify();
// queue up an initial sync after the bindings have been evaluated.
if (!this.initialSync) {
this.initialSync = true;
this.observerLocator.taskQueue.queueMicroTask(this);
}
}
call(context, splices) {
// called by task queue and array observer.
this.synchronizeOptions();
}
synchronizeOptions() {
let value = this.value, clear, isArray;
if (value === null || value === undefined) {
clear = true;
} else if (Array.isArray(value)) {
isArray = true;
}
let options = this.element.options;
let i = options.length;
let matcher = this.element.matcher || ((a, b) => a === b);
while(i--) {
let option = options.item(i);
if (clear) {
option.selected = false;
continue;
}
let optionValue = option.hasOwnProperty('model') ? option.model : option.value;
if (isArray) {
option.selected = !!value.find(item => !!matcher(optionValue, item));
continue;
}
option.selected = !!matcher(optionValue, value);
}
}
synchronizeValue() {
let options = this.element.options,
count = 0,
value = [];
for (let i = 0, ii = options.length; i < ii; i++) {
let option = options.item(i);
if (!option.selected) {
continue;
}
value.push(option.hasOwnProperty('model') ? option.model : option.value);
count++;
}
if (this.element.multiple) {
// multi-select
if (Array.isArray(this.value)) {
let matcher = this.element.matcher || ((a, b) => a === b);
// remove items that are no longer selected.
let i = 0;
while (i < this.value.length) {
let a = this.value[i];
if (value.findIndex(b => matcher(a, b)) === -1) {
this.value.splice(i, 1);
} else {
i++;
}
}
// add items that have been selected.
i = 0;
while (i < value.length) {
let a = value[i];
if (this.value.findIndex(b => matcher(a, b)) === -1) {
this.value.push(a);
}
i++;
}
return; // don't notify.
}
} else {
// single-select
if (count === 0) {
value = null;
} else {
value = value[0];
}
}
if (value !== this.value) {
this.oldValue = this.value;
this.value = value;
this.notify();
}
}
notify() {
let oldValue = this.oldValue;
let newValue = this.value;
this.callSubscribers(newValue, oldValue);
}
subscribe(context, callable) {
if (!this.hasSubscribers()) {
this.disposeHandler = this.handler.subscribe(this.element, this.synchronizeValue.bind(this, false));
}
this.addSubscriber(context, callable);
}
unsubscribe(context, callable) {
if (this.removeSubscriber(context, callable) && !this.hasSubscribers()) {
this.disposeHandler();
this.disposeHandler = null;
}
}
bind() {
this.domObserver = DOM.createMutationObserver(() => {
this.synchronizeOptions();
this.synchronizeValue();
});
this.domObserver.observe(this.element, { childList: true, subtree: true });
}
unbind() {
this.domObserver.disconnect();
this.domObserver = null;
if (this.arrayObserver) {
this.arrayObserver.unsubscribe(selectArrayContext, this);
this.arrayObserver = null;
}
}
}