UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

470 lines (469 loc) • 16.9 kB
/** * DevExtreme (esm/__internal/ui/speed_dial_action/speed_dial_main_item.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import eventsEngine from "../../../common/core/events/core/events_engine"; import config from "../../../core/config"; import $ from "../../../core/renderer"; import { extend } from "../../../core/utils/extend"; import { getHeight } from "../../../core/utils/size"; import { current, isCompact, isFluent, isMaterial } from "../../../ui/themes"; import errors from "../../../ui/widget/ui.errors"; import swatchContainer from "../../core/utils/swatch_container"; import SpeedDialItem from "./speed_dial_item"; const { getSwatchContainer: getSwatchContainer } = swatchContainer; const FAB_MAIN_CLASS = "dx-fa-button-main"; const FAB_MAIN_CLASS_WITH_LABEL = "dx-fa-button-with-label"; const FAB_MAIN_CLASS_WITHOUT_ICON = "dx-fa-button-without-icon"; const FAB_CLOSE_ICON_CLASS = "dx-fa-button-icon-close"; const INVISIBLE_STATE_CLASS = "dx-state-invisible"; let speedDialMainItem = null; const modifyActionOptions = action => { const { animation: animation, actionComponent: actionComponent, actionVisible: actionVisible, actions: actions, activeStateEnabled: activeStateEnabled, direction: direction, elementAttr: elementAttr, hint: hint, hoverStateEnabled: hoverStateEnabled, icon: icon, id: id, index: index, label: label, onClick: onClick, onContentReady: onContentReady, parentPosition: parentPosition, position: position, visible: visible, zIndex: zIndex } = action.option(); const actionOptions = extend({}, { animation: animation, actionComponent: actionComponent, actionVisible: actionVisible, actions: actions, activeStateEnabled: activeStateEnabled, direction: direction, elementAttr: elementAttr, hint: hint, hoverStateEnabled: hoverStateEnabled, icon: icon, id: id, index: index, label: label, onClick: onClick, onContentReady: onContentReady, parentPosition: parentPosition, position: position, visible: visible, zIndex: zIndex }, { onInitialized: null, onDisposing: null }); return actionOptions }; class SpeedDialMainItem extends SpeedDialItem { _getDefaultOptions() { const defaultOptions = { icon: "add", closeIcon: "close", position: { at: "right bottom", my: "right bottom", offset: { x: -16, y: -16 } }, maxSpeedDialActionCount: 5, hint: "", label: "", direction: "auto", actions: [], activeStateEnabled: true, hoverStateEnabled: true, indent: isCompact(current()) ? 49 : 55, childIndent: 40, childOffset: isCompact(current()) ? 2 : 9, callOverlayRenderShading: true, hideOnOutsideClick: true }; return Object.assign({}, super._getDefaultOptions(), defaultOptions, config().floatingActionButtonConfig, { shading: false }) } _defaultOptionsRules() { const rules = [{ device: () => isFluent(current()) && !isCompact(current()), options: { indent: 60, childIndent: 60, childOffset: 0 } }, { device: () => isFluent(current()) && isCompact(current()), options: { indent: 48, childIndent: 48, childOffset: 0 } }, { device: () => isMaterial(current()) && !isCompact(current()), options: { indent: 72, childIndent: 56, childOffset: 8 } }, { device: () => isMaterial(current()) && isCompact(current()), options: { indent: 58, childIndent: 48, childOffset: 1 } }]; return super._defaultOptionsRules().concat(rules) } _render() { this.$element().addClass(FAB_MAIN_CLASS); super._render(); this._moveToContainer(); this._renderCloseIcon(); this._renderClick() } _renderLabel() { super._renderLabel(); this.$element().toggleClass("dx-fa-button-with-label", !!this._$label) } _renderIcon() { super._renderIcon(); const { icon: icon } = this.option(); this.$element().toggleClass("dx-fa-button-without-icon", !icon) } _renderCloseIcon() { this._$closeIcon = this._renderButtonIcon(this._$closeIcon, this._options.silent("closeIcon"), FAB_CLOSE_ICON_CLASS); this._$closeIcon.addClass("dx-state-invisible") } _renderClick() { this._clickAction = 1 === this._getVisibleActions().length ? this._getActionComponent()._createActionByOption("onClick") : this._createAction(this._clickHandler.bind(this)); this._setClickAction() } _getVisibleActions(actions) { const { actions: ownActions } = this.option(); const currentActions = actions ?? ownActions ?? []; return currentActions.filter(action => { const { visible: visible = false } = action.option(); return visible }) } _getCurrentOptions(actions) { const visibleActions = this._getVisibleActions(actions); const currentOptions = 1 === visibleActions.length ? extend(modifyActionOptions(visibleActions[0]), { position: this._getPosition() }) : extend(this._getDefaultOptions(), { visible: 0 !== visibleActions.length }); return currentOptions } _clickHandler() { var _config$floatingActio; const actions = (this._actionItems ?? []).filter(action => { const { actionVisible: actionVisible = false } = action.option(); return actionVisible }).sort((action, nextAction) => { const { index: actionIndex = 0 } = action.option(); const { index: nextActionIndex = 0 } = nextAction.option(); return actionIndex - nextActionIndex }); if (1 === actions.length) { return } const lastActionIndex = actions.length - 1; for (let i = 0; i < actions.length; i += 1) { actions[i].option("animation", this._getActionAnimation(actions[i], i, lastActionIndex)); actions[i].option("position", this._getActionPosition(actions, i)); actions[i]._$wrapper.css("position", this._$wrapper.css("position") ?? ""); actions[i].toggle() } if (null !== (_config$floatingActio = config().floatingActionButtonConfig) && void 0 !== _config$floatingActio && _config$floatingActio.shading) { const { shading: shading = false } = this.option(); this._isShadingShown = !shading; this.option("shading", this._isShadingShown) } this._$icon.toggleClass("dx-state-invisible"); this._$closeIcon.toggleClass("dx-state-invisible") } _updateZIndexStackPosition() { super._updateZIndexStackPosition(); const overlayStack = this._overlayStack(); overlayStack.push(this) } _renderActions() { var _this$_actionItems; const { actions: actions = [] } = this.option(); if (null !== (_this$_actionItems = this._actionItems) && void 0 !== _this$_actionItems && _this$_actionItems.length) { this._actionItems.forEach(actionItem => { actionItem.dispose(); actionItem.$element().remove() }) } const actionItems = []; this._actionItems = actionItems; if (1 === actions.length) { return } for (const action of actions) { const $actionElement = $("<div>").appendTo(getSwatchContainer(action.$element())); eventsEngine.off($actionElement, "click"); eventsEngine.on($actionElement, "click", () => { this._clickHandler() }); action._options.silent("actionComponent", action); action._options.silent("parentPosition", this._getPosition()); action._options.silent("actionVisible", action._options.silent("visible")); actionItems.push(this._createComponent($actionElement, SpeedDialItem, extend({}, modifyActionOptions(action), { visible: false }))) } } _getActionAnimation(action, index, lastActionIndex) { action._options.silent("animation.show.delay", 30 * index); action._options.silent("animation.hide.delay", 30 * (lastActionIndex - index)); const animation = action._options.silent("animation"); return animation } _getDirectionIndex(actions, direction) { if ("auto" === direction) { var _this$$content; const contentHeight = getHeight(this.$content()); const indent = this.initialOption("indent"); const childIndent = this.initialOption("childIndent"); const actionsHeight = indent + childIndent * actions.length - contentHeight; const offsetTop = (null === (_this$$content = this.$content()) || void 0 === _this$$content || null === (_this$$content = _this$$content.offset()) || void 0 === _this$$content ? void 0 : _this$$content.top) ?? 0; if (actionsHeight < offsetTop) { return -1 } const { $visualContainer: $visualContainer } = this._positionController; const containerHeight = $visualContainer ? getHeight($visualContainer) : 0; const offsetBottom = containerHeight - contentHeight - offsetTop; return offsetTop >= offsetBottom ? -1 : 1 } return "down" !== direction ? -1 : 1 } _getActionPosition(actions, index) { const action = actions[index]; const hasActionLabel = Boolean(action._options.silent("label")); const actionOffsetXValue = this.initialOption("childOffset"); let actionOffsetX = 0; if (hasActionLabel && !this._$label) { actionOffsetX = this._isPositionLeft(this._getPosition()) ? actionOffsetXValue : -actionOffsetXValue } const indent = this.initialOption("indent"); const childIndent = this.initialOption("childIndent"); const actionOffsetYValue = indent + childIndent * index; const { direction: direction } = this.option(); const actionOffsetY = this._getDirectionIndex(actions, direction) * actionOffsetYValue; let actionPositionAtMy = "center"; if (hasActionLabel) { actionPositionAtMy = this._isPositionLeft(this._getPosition()) ? "left" : "right" } return { of: this.$content(), at: actionPositionAtMy, my: actionPositionAtMy, offset: { x: actionOffsetX, y: actionOffsetY } } } _outsideClickHandler(e) { if (this._isShadingShown) { const isShadingClick = $(e.target)[0] === this._$wrapper[0]; if (isShadingClick) { e.preventDefault(); this._clickHandler() } } } _setPosition() { const { visible: visible } = this.option(); if (visible) { this._hide(); this._show() } } _getPosition() { return this._getDefaultOptions().position } _getInkRippleContainer() { return this.$content() } _optionChanged(args) { switch (args.name) { case "actions": if (this._isVisible()) { this._renderIcon(); this._renderLabel() } this._renderCloseIcon(); this._renderClick(); this._renderActions(); break; case "maxSpeedDialActionCount": this._renderActions(); break; case "closeIcon": this._renderCloseIcon(); break; case "position": super._optionChanged(args); this._setPosition(); break; case "label": if (this._isVisible()) { this._renderLabel() } this._setPosition(); break; case "icon": if (this._isVisible()) { this._renderIcon() } break; default: super._optionChanged(args) } } } export function initAction(newAction) { newAction._options.silent("onInitializing", null); let isActionExist = false; if (!speedDialMainItem) { const $fabMainElement = $("<div>").appendTo(getSwatchContainer(newAction.$element())); speedDialMainItem = newAction._createComponent($fabMainElement, SpeedDialMainItem, extend({}, modifyActionOptions(newAction), { actions: [newAction] })) } else { const { actions: savedActions = [] } = speedDialMainItem.option(); savedActions.forEach(action => { if (action._options.silent("id") === newAction._options.silent("id")) { isActionExist = true } }); delete speedDialMainItem._options.position; if (!isActionExist) { const { maxSpeedDialActionCount: maxSpeedDialActionCount } = speedDialMainItem.option(); if (speedDialMainItem._getVisibleActions(savedActions).length >= maxSpeedDialActionCount) { newAction.dispose(); errors.log("W1014"); return } savedActions.push(newAction); speedDialMainItem.option(extend(speedDialMainItem._getCurrentOptions(savedActions), { actions: savedActions })) } else if (1 === savedActions.length) { speedDialMainItem.option(extend({}, modifyActionOptions(savedActions[0]), { actions: savedActions, position: speedDialMainItem._getPosition() })) } else { speedDialMainItem.option(extend(speedDialMainItem._getCurrentOptions(savedActions), { actions: savedActions })) } } } export function disposeAction(actionId) { if (!speedDialMainItem) { return } const { actions: actions = [] } = speedDialMainItem.option(); const savedActions = actions.filter(action => action._options.silent("id") !== actionId); if (actions.length === savedActions.length) { return } if (!savedActions.length) { speedDialMainItem.dispose(); speedDialMainItem.$element().remove(); speedDialMainItem = null } else if (1 === savedActions.length) { speedDialMainItem.option(extend({}, modifyActionOptions(savedActions[0]), { actions: savedActions })) } else { speedDialMainItem.option({ actions: savedActions }) } } export function repaint() { if (!speedDialMainItem) { return } const defaultOptions = speedDialMainItem._getDefaultOptions(); const visibleActions = speedDialMainItem._getVisibleActions(); const isSingleActionVisible = 1 === visibleActions.length; const icon = isSingleActionVisible ? visibleActions[0].option().icon : defaultOptions.icon; const label = isSingleActionVisible ? visibleActions[0].option().label : defaultOptions.label; const { actions: actions } = speedDialMainItem.option(); speedDialMainItem.option({ actions: actions, icon: icon, closeIcon: defaultOptions.closeIcon, position: speedDialMainItem._getPosition(), label: label, maxSpeedDialActionCount: defaultOptions.maxSpeedDialActionCount, direction: defaultOptions.direction }) }