"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];

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

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));

var _tslib = require("tslib");

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _autobind = _interopRequireDefault(require("../_util/autobind"));

var _icon = _interopRequireDefault(require("../icon"));

var _enum = require("./enum");

var _DaysView2 = _interopRequireWildcard(require("./DaysView"));

var _enum2 = require("../data-set/enum");

var _EventManager = require("../_util/EventManager");

var YearsView = /*#__PURE__*/function (_DaysView) {
  (0, _inherits2["default"])(YearsView, _DaysView);

  var _super = (0, _createSuper2["default"])(YearsView);

  function YearsView() {
    (0, _classCallCheck2["default"])(this, YearsView);
    return _super.apply(this, arguments);
  }

  (0, _createClass2["default"])(YearsView, [{
    key: "getViewClassName",
    value: function getViewClassName() {
      var prefixCls = this.prefixCls;
      return "".concat(prefixCls, "-year");
    }
  }, {
    key: "handlePrevYearClick",
    value: function handlePrevYearClick() {
      this.changeCursorDate(this.getCloneDate().subtract(10, 'y'), _enum.ViewMode.decade);
    }
  }, {
    key: "handleYearSelect",
    value: function handleYearSelect() {
      this.changeViewMode(_enum.ViewMode.decade);
    }
  }, {
    key: "handleNextYearClick",
    value: function handleNextYearClick() {
      this.changeCursorDate(this.getCloneDate().add(10, 'y'), _enum.ViewMode.decade);
    }
  }, {
    key: "handleKeyDownHome",
    value: function handleKeyDownHome(e) {
      (0, _EventManager.stopEvent)(e);
      var date = this.getCloneDate();
      this.changeCursorDate(date.subtract(date.year() % 10, 'y'));
    }
  }, {
    key: "handleKeyDownEnd",
    value: function handleKeyDownEnd(e) {
      (0, _EventManager.stopEvent)(e);
      var date = this.getCloneDate();
      this.changeCursorDate(date.add(9 - date.year() % 10, 'y'));
    }
  }, {
    key: "handleKeyDownLeft",
    value: function handleKeyDownLeft(e) {
      (0, _EventManager.stopEvent)(e);

      if (e.altKey) {
        this.changeViewMode(_enum.ViewMode.decade);
      } else {
        this.changeCursorDate(this.getCloneDate().subtract(1, 'y'));
      }
    }
  }, {
    key: "handleKeyDownRight",
    value: function handleKeyDownRight(e) {
      (0, _EventManager.stopEvent)(e);

      if (e.altKey) {
        if (this.props.mode !== _enum.ViewMode.year) {
          this.changeViewMode(_enum.ViewMode.month);
        }
      } else {
        this.changeCursorDate(this.getCloneDate().add(1, 'y'));
      }
    }
  }, {
    key: "handleKeyDownUp",
    value: function handleKeyDownUp(e) {
      (0, _EventManager.stopEvent)(e);
      this.changeCursorDate(this.getCloneDate().subtract(3, 'y'));
    }
  }, {
    key: "handleKeyDownDown",
    value: function handleKeyDownDown(e) {
      (0, _EventManager.stopEvent)(e);
      this.changeCursorDate(this.getCloneDate().add(3, 'y'));
    }
  }, {
    key: "handleKeyDownPageUp",
    value: function handleKeyDownPageUp(e) {
      (0, _EventManager.stopEvent)(e);
      this.changeCursorDate(this.getCloneDate().subtract(e.altKey ? 100 : 10, 'y'));
    }
  }, {
    key: "handleKeyDownPageDown",
    value: function handleKeyDownPageDown(e) {
      (0, _EventManager.stopEvent)(e);
      this.changeCursorDate(this.getCloneDate().add(e.altKey ? 100 : 10, 'y'));
    }
  }, {
    key: "renderHeader",
    value: function renderHeader() {
      var prefixCls = this.prefixCls,
          date = this.props.date;
      var year = date.year() % 10;
      var from = date.clone().subtract(year, 'y');
      var to = from.clone().add(9, 'y');
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "".concat(prefixCls, "-header")
      }, /*#__PURE__*/_react["default"].createElement("a", {
        className: "".concat(prefixCls, "-prev-year"),
        onClick: this.handlePrevYearClick
      }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
        type: "first_page"
      })), /*#__PURE__*/_react["default"].createElement("a", {
        className: "".concat(prefixCls, "-view-select"),
        onClick: this.handleYearSelect
      }, from.year(), " - ", to.year()), /*#__PURE__*/_react["default"].createElement("a", {
        className: "".concat(prefixCls, "-next-year")
      }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
        type: "last_page",
        onClick: this.handleNextYearClick
      })));
    }
  }, {
    key: "renderPanelHead",
    value: function renderPanelHead() {
      return undefined;
    }
  }, {
    key: "renderPanelBody",
    value: function renderPanelBody() {
      var prefixCls = this.prefixCls,
          _this$props = this.props,
          date = _this$props.date,
          _this$props$renderer = _this$props.renderer,
          renderer = _this$props$renderer === void 0 ? this.renderCell : _this$props$renderer,
          _this$props$isValidDa = _this$props.isValidDate,
          isValidDate = _this$props$isValidDa === void 0 ? _DaysView2.alwaysValidDate : _this$props$isValidDa,
          onDateMouseLeave = _this$props.onDateMouseLeave;
      var selected = date.clone();
      var from = date.clone().startOf('y').subtract(date.year() % 10, 'y');
      var to = from.clone().add(10, 'y');
      var prevYear = from.clone().subtract(1, 'y');
      var lastYear = to.clone().add(1, 'y');
      var rows = [];
      var cells = [];

      while (prevYear.isBefore(lastYear)) {
        var _classNames;

        var currentYear = prevYear.clone();
        var isDisabled = !isValidDate(currentYear, selected);
        var className = (0, _classnames["default"])("".concat(prefixCls, "-cell"), (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-old"), currentYear.isBefore(from, 'y')), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-new"), currentYear.isSame(to, 'y')), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-selected"), prevYear.isSame(selected, 'y')), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-disabled"), isDisabled), _classNames));
        var text = String(prevYear.year());
        var yearProps = {
          key: text,
          className: className,
          children: this.renderInner(text)
        };

        if (!isDisabled) {
          yearProps.onClick = this.handleCellClick.bind(this, currentYear);
          yearProps.onMouseEnter = this.handleDateMouseEnter.bind(this, currentYear);
          yearProps.onMouseLeave = onDateMouseLeave;
        }

        cells.push(renderer(yearProps, text, currentYear, selected));

        if (cells.length === 3) {
          rows.push( /*#__PURE__*/_react["default"].createElement("tr", {
            key: text
          }, cells));
          cells = [];
        }

        prevYear.add(1, 'y');
      }

      return rows;
    }
  }, {
    key: "renderFooter",
    value: function renderFooter() {
      return undefined;
    }
  }, {
    key: "getPanelClass",
    value: function getPanelClass() {
      return "".concat(this.prefixCls, "-year-panel");
    }
  }, {
    key: "choose",
    value: function choose(date) {
      var mode = this.props.mode;

      if (mode !== _enum.ViewMode.year) {
        this.changeCursorDate(date);
        this.changeViewMode(_enum.ViewMode.month);
      } else {
        (0, _get2["default"])((0, _getPrototypeOf2["default"])(YearsView.prototype), "choose", this).call(this, date);
      }
    }
  }]);
  return YearsView;
}(_DaysView2["default"]);

exports["default"] = YearsView;
YearsView.displayName = 'YearsView';
YearsView.type = _enum2.FieldType.year;
(0, _tslib.__decorate)([_autobind["default"]], YearsView.prototype, "handlePrevYearClick", null);
(0, _tslib.__decorate)([_autobind["default"]], YearsView.prototype, "handleYearSelect", null);
(0, _tslib.__decorate)([_autobind["default"]], YearsView.prototype, "handleNextYearClick", null);
//# sourceMappingURL=YearsView.js.map
