/*! bxs-vue-ui v3.3.0 */
'use strict';

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

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

require('../utils/entry-virtual.js');
var normalizeComponent = require('../utils/normalize-component.js');
require('../utils/array-iteration.js');
require('../utils/sloppy-array-method.js');
var forEach = require('../utils/for-each.js');
require('../utils/string-trim.js');
var _parseInt = require('../utils/parse-int.js');
var _extends = require('../utils/extends.js');
var Vue = _interopDefault(require('vue'));
var keys = require('../utils/keys.js');

var empty = {
	image: "//media.winbaoxian.com/autoUpload/common/error-empty_71d507cd6a90807.png",
	text: "什么都没有"
};
var message = {
	image: "//media.winbaoxian.com/autoUpload/common/error-message_9b45ca72412df4b.png",
	text: "没有消息"
};
var activity = {
	image: "//media.winbaoxian.com/autoUpload/common/error-activity_d6fd3d38e6cda2b.png",
	text: "没有活动"
};
var search = {
	image: "//media.winbaoxian.com/autoUpload/common/error-search_05ae4e97a2d63b3.png",
	text: "没有搜索结果"
};
var comment = {
	image: "//media.winbaoxian.com/autoUpload/common/error-comment_40b84b4a78811f2.png",
	text: "没有评论"
};
var collect = {
	image: "//media.winbaoxian.com/autoUpload/common/error-collect_c718dfd03a76400.png",
	text: "没有收藏"
};
var address = {
	image: "//media.winbaoxian.com/autoUpload/common/error-address_cd82052533577c9.png",
	text: "没有地址"
};
var redpack = {
	image: "//media.winbaoxian.com/autoUpload/common/error-redpack_44e982d000216fb.png",
	text: "没有奖励券"
};
var network = {
	image: "//media.winbaoxian.com/autoUpload/common/error-network_3fb1d4ef31a65e1.png",
	text: "网络异常"
};
var loaded = {
	image: "//media.winbaoxian.com/autoUpload/common/error-loaded_51196385d445198.png",
	text: "加载失败"
};
var upgrade = {
	image: "//media.winbaoxian.com/autoUpload/common/error-upgrade_a35b243ef7e6ef9.png",
	text: "服务器升级中"
};
var defaultPageJson = {
	empty: empty,
	message: message,
	activity: activity,
	search: search,
	comment: comment,
	collect: collect,
	address: address,
	redpack: redpack,
	network: network,
	loaded: loaded,
	upgrade: upgrade
};

var script = normalizeComponent.create({
  name: 'default-page',
  props: {
    // 是否是局部布局
    inline: {
      type: Boolean,
      default: true
    },
    // 是否是更大的
    large: {
      type: Boolean,
      default: false
    },
    // 局部显示是否自适应
    responsive: {
      type: Boolean,
      default: true
    },
    // 错误类型
    type: {
      type: String,
      default: 'empty'
    },
    // 错误图片
    image: {
      type: String,
      default: ''
    },
    imageClass: {
      type: String,
      default: ''
    },
    // 错误文案
    text: {
      type: String,
      default: ''
    },
    // 行动按钮文案
    label: {
      type: String,
      default: ''
    }
  },
  data: function data() {
    return {
      visible: true,
      innerStyle: {},
      action: null
    };
  },
  computed: {
    imgData: function imgData() {
      return this.getImage();
    },
    txtData: function txtData() {
      return this.getText();
    }
  },
  methods: {
    // 触发行动按钮事件
    handleAction: function handleAction() {
      if (this.action) {
        this.action();
      }

      this.$emit('action');
    },
    // 计算页面还留多少空白高度
    calculateBlank: function calculateBlank() {
      var _this = this;

      return new normalizeComponent._Promise(function (resolve) {
        // 图片loaded后再计算高度
        var $img = _this.$refs.defaultpageImage;

        $img.onload = function () {
          var blankHeight = 0; // 查看body是否存在留白

          blankHeight = window.innerHeight - document.body.clientHeight;

          if (blankHeight > 0) {
            resolve(blankHeight);
          } // 若body发现没有留白，再查找body下的第一个元素，查看是否存在留白


          var firstElement = document.body.firstElementChild; // 第一个元素不是div，则不再处理

          if (firstElement.nodeName !== 'DIV') {
            resolve(0);
          }

          blankHeight = window.innerHeight - firstElement.clientHeight;

          if (blankHeight > 0) {
            resolve(blankHeight);
          } // 未有留白统一返回0


          resolve(0);
        };

        $img.onerror = function () {
          resolve(0);
        };
      });
    },
    getImage: function getImage() {
      if (this.image) {
        return this.image;
      }

      var data = defaultPageJson[this.type];
      return data ? data.image : defaultPageJson.empty.image;
    },
    getText: function getText() {
      if (this.text) {
        return this.text;
      }

      var data = defaultPageJson[this.type];
      return data ? data.text : defaultPageJson.empty.text;
    }
  },
  mounted: function mounted() {
    var _this2 = this;

    if (this.inline && this.responsive) {
      this.$nextTick(function () {
        // 动态取paddingBottom的值
        var paddingBottom = window.getComputedStyle(_this2.$el.childNodes[0]).paddingBottom.replace('px', '');
        paddingBottom = _parseInt._parseInt(paddingBottom, 10);

        _this2.calculateBlank().then(function (height) {
          paddingBottom += height;

          _this2.$set(_this2.innerStyle, 'paddingBottom', paddingBottom + "px");
        });
      });
    }
  }
});

