popper-novalue.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * https://github.com/freeze-component/vue-popper
  3. * */
  4. import Vue from 'vue';
  5. const isServer = Vue.prototype.$isServer;
  6. const Popper = isServer ? function() {} : require('popper.js/dist/umd/popper.js'); // eslint-disable-line
  7. export default {
  8. props: {
  9. placement: {
  10. type: String,
  11. default: 'bottom'
  12. },
  13. boundariesPadding: {
  14. type: Number,
  15. default: 5
  16. },
  17. reference: Object,
  18. popper: Object,
  19. offset: {
  20. default: 0
  21. },
  22. transition: String,
  23. options: {
  24. type: Object,
  25. default () {
  26. return {
  27. modifiers: {
  28. computeStyle:{
  29. gpuAcceleration: false,
  30. },
  31. preventOverflow :{
  32. boundariesElement: 'window'
  33. }
  34. }
  35. };
  36. }
  37. }
  38. },
  39. data () {
  40. return {
  41. visible: false
  42. };
  43. },
  44. watch: {
  45. visible(val) {
  46. if (val) {
  47. if (this.handleIndexIncrease) this.handleIndexIncrease(); // just use for Poptip
  48. this.updatePopper();
  49. this.$emit('on-popper-show');
  50. } else {
  51. this.$emit('on-popper-hide');
  52. }
  53. }
  54. },
  55. methods: {
  56. createPopper() {
  57. if (isServer) return;
  58. if (!/^(top|bottom|left|right)(-start|-end)?$/g.test(this.placement)) {
  59. return;
  60. }
  61. const options = this.options;
  62. const popper = this.popper || this.$refs.popper;
  63. const reference = this.reference || this.$refs.reference;
  64. if (!popper || !reference) return;
  65. if (this.popperJS && this.popperJS.hasOwnProperty('destroy')) {
  66. this.popperJS.destroy();
  67. }
  68. options.placement = this.placement;
  69. if (!options.modifiers.offset) {
  70. options.modifiers.offset = {};
  71. }
  72. options.modifiers.offset.offset = this.offset;
  73. options.onCreate =()=>{
  74. this.$nextTick(this.updatePopper);
  75. this.$emit('created', this);
  76. };
  77. this.popperJS = new Popper(reference, popper, options);
  78. },
  79. updatePopper() {
  80. if (isServer) return;
  81. this.popperJS ? this.popperJS.update() : this.createPopper();
  82. },
  83. doDestroy() {
  84. if (isServer) return;
  85. if (this.visible) return;
  86. this.popperJS.destroy();
  87. this.popperJS = null;
  88. }
  89. },
  90. updated (){
  91. this.$nextTick(()=>this.updatePopper());
  92. },
  93. beforeDestroy() {
  94. if (isServer) return;
  95. if (this.popperJS) {
  96. this.popperJS.destroy();
  97. }
  98. }
  99. };