2019-08-14 09:48:44 +00:00
|
|
|
<template>
|
2019-11-17 08:45:05 +00:00
|
|
|
<ul
|
|
|
|
v-if="cannedMessages.length"
|
|
|
|
id="canned-list"
|
|
|
|
class="vertical dropdown menu canned"
|
|
|
|
:style="{ top: getTopPadding() + 'rem' }"
|
|
|
|
>
|
2019-08-14 09:48:44 +00:00
|
|
|
<li
|
|
|
|
v-for="(item, index) in cannedMessages"
|
|
|
|
:id="`canned-${index}`"
|
2019-11-17 08:45:05 +00:00
|
|
|
:key="item.short_code"
|
|
|
|
:class="{ active: index === selectedIndex }"
|
|
|
|
@click="onListItemSelection(index)"
|
|
|
|
@mouseover="onHover(index)"
|
2019-08-14 09:48:44 +00:00
|
|
|
>
|
2019-11-17 08:45:05 +00:00
|
|
|
<a class="text-truncate">
|
|
|
|
<strong>{{ item.short_code }}</strong> - {{ item.content }}
|
|
|
|
</a>
|
2019-08-14 09:48:44 +00:00
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import { mapGetters } from 'vuex';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
props: ['onKeyenter', 'onClick'],
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
selectedIndex: 0,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
...mapGetters({
|
|
|
|
cannedMessages: 'getCannedResponses',
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
document.addEventListener('keydown', this.keyListener);
|
|
|
|
},
|
2019-11-17 08:45:05 +00:00
|
|
|
beforeDestroy() {
|
|
|
|
document.removeEventListener('keydown', this.keyListener);
|
|
|
|
},
|
2019-08-14 09:48:44 +00:00
|
|
|
methods: {
|
|
|
|
getTopPadding() {
|
|
|
|
if (this.cannedMessages.length <= 4) {
|
|
|
|
return -this.cannedMessages.length * 3.5;
|
|
|
|
}
|
|
|
|
return -14;
|
|
|
|
},
|
|
|
|
isUp(e) {
|
2019-11-17 08:45:05 +00:00
|
|
|
return e.keyCode === 38 || (e.ctrlKey && e.keyCode === 80); // UP, Ctrl-P
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
isDown(e) {
|
2019-11-17 08:45:05 +00:00
|
|
|
return e.keyCode === 40 || (e.ctrlKey && e.keyCode === 78); // DOWN, Ctrl-N
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
isEnter(e) {
|
|
|
|
return e.keyCode === 13;
|
|
|
|
},
|
|
|
|
keyListener(e) {
|
|
|
|
if (this.isUp(e)) {
|
|
|
|
if (!this.selectedIndex) {
|
|
|
|
this.selectedIndex = this.cannedMessages.length - 1;
|
|
|
|
} else {
|
|
|
|
this.selectedIndex -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.isDown(e)) {
|
|
|
|
if (this.selectedIndex === this.cannedMessages.length - 1) {
|
|
|
|
this.selectedIndex = 0;
|
|
|
|
} else {
|
|
|
|
this.selectedIndex += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.isEnter(e)) {
|
|
|
|
this.onKeyenter(this.cannedMessages[this.selectedIndex].content);
|
|
|
|
}
|
2019-11-20 10:10:34 +00:00
|
|
|
this.$el.scrollTop = 34 * this.selectedIndex;
|
2019-08-14 09:48:44 +00:00
|
|
|
},
|
|
|
|
onHover(index) {
|
|
|
|
this.selectedIndex = index;
|
|
|
|
},
|
|
|
|
onListItemSelection(index) {
|
|
|
|
this.selectedIndex = index;
|
|
|
|
this.onClick(this.cannedMessages[this.selectedIndex].content);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|