2019-04-04 14:54:14 +11:00
|
|
|
import List from '../list/list.vue'
|
2019-04-07 03:16:26 +10:00
|
|
|
import Checkbox from '../checkbox/checkbox.vue'
|
2019-04-04 12:43:24 +11:00
|
|
|
|
2019-04-04 13:38:48 +11:00
|
|
|
const SelectableList = {
|
2019-04-04 12:43:24 +11:00
|
|
|
components: {
|
2019-04-04 14:54:14 +11:00
|
|
|
List,
|
2019-04-04 12:43:24 +11:00
|
|
|
Checkbox
|
|
|
|
},
|
2019-04-04 19:32:36 +11:00
|
|
|
props: {
|
|
|
|
items: {
|
|
|
|
type: Array,
|
|
|
|
default: () => []
|
|
|
|
},
|
|
|
|
getKey: {
|
|
|
|
type: Function,
|
|
|
|
default: item => item.id
|
|
|
|
}
|
|
|
|
},
|
2019-04-04 14:26:13 +11:00
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
selected: []
|
|
|
|
}
|
|
|
|
},
|
2019-04-04 19:32:36 +11:00
|
|
|
computed: {
|
2019-04-07 04:00:39 +10:00
|
|
|
allKeys () {
|
|
|
|
return this.items.map(this.getKey)
|
|
|
|
},
|
|
|
|
filteredSelected () {
|
|
|
|
return this.allKeys.filter(key => this.selected.indexOf(key) !== -1)
|
|
|
|
},
|
2019-04-04 19:32:36 +11:00
|
|
|
allSelected () {
|
2019-04-07 04:00:39 +10:00
|
|
|
return this.filteredSelected.length === this.items.length
|
2019-04-07 03:45:28 +10:00
|
|
|
},
|
|
|
|
noneSelected () {
|
2019-04-07 04:00:39 +10:00
|
|
|
return this.filteredSelected.length === 0
|
2019-04-07 03:45:28 +10:00
|
|
|
},
|
|
|
|
someSelected () {
|
|
|
|
return !this.allSelected && !this.noneSelected
|
2019-04-04 19:32:36 +11:00
|
|
|
}
|
|
|
|
},
|
2019-04-04 14:26:13 +11:00
|
|
|
methods: {
|
2019-04-04 19:32:36 +11:00
|
|
|
isSelected (item) {
|
2019-04-07 04:00:39 +10:00
|
|
|
return this.filteredSelected.indexOf(this.getKey(item)) !== -1
|
2019-04-04 19:32:36 +11:00
|
|
|
},
|
|
|
|
toggle (checked, item) {
|
|
|
|
const key = this.getKey(item)
|
2019-04-04 15:22:55 +11:00
|
|
|
const oldChecked = this.isSelected(key)
|
2019-04-04 14:26:13 +11:00
|
|
|
if (checked !== oldChecked) {
|
|
|
|
if (checked) {
|
|
|
|
this.selected.push(key)
|
|
|
|
} else {
|
|
|
|
this.selected.splice(this.selected.indexOf(key), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-04-04 19:32:36 +11:00
|
|
|
toggleAll (value) {
|
|
|
|
if (value) {
|
2019-04-07 04:00:39 +10:00
|
|
|
this.selected = this.allKeys.slice(0)
|
2019-04-04 19:32:36 +11:00
|
|
|
} else {
|
|
|
|
this.selected = []
|
|
|
|
}
|
2019-04-04 13:48:00 +11:00
|
|
|
}
|
2019-04-04 12:43:24 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 13:38:48 +11:00
|
|
|
export default SelectableList
|