woocommerce/assets/js/admin/wc-shipping-classes.min.js

1 line
4.4 KiB
JavaScript

!function(c,l,r,h){c(function(){var e=c(".wc-shipping-class-rows"),i=c(".wc-shipping-class-save"),s=r.template("wc-shipping-class-row"),n=r.template("wc-shipping-class-row-blank"),t=Backbone.Model.extend({changes:{},logChanges:function(e){var s=this.changes||{};_.each(e,function(e,i){s[i]=_.extend(s[i]||{term_id:i},e)}),this.changes=s,this.trigger("change:classes")},save:function(){_.size(this.changes)?c.post(h+(0<h.indexOf("?")?"&":"?")+"action=woocommerce_shipping_classes_save_changes",{wc_shipping_classes_nonce:l.wc_shipping_classes_nonce,changes:this.changes},this.onSaveResponse,"json"):o.trigger("saved:classes")},discardChanges:function(e){delete(this.changes||{})[e],0===_.size(this.changes)&&d.clearUnloadConfirmation()},onSaveResponse:function(e,i){"success"===i&&(e.success?(o.set("classes",e.data.shipping_classes),o.trigger("change:classes"),o.changes={},o.trigger("saved:classes")):e.data?window.alert(e.data):window.alert(l.strings.save_failed)),d.unblock()}}),a=Backbone.View.extend({rowTemplate:s,initialize:function(){this.listenTo(this.model,"change:classes",this.setUnloadConfirmation),this.listenTo(this.model,"saved:classes",this.clearUnloadConfirmation),this.listenTo(this.model,"saved:classes",this.render),e.on("change",{view:this},this.updateModelOnChange),c(window).on("beforeunload",{view:this},this.unloadConfirmation),i.on("click",{view:this},this.onSubmit),c(document.body).on("click",".wc-shipping-class-add",{view:this},this.onAddNewRow),c(document.body).on("click",".wc-shipping-class-save-changes",{view:this},this.onSubmit)},block:function(){c(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){c(this.el).unblock()},render:function(){var e=_.indexBy(this.model.get("classes"),"term_id"),s=this;this.$el.empty(),this.unblock(),_.size(e)?(e=_.sortBy(e,function(e){return e.name}),c.each(e,function(e,i){s.renderRow(i)})):s.$el.append(n)},renderRow:function(e){var i=this;i.$el.append(i.rowTemplate(e)),i.initRow(e)},initRow:function(i){var e=this.$el.find('tr[data-id="'+i.term_id+'"]');e.find("select").each(function(){var e=c(this).data("attribute");c(this).find('option[value="'+i[e]+'"]').prop("selected",!0)}),e.find(".view").show(),e.find(".edit").hide(),e.find(".wc-shipping-class-edit").on("click",{view:this},this.onEditRow),e.find(".wc-shipping-class-delete").on("click",{view:this},this.onDeleteRow),e.find(".editing .wc-shipping-class-edit").trigger("click"),e.find(".wc-shipping-class-cancel-edit").on("click",{view:this},this.onCancelEditRow),!0===i.editing&&(e.addClass("editing"),e.find(".wc-shipping-class-edit").trigger("click"))},onSubmit:function(e){e.data.view.block(),e.data.view.model.save(),e.preventDefault()},onAddNewRow:function(e){e.preventDefault();var i=e.data.view,s=i.model,n=_.indexBy(s.get("classes"),"term_id"),t={},a=_.size(n),o=_.extend({},l.default_shipping_class,{term_id:"new-"+a+"-"+Date.now(),editing:!0,newRow:!0});t[o.term_id]=o,s.logChanges(t),i.renderRow(o),c(".wc-shipping-classes-blank-state").remove()},onEditRow:function(e){e.preventDefault(),c(this).closest("tr").addClass("editing"),c(this).closest("tr").find(".view").hide(),c(this).closest("tr").find(".edit").show(),e.data.view.model.trigger("change:classes")},onDeleteRow:function(e){var i=e.data.view,s=i.model,n=_.indexBy(s.get("classes"),"term_id"),t={},a=c(this).closest("tr").data("id");e.preventDefault(),n[a]&&(delete n[a],t[a]=_.extend(t[a]||{},{deleted:"deleted"}),s.set("classes",n),s.logChanges(t)),i.render()},onCancelEditRow:function(e){var i=e.data.view,s=i.model,n=c(this).closest("tr"),t=c(this).closest("tr").data("id"),a=_.indexBy(s.get("classes"),"term_id");e.preventDefault(),s.discardChanges(t),a[t]&&(a[t].editing=!1,n.after(i.rowTemplate(a[t])),i.initRow(a[t])),n.remove()},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,i.removeAttr("disabled")},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,i.attr("disabled","disabled")},unloadConfirmation:function(e){if(e.data.view.needsUnloadConfirm)return e.returnValue=l.strings.unload_confirmation_msg,window.event.returnValue=l.strings.unload_confirmation_msg,l.strings.unload_confirmation_msg},updateModelOnChange:function(e){var i=e.data.view.model,s=c(e.target),n=s.closest("tr").data("id"),t=s.data("attribute"),a=s.val(),o=_.indexBy(i.get("classes"),"term_id"),d={};o[n]&&o[n][t]===a||(d[n]={},d[n][t]=a),i.logChanges(d)}}),o=new t({classes:l.classes}),d=new a({model:o,el:e});d.render()})}(jQuery,shippingClassesLocalizeScript,wp,ajaxurl);