UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

240 lines (239 loc) • 7.92 kB
/** * DevExtreme (esm/__internal/ui/date_box/m_date_box.strategy.calendar.js) * Version: 26.1.3 * Build date: Wed Jun 10 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import messageLocalization from "../../../common/core/localization/message"; import dateUtils from "../../../core/utils/date"; import { extend } from "../../../core/utils/extend"; import { isEmptyObject, isFunction } from "../../../core/utils/type"; import { current, isMaterial } from "../../../ui/themes"; import { getGlobalFormatByDataType } from "../../core/m_global_format_config"; import { splitPair } from "../../core/utils/m_common"; import Calendar from "../../ui/calendar/calendar"; import DateBoxStrategy from "./m_date_box.strategy"; const TODAY_BUTTON_CLASS = "dx-button-today"; class CalendarStrategy extends DateBoxStrategy { constructor(dateBox) { super(dateBox); this.NAME = "Calendar" } getWidget() { return this._widget } getDefaultOptions() { const { todayButtonText: todayButtonText } = this.dateBox.option(); return Object.assign({}, super.getDefaultOptions(), { todayButtonText: todayButtonText ?? messageLocalization.format("dxCalendar-todayButtonText") }) } supportedKeys() { const homeEndHandler = e => { if (this.dateBox.option("opened")) { e.preventDefault(); return true } return false }; return { rightArrow() { if (this.option("opened")) { return true } return }, leftArrow() { if (this.option("opened")) { return true } return }, enter: e => { if (this.dateBox.option("opened")) { e.preventDefault(); const { zoomLevel: zoomLevel, maxZoomLevel: maxZoomLevel } = this.getWidget().option(); if (zoomLevel === maxZoomLevel) { const viewValue = this._getContouredValue(); const lastActionElement = this._lastActionElement; const shouldCloseDropDown = this._closeDropDownByEnter(); if (shouldCloseDropDown && viewValue && "calendar" === lastActionElement) { this.dateBoxValue(viewValue, e) } if (shouldCloseDropDown) { this.dateBox.close() } this.dateBox._valueChangeEventHandler(e); return !shouldCloseDropDown } return true } this.dateBox._valueChangeEventHandler(e); return }, home: homeEndHandler, end: homeEndHandler } } getDisplayFormat(displayFormat) { const globalDateFormat = getGlobalFormatByDataType("date"); return displayFormat || globalDateFormat || "shortdate" } _closeDropDownByEnter() { return true } _getWidgetName() { return Calendar } _getContouredValue() { const { contouredDate: contouredDate } = this.getWidget()._view.option(); return contouredDate } getKeyboardListener() { return this.getWidget() } _getWidgetOptions() { const { disabledDates: disabledDates, min: min, max: max, todayButtonText: todayButtonText, calendarOptions: calendarOptions = {} } = this.dateBox.option(); return extend(calendarOptions, { value: this.dateBoxValue() ?? null, selectionMode: "single", dateSerializationFormat: null, min: min, max: max, onValueChanged: this._valueChangedHandler.bind(this), onCellClick: this._cellClickHandler.bind(this), disabledDates: isFunction(disabledDates) ? this._injectComponent(disabledDates.bind(this.dateBox)) : disabledDates, onContouredChanged: this._refreshActiveDescendant.bind(this), skipFocusCheck: true, todayButtonText: todayButtonText }) } _injectComponent(func) { return params => func(Object.assign({}, params, { component: this.dateBox })) } _refreshActiveDescendant(e) { this._lastActionElement = "calendar"; this.dateBox.setAria("activedescendant", e.actionValue) } _getTodayButtonConfig() { const { buttonsLocation: buttonsLocation } = this.dateBox.option(); const isButtonsLocationDefault = "default" === buttonsLocation; const position = isButtonsLocationDefault ? ["bottom", "center"] : splitPair(buttonsLocation); const stylingMode = isMaterial(current()) ? "text" : "outlined"; return { widget: "dxButton", toolbar: position[0], location: "after" === position[1] ? "before" : position[1], options: { onClick: args => { this.getWidget()._toTodayView(args) }, text: this.dateBox.option("todayButtonText"), elementAttr: { class: "dx-button-today" }, stylingMode: stylingMode } } } _isCalendarVisible() { const { calendarOptions: calendarOptions = {} } = this.dateBox.option(); return isEmptyObject(calendarOptions) || false !== calendarOptions.visible } _getPopupToolbarItems(toolbarItems) { const { applyValueMode: applyValueMode } = this.dateBox.option(); const useButtons = "useButtons" === applyValueMode; const shouldRenderTodayButton = useButtons && this._isCalendarVisible(); if (shouldRenderTodayButton) { const todayButton = this._getTodayButtonConfig(); return [todayButton, ...toolbarItems] } return toolbarItems } popupConfig(popupConfig) { return extend(true, popupConfig, { position: { collision: "flipfit flip" }, width: "auto" }) } _valueChangedHandler(e) { const { value: value } = e; const prevValue = e.previousValue; if (dateUtils.sameDate(value, prevValue) && dateUtils.sameHoursAndMinutes(value, prevValue)) { return } const { applyValueMode: applyValueMode } = this.dateBox.option(); if ("instantly" === applyValueMode) { this.dateBoxValue(this.getValue(), e.event) } } _updateValue() { if (!this._widget) { return } const value = this.dateBoxValue(); this._widget.option({ value: value }) } textChangedHandler() { this._lastActionElement = "input"; if (this.dateBox.option("opened") && this._widget) { this._updateValue() } } _cellClickHandler(e) { const { dateBox: dateBox } = this; const { applyValueMode: applyValueMode } = dateBox.option(); if ("instantly" === applyValueMode) { dateBox.option("opened", false); this.dateBoxValue(this.getValue(), e.event) } } } export default CalendarStrategy;