'use strict';

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

var _regenerator2 = _interopRequireDefault(_regenerator);

var _maxiloVue = require('maxilo-vue');

var _maxiloVue2 = _interopRequireDefault(_maxiloVue);

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"); }); }; }

_maxiloVue2.default.vue.use({
    install: function install(vue) {
        vue.directive('mxlLoading', {
            bind: function () {
                var _ref = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee(el, _bind, vNode) {
                    var flag, tag, node, instance, title, block;
                    return _regenerator2.default.wrap(function _callee$(_context) {
                        while (1) {
                            switch (_context.prev = _context.next) {
                                case 0:
                                    /* 将吸附区设置相对定位 */
                                    el.style.position = 'relative';
                                    /* loading版式 */
                                    flag = _bind.arg ? _bind.arg : 'build';
                                    /* 等待el插入dom */
                                    /* 生成loading标识 */

                                    tag = _maxiloVue2.default.utils.tool.random('mxl-loading-');
                                    /* 由吸附区保存loading标识 */

                                    el.setAttribute('mxl-loading-id', tag);
                                    /* 创建loading容器 */
                                    node = document.createElement('div');
                                    /* 设置loading标识 */

                                    node.setAttribute('id', tag);
                                    el.appendChild(node);
                                    _context.next = 9;
                                    return vue.nextTick();

                                case 9:
                                    /* 获取存储loading vNode的容器 */
                                    instance = vNode.componentInstance ? vNode.componentInstance : vNode.elm;
                                    /* 获取自定义提示 */

                                    title = el.getAttribute('mxl-loading-title');

                                    title = title ? title : '请等待';
                                    /* 获取块模式 */
                                    block = el.getAttribute('mxl-loading-block', 'block');
                                    /* 生成loading vNode */

                                    instance.mxlLoadingInstance = new vue({
                                        el: '#' + tag,
                                        template: '\n                    <mxl-loading-' + flag + ' \n                        style="background: #fff;border: 1px solid #cecece;border-radius: 4px;z-index: 20000;width: 100%;height: 100%;position: absolute;\n                        top: 0;left: 0;"\n                        id="' + tag + '" \n                        title="' + title + '"\n                        :block="\'' + block + '\' == \'block\'"></mxl-loading-' + flag + '>'
                                    })._vnode.componentInstance;
                                    _context.next = 16;
                                    return instance.mxlLoadingInstance.$nextTick();

                                case 16:
                                    instance.mxlLoadingInstance.$data.running = !!_bind.value;

                                case 17:
                                case 'end':
                                    return _context.stop();
                            }
                        }
                    }, _callee, this);
                }));

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

                return bind;
            }(),
            componentUpdated: function () {
                var _ref2 = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee2(el, bind, vNode) {
                    var instance, title;
                    return _regenerator2.default.wrap(function _callee2$(_context2) {
                        while (1) {
                            switch (_context2.prev = _context2.next) {
                                case 0:
                                    _context2.next = 2;
                                    return vue.nextTick();

                                case 2:
                                    instance = vNode.componentInstance ? vNode.componentInstance : vNode.elm;

                                    instance.mxlLoadingInstance.$data.running = bind.value;

                                    title = el.getAttribute('mxl-loading-title');

                                    title = title ? title : '请等待';
                                    instance.mxlLoadingInstance.$props.title = title;
                                    instance.mxlLoadingInstance.$props.block = el.getAttribute('mxl-loading-block', 'block') == 'block';

                                case 8:
                                case 'end':
                                    return _context2.stop();
                            }
                        }
                    }, _callee2, this);
                }));

                function componentUpdated(_x4, _x5, _x6) {
                    return _ref2.apply(this, arguments);
                }

                return componentUpdated;
            }()
        });
    }
});