"use strict";

var _interopRequireDefault = require("F:/GIT/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");

var _objectSpread2 = _interopRequireDefault(require("F:/GIT/opu-components-vue/node_modules/@babel/runtime/helpers/esm/objectSpread2.js"));

var _index = require("ant-design-vue/lib/time-picker/index");

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

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

var _default = {
  name: "OpuTimePicker",
  components: {},
  props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, (0, _index.TimePickerProps)()), {}, {
    fieldName: {
      type: String,
      default: ""
    }
  }),
  model: {
    prop: "value",
    event: "update"
  },
  data: function data() {
    return {
      isOpen: false,
      className: ""
    };
  },
  computed: {
    componentProps: function componentProps() {
      var _this = this;

      var $listeners = this.$listeners,
          $options = this.$options;
      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)((0, _objectSpread2.default)({}, propsData), {}, {
          popupClassName: this.className
        }),
        on: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, ons), {}, {
          change: this.updateValue,
          openChange: this.onOpenChange
        })
      };
      return props;
    },
    currentFormat: function currentFormat() {
      var format = "HH:mm:ss";

      if (this.componentProps.props && this.componentProps.props.format) {
        format = this.componentProps.props.format;
      }

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

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

      if (box && box.length) {
        box[0].click();
      } // this.onInputEvent();

    },
    // 监听输入事件
    onInputEvent: function onInputEvent() {
      var _this2 = this;

      var that = this;
      this.$nextTick(function () {
        setTimeout(function () {
          var input = document.querySelector(".".concat(_this2.className, " .ant-time-picker-panel-input"));

          if (input) {
            input.selectionStart = 0; // 选中开始位置

            input.selectionEnd = input.value.length;
            input.removeEventListener("keydown", _this2.onKeydown);
            input.addEventListener("keydown", _this2.onKeydown);
            input.removeEventListener("keyup", _this2.onKeyup);
            input.addEventListener("keyup", _this2.onKeyup);

            input.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;
              }
            };
          }
        }, 100);
      });
    },
    onKeydown: function onKeydown(e) {
      e.stopPropagation();
      this.$emit("keydown");
    },
    onKeyup: function onKeyup(e) {
      if (e.key === "Enter") {
        e.stopPropagation();
        this.isOpen = false;
        this.$emit("inputPressEnter", e);
      }
    },
    onOpenChange: function onOpenChange(status) {
      this.isOpen = status;

      if (status) {
        this.onInputEvent();
      }

      this.$emit("openChange", status);
    }
  },
  render: function render(h) {
    var componentProps = this.componentProps,
        $scopedSlots = this.$scopedSlots,
        isOpen = this.isOpen,
        value = this.value;
    return h("a-time-picker", {
      ref: "inputComponent",
      props: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, componentProps.props), {}, {
        open: isOpen,
        value: value
      }),
      on: (0, _objectSpread2.default)({}, componentProps.on),
      scopedSlots: $scopedSlots
    }, []);
  }
};
exports.default = _default;