{"version":3,"file":"useDownloadDatasetOnMount.mjs","sources":["../../lib/composables/useDownloadDatasetOnMount.ts"],"sourcesContent":["import { onMounted, watch } from \"vue\";\n\n/**\n * Initiates a download of the dataset on mount and on route change\n * Needs router param ds_id and query param dl to be available\n * @param options\n */\nexport function useDownloadDatasetOnMount(options: { route: any, hubUrl: string }) {\n  const { route, hubUrl } = options;\n  \n  if (!route.params.ds_id) {\n    console.error('No dataset id found in route params');\n    return;\n  }\n\n  const dlFormat = route.query.dl;\n  const downloadOnRouteChange = () => {\n    if (route.query.dl) {\n      const url = `${hubUrl}${route.path}.${dlFormat}?useNormalizedId=true${route.query.locale ? `&locale=${route.query.locale}` : ''}`;\n      if (!url) {\n        return;\n      }\n\n      // Create a link element, hide it, direct it towards the URL, and then 'click' it programatically\n      // This is the most cross browser solution to programmatically downloading content\n      const link = document.createElement('a');\n      link.href = url;\n      const datasetId = route.params.ds_id;\n      link.download = `${datasetId}.${dlFormat}`;\n      document.body.appendChild(link);\n      link.click();\n      document.body.removeChild(link);\n    }\n  };\n\n  onMounted(downloadOnRouteChange);\n\n  watch(() => route, downloadOnRouteChange);\n}\n"],"names":["useDownloadDatasetOnMount","options","route","hubUrl","dlFormat","downloadOnRouteChange","url","link","datasetId","onMounted","watch"],"mappings":";AAOO,SAASA,EAA0BC,GAAyC;AAC3E,QAAA,EAAE,OAAAC,GAAO,QAAAC,EAAW,IAAAF;AAEtB,MAAA,CAACC,EAAM,OAAO,OAAO;AACvB,YAAQ,MAAM,qCAAqC;AACnD;AAAA,EACF;AAEM,QAAAE,IAAWF,EAAM,MAAM,IACvBG,IAAwB,MAAM;AAC9B,QAAAH,EAAM,MAAM,IAAI;AAClB,YAAMI,IAAM,GAAGH,CAAM,GAAGD,EAAM,IAAI,IAAIE,CAAQ,wBAAwBF,EAAM,MAAM,SAAS,WAAWA,EAAM,MAAM,MAAM,KAAK,EAAE;AAC/H,UAAI,CAACI;AACH;AAKI,YAAAC,IAAO,SAAS,cAAc,GAAG;AACvC,MAAAA,EAAK,OAAOD;AACN,YAAAE,IAAYN,EAAM,OAAO;AAC/B,MAAAK,EAAK,WAAW,GAAGC,CAAS,IAAIJ,CAAQ,IAC/B,SAAA,KAAK,YAAYG,CAAI,GAC9BA,EAAK,MAAM,GACF,SAAA,KAAK,YAAYA,CAAI;AAAA,IAChC;AAAA,EAAA;AAGF,EAAAE,EAAUJ,CAAqB,GAEzBK,EAAA,MAAMR,GAAOG,CAAqB;AAC1C;"}