2016-10-17 12:34:20 +08:00
|
|
|
<template>
|
2017-01-13 17:10:57 +08:00
|
|
|
<div class="ivu-select-dropdown" :style="styles"><slot></slot></div>
|
2016-10-17 12:34:20 +08:00
|
|
|
</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';
|
2017-07-10 15:36:09 +08:00
|
|
|
const Popper = isServer ? function() {} : require('popper.js'); // eslint-disable-line
|
2016-10-17 12:34:20 +08:00
|
|
|
|
|
|
|
export default {
|
2017-03-06 13:31:48 +08:00
|
|
|
name: 'Drop',
|
2016-12-03 23:46:25 +08:00
|
|
|
props: {
|
|
|
|
placement: {
|
|
|
|
type: String,
|
|
|
|
default: 'bottom-start'
|
|
|
|
}
|
|
|
|
},
|
2016-10-17 12:34:20 +08:00
|
|
|
data () {
|
|
|
|
return {
|
2017-01-13 17:10:57 +08:00
|
|
|
popper: null,
|
2017-03-03 17:46:09 +08:00
|
|
|
width: ''
|
2016-12-25 22:49:42 +08:00
|
|
|
};
|
2016-10-17 12:34:20 +08:00
|
|
|
},
|
2017-01-13 17:10:57 +08:00
|
|
|
computed: {
|
|
|
|
styles () {
|
|
|
|
let style = {};
|
|
|
|
if (this.width) style.width = `${this.width}px`;
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
},
|
2016-10-17 12:34:20 +08:00
|
|
|
methods: {
|
|
|
|
update () {
|
2017-07-10 15:36:09 +08:00
|
|
|
if (isServer) return;
|
2016-10-17 12:34:20 +08:00
|
|
|
if (this.popper) {
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.popper.update();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.$nextTick(() => {
|
2017-03-03 17:46:09 +08:00
|
|
|
this.popper = new Popper(this.$parent.$refs.reference, this.$el, {
|
2016-10-17 12:34:20 +08:00
|
|
|
gpuAcceleration: false,
|
2016-12-03 23:46:25 +08:00
|
|
|
placement: this.placement,
|
2016-10-17 12:34:20 +08:00
|
|
|
boundariesPadding: 0,
|
2016-11-01 12:42:57 +08:00
|
|
|
forceAbsolute: true,
|
|
|
|
boundariesElement: 'body'
|
2016-10-17 12:34:20 +08:00
|
|
|
});
|
|
|
|
this.popper.onCreate(popper => {
|
|
|
|
this.resetTransformOrigin(popper);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
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'));
|
|
|
|
}
|
2016-10-17 12:34:20 +08:00
|
|
|
},
|
|
|
|
destroy () {
|
|
|
|
if (this.popper) {
|
|
|
|
this.resetTransformOrigin(this.popper);
|
|
|
|
setTimeout(() => {
|
|
|
|
this.popper.destroy();
|
|
|
|
this.popper = null;
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resetTransformOrigin(popper) {
|
|
|
|
let placementMap = {top: 'bottom', bottom: 'top'};
|
|
|
|
let placement = popper._popper.getAttribute('x-placement').split('-')[0];
|
|
|
|
let origin = placementMap[placement];
|
|
|
|
popper._popper.style.transformOrigin = `center ${ origin }`;
|
|
|
|
}
|
|
|
|
},
|
2017-03-03 17:46:09 +08:00
|
|
|
created () {
|
2016-10-17 12:34:20 +08:00
|
|
|
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>
|