utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { forceUpdate } from "@stencil/core";
  7. import { SLOTS as ACTION_MENU_SLOTS } from "../action-menu/resources";
  8. import { SLOTS as ACTION_GROUP_SLOTS } from "../action-group/resources";
  9. export const overflowActionsDebounceInMs = 150;
  10. const groupBufferPx = 2;
  11. const getAverage = (arr) => arr.reduce((p, c) => p + c, 0) / arr.length;
  12. export const geActionDimensions = (actions) => {
  13. const actionLen = actions === null || actions === void 0 ? void 0 : actions.length;
  14. return {
  15. actionWidth: actionLen ? getAverage(actions.map((action) => action.clientWidth || 0)) : 0,
  16. actionHeight: actionLen ? getAverage(actions.map((action) => action.clientHeight || 0)) : 0
  17. };
  18. };
  19. const getMaxActionCount = ({ width, actionWidth, layout, height, actionHeight, groupCount }) => {
  20. const maxContainerPx = layout === "horizontal" ? width : height;
  21. const avgItemPx = layout === "horizontal" ? actionWidth : actionHeight;
  22. return Math.floor((maxContainerPx - groupCount * groupBufferPx) / avgItemPx);
  23. };
  24. export const getOverflowCount = ({ layout, actionCount, actionWidth, width, actionHeight, height, groupCount }) => {
  25. return Math.max(actionCount - getMaxActionCount({ width, actionWidth, layout, height, actionHeight, groupCount }), 0);
  26. };
  27. export const queryActions = (el) => {
  28. return Array.from(el.querySelectorAll("calcite-action")).filter((action) => action.closest("calcite-action-menu") ? action.slot === ACTION_MENU_SLOTS.trigger : true);
  29. };
  30. export const overflowActions = ({ actionGroups, expanded, overflowCount }) => {
  31. let needToSlotCount = overflowCount;
  32. actionGroups.reverse().forEach((group) => {
  33. let slottedWithinGroupCount = 0;
  34. const groupActions = queryActions(group).reverse();
  35. groupActions.forEach((groupAction) => {
  36. if (groupAction.slot === ACTION_GROUP_SLOTS.menuActions) {
  37. groupAction.removeAttribute("slot");
  38. groupAction.textEnabled = expanded;
  39. }
  40. });
  41. if (needToSlotCount > 0) {
  42. groupActions.some((groupAction) => {
  43. const unslottedActions = groupActions.filter((action) => !action.slot);
  44. if (unslottedActions.length > 1 && groupActions.length > 2 && !groupAction.closest("calcite-action-menu")) {
  45. groupAction.textEnabled = true;
  46. groupAction.setAttribute("slot", ACTION_GROUP_SLOTS.menuActions);
  47. slottedWithinGroupCount++;
  48. if (slottedWithinGroupCount > 1) {
  49. needToSlotCount--;
  50. }
  51. }
  52. return needToSlotCount < 1;
  53. });
  54. }
  55. forceUpdate(group);
  56. });
  57. };