'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.PortalMixin = PortalMixin;

var _vue = require('vue');

var _vue2 = _interopRequireDefault(_vue);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

function getElement(selector) {
    if (typeof selector === 'string') {
        return document.querySelector(selector);
    }

    return selector();
}

function PortalMixin(_ref) {
    var ref = _ref.ref,
        afterPortal = _ref.afterPortal;

    return _vue2['default'].extend({
        props: {
            getContainer: [String, Function]
        },
        watch: {
            getContainer: 'portal'
        },
        mounted: function mounted() {
            if (this.getContainer) {
                this.portal();
            }
        },

        methods: {
            portal: function portal() {
                var getContainer = this.getContainer;

                var el = ref ? this.$refs[ref] : this.$el;

                var container = void 0;
                if (getContainer) {
                    container = getElement(getContainer);
                } else if (this.$parent) {
                    container = this.$parent.$el;
                }

                if (container && container !== el.parentNode) {
                    container.appendChild(el);
                }

                if (afterPortal) {
                    afterPortal.call(this);
                }
            }
        }
    });
}