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