( itemsAndGroups, max, filteredChildrenLineReserved = false )
| 1884 | * @returns {Children<T>} result |
| 1885 | */ |
| 1886 | const spaceLimited = ( |
| 1887 | itemsAndGroups, |
| 1888 | max, |
| 1889 | filteredChildrenLineReserved = false |
| 1890 | ) => { |
| 1891 | if (max < 1) { |
| 1892 | return /** @type {Children<T>} */ ({ |
| 1893 | children: undefined, |
| 1894 | filteredChildren: getTotalItems(itemsAndGroups) |
| 1895 | }); |
| 1896 | } |
| 1897 | /** @type {Children<T>[] | undefined} */ |
| 1898 | let children; |
| 1899 | /** @type {number | undefined} */ |
| 1900 | let filteredChildren; |
| 1901 | // This are the groups, which take 1+ lines each |
| 1902 | /** @type {Children<T>[] | undefined} */ |
| 1903 | const groups = []; |
| 1904 | // The sizes of the groups are stored in groupSizes |
| 1905 | /** @type {number[]} */ |
| 1906 | const groupSizes = []; |
| 1907 | // This are the items, which take 1 line each |
| 1908 | /** @type {Children<T>[]} */ |
| 1909 | const items = []; |
| 1910 | // The total of group sizes |
| 1911 | let groupsSize = 0; |
| 1912 | |
| 1913 | for (const itemOrGroup of itemsAndGroups) { |
| 1914 | // is item |
| 1915 | if (!itemOrGroup.children && !itemOrGroup.filteredChildren) { |
| 1916 | items.push(itemOrGroup); |
| 1917 | } else { |
| 1918 | groups.push(itemOrGroup); |
| 1919 | const size = getItemSize(itemOrGroup); |
| 1920 | groupSizes.push(size); |
| 1921 | groupsSize += size; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | if (groupsSize + items.length <= max) { |
| 1926 | // The total size in the current state fits into the max |
| 1927 | // keep all |
| 1928 | children = groups.length > 0 ? [...groups, ...items] : items; |
| 1929 | } else if (groups.length === 0) { |
| 1930 | // slice items to max |
| 1931 | // inner space marks that lines for filteredChildren already reserved |
| 1932 | const limit = max - (filteredChildrenLineReserved ? 0 : 1); |
| 1933 | filteredChildren = items.length - limit; |
| 1934 | items.length = limit; |
| 1935 | children = items; |
| 1936 | } else { |
| 1937 | // limit is the size when all groups are collapsed |
| 1938 | const limit = |
| 1939 | groups.length + |
| 1940 | (filteredChildrenLineReserved || items.length === 0 ? 0 : 1); |
| 1941 | if (limit < max) { |
| 1942 | // calculate how much we are over the size limit |
| 1943 | // this allows to approach the limit faster |
no test coverage detected