UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

222 lines (221 loc) • 9.68 kB
/** * DevExtreme (esm/__internal/scheduler/tooltip_strategies/m_tooltip_strategy_base.js) * Version: 25.2.7 * Build date: Tue May 05 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import $ from "../../../core/renderer"; import { FunctionTemplate } from "../../../core/templates/function_template"; import { isRenderer } from "../../../core/utils/type"; import Button from "../../../ui/button"; import { createPromise } from "../../core/utils/promise"; import List from "../../ui/list/list.edit"; const TOOLTIP_APPOINTMENT_ITEM = "dx-tooltip-appointment-item"; const TOOLTIP_APPOINTMENT_ITEM_CONTENT = `${TOOLTIP_APPOINTMENT_ITEM}-content`; const TOOLTIP_APPOINTMENT_ITEM_CONTENT_SUBJECT = `${TOOLTIP_APPOINTMENT_ITEM}-content-subject`; const TOOLTIP_APPOINTMENT_ITEM_CONTENT_DATE = `${TOOLTIP_APPOINTMENT_ITEM}-content-date`; const TOOLTIP_APPOINTMENT_ITEM_MARKER = `${TOOLTIP_APPOINTMENT_ITEM}-marker`; const TOOLTIP_APPOINTMENT_ITEM_MARKER_BODY = `${TOOLTIP_APPOINTMENT_ITEM}-marker-body`; const TOOLTIP_APPOINTMENT_ITEM_DELETE_BUTTON_CONTAINER = `${TOOLTIP_APPOINTMENT_ITEM}-delete-button-container`; const TOOLTIP_APPOINTMENT_ITEM_DELETE_BUTTON = `${TOOLTIP_APPOINTMENT_ITEM}-delete-button`; const APPOINTMENT_TOOLTIP_TEMPLATE = "appointmentTooltipTemplate"; export class TooltipStrategyBase { constructor(options) { this.asyncTemplatePromises = new Set; this.tooltip = null; this._options = options; this.extraOptions = null } show(target, dataList, extraOptions) { if (this.canShowTooltip(dataList)) { this.hide(); this.extraOptions = extraOptions; this.showCore(target, dataList) } } showCore(target, dataList) { const describedByValue = isRenderer(target) && target.attr("aria-describedby"); if (!this.tooltip) { this.tooltip = this.createTooltip(target, dataList) } else { this.shouldUseTarget() && this.tooltip.option("target", target); this.list.option("dataSource", dataList) } this.prepareBeforeVisibleChanged(dataList); this.tooltip.option("visible", true); describedByValue && target.attr("aria-describedby", describedByValue) } prepareBeforeVisibleChanged(dataList) {} isDeletingAllowed(appointment) { const { editing: editing } = this.extraOptions; const disabled = this._options.getAppointmentDisabled(appointment); const isDeletingAllowed = true === editing || true === (null === editing || void 0 === editing ? void 0 : editing.allowDeleting); return !disabled && isDeletingAllowed } getContentTemplate(dataList) { return container => { var _this$list$registerKe, _this$list, _this$list$registerKe2, _this$list2; const listElement = $("<div>"); $(container).append(listElement); this.list = this.createList(listElement, dataList); null === (_this$list$registerKe = (_this$list = this.list).registerKeyHandler) || void 0 === _this$list$registerKe || _this$list$registerKe.call(_this$list, "escape", () => { this.hide(); this.tooltip.option("target").focus() }); null === (_this$list$registerKe2 = (_this$list2 = this.list).registerKeyHandler) || void 0 === _this$list$registerKe2 || _this$list$registerKe2.call(_this$list2, "del", () => { const { focusedElement: focusedElement } = this.list.option(); if (!focusedElement) { return } const { appointment: appointment, targetedAppointment: targetedAppointment } = this.list._getItemData(focusedElement); if (!appointment) { return } if (this.isDeletingAllowed(appointment)) { this.hide(); this._options.checkAndDeleteAppointment(appointment, targetedAppointment) } }) } } isAlreadyShown(target) { if (this.tooltip && this.tooltip.option("visible")) { return this.tooltip.option("target")[0] === target[0] } return } onShown() { this.list.option("focusStateEnabled", this.extraOptions.focusStateEnabled) } dispose() {} hide() { if (this.tooltip) { this.tooltip.option("visible", false) } } shouldUseTarget() { return true } createTooltip(target, dataList) {} canShowTooltip(dataList) { if (!dataList.length) { return false } return true } createListOption(dataList) { return { dataSource: dataList, onContentReady: this.onListRender.bind(this), onItemClick: e => this.onListItemClick(e), onItemContextMenu: this.onListItemContextMenu.bind(this), itemTemplate: (item, index) => this.renderTemplate(item.appointment, item.targetedAppointment, index, item.color), _swipeEnabled: false, pageLoadMode: "scrollBottom" } } onListRender(e) {} createTooltipElement(wrapperClass) { return $("<div>").appendTo(this._options.container).addClass(wrapperClass) } createList(listElement, dataList) { return this._options.createComponent(listElement, List, this.createListOption(dataList)) } renderTemplate(appointment, targetedAppointment, index, color) { const itemListContent = this.createItemListContent(appointment, targetedAppointment, color); this._options.addDefaultTemplates({ appointmentTooltip: new FunctionTemplate(options => { const $container = $(options.container); $container.append(itemListContent); return $container }) }); const template = this._options.getAppointmentTemplate("appointmentTooltipTemplate"); return this.createFunctionTemplate(template, appointment, targetedAppointment, index) } createFunctionTemplate(template, appointmentData, targetedAppointmentData, index) { const isButtonClicked = Boolean(this.extraOptions.isButtonClick); return new FunctionTemplate(options => { const { promise: promise, resolve: resolve } = createPromise(); this.asyncTemplatePromises.add(promise); return template.render({ model: { appointmentData: appointmentData, targetedAppointmentData: targetedAppointmentData, isButtonClicked: isButtonClicked }, container: options.container, index: index, onRendered: () => { this.asyncTemplatePromises.delete(promise); resolve() } }) }) } onListItemClick(e) { this.hide(); this.extraOptions.clickEvent && this.extraOptions.clickEvent(e); this._options.showAppointmentPopup(e.itemData.appointment, false, e.itemData.targetedAppointment) } onListItemContextMenu(e) {} createItemListContent(appointment, targetedAppointment, color) { const $itemElement = $("<div>").addClass(TOOLTIP_APPOINTMENT_ITEM); $itemElement.append(this.createItemListMarker(color)); $itemElement.append(this.createItemListInfo(this._options.createFormattedDateText(appointment, targetedAppointment))); if (this.isDeletingAllowed(appointment)) { $itemElement.append(this.createDeleteButton(appointment, targetedAppointment)) } return $itemElement } createItemListMarker(color) { const $marker = $("<div>").addClass(TOOLTIP_APPOINTMENT_ITEM_MARKER); const $markerBody = $("<div>").addClass(TOOLTIP_APPOINTMENT_ITEM_MARKER_BODY); $marker.append($markerBody); color.then(value => { if (value) { $markerBody.css("background", value) } }); return $marker } createItemListInfo(object) { const result = $("<div>").addClass(TOOLTIP_APPOINTMENT_ITEM_CONTENT); const $title = $("<div>").addClass(TOOLTIP_APPOINTMENT_ITEM_CONTENT_SUBJECT).text(object.text); const $date = $("<div>").addClass(TOOLTIP_APPOINTMENT_ITEM_CONTENT_DATE).text(object.formatDate); return result.append($title).append($date) } createDeleteButton(appointment, targetedAppointment) { const $container = $("<div>").addClass(TOOLTIP_APPOINTMENT_ITEM_DELETE_BUTTON_CONTAINER); const $deleteButton = $("<div>").addClass(TOOLTIP_APPOINTMENT_ITEM_DELETE_BUTTON); $container.append($deleteButton); this._options.createComponent($deleteButton, Button, { icon: "trash", stylingMode: "text", onClick: e => { this.hide(); e.event.stopPropagation(); this._options.checkAndDeleteAppointment(appointment, targetedAppointment) } }); return $container } }