'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _popperLite = require('@popperjs/core/lib/popper-lite');

var _flip = require('@popperjs/core/lib/modifiers/flip');

var _flip2 = _interopRequireDefault(_flip);

var _arrow = require('@popperjs/core/lib/modifiers/arrow');

var _arrow2 = _interopRequireDefault(_arrow);

var _preventOverflow = require('@popperjs/core/lib/modifiers/preventOverflow');

var _preventOverflow2 = _interopRequireDefault(_preventOverflow);

var _offset = require('@popperjs/core/lib/modifiers/offset');

var _offset2 = _interopRequireDefault(_offset);

var _icon = require('../icon');

var _icon2 = _interopRequireDefault(_icon);

var _popup = require('../popup');

var _popup2 = _interopRequireDefault(_popup);

var _raf = require('../_utils/dom/raf');

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

var _transferDom = require('../directives/transfer-dom');

var _transferDom2 = _interopRequireDefault(_transferDom);

require('./index.less');

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

var Popover = {
    name: 'SnPopover',
    directives: { TransferDom: _transferDom2['default'] },
    props: {
        value: Boolean,
        message: String,
        placement: {
            type: String,
            'default': 'bottom',
            validator: function validator(value) {
                return ['bottom', 'top'].indexOf(value) > -1;
            }
        },
        offset: {
            type: Array,
            'default': function _default() {
                return [0, 8];
            }
        },
        overflowPadding: {
            type: Number,
            'default': 15
        },
        closeable: {
            type: Boolean,
            'default': true
        },
        container: {
            type: String,
            'default': 'body'
        },
        trigger: {
            type: String,
            'default': 'click',
            validator: function validator(value) {
                return ['click', 'hover', 'focus'].indexOf(value) > -1;
            }
        },
        closeOnClickOutside: {
            type: Boolean,
            'default': true
        },
        mouseEnterDelay: {
            type: Number,
            'default': 100
        },
        mouseLeaveDelay: {
            type: Number,
            'default': 100
        },
        disabled: Boolean,
        popperClass: String
    },

    watch: {
        value: 'updateLocation',
        placement: 'updateLocation',
        message: 'updateLocation',
        trigger: function trigger(val) {
            var _$refs = this.$refs,
                wrapperRef = _$refs.wrapperRef,
                popoverRef = _$refs.popoverRef;


            if (val === 'hover') {
                (0, _event.on)(wrapperRef, 'mouseenter', this.onMouseEnter, true);
                (0, _event.on)(popoverRef.$el, 'mouseenter', this.onMouseEnter, true);
                (0, _event.on)(wrapperRef, 'mouseleave', this.onMouseLeave, true);
                (0, _event.on)(popoverRef.$el, 'mouseleave', this.onMouseLeave, true);
            } else if (val === 'click') {
                (0, _event.off)(wrapperRef, 'mouseenter', this.onMouseEnter);
                (0, _event.off)(popoverRef.$el, 'mouseenter', this.onMouseEnter);
                (0, _event.off)(wrapperRef, 'mouseleave', this.onMouseLeave);
                (0, _event.off)(popoverRef.$el, 'mouseleave', this.onMouseLeave);

                clearTimeout(this._timer);
            }
        }
    },

    mounted: function mounted() {
        this.updateLocation();

        var wrapperRef = this.$refs.wrapperRef;


        if (this.trigger === 'hover') {
            (0, _event.on)(wrapperRef, 'mouseenter', this.onMouseEnter, true);
            (0, _event.on)(wrapperRef, 'mouseleave', this.onMouseLeave, true);
        }

        if (this.trigger === 'focus') {
            (0, _event.on)(wrapperRef, 'focusin', this.doShow, true);
            (0, _event.on)(wrapperRef, 'focusout', this.doHide, true);
        }
    },
    beforeDestroy: function beforeDestroy() {
        if (this.popper) {
            this.popper.destroy();
            this.popper = null;
            (0, _event.off)(document, 'click', this.onClickOutside);

            var _$refs2 = this.$refs,
                wrapperRef = _$refs2.wrapperRef,
                popoverRef = _$refs2.popoverRef;

            (0, _event.off)(wrapperRef, 'mouseenter', this.onMouseEnter);
            (0, _event.off)(popoverRef.$el, 'mouseenter', this.onMouseEnter);
            (0, _event.off)(wrapperRef, 'mouseleave', this.onMouseLeave);
            (0, _event.off)(popoverRef.$el, 'mouseleave', this.onMouseLeave);
            (0, _event.off)(wrapperRef, 'focusin', this.onShow);
            (0, _event.off)(wrapperRef, 'focusout', this.onHide);

            clearTimeout(this._timer);
        }
    },


    methods: {
        createPopper: function createPopper() {
            var _$refs3 = this.$refs,
                wrapperRef = _$refs3.wrapperRef,
                popoverRef = _$refs3.popoverRef,
                arrowRef = _$refs3.arrowRef;


            return (0, _popperLite.createPopper)(wrapperRef, popoverRef.$el, {
                placement: this.placement,
                shift: true,
                modifiers: [{
                    name: 'computeStyles',
                    options: {
                        gpuAcceleration: false,
                        adaptive: false
                    }
                }, (0, _extends3['default'])({}, _offset2['default'], {
                    options: {
                        offset: this.offset
                    }
                }), (0, _extends3['default'])({}, _preventOverflow2['default'], {
                    options: {
                        boundariesElement: document,
                        padding: this.overflowPadding
                    }
                }), (0, _extends3['default'])({}, _arrow2['default'], {
                    options: {
                        element: arrowRef
                    }
                }), (0, _extends3['default'])({}, _flip2['default'], {
                    options: {
                        fallbackPlacements: []
                    }
                })]
            });
        },
        updateLocation: function updateLocation() {
            var _this = this;

            this.$nextTick(function () {
                if (!_this.value) {
                    return;
                }

                if (!_this.popper) {
                    _this.popper = _this.createPopper();

                    (0, _raf.doubleRaf)(function () {
                        var _popper$state = _this.popper.state,
                            placement = _popper$state.placement,
                            styles = _popper$state.styles;

                        var _ref = styles || {},
                            arrow = _ref.arrow;

                        _this.$refs.popoverRef.$el.style['transform-origin'] = arrow.left + ' ' + (placement.indexOf('top') !== -1 ? '100%' : '0');

                        var popoverRef = _this.$refs.popoverRef;


                        if (_this.trigger === 'hover') {
                            (0, _event.on)(popoverRef.$el, 'mouseenter', _this.onMouseEnter, true);
                            (0, _event.on)(popoverRef.$el, 'mouseleave', _this.onMouseLeave, true);
                        }

                        // 设置延时处理外部点击在弹起气泡时触发
                        setTimeout(function () {
                            (0, _event.on)(document, 'click', _this.onClickOutside);
                        });
                    });
                } else {
                    _this.popper.setOptions({
                        placement: _this.placement
                    });

                    _this.$nextTick(function () {
                        var _popper$state2 = _this.popper.state,
                            placement = _popper$state2.placement,
                            styles = _popper$state2.styles;

                        var _ref2 = styles || {},
                            arrow = _ref2.arrow;

                        _this.$refs.popoverRef.$el.style['transform-origin'] = arrow.left + ' ' + (placement.indexOf('top') !== -1 ? '100%' : '0');
                    });
                }
            });
        },
        onClick: function onClick() {
            this.$emit('click');
        },
        onToggle: function onToggle(value) {
            if (this.disabled) return;

            this.$emit('input', !value);
        },
        onClickWrapper: function onClickWrapper() {
            var trigger = this.trigger;


            if (trigger === 'click') {
                this.onToggle(this.value);
            }
        },
        onContentClick: function onContentClick(e) {
            e.stopPropagation();
        },
        onCloseClick: function onCloseClick() {
            if (this.closeable) {
                this.$emit('inside-close');
                this.$emit('input', false);
            }
        },
        onClickOutside: function onClickOutside(e) {
            if (this.closeOnClickOutside && this.$el && !this.$el.contains(e.target)) {
                this.$emit('input', false);
            }
        },
        onPopupInput: function onPopupInput(val) {
            this.$emit('input', val);
        },
        onOpen: function onOpen() {
            this.$emit('open');
        },
        onOpened: function onOpened() {
            this.$emit('opened');
        },
        onClose: function onClose() {
            this.$emit('close');
        },
        onClosed: function onClosed() {
            this.$emit('closed');
        },
        onMouseEnter: function onMouseEnter() {
            var _this2 = this;

            if (this.disabled) return;

            clearTimeout(this._timer);
            this._timer = setTimeout(function () {
                _this2.$emit('input', true);
            }, this.mouseEnterDelay);
        },
        onMouseLeave: function onMouseLeave() {
            var _this3 = this;

            if (this.disabled) return;

            clearTimeout(this._timer);
            this._timer = setTimeout(function () {
                _this3.$emit('input', false);
            }, this.mouseLeaveDelay);
        },
        doShow: function doShow() {
            if (this.disabled) return;

            this.$emit('input', true);
        },
        doHide: function doHide() {
            if (this.disabled) return;

            this.$emit('input', false);
        }
    },

    render: function render() {
        var h = arguments[0];
        var value = this.value,
            message = this.message,
            container = this.container,
            closeable = this.closeable,
            popperClass = this.popperClass,
            $slots = this.$slots;


        return h(
            'span',
            {
                ref: 'wrapperRef',
                'class': 'sn-popover-wrapper',
                on: {
                    'click': this.onClickWrapper
                }
            },
            [$slots['default'], h(
                'div',
                {
                    directives: [{
                        name: 'transfer-dom',
                        value: container
                    }]
                },
                [h(
                    _popup2['default'],
                    {
                        ref: 'popoverRef',
                        attrs: { role: 'tooltip',

                            value: value,
                            overlay: false,
                            lockScroll: false,
                            position: null,
                            transition: 'sn-popover-zoom',
                            adjustKeyboard: false
                        },
                        'class': ['sn-popover', popperClass], on: {
                            'input': this.onPopupInput,
                            'open': this.onOpen,
                            'opened': this.onOpened,
                            'close': this.onClose,
                            'closed': this.onClosed
                        }
                    },
                    [h(
                        'div',
                        {
                            'class': 'sn-popover-content',
                            on: {
                                'click': this.onContentClick
                            }
                        },
                        [h(
                            'div',
                            { 'class': 'sn-popover-content-message' },
                            [$slots['message'] || message]
                        ), closeable ? h(_icon2['default'], {
                            attrs: {
                                type: 'close'
                            },
                            'class': 'sn-popover-content-close',
                            on: {
                                'click': this.onCloseClick
                            }
                        }) : null]
                    ), h(_icon2['default'], {
                        ref: 'arrowRef',
                        attrs: { type: 'popover'
                        },
                        'class': 'sn-popover-arrow'
                    })]
                )]
            )]
        );
    }
};

Popover.install = function (Vue) {
    Vue.component(Popover.name, Popover);
};

exports['default'] = Popover;
module.exports = exports.default;