@iqmo/browser
Version:
Browser Stlite
2 lines (1 loc) • 3.19 kB
JavaScript
import{r as d,L as A,bE as h,ca as C,j as f,bG as R,bW as T,bD as E,bH as v}from"./index-0xtYjrTa.js";import{c as L}from"./createDownloadLinkElement-CbRjLJ8e.js";const $=/%([0-9A-Fa-f]{2})/g,k=/[^\x20-\x7e\xa0-\xff]/g,z=/\\([\u0000-\u007f])/g,w=/;[\x09\x20]*([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*=[\x09\x20]*("(?:[\x20!\x23-\x5b\x5d-\x7e\x80-\xff]|\\[\x20-\x7e])*"|[!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*/g,D=/^([A-Za-z0-9!#$%&+\-^_`{}~]+)'(?:[A-Za-z]{2,3}(?:-[A-Za-z]{3}){0,3}|[A-Za-z]{4,8}|)'((?:%[0-9A-Fa-f]{2}|[A-Za-z0-9!#$&+.^_`|~-])+)$/,I=/^([!#$%&'*+.0-9A-Z^_`a-z|~-]+)[\x09\x20]*(?:$|;)/,U=r=>String(r).replace(k,"?");class j{constructor(e,t){this.type=e,this.parameters=t}}const Z=(r,e)=>String.fromCharCode(Number.parseInt(e,16));function _(r){const e=D.exec(r);if(!e)throw new TypeError("invalid extended field value");const t=e[1].toLowerCase(),a=e[2];let n;switch(t){case"iso-8859-1":n=U(a.replace($,Z));break;case"utf-8":try{n=decodeURIComponent(a)}catch{throw new TypeError("invalid encoded utf-8")}break;default:throw new TypeError("unsupported charset in extended field")}return n}function N(r){let e=I.exec(r);if(!e)throw new TypeError("invalid type format");let t=e[0].length;const a=e[1].toLowerCase();let n;const l=[],i={};let o;for(t=w.lastIndex=e[0].slice(-1)===";"?t-1:t;e=w.exec(r);){if(e.index!==t)throw new TypeError("invalid parameter format");if(t+=e[0].length,n=e[1].toLowerCase(),o=e[2],l.indexOf(n)!==-1)throw new TypeError("invalid duplicate parameter");if(l.push(n),n.indexOf("*")+1===n.length){n=n.slice(0,-1),o=_(o),i[n]=o;continue}typeof i[n]!="string"&&(o[0]==='"'&&(o=o.slice(1,o.length-1).replace(z,"$1")),i[n]=o)}if(t!==-1&&t!==r.length)throw new TypeError("invalid parameter format");return new j(a,i)}function S(r){const e=N(r);if(e.type!=="attachment")return;const t=e.parameters.filename;if(typeof t=="string")return t}function B(r,e){r.sendHttpRequest({method:"GET",path:`${e}?title=${encodeURIComponent(document.title)}`,headers:{},body:""}).then(({statusCode:t,headers:a,body:n})=>{if(t!==200)return;const l=a.get("Content-Disposition"),i=(l&&S(l))??"",o=a.get("Content-Type"),u=new Blob([n],o?{type:o}:void 0),c=URL.createObjectURL(u),s=document.createElement("a");s.setAttribute("href",c),s.setAttribute("target","_blank"),s.setAttribute("download",i),s.click(),URL.revokeObjectURL(c),s.remove()})}function O(r){const{disabled:e,element:t,widgetMgr:a,endpoints:n,fragmentId:l}=r,{help:i,label:o,icon:u,ignoreRerun:c,type:s,url:m}=t,{libConfig:{enforceDownloadInNewTab:b=!1}}=d.useContext(A);let p=h.SECONDARY;s==="primary"?p=h.PRIMARY:s==="tertiary"&&(p=h.TERTIARY);const x=d.useMemo(()=>n.buildDownloadUrl(m),[n,m]);d.useEffect(()=>{n.checkSourceUrlResponse(x,"Download Button")},[x,n]);const g=C(),y=()=>{if(c||a.setTriggerValue(t,{fromUi:!0},l),t.url.startsWith("/media")){B(g,t.url);return}L({filename:"",url:x,enforceDownloadInNewTab:b}).click()};return f.jsx("div",{className:"stDownloadButton","data-testid":"stDownloadButton",children:f.jsx(R,{help:i,containerWidth:!0,children:f.jsx(T,{kind:p,size:E.SMALL,disabled:e,onClick:y,containerWidth:!0,children:f.jsx(v,{icon:u,label:o})})})})}const M=d.memo(O);export{M as default};