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

1 line
6.0 KiB
JavaScript
Raw Normal View History

2017-06-19 11:49:34 +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"),d=t("#rates"),c=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&&(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),d.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),c.attr("disabled","disabled"),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),d=this.per_page*this.page,c=_.toArray(a).slice(s,d),u=this;this.$el.empty(),c.length?t.each(c,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,c=_.indexBy(s.get("rates"),"tax_rate_id"),l={},u=_.size(c),h=_.extend({},e.default_rate,{tax_rate_id:"new-"+u+"-"+Date.now(),newRow:!0});(a=d.children(".current")).length?(n=a.last().data("id"),i=parseInt(c[n].tax_rate_order,10),h.tax_rate_order=1+i,r=_.filter(c,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(c,"tax_rate_order"),function(t){return parseInt(t,10)}),o.page=o.qty_pages),c[h.tax_rate_id]=h,l[h.tax_rate_id]=h,s.set("rate