iview/src/components/select/dropdown.vue

119 lines
4.2 KiB
Vue
Raw Normal View History

<template>
2017-08-18 16:49:42 +08:00
<div class="ivu-select-dropdown" :class="className" :style="styles"><slot></slot></div>
</template>
<script>
2017-07-10 15:36:09 +08:00
import Vue from 'vue';
const isServer = Vue.prototype.$isServer;
2017-01-13 17:10:57 +08:00
import { getStyle } from '../../utils/assist';
2018-03-15 10:43:25 +08:00
const Popper = isServer ? function() {} : require('popper.js/dist/umd/popper.js'); // eslint-disable-line
2018-09-10 16:10:35 +08:00
import { transferIndex, transferIncrease } from '../../utils/transfer-queue';
export default {
name: 'Drop',
props: {
placement: {
type: String,
default: 'bottom-start'
2017-08-18 16:49:42 +08:00
},
className: {
type: String
2018-09-10 16:10:35 +08:00
},
transfer: {
type: Boolean
}
},
data () {
return {
2017-01-13 17:10:57 +08:00
popper: null,
2018-03-29 17:35:56 +08:00
width: '',
2018-09-10 16:10:35 +08:00
popperStatus: false,
tIndex: this.handleGetIndex()
2016-12-25 22:49:42 +08:00
};
},
2017-01-13 17:10:57 +08:00
computed: {
styles () {
let style = {};
2018-08-08 15:18:24 +08:00
if (this.width) style.minWidth = `${this.width}px`;
2018-09-10 16:10:35 +08:00
if (this.transfer) style['z-index'] = 1060 + this.tIndex;
2017-01-13 17:10:57 +08:00
return style;
}
},
methods: {
update () {
2017-07-10 15:36:09 +08:00
if (isServer) return;
if (this.popper) {
this.$nextTick(() => {
this.popper.update();
2018-03-29 17:35:56 +08:00
this.popperStatus = true;
});
} else {
this.$nextTick(() => {
2017-03-03 17:46:09 +08:00
this.popper = new Popper(this.$parent.$refs.reference, this.$el, {
placement: this.placement,
2018-03-15 00:07:11 +08:00
modifiers: {
computeStyle:{
2018-04-10 16:13:09 +08:00
gpuAcceleration: false
},
preventOverflow :{
2018-05-21 17:20:01 +08:00
boundariesElement: 'window'
2018-03-15 00:07:11 +08:00
}
2018-03-16 00:43:11 +08:00
},
onCreate:()=>{
this.resetTransformOrigin();
2018-03-16 16:58:20 +08:00
this.$nextTick(this.popper.update());
},
onUpdate:()=>{
this.resetTransformOrigin();
2018-03-15 00:07:11 +08:00
}
});
});
}
2017-01-13 17:10:57 +08:00
// set a height for parent is Modal and Select's width is 100%
if (this.$parent.$options.name === 'iSelect') {
this.width = parseInt(getStyle(this.$parent.$el, 'width'));
}
2018-09-10 16:10:35 +08:00
this.tIndex = this.handleGetIndex();
},
destroy () {
if (this.popper) {
setTimeout(() => {
2018-03-29 17:35:56 +08:00
if (this.popper && !this.popperStatus) {
2018-01-12 09:51:33 +08:00
this.popper.destroy();
this.popper = null;
}
2018-03-29 17:35:56 +08:00
this.popperStatus = false;
}, 300);
}
2018-03-16 00:43:11 +08:00
},
resetTransformOrigin() {
2018-05-04 11:00:15 +08:00
// 不判断Select 会报错,不知道为什么
if (!this.popper) return;
2018-04-11 22:06:53 +08:00
let x_placement = this.popper.popper.getAttribute('x-placement');
let placementStart = x_placement.split('-')[0];
let placementEnd = x_placement.split('-')[1];
const leftOrRight = x_placement === 'left' || x_placement === 'right';
if(!leftOrRight){
this.popper.popper.style.transformOrigin = placementStart==='bottom' || ( placementStart !== 'top' && placementEnd === 'start') ? 'center top' : 'center bottom';
}
2018-09-10 16:10:35 +08:00
},
handleGetIndex () {
transferIncrease();
return transferIndex;
},
},
2017-03-03 17:46:09 +08:00
created () {
this.$on('on-update-popper', this.update);
this.$on('on-destroy-popper', this.destroy);
},
beforeDestroy () {
if (this.popper) {
this.popper.destroy();
}
}
2016-12-25 22:49:42 +08:00
};
</script>