woocommerce/assets/js/admin/settings-views-html-setting...

1 line
6.8 KiB
JavaScript

!function(a,b,c,d){a(function(){String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")});var e=c.template("wc-tax-table-row"),f=c.template("wc-tax-table-row-empty"),g=c.template("wc-tax-table-pagination"),h=a(".wc_tax_rates"),i=a("#rates"),j=a('input[name="save"]'),k=a("#rates-pagination"),l=a("#rates-search .wc-tax-rates-search-field"),m=a(".submit .button-primary[type=submit]"),n=Backbone.Model.extend({changes:{},setRateAttribute:function(a,b,c){var d=_.indexBy(this.get("rates"),"tax_rate_id"),e={};d[a][b]!==c&&(e[a]={},e[a][b]=c,d[a][b]=c),this.logChanges(e)},logChanges:function(a){var b=this.changes||{};_.each(a,function(a,c){b[c]=_.extend(b[c]||{tax_rate_id:c},a)}),this.changes=b,this.trigger("change:rates")},getFilteredRates:function(){var a=this.get("rates"),b=l.val().toLowerCase();return b.length&&(a=_.filter(a,function(a){var c=_.toArray(a).join(" ").toLowerCase();return-1!==c.indexOf(b)})),a=_.sortBy(a,function(a){return parseInt(a.tax_rate_order,10)})},save:function(){a.post(d+"?action=woocommerce_tax_rates_save_changes",{current_class:b.current_class,wc_tax_nonce:b.wc_tax_nonce,changes:this.changes},this.onSaveResponse,"json")},onSaveResponse:function(a,b){"success"===b&&(p.set("rates",a.data.rates),p.trigger("change:rates"),p.changes={},p.trigger("saved:rates"))}}),o=Backbone.View.extend({rowTemplate:e,per_page:b.limit,page:b.page,initialize:function(){var c=Math.ceil(_.toArray(this.model.get("rates")).length/this.per_page);this.qty_pages=0===c?1:c,this.page=this.sanitizePage(b.page),this.listenTo(this.model,"change:rates",this.setUnloadConfirmation),this.listenTo(this.model,"saved:rates",this.clearUnloadConfirmation),i.on("change",{view:this},this.updateModelOnChange),i.on("sortupdate",{view:this},this.updateModelOnSort),l.on("keyup search",{view:this},this.onSearchField),k.on("click","a",{view:this},this.onPageChange),k.on("change","input",{view:this},this.onPageChange),a(window).on("beforeunload",{view:this},this.unloadConfirmation),m.on("click",{view:this},this.onSubmit),j.attr("disabled","disabled"),h.find(".insert").on("click",{view:this},this.onAddNewRow),h.find(".remove_tax_rates").on("click",{view:this},this.onDeleteRow),h.find(".export").on("click",{view:this},this.onExport)},render:function(){var c=this.model.getFilteredRates(),d=_.size(c),e=Math.ceil(d/this.per_page),h=0===d?0:this.per_page*(this.page-1),j=this.per_page*this.page,m=_.toArray(c).slice(h,j),n=this;this.$el.empty(),m.length?a.each(m,function(a,b){n.$el.append(n.rowTemplate(b))}):n.$el.append(f()),this.$el.find("td.country input").autocomplete({source:b.countries,minLength:2}),this.$el.find("td.state input").autocomplete({source:b.states,minLength:3}),this.$el.find("td.postcode input, td.city input").change(function(){a(this).attr("name",a(this).data("name"))}),e>1&&k.html(g({qty_rates:d,current_page:this.page,qty_pages:e})),l.val()?i.sortable("disable"):i.sortable("enable")},updateUrl:function(){if(window.history.replaceState){var a=b.base_url,c=l.val();1<this.page&&(a+="&p="+encodeURIComponent(this.page)),c.length&&(a+="&s="+encodeURIComponent(c)),window.history.replaceState({},"",a)}},onSubmit:function(a){a.data.view.model.save(),a.preventDefault()},onAddNewRow:function(a){var c,d,e,f,g,h=a.data.view,j=h.model,k=_.indexBy(j.get("rates"),"tax_rate_id"),l={},m=_.size(k),n=_.extend({},b.default_rate,{tax_rate_id:"new-"+m+"-"+Date.now(),newRow:!0});c=i.children(".current"),c.length?(d=c.last().data("id"),e=parseInt(k[d].tax_rate_order,10),n.tax_rate_order=1+e,f=_.filter(k,function(a){return parseInt(a.tax_rate_order,10)>e?!0:!1}),g=_.map(f,function(a){return a.tax_rate_order++,l[a.tax_rate_id]=_.extend(l[a.tax_rate_id]||{},{tax_rate_order:a.tax_rate_order}),a})):n.tax_rate_order=1+_.max(_.pluck(k,"tax_rate_order"),function(a){return parseInt(a,10)}),k[n.tax_rate_id]=n,l[n.tax_rate_id]=n,j.set("rates",k),j.logChanges(l),h.render()},onDeleteRow:function(c){var d,e,f,g,h,j=c.data.view,k=j.model,l=_.indexBy(k.get("rates"),"tax_rate_id"),m={};c.preventDefault(),(d=i.children(".current"))?(d.each(function(){e=a(this).data("id"),f=parseInt(l[e].tax_rate_order,10),g=_.filter(l,function(a){return parseInt(a.tax_rate_order,10)>f?!0:!1}),h=_.map(g,function(a){return a.tax_rate_order--,m[a.tax_rate_id]=_.extend(m[a.tax_rate_id]||{},{tax_rate_order:a.tax_rate_order}),a}),delete l[e],m[e]=_.extend(m[e]||{},{deleted:"deleted"})}),k.set("rates",l),k.logChanges(m),j.render()):window.alert(b.strings.no_rows_selected)},onSearchField:function(a){a.data.view.updateUrl(),a.data.view.render()},onPageChange:function(b){var c=a(b.currentTarget);b.preventDefault(),b.data.view.page=c.data("goto")?c.data("goto"):c.val(),b.data.view.render(),b.data.view.updateUrl()},onExport:function(c){var d="data:application/csv;charset=utf-8,"+b.strings.csv_data_cols.join(",")+"\n";return a.each(c.data.view.model.getFilteredRates(),function(a,c){var e="";e+=c.tax_rate_country+",",e+=c.tax_rate_state+",",e+=(c.postcode?c.postcode.join("; "):"")+",",e+=(c.city?c.city.join("; "):"")+",",e+=c.tax_rate+",",e+=c.tax_rate_name+",",e+=c.tax_rate_priority+",",e+=c.tax_rate_compound+",",e+=c.tax_rate_shipping+",",e+=b.current_class,d+=e+"\n"}),a(this).attr("href",encodeURI(d)),!0},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,j.removeAttr("disabled")},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,j.attr("disabled","disabled")},unloadConfirmation:function(a){return a.data.view.needsUnloadConfirm?(a.returnValue=b.strings.unload_confirmation_msg,window.event.returnValue=b.strings.unload_confirmation_msg,b.strings.unload_confirmation_msg):void 0},updateModelOnChange:function(b){var c=b.data.view.model,d=a(b.target),e=d.closest("tr").data("id"),f=d.data("attribute"),g=d.val();("city"===f||"postcode"===f)&&(g=g.split(";"),g=a.map(g,function(a){return a.trim()})),("tax_rate_compound"===f||"tax_rate_shipping"===f)&&(g=d.is(":checked")?1:0),c.setRateAttribute(e,f,g)},updateModelOnSort:function(a,b){var c,d,e=a.data.view,f=e.model,g=b.item,h=g.data("id"),i=_.indexBy(f.get("rates"),"tax_rate_id"),j=i[h].tax_rate_order,k=g.index()+(e.page-1)*e.per_page,l=k>j?"higher":"lower",m={};c=_.filter(i,function(a){var b=parseInt(a.tax_rate_order,10),c=[j,k];return parseInt(a.tax_rate_id,10)===parseInt(h,10)?!0:b>_.min(c)&&b<_.max(c)?!0:"higher"===l&&b===_.max(c)?!0:"lower"===l&&b===_.min(c)?!0:!1}),d=_.map(c,function(a){var b=parseInt(a.tax_rate_order,10);return parseInt(a.tax_rate_id,10)===parseInt(h,10)?a.tax_rate_order=k:"higher"===l?a.tax_rate_order=b-1:"lower"===l&&(a.tax_rate_order=b+1),m[a.tax_rate_id]=_.extend(m[a.tax_rate_id]||{},{tax_rate_order:a.tax_rate_order}),a}),d.length&&(f.logChanges(m),e.render())},sanitizePage:function(a){return a=parseInt(a,10),1>a?a=1:a>this.qty_pages&&(a=this.qty_pages),a}}),p=new n({rates:b.rates}),q=new o({model:p,el:"#rates"});q.render()})}(jQuery,htmlSettingsTaxLocalizeScript,wp,ajaxurl);