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

1 line
6.5 KiB
JavaScript
Raw Normal View History

2016-07-14 13:48:11 +00:00
!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)})},block:function(){a(".wc_tax_rates").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){a(".wc_tax_rates").unblock()},save:function(){var a=this;a.block(),Backbone.ajax({method:"POST",dataType:"json",url:d+(d.indexOf("?")>0?"&":"?")+"action=woocommerce_tax_rates_save_changes",data:{current_class:b.current_class,wc_tax_nonce:b.wc_tax_nonce,changes:a.changes},success:function(b,c){"success"===c&&(p.set("rates",b.data.rates),p.trigger("change:rates"),p.changes={},p.trigger("saved:rates"),q.render()),a.unblock()}})}}),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 autocompletechange",":input",{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})):(k.empty(),n.page=1),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}),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_r