UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

270 lines (269 loc) • 9.66 kB
/** * DevExtreme (esm/__internal/scheduler/header/m_header.js) * Version: 25.1.3 * Build date: Wed Jun 25 2025 * * Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import _extends from "@babel/runtime/helpers/esm/extends"; import "../../../ui/button_group"; import "../../../ui/drop_down_button"; import registerComponent from "../../../core/component_registrator"; import devices from "../../../core/devices"; import errors from "../../../core/errors"; import $ from "../../../core/renderer"; import { getPathParts } from "../../../core/utils/data"; import dateUtils from "../../../core/utils/date"; import { extend } from "../../../core/utils/extend"; import Toolbar from "../../../ui/toolbar"; import Widget from "../../../ui/widget/ui.widget"; import { viewsUtils } from "../../scheduler/r1/utils/index"; import SchedulerCalendar from "./m_calendar"; import { getDateNavigator } from "./m_date_navigator"; import { getCaption, getNextIntervalDate, getStep, getViewName, getViewType, nextWeek, validateViews } from "./m_utils"; import { getDropDownViewSwitcher, getTabViewSwitcher } from "./m_view_switcher"; import { getTodayButtonOptions } from "./today"; const CLASSES = { component: "dx-scheduler-header", invisible: "dx-state-invisible" }; const ITEM_NAMES = { today: "today", dateNavigator: "dateNavigator", viewSwitcher: "viewSwitcher" }; export class SchedulerHeader extends Widget { get views() { return this.option("views") } get captionText() { return this._getCaption().text } get intervalOptions() { const step = getStep(this.currentView); const intervalCount = this.option("intervalCount"); const firstDayOfWeek = this.option("firstDayOfWeek"); const agendaDuration = this.option("agendaDuration"); return { step: step, intervalCount: intervalCount, firstDayOfWeek: firstDayOfWeek, agendaDuration: agendaDuration } } _getDefaultOptions() { return extend(super._getDefaultOptions(), { _useShortDateFormat: !devices.real().generic || devices.isSimulator() }) } _createEventMap() { this.eventMap = new Map([ ["currentView", [view => { this.currentView = viewsUtils.getCurrentView(getViewName(view), this.option("views")) }]], ["views", [validateViews]], ["currentDate", [this._getCalendarOptionUpdater("value")]], ["min", [this._getCalendarOptionUpdater("min")]], ["max", [this._getCalendarOptionUpdater("max")]], ["tabIndex", [this.repaint.bind(this)]], ["focusStateEnabled", [this.repaint.bind(this)]], ["useDropDownViewSwitcher", [this.repaint.bind(this)]], ["indicatorTime", []] ]) } _addEvent(name, event) { if (!this.eventMap.has(name)) { this.eventMap.set(name, []) } const events = this.eventMap.get(name); this.eventMap.set(name, [...events, event]) } _optionChanged(args) { const { name: name, value: value } = args; if (this.eventMap.has(name)) { const events = this.eventMap.get(name); events.forEach((event => { event(value) })) } } onToolbarOptionChanged(fullName, value) { const parts = getPathParts(fullName); const optionName = fullName.replace(/^toolbar\./, ""); this.option(fullName, value); this._toggleVisibility(); switch (true) { case "toolbar" === fullName: this.repaint(); break; case "toolbar.items" === fullName: this._toolbar.option("items", value.map((item => this._parseItem(item)))); break; case "items" === parts[1] && 3 === parts.length: this._toolbar.option(optionName, this._parseItem(value)); break; default: this._toolbar.option(optionName, value) } } _init() { super._init(); this._createEventMap(); this.$element().addClass(CLASSES.component); this.currentView = viewsUtils.getCurrentView(getViewName(this.option("currentView")), this.option("views")) } _render() { super._render(); this._createEventMap(); this._renderToolbar(); this._toggleVisibility() } _renderToolbar() { const config = this._createToolbarConfig(); const toolbarElement = $("<div>"); toolbarElement.appendTo(this.$element()); this._toolbar = this._createComponent(toolbarElement, Toolbar, config) } _toggleVisibility() { const toolbarOptions = this.option("toolbar"); const isHeaderShown = toolbarOptions.visible || void 0 === toolbarOptions.visible && toolbarOptions.items.length; if (isHeaderShown) { this.$element().removeClass(CLASSES.invisible) } else { this.$element().addClass(CLASSES.invisible) } } _createToolbarConfig() { const options = this.option("toolbar"); const parsedItems = options.items.map((element => this._parseItem(element))); return _extends({}, options, { items: parsedItems }) } _parseItem(item) { const itemName = "string" === typeof item ? item : item.name; const itemOptions = "string" === typeof item ? {} : item; if (itemName) { switch (itemName) { case ITEM_NAMES.today: return getTodayButtonOptions(this, itemOptions); case ITEM_NAMES.viewSwitcher: return this.option("useDropDownViewSwitcher") ? getDropDownViewSwitcher(this, itemOptions) : getTabViewSwitcher(this, itemOptions); case ITEM_NAMES.dateNavigator: this._renderCalendar(); return getDateNavigator(this, itemOptions); default: errors.log(`Unknown default element type: ${itemName}`) } } return extend(true, {}, item) } _callEvent(event, arg) { if (this.eventMap.has(event)) { const events = this.eventMap.get(event); events.forEach((event => event(arg))) } } _updateCurrentView(view) { const onCurrentViewChange = this.option("onCurrentViewChange"); onCurrentViewChange(view.name); this._callEvent("currentView", view) } _updateCalendarValueAndCurrentDate(date) { this._updateCurrentDate(date); this._calendar.option("value", date) } _updateCurrentDate(date) { const onCurrentDateChange = this.option("onCurrentDateChange"); onCurrentDateChange(date); this._callEvent("currentDate", date) } _renderCalendar() { this._calendar = this._createComponent("<div>", SchedulerCalendar, { value: this.option("currentDate"), min: this.option("min"), max: this.option("max"), firstDayOfWeek: this.option("firstDayOfWeek"), focusStateEnabled: this.option("focusStateEnabled"), tabIndex: this.option("tabIndex"), onValueChanged: e => { this._updateCurrentDate(e.value); this._calendar.hide() } }); this._calendar.$element().appendTo(this.$element()) } _getCalendarOptionUpdater(name) { return value => { if (this._calendar) { this._calendar.option(name, value) } } } _getNextDate(direction, initialDate) { const date = initialDate ?? this.option("currentDate"); const options = _extends({}, this.intervalOptions, { date: date }); return getNextIntervalDate(options, direction) } _isMonth() { return "month" === getViewType(this.currentView) } _getDisplayedDate() { const startViewDate = new Date(this.option("startViewDate")); return this._isMonth() ? nextWeek(startViewDate) : startViewDate } _getCaptionOptions() { let date = this.option("currentDate"); if (this.option("startViewDate")) { date = this._getDisplayedDate() } date = dateUtils.trimTime(date); return _extends({}, this.intervalOptions, { date: date }) } _getCaption() { const options = this._getCaptionOptions(); const customizationFunction = this.option("customizeDateNavigatorText"); const useShortDateFormat = this.option("_useShortDateFormat"); return getCaption(options, Boolean(useShortDateFormat), customizationFunction) } _updateDateByDirection(direction) { const date = this._getNextDate(direction); this._updateCalendarValueAndCurrentDate(date) } _showCalendar(e) { this._calendar.show(e.element) } _hideCalendar() { this._calendar.hide() } } registerComponent("dxSchedulerHeader", SchedulerHeader);