{"version":3,"file":"basic-month-table.mjs","names":[],"sources":["../../../../../../../packages/components/date-picker-panel/src/date-picker-com/basic-month-table.vue"],"sourcesContent":["<template>\n  <table\n    role=\"grid\"\n    :aria-label=\"t('el.datepicker.monthTablePrompt')\"\n    :class=\"ns.b()\"\n    @click=\"handleMonthTableClick\"\n    @mousemove=\"handleMouseMove\"\n  >\n    <tbody ref=\"tbodyRef\">\n      <tr v-for=\"(row, key) in rows\" :key=\"key\">\n        <td\n          v-for=\"(cell, key_) in row\"\n          :key=\"key_\"\n          :ref=\"(el) => cell.isSelected && (currentCellRef = el as HTMLElement)\"\n          :class=\"getCellStyle(cell)\"\n          :aria-selected=\"!!cell.isSelected\"\n          :aria-label=\"t(`el.datepicker.month${+cell.text + 1}`)\"\n          :tabindex=\"cell.isSelected ? 0 : -1\"\n          @keydown.space.prevent.stop=\"handleMonthTableClick\"\n          @keydown.enter.prevent.stop=\"handleMonthTableClick\"\n        >\n          <el-date-picker-cell\n            :cell=\"{\n              ...cell,\n              renderText: t('el.datepicker.months.' + months[cell.text]),\n            }\"\n          />\n        </td>\n      </tr>\n    </tbody>\n  </table>\n</template>\n\n<script lang=\"ts\" setup>\nimport { computed, nextTick, ref, watch } from 'vue'\nimport dayjs from 'dayjs'\nimport { useLocale, useNamespace } from '@element-plus/hooks'\nimport { castArray, hasClass } from '@element-plus/utils'\nimport { basicMonthTableProps } from '../props/basic-month-table'\nimport { datesInMonth, getValidDateOfMonth } from '../utils'\nimport ElDatePickerCell from './basic-cell-render'\n\nimport type { Dayjs } from 'dayjs'\n\ntype MonthCell = {\n  column: number\n  customClass: string | undefined\n  disabled: boolean\n  end: boolean\n  inRange: boolean\n  row: number\n  selected: Dayjs | undefined\n  isCurrent: boolean | undefined\n  isSelected: boolean\n  start: boolean\n  text: number\n  renderText: string | undefined\n  timestamp: number | undefined\n  date: Date | undefined\n  dayjs: Dayjs | undefined\n  type: 'normal' | 'today'\n}\n\nconst props = defineProps(basicMonthTableProps)\nconst emit = defineEmits(['changerange', 'pick', 'select'])\n\nconst ns = useNamespace('month-table')\n\nconst { t, lang } = useLocale()\nconst tbodyRef = ref<HTMLElement>()\nconst currentCellRef = ref<HTMLElement>()\nconst months = ref(\n  props.date\n    .locale('en')\n    .localeData()\n    .monthsShort()\n    .map((_) => _.toLowerCase())\n)\nconst tableRows = ref<MonthCell[][]>([[], [], []])\nconst lastRow = ref<number>()\nconst lastColumn = ref<number>()\nconst rows = computed<MonthCell[][]>(() => {\n  const rows = tableRows.value\n\n  const now = dayjs().locale(lang.value).startOf('month')\n\n  for (let i = 0; i < 3; i++) {\n    const row = rows[i]\n    for (let j = 0; j < 4; j++) {\n      const cell = (row[j] ||= {\n        row: i,\n        column: j,\n        type: 'normal',\n        inRange: false,\n        start: false,\n        end: false,\n        text: -1,\n        disabled: false,\n        isSelected: false,\n        customClass: undefined,\n        date: undefined,\n        dayjs: undefined,\n        isCurrent: undefined,\n        selected: undefined,\n        renderText: undefined,\n        timestamp: undefined,\n      })\n\n      cell.type = 'normal'\n\n      const index = i * 4 + j\n      const calTime = props.date.startOf('year').month(index)\n\n      const calEndDate =\n        props.rangeState.endDate ||\n        props.maxDate ||\n        (props.rangeState.selecting && props.minDate) ||\n        null\n\n      cell.inRange =\n        !!(\n          props.minDate &&\n          calTime.isSameOrAfter(props.minDate, 'month') &&\n          calEndDate &&\n          calTime.isSameOrBefore(calEndDate, 'month')\n        ) ||\n        !!(\n          props.minDate &&\n          calTime.isSameOrBefore(props.minDate, 'month') &&\n          calEndDate &&\n          calTime.isSameOrAfter(calEndDate, 'month')\n        )\n\n      if (props.minDate?.isSameOrAfter(calEndDate)) {\n        cell.start = !!(calEndDate && calTime.isSame(calEndDate, 'month'))\n        cell.end = props.minDate && calTime.isSame(props.minDate, 'month')\n      } else {\n        cell.start = !!(props.minDate && calTime.isSame(props.minDate, 'month'))\n        cell.end = !!(calEndDate && calTime.isSame(calEndDate, 'month'))\n      }\n\n      const isToday = now.isSame(calTime)\n      if (isToday) {\n        cell.type = 'today'\n      }\n\n      const cellDate = calTime.toDate()\n      cell.text = index\n      cell.disabled = props.disabledDate?.(cellDate) || false\n      cell.date = cellDate\n      cell.customClass = props.cellClassName?.(cellDate)\n      cell.dayjs = calTime\n      cell.timestamp = calTime.valueOf()\n      cell.isSelected = isSelectedCell(cell)\n    }\n  }\n  return rows\n})\n\nconst focus = () => {\n  currentCellRef.value?.focus()\n}\n\nconst getCellStyle = (cell: MonthCell) => {\n  const style = {} as any\n  const year = props.date.year()\n  const today = new Date()\n  const month = cell.text\n\n  style.disabled =\n    props.disabled ||\n    (props.disabledDate\n      ? datesInMonth(props.date, year, month, lang.value).every(\n          props.disabledDate\n        )\n      : false)\n  style.current = castArray(props.parsedValue).some(\n    (date) =>\n      dayjs.isDayjs(date) && date.year() === year && date.month() === month\n  )\n  style.today = today.getFullYear() === year && today.getMonth() === month\n\n  if (cell.customClass) {\n    style[cell.customClass] = true\n  }\n  if (cell.inRange) {\n    style['in-range'] = true\n\n    if (cell.start) {\n      style['start-date'] = true\n    }\n\n    if (cell.end) {\n      style['end-date'] = true\n    }\n  }\n  return style\n}\n\nconst isSelectedCell = (cell: MonthCell) => {\n  const year = props.date.year()\n  const month = cell.text\n  return castArray(props.date).some(\n    (date) => date.year() === year && date.month() === month\n  )\n}\n\nconst handleMouseMove = (event: MouseEvent) => {\n  if (!props.rangeState.selecting) return\n\n  let target = event.target as HTMLElement\n  if (target.tagName === 'SPAN') {\n    target = target.parentNode?.parentNode as HTMLElement\n  }\n  if (target.tagName === 'DIV') {\n    target = target.parentNode as HTMLElement\n  }\n  if (target.tagName !== 'TD') return\n\n  const row = (target.parentNode as HTMLTableRowElement).rowIndex\n  const column = (target as HTMLTableCellElement).cellIndex\n  // can not select disabled date\n  if (rows.value[row][column].disabled) return\n\n  // only update rangeState when mouse moves to a new cell\n  // this avoids frequent Date object creation and improves performance\n  if (row !== lastRow.value || column !== lastColumn.value) {\n    lastRow.value = row\n    lastColumn.value = column\n    emit('changerange', {\n      selecting: true,\n      endDate: props.date.startOf('year').month(row * 4 + column),\n    })\n  }\n}\nconst handleMonthTableClick = (event: MouseEvent | KeyboardEvent) => {\n  if (props.disabled) return\n  const target = (event.target as HTMLElement)?.closest(\n    'td'\n  ) as HTMLTableCellElement\n  if (target?.tagName !== 'TD') return\n  if (hasClass(target, 'disabled')) return\n  const column = target.cellIndex\n  const row = (target.parentNode as HTMLTableRowElement).rowIndex\n  const month = row * 4 + column\n  const newDate = props.date.startOf('year').month(month)\n  if (props.selectionMode === 'months') {\n    if (event.type === 'keydown') {\n      emit('pick', castArray(props.parsedValue), false)\n      return\n    }\n    const newMonth = getValidDateOfMonth(\n      props.date,\n      props.date.year(),\n      month,\n      lang.value,\n      props.disabledDate\n    )\n    const newValue = hasClass(target, 'current')\n      ? castArray(props.parsedValue).filter(\n          (d) =>\n            // Filter out the selected month only when both year and month match\n            // This allows remove same months from different years #20019\n            d?.year() !== newMonth.year() || d?.month() !== newMonth.month()\n        )\n      : castArray(props.parsedValue).concat([dayjs(newMonth)])\n    emit('pick', newValue)\n  } else if (props.selectionMode === 'range') {\n    if (!props.rangeState.selecting) {\n      emit('pick', { minDate: newDate, maxDate: null })\n      emit('select', true)\n    } else {\n      if (props.minDate && newDate >= props.minDate) {\n        emit('pick', { minDate: props.minDate, maxDate: newDate })\n      } else {\n        emit('pick', { minDate: newDate, maxDate: props.minDate })\n      }\n      emit('select', false)\n    }\n  } else {\n    emit('pick', month)\n  }\n}\n\nwatch(\n  () => props.date,\n  async () => {\n    if (tbodyRef.value?.contains(document.activeElement)) {\n      await nextTick()\n      currentCellRef.value?.focus()\n    }\n  }\n)\n\ndefineExpose({\n  /**\n   * @description focus current cell\n   */\n  focus,\n})\n</script>\n"],"mappings":""}