'use strict';

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

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = {
    data: function data() {
        return {
            running: false
        };
    },

    props: {
        value: {
            type: Boolean,
            default: false
        },
        block: {
            default: true
        },
        title: {
            default: '正在处理您的请求',
            type: String
        }
    },
    mounted: function mounted() {
        this.$el.style.cssText = this.$el.style.cssText + ';display: none !important;';
    },

    watch: {
        running: function running() {
            if (this.running) {
                this.showTriggle && this.showTriggle();
            } else {
                this.hideTriggle && this.hideTriggle();
            }
            this.render();
            this.$emit('input', this.running);
        },

        value: {
            handler: function handler() {
                this.running = this.value;
            },

            immediate: true
        }
    },
    methods: {
        render: function render() {
            this.$el.style.cssText = this.$el.style.cssText + ';display: ' + (this.running ? this.block ? 'block !important' : 'inline-block !important' : 'none !important') + ';';
        },
        show: function show() {
            this.running = true;
        },
        hide: function hide() {
            this.running = false;
        },
        wait: function wait(fn) {
            var _this = this;

            this.show();
            return new Promise(function () {
                var _ref = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee(ok) {
                    var ds;
                    return _regenerator2.default.wrap(function _callee$(_context) {
                        while (1) {
                            switch (_context.prev = _context.next) {
                                case 0:
                                    _context.next = 2;
                                    return fn();

                                case 2:
                                    ds = _context.sent;

                                    _this.hide();
                                    ok(ds);

                                case 5:
                                case 'end':
                                    return _context.stop();
                            }
                        }
                    }, _callee, _this);
                }));

                return function (_x) {
                    return _ref.apply(this, arguments);
                };
            }());
        }
    }
};