forked from vuematerial/vue-material
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MdTable.vue
425 lines (386 loc) · 11 KB
/
MdTable.vue
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<template>
<md-tag-switcher :md-tag="contentTag" class="md-table">
<slot name="md-table-toolbar" />
<keep-alive>
<md-table-alternate-header v-if="$scopedSlots['md-table-alternate-header'] && selectedCount">
<slot name="md-table-alternate-header" :count="selectedCount" />
</md-table-alternate-header>
</keep-alive>
<div class="md-table-fixed-header" :class="headerClasses" :style="headerStyles" v-if="mdFixedHeader">
<div class="md-table-fixed-header-container" ref="fixedHeaderContainer" @scroll="setHeaderScroll">
<table :style="fixedHeaderTableStyles">
<md-table-thead />
</table>
</div>
</div>
<md-content class="md-table-content md-scrollbar" :class="contentClasses" :style="contentStyles" @scroll="setScroll">
<table ref="contentTable">
<md-table-thead :class="headerClasses" v-if="!mdFixedHeader && $scopedSlots['md-table-row']" />
<tbody v-if="!$scopedSlots['md-table-row']">
<slot />
</tbody>
<tbody v-else-if="value.length">
<md-table-row-ghost
v-for="(item, index) in value"
:key="getRowId(item, mdModelId)"
:md-id="getRowId(item, mdModelId)"
:md-index="index"
:md-item="item">
<slot name="md-table-row" :item="item" />
</md-table-row-ghost>
</tbody>
<tbody v-else-if="$scopedSlots['md-table-empty-state']">
<tr>
<td :colspan="headerCount">
<slot name="md-table-empty-state" />
</td>
</tr>
</tbody>
</table>
<slot name="md-table-pagination" />
</md-content>
<slot v-if="!hasValue && $scopedSlots['md-table-row']" />
</md-tag-switcher>
</template>
<script>
import raf from 'raf'
import MdTagSwitcher from 'components/MdTagSwitcher/MdTagSwitcher'
import MdUuid from 'core/utils/MdUuid'
import MdPropValidator from 'core/utils/MdPropValidator'
import MdTableThead from './MdTableThead'
import MdTableAlternateHeader from './MdTableAlternateHeader'
import MdTableRow from './MdTableRow'
import MdTableRowGhost from './MdTableRowGhost'
import MdTableCellSelection from './MdTableCellSelection'
import MdResizeObserver from 'core/utils/MdResizeObserver'
const getObjectAttribute = (object, key) => {
let value = object
for (const attribute of key.split('.')) {
value = value[attribute]
}
return value
}
export default {
name: 'MdTable',
components: {
MdTagSwitcher,
MdTableAlternateHeader,
MdTableThead,
MdTableRow,
MdTableRowGhost,
MdTableCellSelection
},
props: {
value: [Array, Object],
mdModelId: {
type: String,
default: 'id'
},
mdCard: Boolean,
mdFixedHeader: Boolean,
mdHeight: {
type: [Number, String],
default: 400
},
mdSort: String,
mdSortOrder: {
type: String,
default: 'asc',
...MdPropValidator('md-sort-order', ['asc', 'desc'])
},
mdSortFn: {
type: Function,
default (value) {
return value.sort((a, b) => {
const sortBy = this.MdTable.sort
const aAttr = getObjectAttribute(a, sortBy)
const bAttr = getObjectAttribute(b, sortBy)
const isAsc = this.MdTable.sortOrder === 'asc'
let isNumber = typeof aAttr === 'number'
if (isNumber) {
return isAsc ? (aAttr - bAttr) : (bAttr - aAttr)
}
return isAsc ?
aAttr.localeCompare(bAttr) :
bAttr.localeCompare(aAttr)
})
}
},
mdSelectedValue: {
type: [Array, Object]
}
},
data () {
return {
windowResizeObserver: null,
fixedHeaderTableWidth: 0,
fixedHeaderPadding: 0,
hasContentScroll: false,
MdTable: {
items: {},
sort: null,
sortOrder: null,
singleSelection: null,
selectedItems: [],
selectable: [],
fixedHeader: null,
contentPadding: null,
contentEl: null,
// computed
hasValue: this.hasValue,
// methods
emitEvent: this.emitEvent,
sortTable: this.sortTable,
manageItemSelection: this.manageItemSelection,
getModel: this.getModel,
getModelItem: this.getModelItem,
selectingMode: null
},
itemsUuidMap: new WeakMap()
}
},
computed: {
contentTag () {
if (this.mdCard) {
return 'md-card'
}
return 'md-content'
},
headerCount () {
return Object.keys(this.MdTable.items).length
},
selectedCount () {
return this.MdTable.selectedItems.length
},
headerStyles () {
if (this.mdFixedHeader) {
return `padding-right: ${this.fixedHeaderPadding}px`
}
},
hasValue () {
return this.value && this.value.length !== 0
},
headerClasses () {
if ((this.mdFixedHeader && this.hasContentScroll) || !this.hasValue) {
return 'md-table-fixed-header-active'
}
},
contentStyles () {
if (this.mdFixedHeader) {
const height = typeof this.mdHeight === 'number'
? `${this.mdHeight}px`
: this.mdHeight
return `height: ${height};max-height: ${height}`
}
},
contentClasses () {
if (this.mdFixedHeader && this.value.length === 0) {
return `md-table-empty`
}
},
fixedHeaderTableStyles () {
return {
width: this.fixedHeaderTableWidth + 'px'
}
}
},
provide () {
const MdTable = this.MdTable
return { MdTable }
},
watch: {
mdSort: {
immediate: true,
handler () {
this.MdTable.sort = this.mdSort
}
},
mdSortOrder: {
immediate: true,
handler () {
this.MdTable.sortOrder = this.mdSortOrder
}
},
mdFixedHeader: {
immediate: true,
handler () {
this.MdTable.fixedHeader = this.mdFixedHeader
}
},
hasValue: {
immediate: true,
handler () {
this.MdTable.hasValue = this.hasValue
}
},
'MdTable.selectedItems' (val, old) {
let changed = (() => {
let isValEmpty = this.isEmpty(val)
let isOldEmpty = this.isEmpty(old)
let hasValues = isValEmpty && isOldEmpty
if (hasValues) {
return false
} else if (!hasValues) {
return (val.length !== old.length) ? true : !val.every((item, index) => item == old[index])
}
return true
})()
if (changed) {
this.select(val)
}
},
'MdTable.singleSelection' (val, old) {
if (val != old) {
this.select(val)
}
},
mdSelectedValue () {
this.syncSelectedValue()
},
value () {
this.syncSelectedValue()
}
},
methods: {
isEmpty (value) {
return !value || value.length === 0
},
emitEvent (eventName, value) {
this.$emit(eventName, value)
},
getRowId (item, propertyName) {
let id = item[propertyName]
if (id) {
return id
}
id = this.itemsUuidMap.get(item)
if (!id) {
id = 'md-row-' + MdUuid()
this.itemsUuidMap.set(item, id)
}
return id
},
setScroll ($event) {
raf(() => {
if (this.mdFixedHeader) {
this.$refs.fixedHeaderContainer.scrollLeft = $event.target.scrollLeft
}
this.hasContentScroll = $event.target.scrollTop > 0
})
},
setHeaderScroll ($event) {
raf(() => {
this.MdTable.contentEl.scrollLeft = $event.target.scrollLeft
})
},
getContentEl () {
return this.$el.querySelector('.md-table-content')
},
setContentEl () {
this.MdTable.contentEl = this.getContentEl()
},
setHeaderPadding () {
this.setContentEl()
const { contentEl } = this.MdTable
const tableEl = contentEl.childNodes[0]
this.fixedHeaderPadding = contentEl.offsetWidth - tableEl.offsetWidth
},
getModel () {
return this.value
},
getModelItem (index) {
return this.value[index]
},
manageItemSelection (item) {
if (this.MdTable.selectedItems.includes(item)) {
this.MdTable.selectedItems = this.MdTable.selectedItems.filter(target => target !== item)
} else {
this.MdTable.selectedItems = this.MdTable.selectedItems.concat([item])
}
},
sortTable () {
if (Array.isArray(this.value)) {
this.$emit('input', this.mdSortFn(this.value))
}
},
select (val) {
this.$emit('update:mdSelectedValue', val)
this.$emit('md-selected', val)
},
syncSelectedValue: async function () {
await this.$nextTick() // render the table first
if (this.MdTable.selectingMode === 'single') {
this.MdTable.singleSelection = this.mdSelectedValue
} else if (this.MdTable.selectingMode === 'multiple') {
this.MdTable.selectedItems = this.mdSelectedValue || []
}
},
setWidth () {
if (this.mdFixedHeader) {
this.fixedHeaderTableWidth = this.$refs.contentTable.offsetWidth
}
}
},
created () {
if (this.mdSort) {
this.sortTable()
}
this.$nextTick().then(() => {
this.syncSelectedValue()
})
},
mounted () {
this.setContentEl()
this.$nextTick().then(this.setWidth)
if (this.mdFixedHeader) {
this.setHeaderPadding()
this.windowResizeObserver = new MdResizeObserver(window, this.setWidth)
}
},
beforeDestroy () {
if (this.windowResizeObserver) {
this.windowResizeObserver.destroy()
}
}
}
</script>
<style lang="scss">
@import "~components/MdAnimation/variables";
.md-table {
display: flex;
flex-flow: column wrap;
overflow-x: auto;
.md-table-fixed-header {
position: relative;
.md-table-fixed-header-container {
-webkit-box-flex: 1;
flex: 1;
overflow-x: auto;
&::-webkit-scrollbar,
&::-webkit-scrollbar-thumb,
&::-webkit-scrollbar-button {
display: none;
}
}
}
.md-table-fixed-header-active {
border-bottom: 1px solid;
}
.md-table-content {
flex: 1;
overflow-x: auto;
transition: height .3s $md-transition-default-timing;
}
.md-table-empty {
display: flex;
align-items: center;
justify-content: center;
}
table {
width: 100%;
border-spacing: 0;
border-collapse: collapse;
overflow: hidden;
}
}
</style>