@codegouvfr/react-dsfr
Version:
French State Design System React integration library
4 lines (3 loc) • 2.15 kB
JavaScript
/*! DSFR v1.8.5 | SPDX-License-Identifier: MIT | License-Filename: LICENSE.md | restricted use (see terms and conditions) */
const e=window.dsfr,t={DOWNLOAD_ASSESS_FILE:`${e.internals.ns.attr.selector("assess-file")}`,DOWNLOAD_DETAIL:`${e.internals.ns.selector("download__detail")}`};class s extends e.core.Instance{static get instanceClassName(){return"AssessFile"}init(){this.lang=this.getLang(this.node),this.href=this.getAttribute("href"),this.hreflang=this.getAttribute("hreflang"),this.file={},this.detail=this.querySelector(t.DOWNLOAD_DETAIL),this.update()}getFileLength(){void 0!==this.href?fetch(this.href,{method:"HEAD",mode:"cors"}).then((e=>{this.length=e.headers.get("content-length")||-1,-1===this.length&&console.warn("Impossible de détecter le poids du fichier "+this.href+'\nErreur de récupération de l\'en-tête HTTP : "content-length"'),this.update()})):this.length=-1}update(){if(this.isLegacy&&(this.length=-1),!this.length)return void this.getFileLength();const e=[];if(this.detail){if(this.href){const t=this.parseExtension(this.href);t&&e.push(t.toUpperCase())}-1!==this.length&&e.push(this.bytesToSize(this.length)),this.hreflang&&e.push(this.getLangDisplayName(this.hreflang)),this.detail.innerHTML=e.join(" - ")}}getLang(e){return e.lang?e.lang:document.documentElement===e?window.navigator.language:this.getLang(e.parentElement)}parseExtension(e){return e.match(/\.(\w{1,9})(?:$|[?#])/)[0].replace(".","")}getLangDisplayName(e){if(this.isLegacy)return e;const t=new Intl.DisplayNames([this.lang],{type:"language"}).of(e);return t.charAt(0).toUpperCase()+t.slice(1)}bytesToSize(t){if(-1===t)return null;let s=["octets","ko","Mo","Go","To"];"bytes"===this.getAttribute(e.internals.ns.attr("assess-file"))&&(s=["bytes","KB","MB","GB","TB"]);const n=parseInt(Math.floor(Math.log(t)/Math.log(1e3)),10);if(0===n)return`${t} ${s[n]}`;const i=t/1e3**n,h=Math.round(100*(i+Number.EPSILON))/100;return`${String(h).replace(".",",")} ${s[n]}`}}e.download={DownloadSelector:t,AssessFile:s},e.internals.register(e.download.DownloadSelector.DOWNLOAD_ASSESS_FILE,e.download.AssessFile);
//# sourceMappingURL=download.module.min.js.map