portal.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.PortalMixin = PortalMixin;
  4. function getElement(selector) {
  5. if (typeof selector === 'string') {
  6. return document.querySelector(selector);
  7. }
  8. return selector();
  9. }
  10. function PortalMixin(_temp) {
  11. var _ref = _temp === void 0 ? {} : _temp,
  12. ref = _ref.ref,
  13. afterPortal = _ref.afterPortal;
  14. return {
  15. props: {
  16. getContainer: [String, Function]
  17. },
  18. watch: {
  19. getContainer: 'portal'
  20. },
  21. mounted: function mounted() {
  22. if (this.getContainer) {
  23. this.portal();
  24. }
  25. },
  26. methods: {
  27. portal: function portal() {
  28. var getContainer = this.getContainer;
  29. var el = ref ? this.$refs[ref] : this.$el;
  30. var container;
  31. if (getContainer) {
  32. container = getElement(getContainer);
  33. } else if (this.$parent) {
  34. container = this.$parent.$el;
  35. }
  36. if (container && container !== el.parentNode) {
  37. container.appendChild(el);
  38. }
  39. if (afterPortal) {
  40. afterPortal.call(this);
  41. }
  42. }
  43. }
  44. };
  45. }