var tdbFormTaxonomies={};jQuery().ready(function(){tdbFormTaxonomies.init()});
(function(){tdbFormTaxonomies={items:[],item:function(){this.enableTermCreate=this.disableSearch=this.showAllTerms=this.required=this.blockObj=this.uid="";this.orderBy="name";this.order="ASC";this.termIsHerarchical=this.termType="";this.subChildDisplay=this.childDisplay=this.parentDisplay="dropdown";this.subChildDropLabel=this.childDropLabel=this.parentDropLabel="";this.subChildLimit=this.childLimit=this.parentLimit=-1;this.childCfEnable=!1;this.childCfFilter=[];this.subChildCfEnable=!1;this.subChildCfFilter=
[];this.disabledForGuests=!1;this._nonce=this.searchByTxt=this.addNewTxt=this.descriptionTxt=this.selectTxt=this.selectParentTxt=this.parentTxt=this.nameTxt="";this._is_initialized=this._in_composer=!1},init:function(){tdbFormTaxonomies.items=[];this._is_running=!1},_initialize_item:function(a){if(!0!==a._is_initialized){a.blockObj.on("s:open",function(){document.querySelector(".select2-search__field").focus()});if(!a.disabledForGuests){var b=function(a){a=jQuery(a.element).text();a=a.replace(/\[\[/g,
'<span class="tdb-ft-term-name">');a=a.replace(/\{\{/g,'<span class="tdb-ft-term-cf">');a=a.replace(/\(\(/g,'<span class="tdb-ft-term-cf-label">');a=a.replace(/\}\}|\]\]|\)\)/g,"</span>");return a=a.replace(/ *\([^)]*\) */g,"")};jQuery("."+a.uid+" .tdb-s-form-group select").select2({dropdownCssClass:"tdb-s-select2 tdb-s-select2-"+a.uid+" tdb-ft-select2-parent",templateResult:b,templateSelection:b,escapeMarkup:function(a){return a}})}if(!a.showAllTerms)a.blockObj.on("change","select.tdb-s-form-input",
function(c){tdbFormTaxonomies.renderTermChildrenFromDropdown(a,jQuery(c.target))});a.blockObj.on("click",".tdb-s-fc-label",function(c){c.preventDefault();c=jQuery(c.target);var b=c.closest(".tdb-s-form-group").data("display");"checkbox"===b?tdbFormTaxonomies.renderTermChildrenFromCheckbox(a,c):"radio"===b&&tdbFormTaxonomies.renderTermChildrenFromRadio(a,c)});a.blockObj.on("keyup",".tdb-ft-checkbox-search",function(){var a=jQuery(this),b=new RegExp(a.val(),"i");a.closest(".tdb-s-form-group").find(".tdb-s-fc-label").each(function(){var a=
jQuery(this),c=a.closest(".tdb-s-form-check");b.test(a.text())?c.show():c.hide()})});if(a.enableTermCreate)a.blockObj.on("click",".tdb-ft-create-term",function(c){c.preventDefault();c=jQuery(c.target).closest(".tdb-s-form-group");var b=c.data("term-hierarchy"),d="child"===b||"sub-child"===b?c.data("parent-id"):0,h=a.blockObj.find(".tdb-s-content");c='<div class="tdb-s-form"><div class="tdb-s-form-content"><div class="tdb-s-fc-inner"><div class="tdb-s-form-group tdb-s-form-group-sm tdb-s-form-group-name"><label class="tdb-s-form-label" for="tdb-ft-new-term-name">'+
(a.nameTxt+"</label>");c+='<input type="text" class="tdb-s-form-input" name="tdb-ft-new-term-name" id="tdb-ft-new-term-name"></div>';a.termIsHerarchical&&(c=c+'<div class="tdb-s-form-group tdb-s-form-group-sm tdb-s-form-group-parent"><label class="tdb-s-form-label" for="tdb-ft-new-term-parent-id">'+(a.parentTxt+"</label>"),c=c+'<div class="tdb-s-form-select-wrap tdb-s-content tdb-s-content-loading"><select class="tdb-s-form-input" name="tdb-ft-new-term-parent-id" id="tdb-ft-new-term-parent-id"><option value="0">--'+
(a.selectParentTxt+" --</option>"),c+='</select><svg class="tdb-s-form-select-icon" xmlns="http://www.w3.org/2000/svg" width="8.947" height="12.578" viewBox="0 0 8.947 12.578"><g transform="translate(7.947 1) rotate(90)"><path d="M0,7.947A1,1,0,0,1-.58,7.761,1,1,0,0,1-.815,6.366l2.06-2.893L-.815.58A1,1,0,0,1-.58-.815,1,1,0,0,1,.815-.58L3.288,2.893a1,1,0,0,1,0,1.16L.815,7.527A1,1,0,0,1,0,7.947Z" transform="translate(8.104 0)"/><path d="M2.474,7.947a1,1,0,0,1-.815-.42L-.815,4.053a1,1,0,0,1,0-1.16L1.659-.58A1,1,0,0,1,3.053-.815,1,1,0,0,1,3.288.58L1.228,3.473l2.06,2.893a1,1,0,0,1-.814,1.58Z" transform="translate(0 0)"/></g></svg></div></div>');
c=c+'<div class="tdb-s-form-group tdb-s-form-group-sm tdb-s-form-group-descr"><label class="tdb-s-form-label" for="tdb-ft-new-term-descr">'+(a.descriptionTxt+"</label>");c+='<textarea class="tdb-s-form-input" name="tdb-ft-new-term-descr" id="tdb-ft-new-term-descr"></textarea></div></div></div></div>';var g=new tdbModal.item;g.customClasses="tdb-ft-create-term-modal tdb-ft-create-term-modal-"+a.uid;g.title="Create new term";g.body=c;g.validateFormInputs=[{id:"tdb-ft-new-term-name",rules:"required"}];
tdbModal.addItem(g);tdbModal.openModal(g);if(a.termIsHerarchical){var k=g.modalObj.find("#tdb-ft-new-term-parent-id");jQuery.ajax({type:"POST",url:td_ajax_url,data:{action:"tdb_ft_get_terms",_nonce:a._nonce,parentTermID:-1,termType:a.termType,depth:2,orderBy:a.orderBy,order:a.order},success:function(c){function b(a){a=jQuery(a.element).text();a=a.replace(/\[\[/g,'<span class="tdb-ft-term-name">');a=a.replace(/\{\{/g,'<span class="tdb-ft-term-cf">');a=a.replace(/\(\(/g,'<span class="tdb-ft-term-cf-label">');
a=a.replace(/\}\}|\]\]|\)\)/g,"</span>");return a=a.replace(/ *\([^)]*\) */g,"")}c=jQuery.parseJSON(c);Object.keys(c).length&&k.append(tdbFormTaxonomies.render_dropdown_options(c,0,[d.toString()]));k.select2({dropdownCssClass:"tdb-s-select2 tdb-s-select2-"+a.uid,templateResult:b,templateSelection:b,escapeMarkup:function(a){return a}});k.closest(".tdb-s-form-select-wrap").removeClass("tdb-s-content-loading")}})}g.modalObj.on("tdbModal.confirming",function(){var c=g.modalObj.find(".tdb-s-fc-inner"),
d=c.find("#tdb-ft-new-term-name"),b=c.find("#tdb-ft-new-term-parent-id"),f=c.find("#tdb-ft-new-term-descr");d=d.val();var k=b.length?b.val():0;b=f.val();c.find(".tdb-s-notif").remove();!0===tdbModal._validate_form_inputs(g)?jQuery.ajax({type:"POST",url:td_ajax_url,data:{action:"tdb_create_term",_nonce:a._nonce,termName:d,termType:a.termType,termDescr:b,parentTermID:k},success:function(b){b=jQuery.parseJSON(b);if(b.errors.length){var d='<div class="tdb-s-notif tdb-s-notif-xsm tdb-s-notif-error">';
d+='<ul class="tdb-s-notif-list">';jQuery.each(b.errors,function(a,b){d+="<li>"+b+"</li>"});d+="</ul>";d+="</div>";c.append(d);g.modalObj.trigger("tdbModal.confirmingReset")}else{g.modalObj.trigger("tdbModal.confirmed");b=0==k||a.showAllTerms?h.find('.tdb-s-form-group[data-term-hierarchy="parent"]'):h.find('.tdb-s-form-group[data-parent-id="'+k+'"]');var f=b.data("display");if(b.length){var m=[];if("dropdown"===f){var e=b.find("select option:selected");e.length&&m.push(e.val())}else"checkbox"===f?
(e=b.find(".tdb-s-form-check input:checked"),jQuery.each(e,function(a,b){m.push(jQuery(this).val())})):"radio"===f&&(e=b.find(".tdb-s-form-check input:checked"),e.length&&m.push(e.val()));tdbFormTaxonomies.refreshTerms(a,b,k,m,f)}else b=h.find('[value="'+k+'"]'),b.length&&(e=b.closest(".tdb-s-form-group"),f=e.data("term-hierarchy"),e=e.data("display"),"sub-child"!==f&&("dropdown"===e?b.is(":selected")&&(b=b.closest("select"),tdbFormTaxonomies.renderTermChildrenFromDropdown(a,b)):"checkbox"===e?b.is(":checked")&&
(b=b.closest(".tdb-s-fc-label"),tdbFormTaxonomies.renderTermChildrenFromCheckbox(a,b,!0)):"radio"===e&&b.is(":checked")&&(b=b.closest(".tdb-s-fc-label"),tdbFormTaxonomies.renderTermChildrenFromRadio(a,b,!0))))}}}):g.modalObj.trigger("tdbModal.confirmingReset")})});a._is_initialized=!0}},addItem:function(a){if("undefined"===typeof a.uid)throw"item.uid is not defined";tdbFormTaxonomies.items.push(a);tdbFormTaxonomies._initialize_item(a)},renderTermChildrenFromDropdown:function(a,b){var c=b.closest(".tdb-s-form-group").data("term-hierarchy"),
f=b.val(),d=b.find("option:selected").text().replace(/ *\([^)]*\) */g,""),h="",g="",k=b.attr("data-old-term-id");""===b.val()?tdbFormTaxonomies.removeChildTerms(a,k,c):(b.attr("data-old-term-id",f),"parent"===c?h="child":"child"===c&&(h="sub-child"),"child"===h?g=a.childDisplay:"sub-child"===h&&(g=a.subChildDisplay),tdbFormTaxonomies.removeChildTerms(a,k,c),tdbFormTaxonomies.renderTermChildren(a,f,d,c,h,g))},renderTermChildrenFromCheckbox:function(a,b,c){c=void 0===c?!1:c;var f=b.find('input[type="checkbox"]'),
d=b.closest(".tdb-s-form-group").data("term-hierarchy"),h=f.val();b=b.find(".tdb-s-fc-title").text();var g="",k="",e=a.blockObj.find(".tdb-s-form");f.prop("disabled")||("parent"===d?g="child":"child"===d?g="sub-child":"sub-child"===d&&(g="last-level"),"child"===g?k=a.childDisplay:"sub-child"===g&&(k=a.subChildDisplay),f.prop("checked")&&!c?(f.prop("checked",!1),a.showAllTerms?tdbFormTaxonomies.manageLimits(a):tdbFormTaxonomies.removeChildTerms(a,h,d)):(f.prop("checked",!0),a.showAllTerms?tdbFormTaxonomies.manageLimits(a):
"last-level"===g?(e.addClass("tdb-s-content-loading"),setTimeout(function(){tdbFormTaxonomies.manageLimits(a);e.removeClass("tdb-s-content-loading")},400)):tdbFormTaxonomies.renderTermChildren(a,h,b,d,g,k)))},renderTermChildrenFromRadio:function(a,b,c){c=void 0===c?!1:c;var f=b.find('input[type="radio"]'),d=b.closest(".tdb-s-form-group"),h=d.find(".tdb-ft-checkboxes");d=d.data("term-hierarchy");var g=f.val();b=b.find(".tdb-s-fc-title").text();var k="",e="",l=h.attr("data-old-term-id");h.attr("data-old-term-id",
g);"parent"===d?k="child":"child"===d&&(k="sub-child");"child"===k?e=a.childDisplay:"sub-child"===k&&(e=a.subChildDisplay);f.prop("checked")?c&&(tdbFormTaxonomies.removeChildTerms(a,l,d),tdbFormTaxonomies.renderTermChildren(a,g,b,d,k,e)):(h.find('input[value="'+l+'"]').prop("checked",!1),f.prop("checked",!0),tdbFormTaxonomies.removeChildTerms(a,l,d),tdbFormTaxonomies.renderTermChildren(a,g,b,d,k,e))},renderTermChildren:function(a,b,c,f,d,h){var g=a.blockObj.find(".tdb-s-content"),k=g.find(".tdb-s-fc-inner");
f=!1;var e=[];switch(d){case "child":f=a.childCfEnable;e=a.childCfFilter;break;case "sub-child":f=a.subChildCfEnable,e=a.subChildCfFilter}console.log(e);g.addClass("tdb-s-content-loading");jQuery.ajax({type:"POST",url:td_ajax_url,data:{action:"tdb_ft_get_terms",_nonce:a._nonce,parentTermID:b,termType:a.termType,orderBy:a.orderBy,order:a.order,termsCfEnable:f,termsCfFilter:e},success:function(f){f=jQuery.parseJSON(f);var e="";if(Object.keys(f).length||a.enableChildTermCreate){e='<div class="tdb-s-form-group" data-term-hierarchy="'+
d+'" data-parent-id="'+b+'" data-parent-name="'+c+'" data-display="'+h+'">';e+='<label class="tdb-s-form-label">';e+='<span class="tdb-s-form-label-inner">';-1<c.indexOf("[[")&&(c=c.substring(2,c.indexOf("]]")));e+=c.charAt(0).toUpperCase()+c.slice(1);e+="</span>";a.enableTermCreate&&(e+='<a class="tdb-s-btn tdb-s-btn-simple tdb-ft-create-term" href="#">'+a.addNewTxt+"</a>");"checkbox"!==h||a.disableSearch||(e+='<input class="tdb-s-form-input tdb-ft-checkbox-search" type="text" placeholder="'+a.searchByTxt+
'">');e+="</label>";if("dropdown"===h){var l="";switch(d){case "child":l=a.childDropLabel;break;case "sub-child":l=a.subChildDropLabel}""===l&&(l="-- "+a.selectTxt+" "+(a.termName.charAt(0).toLowerCase()+a.termName.slice(1))+" --");e+='<div class="tdb-s-form-select-wrap">';e+='<select class="tdb-s-form-input" name="tdb-posts-form-taxonomies-'+a.uid+'[]">';e+='<option value="">'+l+"</option>";e+=tdbFormTaxonomies.render_dropdown_options(f,0,[]);e+="</select>";e+='<svg class="tdb-s-form-select-icon" xmlns="http://www.w3.org/2000/svg" width="8.947" height="12.578" viewBox="0 0 8.947 12.578"><g transform="translate(7.947 1) rotate(90)"><path d="M0,7.947A1,1,0,0,1-.58,7.761,1,1,0,0,1-.815,6.366l2.06-2.893L-.815.58A1,1,0,0,1-.58-.815,1,1,0,0,1,.815-.58L3.288,2.893a1,1,0,0,1,0,1.16L.815,7.527A1,1,0,0,1,0,7.947Z" transform="translate(8.104 0)"/><path d="M2.474,7.947a1,1,0,0,1-.815-.42L-.815,4.053a1,1,0,0,1,0-1.16L1.659-.58A1,1,0,0,1,3.053-.815,1,1,0,0,1,3.288.58L1.228,3.473l2.06,2.893a1,1,0,0,1-.814,1.58Z" transform="translate(0 0)"/></g></svg>';
e+="</div>"}else"checkbox"===h?(e+='<div class="tdb-ft-checkboxes">',e+='<div class="tdb-s-form-checkboxes-wrap">',e+=tdbFormTaxonomies.render_checkboxes(a,f,0,[]),e+="</div>",e+="</div>"):"radio"===h&&(e+='<div class="tdb-ft-checkboxes">',e+='<div class="tdb-s-form-checkboxes-wrap">',e+=tdbFormTaxonomies.render_radios(a,f,0,[]),e+="</div>",e+="</div>");e+="</div>";e=jQuery(e);"dropdown"===h&&(f=function(a){a=jQuery(a.element).text();a=a.replace(/\[\[/g,'<span class="tdb-ft-term-name">');a=a.replace(/\{\{/g,
'<span class="tdb-ft-term-cf">');a=a.replace(/\(\(/g,'<span class="tdb-ft-term-cf-label">');a=a.replace(/\}\}|\]\]|\)\)/g,"</span>");return a=a.replace(/ *\([^)]*\) */g,"")},e.find("select").select2({dropdownCssClass:"tdb-s-select2 tdb-s-select2-"+a.uid+" tdb-ft-select2-"+d,templateResult:f,templateSelection:f,escapeMarkup:function(a){return a}}))}setTimeout(function(){k.append(e);tdbFormTaxonomies.manageLimits(a);g.removeClass("tdb-s-content-loading")},200)}})},refreshTerms:function(a,b,c,f,d){var h=
a.blockObj.find(".tdb-s-content");h.addClass("tdb-s-content-loading");jQuery.ajax({type:"POST",url:td_ajax_url,data:{action:"tdb_ft_get_terms",_nonce:a._nonce,parentTermID:a.showAllTerms?-1:c,termType:a.termType,orderBy:a.orderBy,order:a.order},success:function(c){var g=jQuery.parseJSON(c);setTimeout(function(){Object.keys(g).length&&("dropdown"===d?(b.find("select option:not(:first-child)").remove(),b.find("select").append(tdbFormTaxonomies.render_dropdown_options(g,0,f))):"checkbox"===d?b.find(".tdb-s-form-checkboxes-wrap").html(tdbFormTaxonomies.render_checkboxes(a,
g,0,f)):"radio"===d&&b.find(".tdb-s-form-checkboxes-wrap").html(tdbFormTaxonomies.render_radios(a,g,0,f)));tdbFormTaxonomies.manageLimits(a);h.removeClass("tdb-s-content-loading")},200)}})},render_dropdown_options:function(a,b,c){var f="",d="-".repeat(b);""!==d&&(d+=" ");b++;jQuery.each(a,function(a,g){var h=-1!==jQuery.inArray(g.id.toString(),c);f+='<option value="'+g.id+'" '+(h?"selected":"")+">[["+d+a+"]]";0<Object.keys(g.acf_fields).length&&jQuery.each(g.acf_fields,function(a,b){if(""===b.value)return!0;
f+="{{(("+b.label+": ))"+b.value+"}}"});f+="</option>";0<Object.keys(g.children).length&&(f+=tdbFormTaxonomies.render_dropdown_options(g.children,b,c))});return f},render_checkboxes:function(a,b,c,f){var d="",h="-".repeat(c);""!==h&&(h+=" ");c++;jQuery.each(b,function(b,k){var e=-1!==jQuery.inArray(k.id.toString(),f);d+='<div class="tdb-s-form-check tdb-ft-term-'+k.id+'">';d+='<label class="tdb-s-fc-label">';d+='<input type="checkbox" name="tdb-posts-form-taxonomies-'+a.uid+'[]" value="'+k.id+'" '+
(e?"checked":"")+">";d+='<span class="tdb-s-fc-check"></span>';d+='<span class="tdb-s-fc-title">'+h+b+"</span>";d+="</label>";0<Object.keys(k.acf_fields).length&&(d+='<div class="tdb-ft-terms-cfs">',jQuery.each(k.acf_fields,function(a,b){if(""===b.value)return!0;d+='<div class="tdb-ft-term-cf">';d+='<span class="tdb-ft-term-cf-label">'+b.label+": </span>"+b.value;d+="</div>"}),d+="</div>");d+="</div>";0<Object.keys(k.children).length&&(d+=tdbFormTaxonomies.render_checkboxes(a,k.children,c,f))});return d},
render_radios:function(a,b,c,f,d){d=void 0===d?"":d;var h="",g="-".repeat(c);""!==g&&(g+=" ");c++;""===d&&(d=Math.floor(65536*(1+Math.random())).toString(16).substring(1));jQuery.each(b,function(b,e){var k=-1!==jQuery.inArray(e.id.toString(),f);h+='<div class="tdb-s-form-check tdb-ft-term-'+e.id+'">';h+='<label class="tdb-s-fc-label">';h+='<input type="radio" name="tdb-posts-form-taxonomies-'+a.uid+"-"+d+'[]" value="'+e.id+'" '+(k?"checked":"")+">";h+='<span class="tdb-s-fc-check"></span>';h+='<span class="tdb-s-fc-title">'+
g+b+"</span>";h+="</label>";0<Object.keys(e.acf_fields).length&&(h+='<div class="tdb-ft-terms-cfs">',jQuery.each(e.acf_fields,function(a,b){if(""===b.value)return!0;h+='<div class="tdb-ft-term-cf">';h+='<span class="tdb-ft-term-cf-label">'+b.label+": </span>"+b.value;h+="</div>"}),h+="</div>");h+="</div>";0<Object.keys(e.children).length&&(h+=tdbFormTaxonomies.render_radios(a,e.children,c,f))});return h},removeChildTerms:function(a,b,c){var f=a.blockObj.find(".tdb-s-content"),d=f.find('.tdb-s-form-group[data-parent-id="'+
b+'"]');f.addClass("tdb-s-content-loading");setTimeout(function(){if(d.length&&(d.remove(),"parent"===c)){var b=void 0,g=d.find("input:checked");g.length&&g.each(function(){b=f.find('.tdb-s-form-group[data-parent-id="'+jQuery(this).val()+'"]')});g=d.find("select");g.length&&(b=f.find('.tdb-s-form-group[data-parent-id="'+g.val()+'"]'));"undefined"!==typeof b&&b.length&&b.remove()}tdbFormTaxonomies.manageLimits(a);f.removeClass("tdb-s-content-loading")},400)},manageLimits:function(a){var b=a.blockObj.find(".tdb-ft-limit-warning"),
c=b.find(".tdb-ft-limit-parent-notif"),f=b.find(".tdb-ft-limit-child-notif"),d=b.find(".tdb-ft-limit-sub-child-notif"),h=a.blockObj.find('.tdb-s-form-group[data-term-hierarchy="parent"]'),g=a.blockObj.find('.tdb-s-form-group[data-term-hierarchy="child"]'),k=a.blockObj.find('.tdb-s-form-group[data-term-hierarchy="sub-child"]'),e=h.find("input:checked").length,l=g.find("input:checked").length,m=k.find("input:checked").length;e=-1<a.parentLimit&&e>=a.parentLimit;l=-1<a.childLimit&&l>=a.childLimit;a=
-1<a.subChildLimit&&m>=a.subChildLimit;e&&c.length||l&&f.length||a&&d.length?b.show():b.hide();e?(h.find('input[type="checkbox"]:not(:checked)').prop("disabled",!0),c.show()):(h.find('input[type="checkbox"]:not(:checked)').prop("disabled",!1),c.hide());l?(g.find('input[type="checkbox"]:not(:checked)').prop("disabled",!0),f.show()):(g.find('input[type="checkbox"]:not(:checked)').prop("disabled",!1),f.hide());a?(k.find('input[type="checkbox"]:not(:checked)').prop("disabled",!0),d.show()):(k.find('input[type="checkbox"]:not(:checked)').prop("disabled",
!1),d.hide())},deleteItem:function(a){for(var b=0;b<tdbFormTaxonomies.items.length;b++)if(tdbFormTaxonomies.items[b].uid===a)return tdbFormTaxonomies.items.splice(b,1),!0;return!1},resetItems:function(){tdbFormTaxonomies.items=[]}}})();
