|
| 1 | +function slugify(text: string) { |
| 2 | + const a = 'àáäâèéëêìíïîòóöôùúüûñçßÿœæŕśńṕẃǵǹḿǘẍźḧ·/_,:;'; |
| 3 | + const b = 'aaaaeeeeiiiioooouuuuncsyoarsnpwgnmuxzh------'; |
| 4 | + const p = new RegExp(a.split('').join('|'), 'g'); |
| 5 | + |
| 6 | + /* eslint-disable */ |
| 7 | + return text |
| 8 | + .toString() |
| 9 | + .toLowerCase() |
| 10 | + .replace(/\s+/g, '-') // Replace spaces with - |
| 11 | + .replace(p, c => b.charAt(a.indexOf(c))) // Replace special chars |
| 12 | + .replace(/&/g, '-and-') // Replace & with 'and' |
| 13 | + .replace(/[^\w\-]+/g, '') // Remove all non-word chars |
| 14 | + .replace(/\-\-+/g, '-') // Replace multiple - with single - |
| 15 | + .replace(/^-+/, '') // Trim - from start of text |
| 16 | + .replace(/-+$/, ''); // Trim - from end of text |
| 17 | + /* eslint-enable */ |
| 18 | +} |
| 19 | + |
| 20 | +export function fileDownload(data: any, filename: string) { |
| 21 | + const blob = new Blob(data, { type: 'application/zip' }); |
| 22 | + const blobURL = window.URL.createObjectURL(blob); |
| 23 | + const tempLink = document.createElement('a'); |
| 24 | + tempLink.style.display = 'none'; |
| 25 | + tempLink.href = blobURL; |
| 26 | + tempLink.setAttribute('download', `${slugify(filename)}.zip`); |
| 27 | + |
| 28 | + // Safari thinks _blank anchor are pop ups. We only want to set _blank |
| 29 | + // target if the browser does not support the HTML5 download attribute. |
| 30 | + // This allows you to download files in desktop safari if pop up blocking |
| 31 | + // is enabled. |
| 32 | + if (typeof tempLink.download === 'undefined') { |
| 33 | + tempLink.setAttribute('target', '_blank'); |
| 34 | + } |
| 35 | + |
| 36 | + document.body.appendChild(tempLink); |
| 37 | + tempLink.click(); |
| 38 | + |
| 39 | + // Fixes "webkit blob resource error 1" |
| 40 | + setTimeout(() => { |
| 41 | + document.body.removeChild(tempLink); |
| 42 | + window.URL.revokeObjectURL(blobURL); |
| 43 | + }, 0); |
| 44 | +} |
0 commit comments