-
Notifications
You must be signed in to change notification settings - Fork 2
/
Configs.js
72 lines (60 loc) · 1.79 KB
/
Configs.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
import _ from 'lodash'
import { el, createEl } from '../helpers/dom_utils'
export default class ConfigsManager {
configs = []
activeConfigIndex = 0
refreshConfigs = async () => {
const configs = await this.getConfigs()
this.configs = configs
if (!this.getActiveConfig()) {
this.setConfig(0)
}
this.refreshHtml()
}
refreshHtml = () => {
const configsContainerEl = el(this.configsContainer)
configsContainerEl.innerHTML = ''
this.configs.forEach((config, index) =>
createEl('div', {
className: 'config',
children: [
createEl('span', {
className: 'name',
innerText: config.name,
}),
index &&
createEl('i', {
className: 'fas fa-times close-icon',
onclick: () => {
window.event.preventDefault()
this.removeConfig(index)
},
}),
],
onclick: () => this.setConfig(index),
parent: configsContainerEl,
})
)
}
setConfig = (index = 0) => {
this.activeConfigIndex = index
this.onSetConfig(this.getActiveConfig())
}
getConfigByIndex = (index = 0) => {
return this.configs[index]
}
getConfigById = id => _.find(this.configs, { id })
removeConfig = index => {
const [removedConfig] = this.configs.splice(index, 1)
this.onRemoveConfig(removedConfig, index)
this.refreshHtml()
}
getActiveConfig = () => this.configs[this.activeConfigIndex]
constructor(configsContainer, { onSetConfig, getConfigs, onRemoveConfig }) {
this.configsContainer = configsContainer
// this.configs = configs
this.onSetConfig = onSetConfig || _.noop
this.onRemoveConfig = onRemoveConfig || _.noop
this.getConfigs = getConfigs || _.noop
}
}