관리-도구
편집 파일: kkart-shipping-classes.min.js
!function(d,c,l,r){d(function(){var e=d(".kkart-shipping-class-rows"),i=d(".kkart-shipping-class-save"),s=l.template("kkart-shipping-class-row"),n=l.template("kkart-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)?d.post(r+(0<r.indexOf("?")?"&":"?")+"action=kkart_shipping_classes_save_changes",{kkart_shipping_classes_nonce:c.kkart_shipping_classes_nonce,changes:this.changes},this.onSaveResponse,"json"):a.trigger("saved:classes")},discardChanges:function(e){delete(this.changes||{})[e],0===_.size(this.changes)&&o.clearUnloadConfirmation()},onSaveResponse:function(e,i){"success"===i&&(e.success?(a.set("classes",e.data.shipping_classes),a.trigger("change:classes"),a.changes={},a.trigger("saved:classes")):e.data?window.alert(e.data):window.alert(c.strings.save_failed)),o.unblock()}}),s=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),d(window).on("beforeunload",{view:this},this.unloadConfirmation),i.on("click",{view:this},this.onSubmit),d(document.body).on("click",".kkart-shipping-class-add",{view:this},this.onAddNewRow),d(document.body).on("click",".kkart-shipping-class-save-changes",{view:this},this.onSubmit)},block:function(){d(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){d(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}),d.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=d(this).data("attribute");d(this).find('option[value="'+i[e]+'"]').prop("selected",!0)}),e.find(".view").show(),e.find(".edit").hide(),e.find(".kkart-shipping-class-edit").on("click",{view:this},this.onEditRow),e.find(".kkart-shipping-class-delete").on("click",{view:this},this.onDeleteRow),e.find(".editing .kkart-shipping-class-edit").trigger("click"),e.find(".kkart-shipping-class-cancel-edit").on("click",{view:this},this.onCancelEditRow),!0===i.editing&&(e.addClass("editing"),e.find(".kkart-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"),e={},n=_.size(n),n=_.extend({},c.default_shipping_class,{term_id:"new-"+n+"-"+Date.now(),editing:!0,newRow:!0});e[n.term_id]=n,s.logChanges(e),i.renderRow(n),d(".kkart-shipping-classes-blank-state").remove()},onEditRow:function(e){e.preventDefault(),d(this).closest("tr").addClass("editing"),d(this).closest("tr").find(".view").hide(),d(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=d(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=d(this).closest("tr"),t=d(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=c.strings.unload_confirmation_msg,window.event.returnValue=c.strings.unload_confirmation_msg,c.strings.unload_confirmation_msg},updateModelOnChange:function(e){var i=e.data.view.model,s=d(e.target),n=s.closest("tr").data("id"),t=s.data("attribute"),a=s.val(),e=_.indexBy(i.get("classes"),"term_id"),s={};e[n]&&e[n][t]===a||(s[n]={},s[n][t]=a),i.logChanges(s)}}),a=new t({classes:c.classes}),o=new s({model:a,el:e});o.render()})}(jQuery,shippingClassesLocalizeScript,wp,ajaxurl);