관리-도구
편집 파일: settings-views-html-settings-tax.min.js
!function(h,p,g,f){h(function(){String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")});var t=g.template("kkart-tax-table-row"),o=g.template("kkart-tax-table-row-empty"),s=g.template("kkart-tax-table-pagination"),e=h(".kkart_tax_rates"),c=h("#rates"),a=h(':input[name="save"]'),d=h("#rates-pagination"),n=h("#rates-search .kkart-tax-rates-search-field"),i=h(".submit .button-primary[type=submit]"),r=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 a=this.changes||{};_.each(t,function(t,e){a[e]=_.extend(a[e]||{tax_rate_id:e},t)}),this.changes=a,this.trigger("change:rates")},getFilteredRates:function(){var t=this.get("rates"),e=n.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(){h(".kkart_tax_rates").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){h(".kkart_tax_rates").unblock()},save:function(){var a=this;a.block(),Backbone.ajax({method:"POST",dataType:"json",url:f+(0<f.indexOf("?")?"&":"?")+"action=kkart_tax_rates_save_changes",data:{current_class:p.current_class,kkart_tax_nonce:p.kkart_tax_nonce,changes:a.changes},success:function(t,e){"success"===e&&t.success&&(l.set("rates",t.data.rates),l.trigger("change:rates"),l.changes={},l.trigger("saved:rates"),u.render()),a.unblock()}})}}),t=Backbone.View.extend({rowTemplate:t,per_page:p.limit,page:p.page,initialize:function(){var t=Math.ceil(_.toArray(this.model.get("rates")).length/this.per_page);this.qty_pages=0===t?1:t,this.page=this.sanitizePage(p.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),n.on("keyup search",{view:this},this.onSearchField),d.on("click","a",{view:this},this.onPageChange),d.on("change","input",{view:this},this.onPageChange),h(window).on("beforeunload",{view:this},this.unloadConfirmation),i.on("click",{view:this},this.onSubmit),a.prop("disabled",!0),e.find(".insert").on("click",{view:this},this.onAddNewRow),e.find(".remove_tax_rates").on("click",{view:this},this.onDeleteRow),e.find(".export").on("click",{view:this},this.onExport)},render:function(){var t=this.model.getFilteredRates(),e=_.size(t),a=Math.ceil(e/this.per_page),n=0===e?0:this.per_page*(this.page-1),i=this.per_page*this.page,i=_.toArray(t).slice(n,i),r=this;this.$el.empty(),i.length?h.each(i,function(t,e){r.$el.append(r.rowTemplate(e))}):r.$el.append(o()),this.$el.find("td.country input").autocomplete({source:p.countries,minLength:2}),this.$el.find("td.state input").autocomplete({source:p.states,minLength:3}),this.$el.find("td.postcode input, td.city input").change(function(){h(this).attr("name",h(this).data("name"))}),1<a?d.html(s({qty_rates:e,current_page:this.page,qty_pages:a})):(d.empty(),r.page=1)},updateUrl:function(){var t,e;window.history.replaceState&&(t=p.base_url,e=n.val(),1<this.page&&(t+="&p="+encodeURIComponent(this.page)),e.length&&(t+="&s="+encodeURIComponent(e)),window.history.replaceState({},"",t))},onSubmit:function(t){t.data.view.model.save(),t.preventDefault()},onAddNewRow:function(t){var e,a=t.data.view,n=a.model,i=_.indexBy(n.get("rates"),"tax_rate_id"),r={},o=_.size(i),t=_.extend({},p.default_rate,{tax_rate_id:"new-"+o+"-"+Date.now(),newRow:!0}),o=c.children(".current");o.length?(o=o.last().data("id"),e=parseInt(i[o].tax_rate_order,10),t.tax_rate_order=1+e,o=_.filter(i,function(t){return parseInt(t.tax_rate_order,10)>e}),_.map(o,function(t){return t.tax_rate_order++,r[t.tax_rate_id]=_.extend(r[t.tax_rate_id]||{},{tax_rate_order:t.tax_rate_order}),t})):(t.tax_rate_order=1+_.max(_.pluck(i,"tax_rate_order"),function(t){return parseInt(t,10)}),a.page=a.qty_pages),i[t.tax_rate_id]=t,r[t.tax_rate_id]=t,n.set("rates",i),n.logChanges(r),a.render()},onDeleteRow:function(t){var e,a=t.data.view,n=a.model,i=_.indexBy(n.get("rates"),"tax_rate_id"),r={};t.preventDefault(),(t=c.children(".current"))?(t.each(function(){e=h(this).data("id"),delete i[e],r[e]=_.extend(r[e]||{},{deleted:"deleted"})}),n.set("rates",i),n.logChanges(r),a.render()):window.alert(p.strings.no_rows_selected)},onSearchField:function(t){t.data.view.updateUrl(),t.data.view.render()},onPageChange:function(t){var e=h(t.currentTarget);t.preventDefault(),t.data.view.page=e.data("goto")?e.data("goto"):e.val(),t.data.view.render(),t.data.view.updateUrl()},onExport:function(t){var n="data:application/csv;charset=utf-8,"+p.strings.csv_data_cols.join(",")+"\n";return h.each(t.data.view.model.getFilteredRates(),function(t,e){var a="";a+=e.tax_rate_country+",",a+=e.tax_rate_state+",",a+=(e.postcode?e.postcode.join("; "):"")+",",a+=(e.city?e.city.join("; "):"")+",",a+=e.tax_rate+",",a+=e.tax_rate_name+",",a+=e.tax_rate_priority+",",a+=e.tax_rate_compound+",",a+=e.tax_rate_shipping+",",a+=p.current_class,n+=a+"\n"}),h(this).attr("href",encodeURI(n)),!0},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,a.prop("disabled",!1)},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,a.prop("disabled",!0)},unloadConfirmation:function(t){if(t.data.view.needsUnloadConfirm)return t.returnValue=p.strings.unload_confirmation_msg,window.event.returnValue=p.strings.unload_confirmation_msg,p.strings.unload_confirmation_msg},updateModelOnChange:function(t){var e=t.data.view.model,a=h(t.target),n=a.closest("tr").data("id"),i=a.data("attribute"),t=a.val();"city"!==i&&"postcode"!==i||(t=t.split(";"),t=h.map(t,function(t){return t.trim()})),"tax_rate_compound"!==i&&"tax_rate_shipping"!==i||(t=a.is(":checked")?1:0),e.setRateAttribute(n,i,t)},sanitizePage:function(t){return(t=parseInt(t,10))<1?t=1:t>this.qty_pages&&(t=this.qty_pages),t}}),l=new r({rates:p.rates}),u=new t({model:l,el:"#rates"});u.render()})}(jQuery,htmlSettingsTaxLocalizeScript,wp,ajaxurl);