'use strict';

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

var _vue = require('vue');

var _vue2 = _interopRequireDefault(_vue);

var _context = require('./context');

var _touch = require('../touch');

var _portal = require('../portal');

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

var _overlay = require('./overlay');

var _scroll = require('../../_utils/dom/scroll');

var _commonUtil = require('../../utils/commonUtil');

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

var PopupMixin = exports.PopupMixin = {
    mixins: [_touch.TouchMixin, (0, _portal.PortalMixin)({
        afterPortal: function afterPortal() {
            if (this.overlay) {
                (0, _overlay.updateOverlay)();
            }
        }
    })],

    props: {
        value: Boolean,
        overlay: Boolean,
        overlayStyle: Object,
        overlayClass: String,
        closeOnClickOverlay: {
            type: Boolean,
            'default': true
        },
        zIndex: [Number, String],
        lockScroll: {
            type: Boolean,
            'default': true
        },
        lazyRender: {
            type: Boolean,
            'default': true
        },
        adjustKeyboard: {
            type: Boolean,
            'default': true
        }
    },

    data: function data() {
        return {
            inited: this.value
        };
    },


    computed: {
        shouldRender: function shouldRender() {
            return this.inited || !this.lazyRender;
        }
    },

    watch: {
        value: function value(val) {
            var _this = this;

            var type = val ? 'open' : 'close';
            this.inited = this.inited || this.value;
            var activeTagName = window.activeTagName || '';

            if (val) {
                if (this.adjustKeyboard && !(0, _commonUtil.isPC)() && (activeTagName.toUpperCase() === 'INPUT' || activeTagName.toUpperCase() === 'TEXTAREA')) {
                    setTimeout(function () {
                        _this[type]();
                        _this.$emit(val ? 'show' : 'hide');
                    }, 500);
                } else {
                    this[type]();
                    this.$emit(val ? 'show' : 'hide');
                }
            } else {
                this[type]();
                this.$emit(val ? 'show' : 'hide');
            }
        },

        overlay: 'renderOverlay'
    },

    mounted: function mounted() {
        if (this.value) {
            this.open();
        }
    },


    /* istanbul ignore next */
    activated: function activated() {
        if (this.value) {
            this.open();
        }
    },
    beforeDestroy: function beforeDestroy() {
        this.close();

        if (this.getContainer && this.$parent && this.$parent.$el) {
            this.$parent.$el.appendChild(this.$el);
        }
    },


    /* istanbul ignore next */
    deactivated: function deactivated() {
        this.close();
    },


    methods: {
        open: function open() {
            /* istanbul ignore next */
            if (this.opened) {
                return;
            }

            // cover default zIndex
            if (this.zIndex !== undefined) {
                _context.context.zIndex = this.zIndex;
            }

            this.opened = true;
            this.renderOverlay();

            if (this.lockScroll) {
                (0, _event.on)(document, 'touchstart', this.touchStart);
                (0, _event.on)(document, 'touchmove', this.onTouchMove);

                if (!_context.context.lockCount) {
                    document.body.classList.add('sn-overflow-hidden');
                }
                _context.context.lockCount++;
            }
        },
        close: function close() {
            if (!this.opened) {
                return;
            }

            if (this.lockScroll) {
                _context.context.lockCount--;
                (0, _event.off)(document, 'touchstart', this.touchStart);
                (0, _event.off)(document, 'touchmove', this.onTouchMove);

                if (!_context.context.lockCount) {
                    document.body.classList.remove('sn-overflow-hidden');
                }
            }

            this.opened = false;
            (0, _overlay.closeOverlay)(this);
            this.$emit('input', false);
        },
        onTouchMove: function onTouchMove(event) {
            this.touchMove(event);
            var direction = this.deltaY > 0 ? '10' : '01';
            var el = (0, _scroll.getScroller)(event.target, this.$el);
            var scrollHeight = el.scrollHeight,
                offsetHeight = el.offsetHeight,
                scrollTop = el.scrollTop;

            var status = '11';

            /* istanbul ignore next */
            if (scrollTop === 0) {
                status = offsetHeight >= scrollHeight ? '00' : '01';
            } else if (scrollTop + offsetHeight >= scrollHeight) {
                status = '10';
            }

            /* istanbul ignore next */
            if (status !== '11' && this.direction === 'vertical' && !(parseInt(status, 2) & parseInt(direction, 2))) {
                (0, _event.preventDefault)(event, true);
            }
        },
        renderOverlay: function renderOverlay() {
            var _this2 = this;

            if (!this.value) {
                return;
            }

            this.$nextTick(function () {
                _this2.updateZIndex(_this2.overlay ? 1 : 0);

                if (_this2.overlay) {
                    (0, _overlay.openOverlay)(_this2, {
                        zIndex: _context.context.zIndex++,
                        duration: _this2.duration,
                        className: _this2.overlayClass,
                        customStyle: _this2.overlayStyle
                    });
                } else {
                    (0, _overlay.closeOverlay)(_this2);
                }
            });
        },
        updateZIndex: function updateZIndex() {
            var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

            this.$el.style.zIndex = ++_context.context.zIndex + value;
        }
    }
};