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