devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
290 lines (289 loc) • 9.58 kB
JavaScript
/**
* DevExtreme (esm/__internal/ui/date_box/m_date_box.strategy.list.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 "../../ui/list/modules/selection";
import dateLocalization from "../../../common/core/localization/date";
import {
ensureDefined,
noop
} from "../../../core/utils/common";
import dateSerialization from "../../../core/utils/date_serialization";
import {
getHeight,
getOuterHeight
} from "../../../core/utils/size";
import {
isDate
} from "../../../core/utils/type";
import {
getWindow
} from "../../../core/utils/window";
import {
getGlobalFormatByDataType
} from "../../core/m_global_format_config";
import {
getSizeValue
} from "../../ui/drop_down_editor/utils";
import List from "../../ui/list/list.edit.search";
import dateUtils from "./date_utils";
import DateBoxStrategy from "./m_date_box.strategy";
const window = getWindow();
const DATE_FORMAT = "date";
const BOUNDARY_VALUES = {
min: new Date(0, 0, 0, 0, 0),
max: new Date(0, 0, 0, 23, 59)
};
class ListStrategy extends DateBoxStrategy {
constructor(dateBox) {
super(dateBox);
this.NAME = "List"
}
getWidget() {
return this._widget
}
supportedKeys() {
return {
space: noop,
home: noop,
end: noop
}
}
getDefaultOptions() {
return Object.assign({}, super.getDefaultOptions(), {
applyValueMode: "instantly"
})
}
getDisplayFormat(displayFormat) {
const globalTimeFormat = getGlobalFormatByDataType("time");
return displayFormat || globalTimeFormat || "shorttime"
}
popupConfig(popupConfig) {
return popupConfig
}
getValue() {
const {
selectedIndex: selectedIndex = -1
} = this.getWidget().option();
if (-1 === selectedIndex) {
const {
value: value
} = this.dateBox.option();
return value
}
const itemData = this._widgetItems[selectedIndex];
return this._getDateByItemData(itemData)
}
useCurrentDateByDefault() {
return true
}
getDefaultDate() {
return new Date(0)
}
popupShowingHandler() {
this.dateBox._dimensionChanged()
}
_renderWidget() {
super._renderWidget();
this._refreshItems()
}
_getWidgetName() {
return List
}
_getWidgetOptions() {
return {
itemTemplate: this._timeListItemTemplate.bind(this),
selectionMode: "single",
tabIndex: -1,
onItemClick: this._listItemClickHandler.bind(this),
onFocusedItemChanged: e => this._refreshActiveDescendant(e)
}
}
_refreshActiveDescendant(e) {
this.dateBox.setAria("activedescendant", "");
this.dateBox.setAria("activedescendant", e.actionValue)
}
_refreshItems() {
this._widgetItems = this._getTimeListItems();
this.getWidget().option("items", this._widgetItems)
}
renderOpenedState() {
if (!this._widget) {
return
}
this._widget.option("focusedElement", null);
this._setSelectedItemsByValue();
const {
templatesRenderAsynchronously: templatesRenderAsynchronously
} = this.getWidget().option();
if (templatesRenderAsynchronously) {
this._asyncScrollTimeout = setTimeout(this._scrollToSelectedItem.bind(this))
} else {
this._scrollToSelectedItem()
}
}
dispose() {
super.dispose();
clearTimeout(this._asyncScrollTimeout)
}
renderValue() {
this._updateValue()
}
_updateValue() {
if (!this._widget) {
return
}
this._refreshItems();
const {
opened: opened
} = this.dateBox.option();
if (opened) {
this._setSelectedItemsByValue();
this._scrollToSelectedItem()
}
}
_setSelectedItemsByValue() {
const value = this.dateBoxValue();
const dateIndex = this._getDateIndex(value);
const widget = this.getWidget();
if (-1 === dateIndex) {
widget.option("selectedItems", [])
} else {
widget.option("selectedIndex", dateIndex)
}
}
_scrollToSelectedItem() {
this.getWidget().scrollToItem(this.getWidget().option("selectedIndex"))
}
_getDateIndex(date) {
let result = -1;
for (let i = 0, n = this._widgetItems.length; i < n; i += 1) {
if (this._areDatesEqual(date, this._widgetItems[i])) {
result = i;
break
}
}
return result
}
_areDatesEqual(first, second) {
return isDate(first) && isDate(second) && first.getHours() === second.getHours() && first.getMinutes() === second.getMinutes()
}
_getTimeListItems() {
let min = this.dateBox.getDateOption("min") ?? this._getBoundaryDate("min");
const max = this.dateBox.getDateOption("max") ?? this._getBoundaryDate("max");
const value = this.dateBox.getDateOption("value") ?? null;
let delta = max - min;
const {
interval: interval = 30
} = this.dateBox.option();
const minutes = min.getMinutes() % interval;
if (delta < 0) {
return []
}
if (delta > dateUtils.ONE_DAY) {
delta = dateUtils.ONE_DAY
}
if (value - min < dateUtils.ONE_DAY) {
return this._getRangeItems(min, new Date(min), delta)
}
min = this._getBoundaryDate("min");
min.setMinutes(minutes);
if (value && Math.abs(value - max) < dateUtils.ONE_DAY) {
delta = (60 * max.getHours() + Math.abs(max.getMinutes() - minutes)) * dateUtils.ONE_MINUTE
}
return this._getRangeItems(min, new Date(min), delta)
}
_getRangeItems(startValue, currentValue, rangeDuration) {
const rangeItems = [];
const {
interval: interval = 30
} = this.dateBox.option();
while (currentValue - startValue <= rangeDuration) {
rangeItems.push(new Date(currentValue));
currentValue.setMinutes(currentValue.getMinutes() + interval)
}
return rangeItems
}
_getBoundaryDate(boundary) {
const boundaryValue = BOUNDARY_VALUES[boundary];
const dateBoxDate = this.dateBox.getDateOption("value");
const newDate = new Date(0);
const definedValue = ensureDefined(dateBoxDate, newDate);
const currentValue = new Date(definedValue);
return new Date(currentValue.getFullYear(), currentValue.getMonth(), currentValue.getDate(), boundaryValue.getHours(), boundaryValue.getMinutes())
}
_timeListItemTemplate(itemData) {
const {
displayFormat: displayFormat
} = this.dateBox.option();
return dateLocalization.format(itemData, this.getDisplayFormat(displayFormat))
}
_listItemClickHandler(e) {
const {
applyValueMode: applyValueMode
} = this.dateBox.option();
if ("useButtons" === applyValueMode) {
return
}
const date = this._getDateByItemData(e.itemData);
this.dateBox.option("opened", false);
this.dateBoxValue(date, e.event)
}
_getDateByItemData(itemData) {
let {
value: date
} = this.dateBox.option();
const hours = itemData.getHours();
const minutes = itemData.getMinutes();
const seconds = itemData.getSeconds();
const year = itemData.getFullYear();
const month = itemData.getMonth();
const day = itemData.getDate();
if (date) {
const {
dateSerializationFormat: dateSerializationFormat
} = this.dateBox.option();
if (dateSerializationFormat) {
date = dateSerialization.deserializeDate(date)
} else {
date = new Date(date)
}
date.setHours(hours);
date.setMinutes(minutes);
date.setSeconds(seconds);
date.setFullYear(year);
date.setMonth(month);
date.setDate(day)
} else {
date = new Date(year, month, day, hours, minutes, 0, 0)
}
return date
}
getKeyboardListener() {
return this.getWidget()
}
_updatePopupHeight() {
const {
dropDownOptions: dropDownOptions
} = this.dateBox.option();
const dropDownOptionsHeight = getSizeValue(null === dropDownOptions || void 0 === dropDownOptions ? void 0 : dropDownOptions.height);
if (void 0 === dropDownOptionsHeight || "auto" === dropDownOptionsHeight) {
this.dateBox._setPopupOption("height", "auto");
const popupHeight = getOuterHeight(this.getWidget().$element());
const maxHeight = .45 * getHeight(window);
this.dateBox._setPopupOption("height", Math.min(popupHeight, maxHeight))
}
}
getParsedText(text, format) {
let value = super.getParsedText(text, format);
if (value) {
value = dateUtils.mergeDates(value, new Date(null), "date")
}
return value
}
}
export default ListStrategy;