((t,$,e,s)=>{"use strict";({init(){this.form=e.tfId("tb_admin_new_gs"),this.loadAddNewForm(),this.addNew(),this.deleteStyle(),this.restore(),this.scalePreview(),this.importFile()},loadAddNewForm(){const i=$(".tb_add_new_gs");if(0===i.length)return;i.magnificPopup({type:"inline",midClick:!0,callbacks:{close(){e.tfId("tb_admin_new_gs").reset()}}});const a=e.tfClass("tb_gs_export");for(let e=a.length-1;e>-1;--e)a[e].tfOn(t.click,(e=>{e.preventDefault(),t.fetch({action:"tb_get_gs_post",id:e.target.dataset.id,nonce:s.nonce}).then((t=>{this.loadJsZip().then((()=>{let s=new JSZip;s.file("builder_gs_data_export.txt",JSON.stringify(t)),s.generateAsync({type:"blob"}).then((t=>{this.donwload(t,e.target.dataset.title+"_export_.zip")}))}))}))}))},addNew(){e.tfClass("tb_admin_save_gs")[0]?.tfOn(t.click,(t=>{t.preventDefault(),this.validateForm()?(t.target.text=s.i18n.creating,$.ajax({type:"POST",url:s.ajaxurl,dataType:"json",data:{action:"tb_save_custom_global_style",nonce:s.nonce,form_data:$(this.form).serialize()},success(e){"failed"===e.status?(alert(e.msg),t.target.text=s.i18n.create):"success"===e.status?window.location=e.url:t.target.text=s.i18n.create}})):alert(s.i18n.formValid)}))},deleteStyle(){const t=$(".tb_remove_gs");0!==t.length&&t.on("click",(function(t){t.preventDefault();const e=$(this),i=e.parents(".tb_admin_gs_list").data("list");confirm("publish"===i?s.i18n.deleteConfirm:s.i18n.deleteConfirm2)&&$.ajax({type:"POST",url:s.ajaxurl,dataType:"json",data:{action:"tb_delete_global_style",nonce:s.nonce,status:i,id:e.attr("data-id")},success(t){"failed"===t.status?(alert(t.msg),e.parents(".tb_gs_element").fadeIn()):"success"===t.status&&e.parents(".tb_gs_element").fadeOut()}})}))},validateForm(){let t=!0;return $.each($(this.form).serializeArray(),((e,s)=>{if(""==s.value)return t=!1,!1})),t},restore(){const t=$(".tb_gs_restore");0!==t.length&&t.on("click",(function(t){t.preventDefault();const e=$(this);e.parents(".tb_gs_element").fadeOut(),$.ajax({type:"POST",url:s.ajaxurl,dataType:"json",data:{action:"tb_restore_global_style",nonce:s.nonce,id:e.attr("data-id")},success(t){"failed"===t.status&&(alert(t.msg),e.parents(".tb_gs_element").fadeIn())}})}))},scalePreview(){$(".themify_builder_content").each((function(){let t=$(this),e=t.parent(),s=Math.min(e.width()/t.outerWidth(),e.height()/t.outerHeight());t.css({transform:"translate(-50%, -50%) scale("+s+")"})}))},importFile(){e.tfClass("tb_import_gs")[0]?.tfOn(t.click,(i=>{i.preventDefault();const a=e.createElement("input");a.type="file",a.accept=".zip,.txt",a.tfOn("change",(e=>{const i=e.currentTarget.files[0],a=e=>{t.fetch({action:"tb_import_gs_posts_ajax",data:e,nonce:s.nonce}).then((()=>{location.reload()}))};if("text/plain"===i.type){(new FileReader).tfOn("loadend",(function(t){if(this.readyState!==FileReader.DONE)throw Error(s.i18n.invalid_file);a(t.target.result)}),{passive:!0,once:!0}).readAsText(i)}else"application/x-zip-compressed"!==i.type&&"application/zip"!==i.type||this.loadJsZip().then((()=>{(new JSZip).loadAsync(i).then((t=>{const e=t.files;if(!e||!e["builder_gs_data_export.txt"])throw Error(s.i18n.missing_file);t.file("builder_gs_data_export.txt").async("text").then((t=>{a(t)}))})).catch((t=>{throw t}))}))})).click()}))},loadJsZip:()=>t.loadJs("https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js",!!window.JSZip,!1),donwload(t,s){let i=e.createElement("a");i.download=s,i.rel="noopener",i.href=URL.createObjectURL(t),setTimeout((()=>{URL.revokeObjectURL(i.href),i=null}),7e3),i.click()}}).init()})(Themify,jQuery,document,themifyGlobalStylesVars);