FileMaster
Search
Toggle Dark Mode
Home
/
.
/
wp-content
/
plugins
/
surecart
/
dist
/
components
/
components
Edit File: sc-downloads-list.js
import{proxyCustomElement,HTMLElement,h}from"@stencil/core/internal/client";import{a as apiFetch}from"./fetch.js";import{o as onFirstVisible}from"./lazy.js";import{d as defineCustomElement$i}from"./sc-alert2.js";import{d as defineCustomElement$h}from"./sc-block-ui2.js";import{d as defineCustomElement$g}from"./sc-button2.js";import{d as defineCustomElement$f}from"./sc-card2.js";import{d as defineCustomElement$e}from"./sc-dashboard-module2.js";import{d as defineCustomElement$d}from"./sc-divider2.js";import{d as defineCustomElement$c}from"./sc-empty2.js";import{d as defineCustomElement$b}from"./sc-flex2.js";import{d as defineCustomElement$a}from"./sc-format-bytes2.js";import{d as defineCustomElement$9}from"./sc-icon2.js";import{d as defineCustomElement$8}from"./sc-pagination2.js";import{d as defineCustomElement$7}from"./sc-skeleton2.js";import{d as defineCustomElement$6}from"./sc-spinner2.js";import{d as defineCustomElement$5}from"./sc-stacked-list2.js";import{d as defineCustomElement$4}from"./sc-stacked-list-row2.js";import{d as defineCustomElement$3}from"./sc-tag2.js";import{d as defineCustomElement$2}from"./sc-visually-hidden2.js";import{a as addQueryArgs}from"./add-query-args.js";const scDownloadsListCss=":host{display:block}.purchase{display:flex;flex-direction:column;gap:var(--sc-spacing-large)}.single-download .single-download__preview{display:flex;align-items:center;justify-content:center;background:var(--sc-color-gray-200);border-radius:var(--sc-border-radius-small);height:4rem;min-width:4rem;width:4rem}",ScDownloadsListStyle0=scDownloadsListCss,ScDownloadsList$1=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.renderFileExt=e=>{var s,t,o,i,n,l,d,a,r;if(null===(s=null==e?void 0:e.media)||void 0===s?void 0:s.filename)return null===(n=null===(i=null===(o=(t=e.media.filename).split)||void 0===o?void 0:o.call(t,"."))||void 0===i?void 0:i.pop)||void 0===n?void 0:n.call(i);if(null==e?void 0:e.url)try{const s=new URL(e.url);if(s.pathname.includes("."))return null===(r=null===(a=null===(d=(l=s.pathname).split)||void 0===d?void 0:d.call(l,"."))||void 0===a?void 0:a.pop)||void 0===r?void 0:r.call(a)}catch(e){console.error(e)}return h("sc-icon",{name:"file"})},this.customerId=void 0,this.productId=void 0,this.heading=void 0,this.downloads=void 0,this.downloading=void 0,this.busy=void 0,this.error=void 0,this.pagination={total:0,total_pages:0},this.query={page:1,per_page:20}}componentWillLoad(){onFirstVisible(this.el,(()=>{this.fetchItems()}))}async fetchItems(){if(this.productId&&this.customerId)try{this.busy=!0,await this.getItems()}catch(e){console.error(this.error),this.error=(null==e?void 0:e.message)||wp.i18n.__("Something went wrong","surecart")}finally{this.busy=!1}}async getItems(){const e=await apiFetch({path:addQueryArgs("surecart/v1/downloads/",{product_ids:[this.productId],customer_ids:[this.customerId],downloadable:!0,...this.query}),parse:!1});return this.pagination={total:parseInt(e.headers.get("X-WP-Total")),total_pages:parseInt(e.headers.get("X-WP-TotalPages"))},this.downloads=await e.json(),this.downloads}nextPage(){this.query.page=this.query.page+1,this.fetchItems()}prevPage(){this.query.page=this.query.page-1,this.fetchItems()}async downloadItem(e){var s,t;if(null==e?void 0:e.url)return void this.downloadFile(e.url,null!==(s=null==e?void 0:e.name)&&void 0!==s?s:"file");const o=null===(t=null==e?void 0:e.media)||void 0===t?void 0:t.id;if(o)try{this.downloading=o;const e=await apiFetch({path:addQueryArgs(`surecart/v1/customers/${this.customerId}/expose/${o}`,{expose_for:60})});if(!(null==e?void 0:e.url))throw{message:wp.i18n.__("Could not download the file.","surecart")};this.downloadFile(null==e?void 0:e.url,e.filename)}catch(e){console.error(e),this.error=(null==e?void 0:e.message)||wp.i18n.__("Something went wrong","surecart")}finally{this.downloading=null}}downloadFile(e,s){const t=document.createElement("a");t.href=e,t.download=s,document.body.appendChild(t),t.click(),setTimeout((()=>{document.body.removeChild(t)}),0)}renderList(){var e,s;if((null==this?void 0:this.busy)&&!(null===(e=null==this?void 0:this.downloads)||void 0===e?void 0:e.length))return this.renderLoading();if(!(null===(s=null==this?void 0:this.downloads)||void 0===s?void 0:s.length))return this.renderEmpty();const t=this.downloads||[];return h("sc-card",{"no-padding":!0},h("sc-stacked-list",null,t.map((e=>{var s,t,o,i;const n=null==e?void 0:e.media;return h("sc-stacked-list-row",{style:{"--columns":"1"}},h("sc-flex",{class:"single-download",justifyContent:"flex-start",alignItems:"center"},h("div",{class:"single-download__preview"},this.renderFileExt(e)),h("div",null,h("div",null,h("strong",null,null!==(t=null!==(s=null==n?void 0:n.filename)&&void 0!==s?s:null==e?void 0:e.name)&&void 0!==t?t:"")),h("sc-flex",{justifyContent:"flex-start",alignItems:"center",style:{gap:"0.5em"}},(null==n?void 0:n.byte_size)&&h("sc-format-bytes",{value:n.byte_size}),!!(null===(o=null==n?void 0:n.release_json)||void 0===o?void 0:o.version)&&h("sc-tag",{type:"primary",size:"small",style:{"--sc-tag-primary-background-color":"#f3e8ff","--sc-tag-primary-color":"#6b21a8"}},"v",null===(i=null==n?void 0:n.release_json)||void 0===i?void 0:i.version)))),h("sc-button",{size:"small",slot:"suffix",onClick:()=>this.downloadItem(e),busy:!!(null==n?void 0:n.id)&&this.downloading==(null==n?void 0:n.id),disabled:!!(null==n?void 0:n.id)&&this.downloading==(null==n?void 0:n.id)},wp.i18n.__("Download","surecart")))}))))}renderLoading(){return h("sc-card",{"no-padding":!0,style:{"--overflow":"hidden"}},h("sc-stacked-list",null,h("sc-stacked-list-row",{style:{"--columns":"2"},"mobile-size":0},h("div",{style:{padding:"0.5em"}},h("sc-skeleton",{style:{width:"30%",marginBottom:"0.75em"}}),h("sc-skeleton",{style:{width:"20%"}})))))}renderEmpty(){return h("div",null,h("sc-divider",{style:{"--spacing":"0"}}),h("slot",{name:"empty"},h("sc-empty",{icon:"download"},wp.i18n.__("You don't have any downloads.","surecart"))))}render(){var e;return h("sc-dashboard-module",{key:"d58912d8bdb1082c7fc6573d8d79d923fb5084cf",class:"purchase",part:"base",heading:wp.i18n.__("Downloads","surecart")},h("span",{key:"f1c5e6ac8b4cd7b8ac3fcf41f68b89a6cdbe22a4",slot:"heading"},h("slot",{key:"53acfe743b074a19f6201ab25f468c4566c4e288",name:"heading"},this.heading||wp.i18n.__("Downloads","surecart"))),this.renderList(),h("sc-pagination",{key:"0da57f246127271134b730be44703289dfe47814",page:this.query.page,perPage:this.query.per_page,total:this.pagination.total,totalPages:this.pagination.total_pages,totalShowing:null===(e=null==this?void 0:this.downloads)||void 0===e?void 0:e.length,onScNextPage:()=>this.nextPage(),onScPrevPage:()=>this.prevPage()}),this.busy&&h("sc-block-ui",{key:"43d573db84fc6693c051b9355da265c3a8f2321f"}))}get el(){return this}static get style(){return ScDownloadsListStyle0}},[1,"sc-downloads-list",{customerId:[1,"customer-id"],productId:[1,"product-id"],heading:[1],query:[1032],downloads:[32],downloading:[32],busy:[32],error:[32],pagination:[32]}]);function defineCustomElement$1(){"undefined"!=typeof customElements&&["sc-downloads-list","sc-alert","sc-block-ui","sc-button","sc-card","sc-dashboard-module","sc-divider","sc-empty","sc-flex","sc-format-bytes","sc-icon","sc-pagination","sc-skeleton","sc-spinner","sc-stacked-list","sc-stacked-list-row","sc-tag","sc-visually-hidden"].forEach((e=>{switch(e){case"sc-downloads-list":customElements.get(e)||customElements.define(e,ScDownloadsList$1);break;case"sc-alert":customElements.get(e)||defineCustomElement$i();break;case"sc-block-ui":customElements.get(e)||defineCustomElement$h();break;case"sc-button":customElements.get(e)||defineCustomElement$g();break;case"sc-card":customElements.get(e)||defineCustomElement$f();break;case"sc-dashboard-module":customElements.get(e)||defineCustomElement$e();break;case"sc-divider":customElements.get(e)||defineCustomElement$d();break;case"sc-empty":customElements.get(e)||defineCustomElement$c();break;case"sc-flex":customElements.get(e)||defineCustomElement$b();break;case"sc-format-bytes":customElements.get(e)||defineCustomElement$a();break;case"sc-icon":customElements.get(e)||defineCustomElement$9();break;case"sc-pagination":customElements.get(e)||defineCustomElement$8();break;case"sc-skeleton":customElements.get(e)||defineCustomElement$7();break;case"sc-spinner":customElements.get(e)||defineCustomElement$6();break;case"sc-stacked-list":customElements.get(e)||defineCustomElement$5();break;case"sc-stacked-list-row":customElements.get(e)||defineCustomElement$4();break;case"sc-tag":customElements.get(e)||defineCustomElement$3();break;case"sc-visually-hidden":customElements.get(e)||defineCustomElement$2()}}))}const ScDownloadsList=ScDownloadsList$1,defineCustomElement=defineCustomElement$1;export{ScDownloadsList,defineCustomElement};
Save
Back