관리-도구
편집 파일: kkart-shipping-zones.min.js
!function(r,l,c,h){r(function(){var e=r(".kkart-shipping-zones"),n=r(".kkart-shipping-zone-rows"),i=r(".kkart-shipping-zone-save"),o=c.template("kkart-shipping-zone-row"),t=c.template("kkart-shipping-zone-row-blank"),s=Backbone.Model.extend({changes:{},logChanges:function(e){var i=this.changes||{};_.each(e,function(e,n){i[n]=_.extend(i[n]||{zone_id:n},e)}),this.changes=i,this.trigger("change:zones")},discardChanges:function(e){var n=this.changes||{},i=null,o=_.indexBy(this.get("zones"),"zone_id");n[e]&&n[e].zone_order!==undefined&&(i=n[e].zone_order),delete n[e],null!==i&&o[e]&&o[e].zone_order!==i&&(n[e]=_.extend(n[e]||{},{zone_id:e,zone_order:i})),this.changes=n,0===_.size(this.changes)&&a.clearUnloadConfirmation()},save:function(){_.size(this.changes)?r.post(h+(0<h.indexOf("?")?"&":"?")+"action=kkart_shipping_zones_save_changes",{kkart_shipping_zones_nonce:l.kkart_shipping_zones_nonce,changes:this.changes},this.onSaveResponse,"json"):d.trigger("saved:zones")},onSaveResponse:function(e,n){"success"===n&&(e.success?(d.set("zones",e.data.zones),d.trigger("change:zones"),d.changes={},d.trigger("saved:zones")):window.alert(l.strings.save_failed))}}),o=Backbone.View.extend({rowTemplate:o,initialize:function(){this.listenTo(this.model,"change:zones",this.setUnloadConfirmation),this.listenTo(this.model,"saved:zones",this.clearUnloadConfirmation),this.listenTo(this.model,"saved:zones",this.render),n.on("change",{view:this},this.updateModelOnChange),n.on("sortupdate",{view:this},this.updateModelOnSort),r(window).on("beforeunload",{view:this},this.unloadConfirmation),r(document.body).on("click",".kkart-shipping-zone-add",{view:this},this.onAddNewRow)},block:function(){r(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){r(this.el).unblock()},render:function(){var e=_.indexBy(this.model.get("zones"),"zone_id"),i=this;i.$el.empty(),i.unblock(),_.size(e)?(e=_(e).chain().sortBy(function(e){return parseInt(e.zone_id,10)}).sortBy(function(e){return parseInt(e.zone_order,10)}).value(),r.each(e,function(e,n){i.renderRow(n)})):i.$el.append(t),i.initRows()},renderRow:function(e){var n=this;n.$el.append(n.rowTemplate(e)),n.initRow(e)},initRow:function(e){var n=this.$el.find('tr[data-id="'+e.zone_id+'"]');this.renderShippingMethods(e.zone_id,e.shipping_methods),n.find(".kkart-shipping-zone-delete").on("click",{view:this},this.onDeleteRow)},initRows:function(){0==r("tbody.kkart-shipping-zone-rows tr").length%2?e.find("tbody.kkart-shipping-zone-rows").next("tbody").find("tr").addClass("odd"):e.find("tbody.kkart-shipping-zone-rows").next("tbody").find("tr").removeClass("odd"),r("#tiptip_holder").removeAttr("style"),r("#tiptip_arrow").removeAttr("style"),r(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:50})},renderShippingMethods:function(e,n){var i=r('.kkart-shipping-zones tr[data-id="'+e+'"]').find(".kkart-shipping-zone-methods ul");i.find(".kkart-shipping-zone-method").remove(),_.size(n)?(n=_.sortBy(n,function(e){return parseInt(e.method_order,10)}),_.each(n,function(e){var n="method_disabled";"yes"===e.enabled&&(n="method_enabled"),i.append('<li class="kkart-shipping-zone-method '+n+'">'+e.title+"</li>")})):i.append('<li class="kkart-shipping-zone-method">'+l.strings.no_shipping_methods_offered+"</li>")},onDeleteRow:function(e){var n=e.data.view.model,i=_.indexBy(n.get("zones"),"zone_id"),o={},t=r(this).closest("tr").data("id");e.preventDefault(),window.confirm(l.strings.delete_confirmation_msg)&&i[t]&&(delete i[t],o[t]=_.extend(o[t]||{},{deleted:"deleted"}),n.set("zones",i),n.logChanges(o),e.data.view.block(),e.data.view.model.save())},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,i.prop("disabled",!1)},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,i.prop("disabled",!0)},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 n=e.data.view.model,i=r(e.target),o=i.closest("tr").data("id"),t=i.data("attribute"),s=i.val(),e=_.indexBy(n.get("zones"),"zone_id"),i={};e[o]&&e[o][t]===s||(i[o]={},i[o][t]=s),n.logChanges(i)},updateModelOnSort:function(e){var n=e.data.view.model,o=_.indexBy(n.get("zones"),"zone_id"),i=r("tbody.kkart-shipping-zone-rows tr"),t={};_.each(i,function(e){var n=r(e).data("id"),i=null,e=parseInt(r(e).index(),10);o[n]&&(i=parseInt(o[n].zone_order,10)),i!==e&&(t[n]=_.extend(t[n]||{},{zone_order:e}))}),_.size(t)&&(n.logChanges(t),e.data.view.block(),e.data.view.model.save())}}),d=new s({zones:l.zones}),a=new o({model:d,el:n});a.render(),n.sortable({items:"tr",cursor:"move",axis:"y",handle:"td.kkart-shipping-zone-sort",scrollSensitivity:40})})}(jQuery,shippingZonesLocalizeScript,wp,ajaxurl);