forked from shentao/vue-multiselect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointerMixin.js
More file actions
144 lines (141 loc) · 4.02 KB
/
pointerMixin.js
File metadata and controls
144 lines (141 loc) · 4.02 KB
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
export default {
data () {
return {
pointer: 0,
pointerDirty: false
}
},
props: {
/**
* Enable/disable highlighting of the pointed value.
* @type {Boolean}
* @default true
*/
showPointer: {
type: Boolean,
default: true
},
optionHeight: {
type: Number,
default: 40
}
},
computed: {
pointerPosition () {
return this.pointer * this.optionHeight
},
visibleElements () {
return this.optimizedHeight / this.optionHeight
}
},
watch: {
filteredOptions () {
this.pointerAdjust()
},
isOpen () {
this.pointerDirty = false
},
pointer () {
this.$refs.search && this.$refs.search.setAttribute('aria-activedescendant', this.id + '-' + this.pointer.toString())
}
},
methods: {
optionHighlight (index, option) {
return {
'multiselect__option--highlight': index === this.pointer && this.showPointer,
'multiselect__option--selected': this.isSelected(option)
}
},
groupHighlight (index, selectedGroup) {
if (!this.groupSelect) {
return [
'multiselect__option--disabled',
{'multiselect__option--group': selectedGroup.$isLabel}
]
}
const group = this.options.find((option) => {
return option[this.groupLabel] === selectedGroup.$groupLabel
})
return group && !this.wholeGroupDisabled(group) ? [
'multiselect__option--group',
{'multiselect__option--highlight': index === this.pointer && this.showPointer},
{'multiselect__option--group-selected': this.wholeGroupSelected(group)}
] : 'multiselect__option--disabled'
},
addPointerElement ({key} = 'Enter') {
/* istanbul ignore else */
if (this.filteredOptions.length > 0) {
this.select(this.filteredOptions[this.pointer], key)
}
this.pointerReset()
},
pointerForward () {
/* istanbul ignore else */
if (this.pointer < this.filteredOptions.length - 1) {
this.pointer++
/* istanbul ignore next */
if (this.$refs.list.scrollTop <= this.pointerPosition - (this.visibleElements - 1) * this.optionHeight) {
this.$refs.list.scrollTop = this.pointerPosition - (this.visibleElements - 1) * this.optionHeight
}
/* istanbul ignore else */
if (
this.filteredOptions[this.pointer] &&
this.filteredOptions[this.pointer].$isLabel &&
!this.groupSelect
) this.pointerForward()
}
this.pointerDirty = true
},
pointerBackward () {
if (this.pointer > 0) {
this.pointer--
/* istanbul ignore else */
if (this.$refs.list.scrollTop >= this.pointerPosition) {
this.$refs.list.scrollTop = this.pointerPosition
}
/* istanbul ignore else */
if (
this.filteredOptions[this.pointer] &&
this.filteredOptions[this.pointer].$isLabel &&
!this.groupSelect
) this.pointerBackward()
} else {
/* istanbul ignore else */
if (
this.filteredOptions[this.pointer] &&
this.filteredOptions[0].$isLabel &&
!this.groupSelect
) this.pointerForward()
}
this.pointerDirty = true
},
pointerReset () {
/* istanbul ignore else */
if (!this.closeOnSelect) return
this.pointer = 0
/* istanbul ignore else */
if (this.$refs.list) {
this.$refs.list.scrollTop = 0
}
},
pointerAdjust () {
/* istanbul ignore else */
if (this.pointer >= this.filteredOptions.length - 1) {
this.pointer = this.filteredOptions.length
? this.filteredOptions.length - 1
: 0
}
if (this.filteredOptions.length > 0 &&
this.filteredOptions[this.pointer] &&
this.filteredOptions[this.pointer].$isLabel &&
!this.groupSelect
) {
this.pointerForward()
}
},
pointerSet (index) {
this.pointer = index
this.pointerDirty = true
}
}
}