{"version":3,"file":"collection.mjs","sources":["../../../../../packages/components/collection/src/collection.ts"],"sourcesContent":["import { ref, inject, onMounted, onBeforeUnmount, provide, unref } from 'vue'\nimport Collection from './collection.vue'\nimport CollectionItem from './collection-item.vue'\n\nimport type { InjectionKey } from 'vue'\nimport type {\n  ElCollectionInjectionContext,\n  ElCollectionItemInjectionContext,\n} from './tokens'\n\nexport const COLLECTION_ITEM_SIGN = `data-el-collection-item`\n\n// Make sure the first letter of name is capitalized\nexport const createCollectionWithScope = (name: string) => {\n  const COLLECTION_NAME = `El${name}Collection`\n  const COLLECTION_ITEM_NAME = `${COLLECTION_NAME}Item`\n  const COLLECTION_INJECTION_KEY: InjectionKey<ElCollectionInjectionContext> =\n    Symbol(COLLECTION_NAME)\n  const COLLECTION_ITEM_INJECTION_KEY: InjectionKey<ElCollectionItemInjectionContext> =\n    Symbol(COLLECTION_ITEM_NAME)\n\n  const ElCollection = {\n    ...Collection,\n    name: COLLECTION_NAME,\n    setup() {\n      const collectionRef = ref<HTMLElement | null>(null)\n      const itemMap: ElCollectionInjectionContext['itemMap'] = new Map()\n      const getItems = () => {\n        const collectionEl = unref(collectionRef)\n\n        if (!collectionEl) return []\n        const orderedNodes = Array.from(\n          collectionEl.querySelectorAll(`[${COLLECTION_ITEM_SIGN}]`)\n        )\n\n        const items = [...itemMap.values()]\n\n        const orderedItems = items.sort(\n          (a, b) => orderedNodes.indexOf(a.ref!) - orderedNodes.indexOf(b.ref!)\n        )\n        return orderedItems\n      }\n\n      provide(COLLECTION_INJECTION_KEY, {\n        itemMap,\n        getItems,\n        collectionRef,\n      })\n    },\n  }\n\n  const ElCollectionItem = {\n    ...CollectionItem,\n    name: COLLECTION_ITEM_NAME,\n    setup(_, { attrs }) {\n      const collectionItemRef = ref<HTMLElement | null>(null)\n      const collectionInjection = inject(COLLECTION_INJECTION_KEY, undefined)!\n\n      provide(COLLECTION_ITEM_INJECTION_KEY, {\n        collectionItemRef,\n      })\n\n      onMounted(() => {\n        const collectionItemEl = unref(collectionItemRef)\n        if (collectionItemEl) {\n          collectionInjection.itemMap.set(collectionItemEl, {\n            ref: collectionItemEl,\n            ...attrs,\n          })\n        }\n      })\n\n      onBeforeUnmount(() => {\n        const collectionItemEl = unref(collectionItemRef)!\n        collectionInjection.itemMap.delete(collectionItemEl)\n      })\n    },\n  }\n\n  return {\n    COLLECTION_INJECTION_KEY,\n    COLLECTION_ITEM_INJECTION_KEY,\n    ElCollection,\n    ElCollectionItem,\n  }\n}\n"],"names":[],"mappings":";;;;AAUO,MAAM,oBAAuB,GAAA,CAAA,uBAAA,EAAA;AAGvB,MAAA,yBAAA,GAA4B,CAAC,IAAiB,KAAA;AACzD,EAAA,MAAM,kBAAkB,CAAK,EAAA,EAAA,IAAA,CAAA,UAAA,CAAA,CAAA;AAC7B,EAAA,MAAM,uBAAuB,CAAG,EAAA,eAAA,CAAA,IAAA,CAAA,CAAA;AAChC,EAAM,MAAA,wBAAA,GACJ,OAAO,eAAe,CAAA,CAAA;AACxB,EAAM,MAAA,6BAAA,GACJ,OAAO,oBAAoB,CAAA,CAAA;AAE7B,EAAA,MAAM,YAAe,GAAA;AAAA,IACnB,GAAG,UAAA;AAAA,IACH,IAAM,EAAA,eAAA;AAAA,IACN,KAAQ,GAAA;AACN,MAAM,MAAA,aAAA,GAAgB,IAAwB,IAAI,CAAA,CAAA;AAClD,MAAM,MAAA,OAAA,uBAAuD,GAAI,EAAA,CAAA;AACjE,MAAA,MAAM,WAAW,MAAM;AACrB,QAAM,MAAA,YAAA,GAAe,MAAM,aAAa,CAAA,CAAA;AAExC,QAAA,IAAI,CAAC,YAAA;AAAc,UAAA,OAAO,EAAC,CAAA;AAC3B,QAAA,MAAM,eAAe,KAAM,CAAA,IAAA;AAAA,UACzB,YAAA,CAAa,gBAAiB,CAAA,CAAA,CAAA,EAAI,oBAAuB,CAAA,CAAA,CAAA,CAAA;AAAA,SAC3D,CAAA;AAEA,QAAA,MAAM,KAAQ,GAAA,CAAC,GAAG,OAAA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