vue-popper.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import Vue from 'vue';
  2. import { PopupManager } from 'element-ui/src/utils/popup';
  3. const PopperJS = Vue.prototype.$isServer ? function() {} : require('./popper');
  4. const stop = e => e.stopPropagation();
  5. /**
  6. * @param {HTMLElement} [reference=$refs.reference] - The reference element used to position the popper.
  7. * @param {HTMLElement} [popper=$refs.popper] - The HTML element used as popper, or a configuration used to generate the popper.
  8. * @param {String} [placement=button] - Placement of the popper accepted values: top(-start, -end), right(-start, -end), bottom(-start, -right), left(-start, -end)
  9. * @param {Number} [offset=0] - Amount of pixels the popper will be shifted (can be negative).
  10. * @param {Boolean} [visible=false] Visibility of the popup element.
  11. * @param {Boolean} [visible-arrow=false] Visibility of the arrow, no style.
  12. */
  13. export default {
  14. props: {
  15. placement: {
  16. type: String,
  17. default: 'bottom'
  18. },
  19. boundariesPadding: {
  20. type: Number,
  21. default: 5
  22. },
  23. reference: {},
  24. popper: {},
  25. offset: {
  26. default: 0
  27. },
  28. value: Boolean,
  29. visibleArrow: Boolean,
  30. transition: String,
  31. appendToBody: {
  32. type: Boolean,
  33. default: true
  34. },
  35. options: {
  36. type: Object,
  37. default() {
  38. return {
  39. gpuAcceleration: false
  40. };
  41. }
  42. }
  43. },
  44. data() {
  45. return {
  46. showPopper: false
  47. };
  48. },
  49. watch: {
  50. value: {
  51. immediate: true,
  52. handler(val) {
  53. this.showPopper = val;
  54. this.$emit('input', val);
  55. }
  56. },
  57. showPopper(val) {
  58. val ? this.updatePopper() : this.destroyPopper();
  59. this.$emit('input', val);
  60. }
  61. },
  62. methods: {
  63. createPopper() {
  64. if (this.$isServer) return;
  65. if (!/^(top|bottom|left|right)(-start|-end)?$/g.test(this.placement)) {
  66. return;
  67. }
  68. const options = this.options;
  69. const popper = this.popperElm = this.popperElm || this.popper || this.$refs.popper;
  70. let reference = this.referenceElm = this.referenceElm || this.reference || this.$refs.reference;
  71. if (!reference &&
  72. this.$slots.reference &&
  73. this.$slots.reference[0]) {
  74. reference = this.referenceElm = this.$slots.reference[0].elm;
  75. }
  76. if (!popper || !reference) return;
  77. if (this.visibleArrow) this.appendArrow(popper);
  78. if (this.appendToBody) document.body.appendChild(this.popperElm);
  79. if (this.popperJS && this.popperJS.destroy) {
  80. this.popperJS.destroy();
  81. }
  82. options.placement = this.placement;
  83. options.offset = this.offset;
  84. this.popperJS = new PopperJS(reference, popper, options);
  85. this.popperJS.onCreate(_ => {
  86. this.$emit('created', this);
  87. this.resetTransformOrigin();
  88. this.$nextTick(this.updatePopper);
  89. });
  90. this.popperJS._popper.style.zIndex = PopupManager.nextZIndex();
  91. this.popperElm.addEventListener('click', stop);
  92. },
  93. updatePopper() {
  94. this.popperJS ? this.popperJS.update() : this.createPopper();
  95. },
  96. doDestroy() {
  97. /* istanbul ignore if */
  98. if (this.showPopper || !this.popperJS) return;
  99. this.popperJS.destroy();
  100. this.popperJS = null;
  101. },
  102. destroyPopper() {
  103. if (this.popperJS) {
  104. this.resetTransformOrigin();
  105. }
  106. },
  107. resetTransformOrigin() {
  108. let placementMap = { top: 'bottom', bottom: 'top', left: 'right', right: 'left' };
  109. let placement = this.popperJS._popper.getAttribute('x-placement').split('-')[0];
  110. let origin = placementMap[placement];
  111. this.popperJS._popper.style.transformOrigin = ['top', 'bottom'].indexOf(placement) > -1
  112. ? `center ${ origin }`
  113. : `${ origin } center`;
  114. },
  115. appendArrow(element) {
  116. let hash;
  117. if (this.appended) {
  118. return;
  119. }
  120. this.appended = true;
  121. for (let item in element.attributes) {
  122. if (/^_v-/.test(element.attributes[item].name)) {
  123. hash = element.attributes[item].name;
  124. break;
  125. }
  126. }
  127. const arrow = document.createElement('div');
  128. if (hash) {
  129. arrow.setAttribute(hash, '');
  130. }
  131. arrow.setAttribute('x-arrow', '');
  132. arrow.className = 'popper__arrow';
  133. element.appendChild(arrow);
  134. }
  135. },
  136. beforeDestroy() {
  137. this.doDestroy();
  138. if (this.popperElm && this.popperElm.parentNode === document.body) {
  139. this.popperElm.removeEventListener('click', stop);
  140. document.body.removeChild(this.popperElm);
  141. }
  142. },
  143. // call destroy in keep-alive mode
  144. deactivated() {
  145. this.$options.beforeDestroy[0].call(this);
  146. }
  147. };