'use strict';

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

var _vue = require('vue');

var _vue2 = _interopRequireDefault(_vue);

var _event = require('../_utils/dom/event');

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

/**
 * Listen to click outside event
 */
var ClickOutsideMixin = exports.ClickOutsideMixin = function ClickOutsideMixin(config) {
    return _vue2['default'].extend({
        props: {
            closeOnClickOutside: {
                type: Boolean,
                'default': true
            }
        },

        data: function data() {
            var _this = this;

            var clickOutsideHandler = function clickOutsideHandler(event) {
                if (_this.closeOnClickOutside && !_this.$el.contains(event.target)) {
                    _this[config.method]();
                }
            };

            return { clickOutsideHandler: clickOutsideHandler };
        },
        mounted: function mounted() {
            (0, _event.on)(document, config.event, this.clickOutsideHandler);
        },
        beforeDestroy: function beforeDestroy() {
            (0, _event.off)(document, config.event, this.clickOutsideHandler);
        }
    });
};