portal.js 1.0 KB

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