{"version":3,"file":"useChipCell.hook.mjs","sources":["../../src/lib/hooks/useChipCell.hook.js"],"sourcesContent":["/*\nCopyright 2019 Iguazio Systems Ltd.\n\nLicensed under the Apache License, Version 2.0 (the \"License\") with\nan addition restriction as set forth herein. You may not use this\nfile except in compliance with the License. You may obtain a copy of\nthe License at http://www.apache.org/licenses/LICENSE-2.0.\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\nimplied. See the License for the specific language governing\npermissions and limitations under the License.\n\nIn addition, you may not use the software for any purposes that are\nillegal under applicable law, and the grant of the foregoing license\nunder the Apache 2.0 license is conditioned upon your compliance with\nsuch restriction.\n*/\nimport { useCallback, useEffect, useMemo, useRef, useState } from 'react'\nimport { throttle } from 'lodash'\n\nimport {\n  getScssVariableValue,\n  getTransitionEndEventName,\n  isEveryObjectValueEmpty\n} from '../utils/common.util'\nimport { getFirstScrollableParent } from '../utils/getFirstScrollableParent.util'\n\nexport const useChipCell = (isEditMode, visibleChipsMaxLength, withInitialParentWidth) => {\n  const [showHiddenChips, setShowHiddenChips] = useState(false)\n  const [chipsSizes, setChipsSizes] = useState({})\n  const [showChips, setShowChips] = useState(false)\n  const [chipCellInitialWidth, setChipCellInitialWidth] = useState(0)\n  const [visibleChipsCount, setVisibleChipsCount] = useState(8)\n\n  const chipBlockMarginRight = useMemo(\n    () => parseInt(getScssVariableValue('--chipBlockMarginRight')),\n    []\n  )\n  const transitionEndEventName = useMemo(() => getTransitionEndEventName(), [])\n\n  const chipsCellRef = useRef()\n  const chipsWrapperRef = useRef()\n  const hiddenChipsCounterRef = useRef()\n  const hiddenChipsPopUpRef = useRef()\n\n  const handleShowElements = useCallback(\n    event => {\n      if (!isEditMode || (isEditMode && visibleChipsMaxLength)) {\n        if (hiddenChipsCounterRef.current?.contains(event.target) && !showHiddenChips) {\n          setShowHiddenChips(true)\n        } else {\n          setShowHiddenChips(false)\n        }\n      }\n\n      event && hiddenChipsCounterRef.current?.contains(event.target) && event.stopPropagation()\n    },\n    [isEditMode, showHiddenChips, visibleChipsMaxLength]\n  )\n\n  useEffect(() => {\n    if (showHiddenChips) {\n      window.addEventListener('click', handleShowElements, true)\n    }\n\n    return () => window.removeEventListener('click', handleShowElements, true)\n  }, [showHiddenChips, handleShowElements])\n\n  useEffect(() => {\n    if (chipsCellRef.current?.getBoundingClientRect().width) {\n      setChipCellInitialWidth(state => {\n        if (!state) {\n          return chipsCellRef.current?.getBoundingClientRect().width\n        }\n      })\n    }\n  }, [])\n\n  const handleScroll = useCallback(\n    event => {\n      if (event.target.parentElement !== hiddenChipsPopUpRef?.current) {\n        setShowHiddenChips(false)\n      }\n    },\n    [hiddenChipsPopUpRef]\n  )\n\n  useEffect(() => {\n    if (showHiddenChips) {\n      window.addEventListener('scroll', handleScroll, true)\n    }\n\n    return () => window.removeEventListener('scroll', handleScroll, true)\n  }, [handleScroll, showHiddenChips])\n\n  const resizeChipCell = useCallback(() => {\n    if (hiddenChipsPopUpRef?.current) {\n      const scrollableParent = getFirstScrollableParent(hiddenChipsCounterRef.current.offsetParent)\n      const scrollableParentRect = scrollableParent.getBoundingClientRect()\n      const hiddenChipsCounterRect = hiddenChipsCounterRef.current?.getBoundingClientRect()\n\n      // Check if the hiddenChipsCounterRect is outside the boundaries of the scrollableParentRect or the window\n      if (\n        hiddenChipsCounterRect.left < scrollableParentRect.left ||\n        hiddenChipsCounterRect.top < scrollableParentRect.top ||\n        hiddenChipsCounterRect.right > scrollableParentRect.right ||\n        hiddenChipsCounterRect.bottom > scrollableParentRect.bottom ||\n        hiddenChipsCounterRect.right > window.innerWidth ||\n        hiddenChipsCounterRect.bottom > window.innerHeight\n      ) {\n        setShowHiddenChips(false)\n      }\n    }\n\n    if (!isEditMode && !isEveryObjectValueEmpty(chipsSizes)) {\n      const parentSize = withInitialParentWidth\n        ? chipCellInitialWidth\n        : chipsCellRef.current?.getBoundingClientRect().width\n      const hiddenChipsCounterWidth =\n        (hiddenChipsCounterRef.current?.getBoundingClientRect().width ?? 0) + chipBlockMarginRight\n\n      let chipsLengthSum = 0\n      let chipIndex = 0\n      const chipsSizesList = Object.values(chipsSizes)\n\n      chipsSizesList.every((chipSize, index) => {\n        const chipSizeWithMargin = chipSize + chipBlockMarginRight\n        const isLastChip = index === chipsSizesList.length - 1\n        const newChipsLengthSum = chipsLengthSum + chipSizeWithMargin\n        const wouldExceedWithCounter = newChipsLengthSum + hiddenChipsCounterWidth > parentSize\n        const wouldExceedWithoutCounter = newChipsLengthSum > parentSize\n\n        // If we've exceeded the limit\n        if (wouldExceedWithCounter) {\n          // Special case: last chip might fit without the counter\n          if (isLastChip && !wouldExceedWithoutCounter) {\n            chipsLengthSum = newChipsLengthSum\n            chipIndex = chipsSizesList.length\n            return true\n          }\n\n          // Stop here - this chip doesn't fit\n          chipIndex = index\n          return false\n        }\n\n        // Chip fits, add it\n        chipsLengthSum = newChipsLengthSum\n\n        if (isLastChip) {\n          chipIndex = chipsSizesList.length\n        }\n\n        return true\n      })\n\n      setVisibleChipsCount(chipIndex)\n      setShowChips(true)\n    }\n  }, [chipBlockMarginRight, chipCellInitialWidth, chipsSizes, isEditMode, withInitialParentWidth])\n\n  useEffect(() => {\n    resizeChipCell()\n  }, [resizeChipCell])\n\n  useEffect(() => {\n    const resizeChipCellDebounced = throttle(resizeChipCell, 500)\n\n    if (!isEditMode) {\n      window.addEventListener('resize', resizeChipCellDebounced)\n      window.addEventListener(transitionEndEventName, resizeChipCellDebounced)\n\n      return () => {\n        window.removeEventListener('resize', resizeChipCellDebounced)\n        window.removeEventListener(transitionEndEventName, resizeChipCellDebounced)\n      }\n    }\n  }, [resizeChipCell, isEditMode, transitionEndEventName])\n\n  return {\n    chipsCellRef,\n    chipsWrapperRef,\n    handleShowElements,\n    hiddenChipsCounterRef,\n    hiddenChipsPopUpRef,\n    setChipsSizes,\n    setShowHiddenChips,\n    showChips,\n    showHiddenChips,\n    visibleChipsCount\n  }\n}\n"],"names":["useChipCell","isEditMode","visibleChipsMaxLength","withInitialParentWidth","showHiddenChips","setShowHiddenChips","useState","chipsSizes","setChipsSizes","showChips","setShowChips","chipCellInitialWidth","setChipCellInitialWidth","visibleChipsCount","setVisibleChipsCount","chipBlockMarginRight","useMemo","getScssVariableValue","transitionEndEventName","getTransitionEndEventName","chipsCellRef","useRef","chipsWrapperRef","hiddenChipsCounterRef","hiddenChipsPopUpRef","handleShowElements","useCallback","event","_a","_b","useEffect","state","handleScroll","resizeChipCell","scrollableParentRect","getFirstScrollableParent","hiddenChipsCounterRect","isEveryObjectValueEmpty","parentSize","hiddenChipsCounterWidth","_c","chipsLengthSum","chipIndex","chipsSizesList","chipSize","index","chipSizeWithMargin","isLastChip","newChipsLengthSum","wouldExceedWithCounter","wouldExceedWithoutCounter","resizeChipCellDebounced","throttle"],"mappings":";;;;AA6BY,MAACA,IAAc,CAACC,GAAYC,GAAuBC,MAA2B;AACxF,QAAM,CAACC,GAAiBC,CAAkB,IAAIC,EAAS,EAAK,GACtD,CAACC,GAAYC,CAAa,IAAIF,EAAS,CAAA,CAAE,GACzC,CAACG,GAAWC,CAAY,IAAIJ,EAAS,EAAK,GAC1C,CAACK,GAAsBC,CAAuB,IAAIN,EAAS,CAAC,GAC5D,CAACO,GAAmBC,CAAoB,IAAIR,EAAS,CAAC,GAEtDS,IAAuBC;AAAA,IAC3B,MAAM,SAASC,EAAqB,wBAAwB,CAAC;AAAA,IAC7D,CAAA;AAAA,EACJ,GACQC,IAAyBF,EAAQ,MAAMG,EAAyB,GAAI,CAAA,CAAE,GAEtEC,IAAeC,EAAM,GACrBC,IAAkBD,EAAM,GACxBE,IAAwBF,EAAM,GAC9BG,IAAsBH,EAAM,GAE5BI,IAAqBC;AAAA,IACzB,CAAAC,MAAS;;AACP,OAAI,CAAC1B,KAAeA,KAAcC,QAC5B0B,IAAAL,EAAsB,YAAtB,QAAAK,EAA+B,SAASD,EAAM,WAAW,CAACvB,IAC5DC,EAAmB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