/**
 * Listen to click outside event
 */
import Vue from 'vue';
import { on, off } from '../_utils/dom/event';

export var ClickOutsideMixin = function ClickOutsideMixin(config) {
    return Vue.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() {
            on(document, config.event, this.clickOutsideHandler);
        },
        beforeDestroy: function beforeDestroy() {
            off(document, config.event, this.clickOutsideHandler);
        }
    });
};