/
Easydropdown.ts
138 lines (108 loc) · 4.38 KB
/
Easydropdown.ts
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
import merge from 'helpful-merge';
import Config from '../Config/Config';
import ICallback from '../Config/Interfaces/ICallback';
import IConfig from '../Config/Interfaces/IConfig';
import ISelectCallback from '../Config/Interfaces/ISelectCallback';
import bindEvents from '../Events/bindEvents';
import EventBinding from '../Events/EventBinding';
import Dom from '../Renderer/Dom';
import Renderer from '../Renderer/Renderer';
import dispatchOpen from '../Shared/Util/dispatchOpen';
import pollForSelectChange from '../Shared/Util/pollForSelectChange';
import pollForSelectMutation from '../Shared/Util/pollForSelectMutation';
import closeOthers from '../State/InjectedActions/closeOthers';
import scrollToView from '../State/InjectedActions/scrollToView';
import IActions from '../State/Interfaces/IActions';
import State from '../State/State';
import StateManager from '../State/StateManager';
import StateMapper from '../State/StateMapper';
import cache from './cache';
import Timers from './Timers';
class Easydropdown {
public actions: IActions;
private config: Config;
private state: State;
private dom: Dom;
private eventBindings: EventBinding[];
private renderer: Renderer;
private timers: Timers;
constructor(selectElement: HTMLSelectElement, options: IConfig) {
this.config = merge(new Config(), options, true);
this.state = StateMapper.mapFromSelect(selectElement, this.config);
this.renderer = new Renderer(this.config.classNames);
this.dom = this.renderer.render(this.state, selectElement);
this.timers = new Timers();
this.actions = StateManager.proxyActions(this.state, {
closeOthers: closeOthers.bind(null, this, cache),
scrollToView: scrollToView.bind(null, this.dom, this.timers)
}, this.handleStateUpdate.bind(this));
this.eventBindings = bindEvents({
actions: this.actions,
config: this.config,
dom: this.dom,
state: this.state,
timers: this.timers
});
this.timers.pollChangeIntervalId = pollForSelectChange(this.dom.select, this.state, this.actions);
if (this.config.behavior.liveUpdates) {
this.timers.pollMutationIntervalId = pollForSelectMutation(
this.dom.select,
this.state,
this.refresh.bind(this)
);
}
}
public get selectElement(): HTMLSelectElement {
return this.dom.select;
}
public get value(): string {
return this.state.value;
}
public set value(nextValue: string) {
if (typeof nextValue !== 'string') {
throw new TypeError('[EasyDropDown] Provided value not a valid string');
}
this.dom.select.value = nextValue;
}
public open(): void {
dispatchOpen(this.actions, this.config, this.dom);
}
public close(): void {
this.actions.close();
}
public refresh(): void {
this.state = merge(
this.state,
StateMapper.mapFromSelect(this.dom.select, this.config)
);
this.renderer.update(this.state);
this.dom.group.length = this.dom.option.length = this.dom.item.length = 0;
Renderer.queryDomRefs(this.dom, ['group', 'option', 'item']);
}
public destroy(): void {
this.timers.clear();
this.eventBindings.forEach(binding => binding.unbind());
this.renderer.destroy();
const cacheIndex = cache.indexOf(this);
cache.splice(cacheIndex, 1);
}
private handleStateUpdate(state: State, key: keyof State): void {
const {callbacks} = this.config;
let cb: ICallback;
this.renderer.update(state, key);
switch (key) {
case 'bodyStatus':
if (state.isOpen) {
cb = callbacks.onOpen;
} else {
cb = callbacks.onClose;
}
if (typeof cb === 'function') cb();
break;
case 'selectedIndex':
cb = callbacks.onSelect;
if (typeof cb === 'function') (cb as ISelectCallback)(state.value);
}
}
}
export default Easydropdown;