2018-02-23 16:55:06 +00:00
|
|
|
!function(t,e,a,n){t(function(){String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")});var i=a.template("wc-tax-table-row"),r=a.template("wc-tax-table-row-empty"),o=a.template("wc-tax-table-pagination"),s=t(".wc_tax_rates"),c=t("#rates"),d=t(':input[name="save"]'),l=t("#rates-pagination"),u=t("#rates-search .wc-tax-rates-search-field"),h=t(".submit .button-primary[type=submit]"),p=Backbone.Model.extend({changes:{},setRateAttribute:function(t,e,a){var n=_.indexBy(this.get("rates"),"tax_rate_id"),i={};n[t][e]!==a&&(i[t]={},i[t][e]=a,n[t][e]=a),this.logChanges(i)},logChanges:function(t){var e=this.changes||{};_.each(t,function(t,a){e[a]=_.extend(e[a]||{tax_rate_id:a},t)}),this.changes=e,this.trigger("change:rates")},getFilteredRates:function(){var t=this.get("rates"),e=u.val().toLowerCase();return e.length&&(t=_.filter(t,function(t){return-1!==_.toArray(t).join(" ").toLowerCase().indexOf(e)})),t=_.sortBy(t,function(t){return parseInt(t.tax_rate_order,10)})},block:function(){t(".wc_tax_rates").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){t(".wc_tax_rates").unblock()},save:function(){var t=this;t.block(),Backbone.ajax({method:"POST",dataType:"json",url:n+(n.indexOf("?")>0?"&":"?")+"action=woocommerce_tax_rates_save_changes",data:{current_class:e.current_class,wc_tax_nonce:e.wc_tax_nonce,changes:t.changes},success:function(e,a){"success"===a&&e.success&&(f.set("rates",e.data.rates),f.trigger("change:rates"),f.changes={},f.trigger("saved:rates"),m.render()),t.unblock()}})}}),g=Backbone.View.extend({rowTemplate:i,per_page:e.limit,page:e.page,initialize:function(){var a=Math.ceil(_.toArray(this.model.get("rates")).length/this.per_page);this.qty_pages=0===a?1:a,this.page=this.sanitizePage(e.page),this.listenTo(this.model,"change:rates",this.setUnloadConfirmation),this.listenTo(this.model,"saved:rates",this.clearUnloadConfirmation),c.on("change autocompletechange",":input",{view:this},this.updateModelOnChange),u.on("keyup search",{view:this},this.onSearchField),l.on("click","a",{view:this},this.onPageChange),l.on("change","input",{view:this},this.onPageChange),t(window).on("beforeunload",{view:this},this.unloadConfirmation),h.on("click",{view:this},this.onSubmit),d.prop("disabled",!0),s.find(".insert").on("click",{view:this},this.onAddNewRow),s.find(".remove_tax_rates").on("click",{view:this},this.onDeleteRow),s.find(".export").on("click",{view:this},this.onExport)},render:function(){var a=this.model.getFilteredRates(),n=_.size(a),i=Math.ceil(n/this.per_page),s=0===n?0:this.per_page*(this.page-1),c=this.per_page*this.page,d=_.toArray(a).slice(s,c),u=this;this.$el.empty(),d.length?t.each(d,function(t,e){u.$el.append(u.rowTemplate(e))}):u.$el.append(r()),this.$el.find("td.country input").autocomplete({source:e.countries,minLength:2}),this.$el.find("td.state input").autocomplete({source:e.states,minLength:3}),this.$el.find("td.postcode input, td.city input").change(function(){t(this).attr("name",t(this).data("name"))}),i>1?l.html(o({qty_rates:n,current_page:this.page,qty_pages:i})):(l.empty(),u.page=1)},updateUrl:function(){if(window.history.replaceState){var t=e.base_url,a=u.val();1<this.page&&(t+="&p="+encodeURIComponent(this.page)),a.length&&(t+="&s="+encodeURIComponent(a)),window.history.replaceState({},"",t)}},onSubmit:function(t){t.data.view.model.save(),t.preventDefault()},onAddNewRow:function(t){var a,n,i,r,o=t.data.view,s=o.model,d=_.indexBy(s.get("rates"),"tax_rate_id"),l={},u=_.size(d),h=_.extend({},e.default_rate,{tax_rate_id:"new-"+u+"-"+Date.now(),newRow:!0});(a=c.children(".current")).length?(n=a.last().data("id"),i=parseInt(d[n].tax_rate_order,10),h.tax_rate_order=1+i,r=_.filter(d,function(t){return parseInt(t.tax_rate_order,10)>i}),_.map(r,function(t){return t.tax_rate_order++,l[t.tax_rate_id]=_.extend(l[t.tax_rate_id]||{},{tax_rate_order:t.tax_rate_order}),t})):(h.tax_rate_order=1+_.max(_.pluck(d,"tax_rate_order"),function(t){return parseInt(t,10)}),o.page=o.qty_pages),d[h.tax_rate_id]=h,l[h.tax_rate_id]=h,s.set("
|