{"version":3,"file":"select.mjs","names":["useProxiedModel","computed","inject","provide","deepEqual","propsFactory","wrapInArray","singleSelectStrategy","showSelectAll","allSelected","select","_ref","items","value","Set","selectAll","_ref2","selected","pageSelectStrategy","_ref3","currentPage","_ref4","item","add","delete","_ref5","allSelectStrategy","_ref6","allItems","_ref7","_ref8","makeDataTableSelectProps","showSelect","Boolean","selectStrategy","type","String","Object","default","modelValue","Array","valueComparator","Function","VDataTableSelectionSymbol","Symbol","for","provideSelection","props","_ref9","v","map","find","values","allSelectable","filter","selectable","currentPageSelectable","isSelected","every","has","isSomeSelected","some","newSelected","toggleSelect","someSelected","size","length","data","useSelection","Error"],"sources":["../../../../src/components/VDataTable/composables/select.ts"],"sourcesContent":["// Composables\nimport { useProxiedModel } from '@/composables/proxiedModel'\n\n// Utilities\nimport { computed, inject, provide } from 'vue'\nimport { deepEqual, propsFactory, wrapInArray } from '@/util'\n\n// Types\nimport type { InjectionKey, PropType, Ref } from 'vue'\nimport type { DataTableItemProps } from './items'\nimport type { EventProp } from '@/util'\n\nexport interface SelectableItem {\n  value: any\n  selectable: boolean\n}\n\nexport interface DataTableSelectStrategy {\n  showSelectAll: boolean\n  allSelected: (data: {\n    allItems: SelectableItem[]\n    currentPage: SelectableItem[]\n  }) => SelectableItem[]\n  select: (data: {\n    items: SelectableItem[]\n    value: boolean\n    selected: Set<unknown>\n  }) => Set<unknown>\n  selectAll: (data: {\n    value: boolean\n    allItems: SelectableItem[]\n    currentPage: SelectableItem[]\n    selected: Set<unknown>\n  }) => Set<unknown>\n}\n\ntype SelectionProps = Pick<DataTableItemProps, 'itemValue'> & {\n  modelValue: readonly any[]\n  selectStrategy: 'single' | 'page' | 'all'\n  valueComparator: typeof deepEqual\n  'onUpdate:modelValue': EventProp<[any[]]> | undefined\n}\n\nconst singleSelectStrategy: DataTableSelectStrategy = {\n  showSelectAll: false,\n  allSelected: () => [],\n  select: ({ items, value }) => {\n    return new Set(value ? [items[0]?.value] : [])\n  },\n  selectAll: ({ selected }) => selected,\n}\n\nconst pageSelectStrategy: DataTableSelectStrategy = {\n  showSelectAll: true,\n  allSelected: ({ currentPage }) => currentPage,\n  select: ({ items, value, selected }) => {\n    for (const item of items) {\n      if (value) selected.add(item.value)\n      else selected.delete(item.value)\n    }\n\n    return selected\n  },\n  selectAll: ({ value, currentPage, selected }) => pageSelectStrategy.select({ items: currentPage, value, selected }),\n}\n\nconst allSelectStrategy: DataTableSelectStrategy = {\n  showSelectAll: true,\n  allSelected: ({ allItems }) => allItems,\n  select: ({ items, value, selected }) => {\n    for (const item of items) {\n      if (value) selected.add(item.value)\n      else selected.delete(item.value)\n    }\n\n    return selected\n  },\n  selectAll: ({ value, allItems, selected }) => allSelectStrategy.select({ items: allItems, value, selected }),\n}\n\nexport const makeDataTableSelectProps = propsFactory({\n  showSelect: Boolean,\n  selectStrategy: {\n    type: [String, Object] as PropType<'single' | 'page' | 'all'>,\n    default: 'page',\n  },\n  modelValue: {\n    type: Array as PropType<readonly any[]>,\n    default: () => ([]),\n  },\n  valueComparator: {\n    type: Function as PropType<typeof deepEqual>,\n    default: deepEqual,\n  },\n}, 'DataTable-select')\n\nexport const VDataTableSelectionSymbol: InjectionKey<ReturnType<typeof provideSelection>> = Symbol.for('vuetify:data-table-selection')\n\nexport function provideSelection (\n  props: SelectionProps,\n  { allItems, currentPage }: { allItems: Ref<SelectableItem[]>, currentPage: Ref<SelectableItem[]> }\n) {\n  const selected = useProxiedModel(props, 'modelValue', props.modelValue, v => {\n    return new Set(wrapInArray(v).map(v => {\n      return allItems.value.find(item => props.valueComparator(v, item.value))?.value ?? v\n    }))\n  }, v => {\n    return [...v.values()]\n  })\n\n  const allSelectable = computed(() => allItems.value.filter(item => item.selectable))\n  const currentPageSelectable = computed(() => currentPage.value.filter(item => item.selectable))\n\n  const selectStrategy = computed(() => {\n    if (typeof props.selectStrategy === 'object') return props.selectStrategy\n\n    switch (props.selectStrategy) {\n      case 'single': return singleSelectStrategy\n      case 'all': return allSelectStrategy\n      case 'page':\n      default: return pageSelectStrategy\n    }\n  })\n\n  function isSelected (items: SelectableItem | SelectableItem[]) {\n    return wrapInArray(items).every(item => selected.value.has(item.value))\n  }\n\n  function isSomeSelected (items: SelectableItem | SelectableItem[]) {\n    return wrapInArray(items).some(item => selected.value.has(item.value))\n  }\n\n  function select (items: SelectableItem[], value: boolean) {\n    const newSelected = selectStrategy.value.select({\n      items,\n      value,\n      selected: new Set(selected.value),\n    })\n\n    selected.value = newSelected\n  }\n\n  function toggleSelect (item: SelectableItem) {\n    select([item], !isSelected([item]))\n  }\n\n  function selectAll (value: boolean) {\n    const newSelected = selectStrategy.value.selectAll({\n      value,\n      allItems: allSelectable.value,\n      currentPage: currentPageSelectable.value,\n      selected: new Set(selected.value),\n    })\n\n    selected.value = newSelected\n  }\n\n  const someSelected = computed(() => selected.value.size > 0)\n  const allSelected = computed(() => {\n    const items = selectStrategy.value.allSelected({\n      allItems: allSelectable.value,\n      currentPage: currentPageSelectable.value,\n    })\n    return !!items.length && isSelected(items)\n  })\n\n  const data = {\n    toggleSelect,\n    select,\n    selectAll,\n    isSelected,\n    isSomeSelected,\n    someSelected,\n    allSelected,\n    showSelectAll: selectStrategy.value.showSelectAll,\n  }\n\n  provide(VDataTableSelectionSymbol, data)\n\n  return data\n}\n\nexport function useSelection () {\n  const data = inject(VDataTableSelectionSymbol)\n\n  if (!data) throw new Error('Missing selection!')\n\n  return data\n}\n"],"mappings":"AAAA;AAAA,SACSA,eAAe,iDAExB;AACA,SAASC,QAAQ,EAAEC,MAAM,EAAEC,OAAO,QAAQ,KAAK;AAAA,SACtCC,SAAS,EAAEC,YAAY,EAAEC,WAAW,mCAE7C;AAoCA,MAAMC,oBAA6C,GAAG;EACpDC,aAAa,EAAE,KAAK;EACpBC,WAAW,EAAEA,CAAA,KAAM,EAAE;EACrBC,MAAM,EAAEC,IAAA,IAAsB;IAAA,IAArB;MAAEC,KAAK;MAAEC;IAAM,CAAC,GAAAF,IAAA;IACvB,OAAO,IAAIG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