/
toolbar.js
161 lines (138 loc) · 3.37 KB
/
toolbar.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
import templateEngine from '@pluginjs/template'
import { prepend, find } from '@pluginjs/dom'
import { addClass } from '@pluginjs/classes'
import { bindEvent } from '@pluginjs/events'
import Filters from '@pluginjs/filters'
import Dropdown from '@pluginjs/dropdown'
class Toolbar {
constructor(instance, opts) {
this.api = instance
this.opts = opts
this.classes = this.api.classes
this.init()
}
init(update = false) {
if (update) {
find(`.${this.classes.TOOLBAR}`, this.api.element).remove()
}
if (this.opts.filters) {
this.filters = templateEngine.render(
this.api.options.templates.filters.call(this),
{
classes: this.classes
}
)
}
if (this.opts.sort) {
this.sort = templateEngine.render(
this.api.options.templates.sort.call(this),
{
classes: this.classes
}
)
}
if (this.opts.reverse) {
this.reverse = templateEngine.render(
this.api.options.templates.reverse.call(this),
{
classes: this.classes
}
)
}
this.toolbar = templateEngine.render(
this.api.options.templates.toolbar.call(this),
{
classes: this.classes,
filters: this.filters,
sort: this.sort,
reverse: this.reverse
}
)
prepend(this.toolbar, this.api.element)
this.handleFilters()
this.handleSort()
this.handleReverse()
}
handleFilters() {
if (!this.opts.filters) {
return
}
const that = this
const tags = this.api.tags
const items = [
{
text: 'all',
id: 'all'
}
]
const config = {
theme: this.api.options.filtertheme,
default: 'all',
responsive: true,
onChange(tag) {
that.api.filter(tag !== 'all' ? [tag] : [])
}
}
tags.forEach(tag => {
items.push({
text: tag,
id: tag
})
})
config.items = items
this.$filters = find(`.${this.classes.FILTERS}`, this.api.element)
Filters.of(this.$filters, config)
}
handleSort() {
if (!this.opts.sort) {
return
}
const that = this
let sorts = ['default']
const config = {
imitateSelect: true,
target: false,
select: this.api.sortby || 'default',
onChange(data) {
const sortby = data === 'default' ? '' : data
that.$sortinner.innerHTML = data
that.api.sort(sortby)
}
}
this.api.chunks.forEach(chunk => {
for (const key in chunk.sort) {
if (Object.prototype.hasOwnProperty.call(chunk.sort, key)) {
sorts.push(key)
}
}
})
sorts = Array.from(new Set(sorts))
sorts.forEach((sort, index) => {
sorts[index] = {
label: sort,
value: sort
}
})
config.data = sorts
this.$sortinner = find(`.${this.classes.SORTINNER}`, this.api.element)
this.$sortinner.innerHTML = sorts[0].label
Dropdown.of(this.$sortinner, config)
}
handleReverse() {
if (!this.opts.reverse) {
return
}
this.$reverse = find(`.${this.classes.REVERSE}`, this.api.element)
if (this.api.options.sortDirection === 'min') {
addClass(this.classes.REVERSEMIN, this.$reverse)
}
bindEvent(
this.api.eventName('click'),
() => {
this.api.reverse()
},
this.$reverse
)
}
}
export default Toolbar