vue-material/src/components/mdMenu/mdMenuItem.vue

79 lines
1.9 KiB
Vue
Raw Normal View History

<template>
2016-10-25 07:09:44 +00:00
<md-list-item
2016-10-25 00:04:00 +00:00
class="md-menu-item"
:class="classes"
2016-10-25 07:09:44 +00:00
@click="close"
:disabled="disabled">
<slot></slot>
</md-list-item>
</template>
<script>
2016-11-12 16:00:15 +00:00
import getClosestVueParent from '../../core/utils/getClosestVueParent';
import 'element.scrollintoviewifneeded-polyfill';
export default {
2016-10-25 07:09:44 +00:00
props: {
disabled: Boolean
},
2016-11-12 16:00:15 +00:00
data: () => ({
parentContent: {},
index: 0
}),
2016-10-25 00:04:00 +00:00
computed: {
classes() {
return {
2016-11-12 16:00:15 +00:00
'md-highlighted': this.highlighted
2016-10-25 00:04:00 +00:00
};
2016-10-25 07:09:44 +00:00
},
2016-11-12 16:00:15 +00:00
highlighted() {
if (this.index === this.parentContent.highlighted) {
2016-10-25 07:09:44 +00:00
if (this.disabled) {
2016-11-12 16:00:15 +00:00
if (this.parentContent.oldHighlight > this.parentContent.highlighted) {
this.parentContent.highlighted--;
2016-10-25 07:09:44 +00:00
} else {
2016-11-12 16:00:15 +00:00
this.parentContent.highlighted++;
2016-10-25 07:09:44 +00:00
}
}
2016-11-12 16:00:15 +00:00
if (this.index === 1) {
this.parentContent.$el.scrollTop = 0;
} else if (this.index === this.parentContent.itemsAmount) {
this.parentContent.$el.scrollTop = this.parentContent.$el.scrollHeight;
} else {
this.$el.scrollIntoViewIfNeeded(false);
}
2016-10-25 07:09:44 +00:00
return true;
}
return false;
2016-10-24 23:26:26 +00:00
}
},
2016-11-12 16:00:15 +00:00
methods: {
close() {
if (!this.disabled) {
2016-11-13 03:17:39 +00:00
if (this.parentMenu.mdCloseOnSelect) {
this.parentContent.close();
}
2016-11-12 16:00:15 +00:00
this.$emit('click');
}
}
},
2016-10-24 23:26:26 +00:00
mounted() {
2016-11-12 16:00:15 +00:00
this.parentContent = getClosestVueParent(this.$parent, 'md-menu-content');
2016-11-13 03:17:39 +00:00
this.parentMenu = getClosestVueParent(this.$parent, 'md-menu');
2016-11-12 16:00:15 +00:00
if (!this.parentContent) {
2016-10-24 23:26:26 +00:00
this.$destroy();
2016-10-24 23:26:26 +00:00
throw new Error('You must wrap the md-menu-item in a md-menu-content');
}
2016-10-25 00:04:00 +00:00
2016-11-12 16:00:15 +00:00
this.parentContent.itemsAmount++;
this.index = this.parentContent.itemsAmount;
2016-10-24 23:26:26 +00:00
}
};
</script>