/* script */
var __vue_script__ = script;
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _vm.visible ? _c('div', {
    class: {
      'bx-defaultpage-inline': _vm.inline,
      'bx-defaultpage-fixed': !_vm.inline,
      'bx-defaultpage-large': _vm.large
    }
  }, [_c('div', {
    staticClass: "bx-defaultpage-inner",
    style: _vm.innerStyle
  }, [_c('div', {
    staticClass: "bx-defaultpage-image",
    class: _vm.imageClass
  }, [_c('img', {
    ref: "defaultpageImage",
    staticClass: "img-responsive",
    attrs: {
      "src": _vm.imgData
    }
  })]), _c('div', {
    staticClass: "bx-defaultpage-text",
    domProps: {
      "innerHTML": _vm._s(_vm.txtData)
    }
  }), _vm.label ? _c('div', {
    staticClass: "bx-defaultpage-group"
  }, [_c('a', {
    staticClass: "bx-defaultpage-btn",
    on: {
      "click": _vm.handleAction
    }
  }, [_vm._v(_vm._s(_vm.label))])]) : _vm._e()])]) : _vm._e();
};

var __vue_staticRenderFns__ = [];
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* style inject */

/* style inject SSR */

var DefaultPageView = normalizeComponent.__vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

var _context;
var defaultOptions = {
  inline: false,
  large: true
};
var DefaultPageConstructor = Vue.extend(DefaultPageView);
var instance = null;

var getInstance = function getInstance() {
  return new DefaultPageConstructor({
    el: document.createElement('div')
  });
};

var DefaultPage = function DefaultPage(options) {
  if (options === void 0) {
    options = {};
  }

  instance = getInstance();

  _extends._extends(instance, defaultOptions, options);

  document.body.appendChild(instance.$el);
};

DefaultPage.common = function (options) {
  if (options === void 0) {
    options = {};
  }

  var defaults = {};
  defaults.image = defaultPageJson[options.type].image;
  defaults.text = defaultPageJson[options.type].text; // eslint-disable-next-line

  new DefaultPage(_extends._extends({}, defaults, options));
};
/* eslint-disable no-param-reassign */


forEach._forEachInstanceProperty(_context = keys._Object$keys(defaultPageJson)).call(_context, function (key) {
  DefaultPage[key] = function (options) {
    if (options === void 0) {
      options = {};
    }

    if (typeof options === 'string') {
      options = {
        text: options
      };
    }

    var defaults = {
      type: key,
      image: defaultPageJson[key].image,
      text: defaultPageJson[key].text
    };
    options = _extends._extends({}, defaults, options); // eslint-disable-next-line

    new DefaultPage(options);
  };
});
/* eslint-enable no-param-reassign */


DefaultPage.view = DefaultPageView;

DefaultPage.remove = function () {
  if (instance) {
    instance.visible = false;
  }
};

DefaultPage.hide = function () {
  if (instance) {
    instance.visible = false;
  }
};

DefaultPage.install = function (vueGlobal) {
  vueGlobal.use(DefaultPageView);
  /* eslint no-param-reassign: ["Error", { "props": false }] */

  vueGlobal.prototype.$defaultPage = DefaultPage;
};

exports.default = DefaultPage;
