{"version":3,"file":"collection.mjs","names":["Collection","CollectionItem"],"sources":["../../../../../../packages/components/collection/src/collection.ts"],"sourcesContent":["import { inject, onBeforeUnmount, onMounted, provide, ref, unref } from 'vue'\nimport Collection from './collection.vue'\nimport CollectionItem from './collection-item.vue'\n\nimport type { InjectionKey, SetupContext } 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 = Object.assign({}, Collection, {\n    name: COLLECTION_NAME,\n    setup() {\n      const collectionRef = ref<HTMLElement>()\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        return items.sort(\n          (a, b) => orderedNodes.indexOf(a.ref!) - orderedNodes.indexOf(b.ref!)\n        )\n      }) as ElCollectionInjectionContext['getItems']\n\n      provide(COLLECTION_INJECTION_KEY, {\n        itemMap,\n        getItems,\n        collectionRef,\n      })\n    },\n  })\n\n  const ElCollectionItem = Object.assign({}, CollectionItem, {\n    name: COLLECTION_ITEM_NAME,\n    setup(_: unknown, { attrs }: SetupContext) {\n      const collectionItemRef = ref<HTMLElement>()\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"],"mappings":";;;;AAUA,MAAa,uBAAuB;AAGpC,MAAa,6BAA6B,SAAiB;CACzD,MAAM,kBAAkB,KAAK,KAAK;CAClC,MAAM,uBAAuB,GAAG,gBAAgB;CAChD,MAAM,2BACJ,OAAO,eAAe;CACxB,MAAM,gCACJ,OAAO,oBAAoB;CAyD7B,OAAO;EACL;EACA;EACA,cA1DmB,OAAO,OAAO,CAAC,GAAGA,oBAAY;GACjD,MAAM;GACN,QAAQ;IACN,MAAM,gBAAgB,IAAiB;IACvC,MAAM,0BAAmD,IAAI,IAAI;IACjE,MAAM,kBAAkB;KACtB,MAAM,eAAe,MAAM,aAAa;KAExC,IAAI,CAAC,cAAc,OAAO,CAAC;KAC3B,MAAM,eAAe,MAAM,KACzB,aAAa,iBAAiB,IAAI,qBAAqB,EAAE,CAC3D;KAIA,OAAO,CAFQ,GAAG,QAAQ,OAAO,CAEtB,CAAC,CAAC,MACV,GAAG,MAAM,aAAa,QAAQ,EAAE,GAAI,IAAI,aAAa,QAAQ,EAAE,GAAI,CACtE;IACF;IAEA,QAAQ,0BAA0B;KAChC;KACA;KACA;IACF,CAAC;GACH;EACF,CAgCa;EACX,kBA/BuB,OAAO,OAAO,CAAC,GAAGC,yBAAgB;GACzD,MAAM;GACN,MAAM,GAAY,EAAE,SAAuB;IACzC,MAAM,oBAAoB,IAAiB;IAC3C,MAAM,sBAAsB,OAAO,0BAA0B,KAAA,CAAS;IAE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