"use strict";

var _interopRequireDefault = require("D:/git/\u6211\u7684/opu-components-vue/node_modules/@babel/runtime/helpers/interopRequireDefault.js").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("core-js/modules/es.array.concat.js");

require("core-js/modules/es.array.includes.js");

var _objectSpread2 = _interopRequireDefault(require("D:/git/\u6211\u7684/opu-components-vue/node_modules/@babel/runtime/helpers/esm/objectSpread2.js"));

var _interface = require("ant-design-vue/lib/date-picker/interface");

var _utils = _interopRequireDefault(require("../../utils"));

var _dateFormat = require("../../utils/dateFormat");

var _moment = _interopRequireDefault(require("moment"));

var _default = {
  name: "OpuDatePicker",
  components: {},
  props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, (0, _interface.DatePickerProps)()), {}, {
    min: [String, Object],
    max: [String, Object]
  }),
  model: {
    prop: "value",
    event: "update"
  },
  data: function data() {
    return {
      inputValue: "",
      blocksData: {
        blocks: [],
        datePatterns: [],
        delimiters: []
      }
    };
  },
  computed: {
    componentProps: function componentProps() {
      var _this = this;

      var $listeners = this.$listeners,
          $options = this.$options,
          ok = this.ok;
      var propsData = $options.propsData;
      var ons = {};

      _utils.default.each($listeners, function (cb, type) {
        ons[type] = function () {
          for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
          }

          _this.$emit.apply(_this, [type].concat(args));
        };
      });

      var props = {
        props: (0, _objectSpread2.default)({}, propsData),
        on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
          change: this.updateValue,
          panelChange: function panelChange(e) {
            if (["year", "month"].includes(propsData.mode)) {
              _this.updateValue(e);
            }
          },
          openChange: this.onOpenChange,
          ok: ok
        })
      };

      if ((this.min || this.max) && !props.props.disabledDate) {
        props.props.disabledDate = function (e) {
          return _this.renderDisabledDate(e);
        };
      }

      return props;
    },
    currentFormat: function currentFormat() {
      var format = "YYYY-MM-DD";

      if (this.componentProps.props && this.componentProps.props.format) {
        format = this.componentProps.props.format;
      } else if (this.componentProps.props && this.componentProps.props.showTime) {
        format = format + " HH:mm:ss";
      }

      return format;
    }
  },
  watch: {
    currentFormat: function currentFormat() {
      this.blocksData = (0, _dateFormat.getBlocks)(this.currentFormat);
    }
  },
  created: function created() {
    this.blocksData = (0, _dateFormat.getBlocks)(this.currentFormat);
    this.focus = _utils.default.throttle(function () {
      this.onFocus();
    }, 100);
  },
  methods: {
    updateValue: function updateValue(value) {
      this.inputValue = value && value.format ? value.format(this.currentFormat) : "";
      this.$emit("update", value);
      this.$emit("change", value);
    },
    onFocus: function onFocus() {
      var input = this.$refs.inputComponent;
      var el = input.$el; // datePicker 获得焦点，弹出选择面板并全选输入框的值

      var box = el.getElementsByClassName("ant-calendar-picker-input");

      if (box && box.length) {
        var boxDom = box[box.length - 1];
        boxDom.click();
      }

      this.onInputEvent();
    },
    onKeyUpEnter: function onKeyUpEnter(e) {
      if (e.key === "Enter") {
        e.stopPropagation();
        this.ok();
        this.$emit("inputPressEnter", e);
      }
    },
    ok: function ok() {
      var val = (0, _dateFormat.mergeInputDate)(this.inputValue, this.value.format(this.currentFormat), this.currentFormat);
      var newVal = (0, _moment.default)(val, this.currentFormat);

      if (newVal.format(this.currentFormat) !== "Invalid date") {
        this.updateValue(newVal);
      }
    },
    // 监听输入事件
    onInputEvent: function onInputEvent() {
      var that = this;
      setTimeout(function () {
        var input = document.getElementsByClassName("ant-calendar-input");

        if (input && input.length) {
          var inputDom = input[input.length - 1];
          inputDom.selectionStart = 0; // 选中开始位置

          inputDom.selectionEnd = inputDom.value.length;
          inputDom.removeEventListener("keyup", that.onKeyUpEnter);
          inputDom.addEventListener("keyup", that.onKeyUpEnter);

          inputDom.oninput = function (e) {
            var value = e.target.value;

            if (e.target.value && e.inputType !== "deleteContentBackward") {
              var newValue = (0, _dateFormat.formatInputDate)(value, that.blocksData);
              e.target.value = newValue;
            }

            that.inputValue = e.target.value;
          };
        }
      }, 100);
    },
    onOpenChange: function onOpenChange(status) {
      if (status) {
        this.onInputEvent();
      }

      this.$emit("openChange", status);
    },
    renderDisabledDate: function renderDisabledDate(current) {
      if (this.min && current) {
        return current < this.min || this.max && current > this.max;
      } else if (this.min || this.max) {
        return current < this.min || current > this.max.endOf("day");
      }
    }
  },
  render: function render(h) {
    var componentProps = this.componentProps,
        $scopedSlots = this.$scopedSlots,
        value = this.value;
    return h("a-date-picker", {
      ref: "inputComponent",
      props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, componentProps.props), {}, {
        value: value
      }),
      on: (0, _objectSpread2.default)({}, componentProps.on),
      scopedSlots: $scopedSlots
    }, []);
  }
};
exports.default = _default;