mirror of https://github.com/docusealco/docuseal
				
				
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							46 lines
						
					
					
						
							1.2 KiB
						
					
					
				
			
		
		
	
	
							46 lines
						
					
					
						
							1.2 KiB
						
					
					
				import { target, targetable } from '@github/catalyst/lib/targetable'
 | 
						|
 | 
						|
export default targetable(class extends HTMLElement {
 | 
						|
  static [target.static] = ['defaultButton', 'loadingButton']
 | 
						|
 | 
						|
  connectedCallback () {
 | 
						|
    this.addEventListener('click', () => this.downloadFiles())
 | 
						|
  }
 | 
						|
 | 
						|
  toggleState () {
 | 
						|
    this.defaultButton?.classList?.toggle('hidden')
 | 
						|
    this.loadingButton?.classList?.toggle('hidden')
 | 
						|
  }
 | 
						|
 | 
						|
  downloadFiles () {
 | 
						|
    if (!this.dataset.src) return
 | 
						|
 | 
						|
    this.toggleState()
 | 
						|
 | 
						|
    fetch(this.dataset.src).then((response) => response.json()).then((urls) => {
 | 
						|
      const fileRequests = urls.map((url) => {
 | 
						|
        return () => {
 | 
						|
          return fetch(url).then(async (resp) => {
 | 
						|
            const blobUrl = URL.createObjectURL(await resp.blob())
 | 
						|
            const link = document.createElement('a')
 | 
						|
 | 
						|
            link.href = blobUrl
 | 
						|
            link.setAttribute('download', decodeURI(resp.headers.get('content-disposition').split('"')[1]))
 | 
						|
 | 
						|
            link.click()
 | 
						|
 | 
						|
            URL.revokeObjectURL(url)
 | 
						|
          })
 | 
						|
        }
 | 
						|
      })
 | 
						|
 | 
						|
      fileRequests.reduce(
 | 
						|
        (prevPromise, request) => prevPromise.then(() => request()),
 | 
						|
        Promise.resolve()
 | 
						|
      )
 | 
						|
 | 
						|
      this.toggleState()
 | 
						|
    })
 | 
						|
  }
 | 
						|
})
 |