Merge branch 'main' of https://github.com/jakevdwerf/jvb
| | |
| | | const termPath = term.path || term.name; |
| | | |
| | | field.selectedTerms.add(parseInt(term.id)); |
| | | |
| | | // Add to store's data map |
| | | this.selector.store.data.set(term.id, { |
| | | id: term.id, |
| | | name: term.name, |
| | |
| | | hasChildren: false, |
| | | slug: term.slug || termName.toLowerCase().replace(/\s+/g, '-') |
| | | }); |
| | | |
| | | this.selector.addTermToDisplay(field.id, term.id, term.name, termPath); |
| | | |
| | | field.input.value = Array.from(field.selectedTerms).join(','); |
| | |
| | | field.autocompleteDropdown.hidden = true; |
| | | if (input) input.value = ''; |
| | | |
| | | // Clear cache AND refresh this taxonomy's data |
| | | this.selector.store.clearCache(); |
| | | // Clear ALL cache for this taxonomy |
| | | // This forces next search to hit the server |
| | | await this.selector.store.clearCache(); |
| | | |
| | | // Trigger a background refresh for this taxonomy |
| | | await this.selector.store.setFilters({ |
| | | taxonomy: field.taxonomy, |
| | | page: 1, |
| | | search: '', |
| | | parent: 0 |
| | | }); |
| | | } else if (response.reason === 'exists' && response.term) { |
| | | const term = response.term; |
| | | field.selectedTerms.add(parseInt(term.id)); |
| | |
| | | window.jvbTaxCreator=class{constructor(e){this.selector=e,e.modal&&(this.createNew=e.modal.querySelector(".create-new-term"),this.toggle=e.modal.querySelector(".new-term-toggle"),this.form=this.createNew?.querySelector(".create-new-term-section")),this.initListeners(),this.form&&this.initTermCreation()}initListeners(){this.clickHandler=this.handleClick.bind(this),document.addEventListener("click",this.clickHandler)}handleClick(e){window.targetCheck(e,".create-new-term summary")&&(this.createNew.open&&this.createNew.querySelector('input[name="term_name"]').focus(),this.resetParentOptions()),window.targetCheck(e,".submit-term")&&this.handleTermCreation(e).then((()=>{})),window.targetCheck(e,".create-term")&&this.handleAutocompleteCreate(e).then((()=>{}))}async handleTermCreation(e){const t=this.selector.currentConfig?.taxonomy;if(!t)return;const r=this.form.querySelector('input[name="term_name"]').value.trim(),o=parseInt(this.form.querySelector("input#select_parent")?.value)||0;if(r)try{const e=this.form.querySelector("button");e&&(e.disabled=!0);const n=await this.createTerm(r,o,t);if(n.success&&n.term){let e=n.term;const a=e.path||e.name;this.createNew.open=!1,await this.selector.store.clearCache(),this.selector.store.data.set(e.id,{id:e.id,name:e.name,path:a,taxonomy:t,parent:o,count:0,hasChildren:!1,slug:e.slug||r.toLowerCase().replace(/\s+/g,"-")}),this.selector.addSelectedTermToModal(e.id,e.name,a),(this.selector.store.filters.parent||0)===o&&await this.selector.store.setFilters({taxonomy:t,parent:o,page:1,search:""}),this.form.querySelector('input[name="term_name"]').value="";const s=this.createNew.querySelector(".term-suggestions");s&&(s.hidden=!0)}}catch(e){console.error("Error creating term:",e),this.selector.error?.log(e,{component:"TaxonomyCreator",action:"handleTermCreation"})}finally{this.form.querySelector("button").disabled=!1}}async handleAutocompleteCreate(e){const t=e.target.closest(".create-term"),r=this.selector.getFieldId(t),o=this.selector.fields.get(r);if(!o)return;const n=o.container.querySelector("input[data-autocomplete]"),a=n?.value.trim()||t.dataset.query;if(!a)return;const s=t.innerHTML;try{t.disabled=!0,t.textContent="Creating...";const e=await this.createTerm(a,0,o.taxonomy);if(e.success&&e.term){const t=e.term,r=t.path||t.name;o.selectedTerms.add(parseInt(t.id)),this.selector.store.data.set(t.id,{id:t.id,name:t.name,path:r,taxonomy:o.taxonomy,parent:0,count:0,hasChildren:!1,slug:t.slug||a.toLowerCase().replace(/\s+/g,"-")}),this.selector.addTermToDisplay(o.id,t.id,t.name,r),o.input.value=Array.from(o.selectedTerms).join(","),o.input.dispatchEvent(new Event("change",{bubbles:!0})),o.autocompleteDropdown.hidden=!0,n&&(n.value=""),this.selector.store.clearCache(),await this.selector.store.setFilters({taxonomy:o.taxonomy,page:1,search:"",parent:0})}else if("exists"===e.reason&&e.term){const t=e.term;o.selectedTerms.add(parseInt(t.id)),this.selector.addTermToDisplay(o.id,t.id,t.name,t.path||t.name),o.input.value=Array.from(o.selectedTerms).join(","),o.input.dispatchEvent(new Event("change",{bubbles:!0})),o.autocompleteDropdown.hidden=!0,n&&(n.value="")}}catch(e){console.error("Error creating term:",e),this.selector.error?.log(e,{component:"TaxonomyCreator",action:"handleAutocompleteCreate"})}finally{t.innerHTML=s,t.disabled=!1}}initTermCreation(){this.form&&this.form.addEventListener("change",(e=>{e.preventDefault(),e.stopPropagation()}))}resetParentOptions(){const e=this.selector.currentConfig?.taxonomy;if(!e)return;let t=this.createNew.querySelector("#select_parent");if(!t)return;let r=t.querySelector("option");if(!r)return;window.removeChildren(t),t.append(r.cloneNode(!0));const o=this.selector.store.filters.parent||0;if(0!==o){const e=this.selector.store.data.get(o);if(e){let o=r.cloneNode(!0);o.value=e.id,o.textContent=e.name,t.append(o)}}const n=[];this.selector.store.data.forEach((t=>{t.taxonomy===e&&t.parent===o&&n.push(t)})),n.sort(((e,t)=>e.name.localeCompare(t.name))),n.forEach((e=>{let o=r.cloneNode(!0);o.id=`select-parent-${e.id}`,o.value=e.id,o.textContent=" — "+e.name,t.append(o)}))}async createTerm(e,t=0,r){try{await this.selector.store.setFilters({taxonomy:r,search:e,page:1,parent:0}),await new Promise((e=>setTimeout(e,100)));const o=Array.from(this.selector.store.data.values()).find((t=>t.taxonomy===r&&t.name.toLowerCase()===e.toLowerCase()));if(o)return this.createNew&&this.showTermSuggestions([o],!0),{success:!1,reason:"exists",term:o};const n=await fetch(`${jvbSettings.api}terms`,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":window.auth.getNonce()},body:JSON.stringify({taxonomy:r,name:e,parent:t})});if(!n.ok)throw new Error(`Server error: ${n.status}`);return await n.json()}catch(e){throw console.error("Error creating term:",e),e}}showTermSuggestions(e,t=!1){const r=this.createNew.querySelector(".term-suggestions")||this.createSuggestionContainer();window.removeChildren(r);const o=document.createElement("h4");o.textContent=t?"This term already exists:":"Similar terms already exist:",r.appendChild(o);const n=document.createElement("ul");n.className="term-suggestion-list",e.forEach((e=>{const t=document.createElement("li"),o=document.createElement("button");o.type="button",o.className="use-existing-term",o.setAttribute("data-id",e.id),o.textContent=e.path||e.name,o.addEventListener("click",(()=>{this.selector.addSelectedTermToModal(e.id,e.name,e.path||e.name),this.createNew.open=!1,r.hidden=!0,this.form.querySelector('input[name="term_name"]').value=""})),t.appendChild(o),n.appendChild(t)})),r.appendChild(n),r.hidden=!1}createSuggestionContainer(){const e=document.createElement("div");return e.className="term-suggestions",e.hidden=!0,this.createNew.querySelector("form").after(e),e}destroy(){this.clickHandler&&document.removeEventListener("click",this.clickHandler);const e=this.createNew?.querySelector(".loading-message.create-term");e&&(e.hidden=!0);const t=this.createNew?.querySelector(".term-suggestions");t&&(t.hidden=!0)}}; |
| | | window.jvbTaxCreator=class{constructor(e){this.selector=e,e.modal&&(this.createNew=e.modal.querySelector(".create-new-term"),this.toggle=e.modal.querySelector(".new-term-toggle"),this.form=this.createNew?.querySelector(".create-new-term-section")),this.initListeners(),this.form&&this.initTermCreation()}initListeners(){this.clickHandler=this.handleClick.bind(this),document.addEventListener("click",this.clickHandler)}handleClick(e){window.targetCheck(e,".create-new-term summary")&&(this.createNew.open&&this.createNew.querySelector('input[name="term_name"]').focus(),this.resetParentOptions()),window.targetCheck(e,".submit-term")&&this.handleTermCreation(e).then((()=>{})),window.targetCheck(e,".create-term")&&this.handleAutocompleteCreate(e).then((()=>{}))}async handleTermCreation(e){const t=this.selector.currentConfig?.taxonomy;if(!t)return;const r=this.form.querySelector('input[name="term_name"]').value.trim(),n=parseInt(this.form.querySelector("input#select_parent")?.value)||0;if(r)try{const e=this.form.querySelector("button");e&&(e.disabled=!0);const o=await this.createTerm(r,n,t);if(o.success&&o.term){let e=o.term;const a=e.path||e.name;this.createNew.open=!1,await this.selector.store.clearCache(),this.selector.store.data.set(e.id,{id:e.id,name:e.name,path:a,taxonomy:t,parent:n,count:0,hasChildren:!1,slug:e.slug||r.toLowerCase().replace(/\s+/g,"-")}),this.selector.addSelectedTermToModal(e.id,e.name,a),(this.selector.store.filters.parent||0)===n&&await this.selector.store.setFilters({taxonomy:t,parent:n,page:1,search:""}),this.form.querySelector('input[name="term_name"]').value="";const s=this.createNew.querySelector(".term-suggestions");s&&(s.hidden=!0)}}catch(e){console.error("Error creating term:",e),this.selector.error?.log(e,{component:"TaxonomyCreator",action:"handleTermCreation"})}finally{this.form.querySelector("button").disabled=!1}}async handleAutocompleteCreate(e){const t=e.target.closest(".create-term"),r=this.selector.getFieldId(t),n=this.selector.fields.get(r);if(!n)return;const o=n.container.querySelector("input[data-autocomplete]"),a=o?.value.trim()||t.dataset.query;if(!a)return;const s=t.innerHTML;try{t.disabled=!0,t.textContent="Creating...";const e=await this.createTerm(a,0,n.taxonomy);if(e.success&&e.term){const t=e.term,r=t.path||t.name;n.selectedTerms.add(parseInt(t.id)),this.selector.store.data.set(t.id,{id:t.id,name:t.name,path:r,taxonomy:n.taxonomy,parent:0,count:0,hasChildren:!1,slug:t.slug||a.toLowerCase().replace(/\s+/g,"-")}),this.selector.addTermToDisplay(n.id,t.id,t.name,r),n.input.value=Array.from(n.selectedTerms).join(","),n.input.dispatchEvent(new Event("change",{bubbles:!0})),n.autocompleteDropdown.hidden=!0,o&&(o.value=""),await this.selector.store.clearCache()}else if("exists"===e.reason&&e.term){const t=e.term;n.selectedTerms.add(parseInt(t.id)),this.selector.addTermToDisplay(n.id,t.id,t.name,t.path||t.name),n.input.value=Array.from(n.selectedTerms).join(","),n.input.dispatchEvent(new Event("change",{bubbles:!0})),n.autocompleteDropdown.hidden=!0,o&&(o.value="")}}catch(e){console.error("Error creating term:",e),this.selector.error?.log(e,{component:"TaxonomyCreator",action:"handleAutocompleteCreate"})}finally{t.innerHTML=s,t.disabled=!1}}initTermCreation(){this.form&&this.form.addEventListener("change",(e=>{e.preventDefault(),e.stopPropagation()}))}resetParentOptions(){const e=this.selector.currentConfig?.taxonomy;if(!e)return;let t=this.createNew.querySelector("#select_parent");if(!t)return;let r=t.querySelector("option");if(!r)return;window.removeChildren(t),t.append(r.cloneNode(!0));const n=this.selector.store.filters.parent||0;if(0!==n){const e=this.selector.store.data.get(n);if(e){let n=r.cloneNode(!0);n.value=e.id,n.textContent=e.name,t.append(n)}}const o=[];this.selector.store.data.forEach((t=>{t.taxonomy===e&&t.parent===n&&o.push(t)})),o.sort(((e,t)=>e.name.localeCompare(t.name))),o.forEach((e=>{let n=r.cloneNode(!0);n.id=`select-parent-${e.id}`,n.value=e.id,n.textContent=" — "+e.name,t.append(n)}))}async createTerm(e,t=0,r){try{await this.selector.store.setFilters({taxonomy:r,search:e,page:1,parent:0}),await new Promise((e=>setTimeout(e,100)));const n=Array.from(this.selector.store.data.values()).find((t=>t.taxonomy===r&&t.name.toLowerCase()===e.toLowerCase()));if(n)return this.createNew&&this.showTermSuggestions([n],!0),{success:!1,reason:"exists",term:n};const o=await fetch(`${jvbSettings.api}terms`,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":window.auth.getNonce()},body:JSON.stringify({taxonomy:r,name:e,parent:t})});if(!o.ok)throw new Error(`Server error: ${o.status}`);return await o.json()}catch(e){throw console.error("Error creating term:",e),e}}showTermSuggestions(e,t=!1){const r=this.createNew.querySelector(".term-suggestions")||this.createSuggestionContainer();window.removeChildren(r);const n=document.createElement("h4");n.textContent=t?"This term already exists:":"Similar terms already exist:",r.appendChild(n);const o=document.createElement("ul");o.className="term-suggestion-list",e.forEach((e=>{const t=document.createElement("li"),n=document.createElement("button");n.type="button",n.className="use-existing-term",n.setAttribute("data-id",e.id),n.textContent=e.path||e.name,n.addEventListener("click",(()=>{this.selector.addSelectedTermToModal(e.id,e.name,e.path||e.name),this.createNew.open=!1,r.hidden=!0,this.form.querySelector('input[name="term_name"]').value=""})),t.appendChild(n),o.appendChild(t)})),r.appendChild(o),r.hidden=!1}createSuggestionContainer(){const e=document.createElement("div");return e.className="term-suggestions",e.hidden=!0,this.createNew.querySelector("form").after(e),e}destroy(){this.clickHandler&&document.removeEventListener("click",this.clickHandler);const e=this.createNew?.querySelector(".loading-message.create-term");e&&(e.hidden=!0);const t=this.createNew?.querySelector(".term-suggestions");t&&(t.hidden=!0)}}; |
| | |
| | | $term = get_term($term_id, $tax); |
| | | |
| | | CacheManager::for(jvbNoBase($taxonomy))->invalidate(); |
| | | CacheManager::for(jvbNoBase($taxonomy))->clear(); |
| | | |
| | | // Clear cache for associated content types |
| | | $checker = Checker::getInstance(); |
| | |
| | | parent::__construct(); |
| | | // $this->cache->invalidateGroup('terms'); |
| | | $this->per_page = 20; |
| | | $this->cache->clear(); |
| | | add_action('edited_term', [$this, 'deleteTermPath']); |
| | | add_action('wp_login', [$this, 'clearUserTaxonomyCache'], 10, 2); |
| | | } |