From eea4e21d9bd7b89f7124fa1acbe3347d68db6d90 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 04 Jan 2026 19:35:27 +0000
Subject: [PATCH] =further taxonomyCreator.js debugging

---
 assets/js/min/creator.min.js |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/assets/js/min/creator.min.js b/assets/js/min/creator.min.js
index 59cb9ab..b33fafe 100644
--- a/assets/js/min/creator.min.js
+++ b/assets/js/min/creator.min.js
@@ -1 +1 @@
-window.jvbTaxCreator=class{constructor(e){this.selector=e,this.taxonomy=e.currentConfig?.taxonomy,this.taxonomy?(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.initTermCreation()):console.error("TaxonomyCreator: No active field or taxonomy found")}initListeners(){document.addEventListener("click",this.handleClick.bind(this))}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)}async handleTermCreation(e){const t=this.form.querySelector('input[name="term_name"]').value.trim(),r=this.form.querySelector("input#select_parent")?.value;try{this.form.querySelector("button").disabled=!0;const e=await this.createTerm(t,r);if(e.success){let t=e.term;this.createNew.open=!1,this.selector.createTermElement({id:parseInt(t.id),name:t.name,hasChildren:t.hasChildren||!1,path:t.path||t.name,show:!1}),this.selector.addSelectedTermToModal(t.id,t.name,t.path),this.form.querySelector('input[name="term_name"]').value=""}}catch(e){console.error("Error creating term:",e),this.selector.showError?.("Failed to create term")||console.error("Failed to create term")}finally{this.form.querySelector("button").disabled=!1}}initTermCreation(){this.form&&this.form.addEventListener("change",(e=>{e.preventDefault(),e.stopPropagation()}))}resetParentOptions(){let e=this.createNew.querySelector("#select_parent");if(!e)return;let t=e.querySelector("option");if(!t)return;if(window.removeChildren(e),e.append(t.cloneNode(!0)),""!==this.selector.currentParentName){let r=t.cloneNode(!0);r.value=this.selector.currentParent,r.textContent=this.selector.currentParentName,e.append(r)}const r=this.selector.currentTerms;r&&r.length>0&&r.forEach((r=>{let n=t.cloneNode(!0);n.id=`select-parent-${r.id}`,n.value=r.id,n.textContent="  — "+r.name,e.append(n)}))}async createTerm(e,t=0){let r=this.createNew.querySelector(".loading-message.create-term"),n=r?.querySelector("span");try{r&&(r.hidden=!1),n&&window.typeText?window.typeText(n,"Checking term..."):n&&(n.textContent="Checking term...");const o=this.selector.searchQuery,s=this.selector.fetchSpecificTerms;this.selector.searchQuery=e,this.selector.fetchSpecificTerms=!1;const i=await this.selector.fetchTerms(this.selector.activeField,!1,!0);this.selector.searchQuery=o,this.selector.fetchSpecificTerms=s;const a=i.filter((t=>t.name.toLowerCase()===e.toLowerCase()));if(a.length>0)return this.showTermSuggestions(a),{success:!1,reason:"exists"};if(i.length>0)return this.showTermSuggestions(i),{success:!1,reason:"similar"};n&&(n.textContent="Creating term...");const c=await fetch(`${jvbSettings.api}terms`,{method:"POST",headers:{"Content-Type":"application/json","X-WP-Nonce":jvbSettings.nonce},body:JSON.stringify({taxonomy:this.taxonomy,name:e,parent:t})});if(!c.ok)throw new Error(`Server error: ${c.status}`);return await c.json()}catch(e){throw console.error("Error creating term:",e),e}finally{this.form.querySelector("button").disabled=!1,r&&(r.hidden=!0)}}showTermSuggestions(e){const t=this.createNew.querySelector(".term-suggestions")||this.createSuggestionContainer();window.removeChildren(t);const r=document.createElement("h4");r.textContent="Similar terms already exist:",t.appendChild(r);const n=document.createElement("ul");n.className="term-suggestion-list",e.forEach((e=>{const r=document.createElement("li");let o=e.path||e.name;const s=document.createElement("button");s.type="button",s.className="use-existing-term",s.setAttribute("data-id",e.id),s.textContent=o,s.addEventListener("click",(()=>{this.selector.addSelectedTermToModal(e.id,e.name,e.path),this.createNew.open=!1,t.hidden=!0,this.form.querySelector('input[name="term_name"]').value=""})),r.appendChild(s),n.appendChild(r)})),t.appendChild(n),t.hidden=!1}createSuggestionContainer(){const e=document.createElement("div");return e.className="term-suggestions",e.hidden=!0,this.createNew.querySelector("form").after(e),e}destroy(){const e=this.createNew?.querySelector(".loading-message.create-term");e&&(e.hidden=!0);const t=this.createNew?.querySelector(".term-suggestions");t&&(t.hidden=!0)}};
\ No newline at end of file
+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)}};
\ No newline at end of file

--
Gitblit v1.10.0