-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite-templates.js
1 lines (1 loc) · 4.56 KB
/
site-templates.js
1
(()=>{"use strict";const{endpoint:e,nonce:t,perPage:n,categoryMap:a}=window.SiteTemplatePicker,s=window.CBOXOL_Group_Create.new_group_type;const o=document.querySelector("#site-template-categories"),i=document.querySelector(".site-template-picker"),r=document.querySelector(".panel-template-picker"),l=document.querySelector(".site-template-pagination"),c=document.querySelector('[name="source_blog"]'),d=document.querySelector("#set-up-site-toggle"),p=document.querySelectorAll('[name="new_or_old"]'),u=window.SiteTemplatePicker.messages,m=window.SiteTemplatePicker.defaultMap,g=window.CBOXOL_Group_Create?.new_group_type||null,v=document.querySelector('[name="group_id"]').value,h=g&&m.hasOwnProperty(g)?m[g]:0,f=h?h.toString():c.value;function _(o,r){i.innerHTML=`<p>${u.loading}</p>`,async function(o){let i,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1,l=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;if(o)i=o;else for(var c in i=[0],a)-1!==a[c].indexOf(s)&&i.push(c);const d=function(e){let t="";const n=Object.entries(e);let a;for(;a=n.shift();){let[e,s]=a;if(Array.isArray(s)||s&&s.constructor===Object){const t=Object.entries(s).reverse();for(const[a,s]of t)n.unshift([`${e}[${a}]`,s])}else void 0!==s&&(null===s&&(s=""),t+="&"+[e,s].map(encodeURIComponent).join("="))}return t.substr(1)}({_fields:["id","title","excerpt","featured_media","template_category","site_id","image","categories"],template_category:i,group_id:l,orderby:"menu_order",order:"asc",per_page:Number(n),page:r,_wpnonce:t}),p=await fetch(e+"?"+d,{credentials:"include"}),u=await p.json();if(!p.ok)throw new Error(u.message);const m=Number(p.headers.get("X-WP-TotalPages"));return{templates:u.map((e=>({id:e.id,title:e.title.rendered,excerpt:e.excerpt.rendered,image:e.image,categories:e.categories,siteId:e.site_id}))),prev:r>1?r-1:null,next:m>r?r+1:null}}(o,r,v).then((e=>{let{templates:t,prev:n,next:a}=e;if(!t.length)return void(i.innerHTML=`<p>${u.noResults}</p>`);const s=t.map((e=>function(e){let{id:t,siteId:n,title:a,excerpt:s,image:o,categories:i}=e;return`\n\t<button type="button" class="site-template-component" data-template-id="${t}" data-template-site-id="${n}">\n\t\t<div class="site-template-component__image">\n\t\t\t${o?`<img src="${o}" alt="${a}">`:'<svg fill="currentColor" width="24" height="24" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4 3a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V5a2 2 0 00-2-2H4zm12 12H4l4-8 3 6 2-4 3 6z" clip-rule="evenodd"></path></svg>'}\n\t\t</div>\n\t\t<div class="site-template-component__meta">\n\t\t\t<span class="site-template-component__category">${i.join(", ")}</span>\n\t\t\t<div class="site-template-component__name">${a}</div>\n\t\t\t<div class="site-template-component__description">${s}</div>\n\t\t</div>\n\t</button>\n\t`}(e))).join("");i.innerHTML=s,y(f),function(e,t){const n=l.querySelector(".prev"),a=l.querySelector(".next"),s=l.classList.contains("hidden");!e&&!t&&!s&&l.classList.add("hidden"),n.disabled=!0,a.disabled=!0,e&&(n.dataset.page=e,n.disabled=!1),t&&(a.dataset.page=t,a.disabled=!1)}(n,a),t.length>1&&w(!d||d.checked),function(){const e=document.querySelector(".site-template-categories");let t=0;const n=e.querySelectorAll("option");for(const e of n)"0"!==e.value&&t++;t<=1?e.classList.add("hidden"):e.classList.remove("hidden")}()}))}function w(){arguments.length>0&&void 0!==arguments[0]&&arguments[0]?i.querySelectorAll(".site-template-component").length>1&&r.classList.remove("hidden"):(r.classList.add("hidden"),y(f))}function y(e){i.querySelectorAll(".site-template-component").forEach((t=>{t.dataset.templateId===e?(t.classList.add("is-selected"),c.value=t.dataset.templateSiteId):t.classList.remove("is-selected")}))}o.addEventListener("change",(function(e){const t="0"!==e.target.value?e.target.value:null;i.innerHTML=`<p>${u.loading}</p>`,_(t)})),i.addEventListener("click",(function(e){const t=e.target.closest(".site-template-component");t&&y(t.dataset.templateId)})),i.addEventListener("mouseover",(function(e){const t=e.target.closest(".site-template-component");t&&(t.classList.contains("has-hover")||t.classList.add("has-hover"))})),i.addEventListener("mouseout",(function(e){const t=e.target.closest(".site-template-component");t&&t.classList.contains("has-hover")&&t.classList.remove("has-hover")})),l.addEventListener("click",(function(e){const t=e.target.closest(".btn");t&&_("0"!==o.value?o.value:null,t.dataset.page?Number(t.dataset.page):null)})),p.forEach((e=>{e.addEventListener("change",(e=>w("new"===e.target.value)))})),d?(d.addEventListener("change",(e=>w(e.target.checked))),d.checked&&w(!0)):w(!0),_()})();