관리-도구
편집 파일: kkart-shipping-zone-methods.min.js
!function(h,c,r,p){h(function(){var s=h(".kkart-shipping-zone-methods"),e=h(".kkart-shipping-zone-method-rows"),n=h(".kkart-shipping-zone-method-save"),t=r.template("kkart-shipping-zone-method-row"),i=r.template("kkart-shipping-zone-method-row-blank"),o=Backbone.Model.extend({changes:{},logChanges:function(e){var t=this.changes||{};_.each(e.methods,function(e,n){t.methods=t.methods||{methods:{}},t.methods[n]=_.extend(t.methods[n]||{instance_id:n},e)}),"undefined"!=typeof e.zone_name&&(t.zone_name=e.zone_name),"undefined"!=typeof e.zone_locations&&(t.zone_locations=e.zone_locations),"undefined"!=typeof e.zone_postcodes&&(t.zone_postcodes=e.zone_postcodes),this.changes=t,this.trigger("change:methods")},save:function(){h.post(p+(0<p.indexOf("?")?"&":"?")+"action=kkart_shipping_zone_methods_save_changes",{kkart_shipping_zones_nonce:c.kkart_shipping_zones_nonce,changes:this.changes,zone_id:c.zone_id},this.onSaveResponse,"json")},onSaveResponse:function(e,n){"success"===n&&(e.success?(e.data.zone_id!==c.zone_id&&(c.zone_id=e.data.zone_id,window.history.pushState&&window.history.pushState({},"","admin.php?page=kkart-settings&tab=shipping&zone_id="+e.data.zone_id)),d.set("methods",e.data.methods),d.trigger("change:methods"),d.changes={},d.trigger("saved:methods")):window.alert(c.strings.save_failed))}}),t=Backbone.View.extend({rowTemplate:t,initialize:function(){this.listenTo(this.model,"change:methods",this.setUnloadConfirmation),this.listenTo(this.model,"saved:methods",this.clearUnloadConfirmation),this.listenTo(this.model,"saved:methods",this.render),e.on("change",{view:this},this.updateModelOnChange),e.on("sortupdate",{view:this},this.updateModelOnSort),h(window).on("beforeunload",{view:this},this.unloadConfirmation),n.on("click",{view:this},this.onSubmit),h(document.body).on("input change","#zone_name, #zone_locations, #zone_postcodes",{view:this},this.onUpdateZone),h(document.body).on("click",".kkart-shipping-zone-method-settings",{view:this},this.onConfigureShippingMethod),h(document.body).on("click",".kkart-shipping-zone-add-method",{view:this},this.onAddShippingMethod),h(document.body).on("kkart_backbone_modal_response",this.onConfigureShippingMethodSubmitted),h(document.body).on("kkart_backbone_modal_response",this.onAddShippingMethodSubmitted),h(document.body).on("change",".kkart-shipping-zone-method-selector select",this.onChangeShippingMethodSelector),h(document.body).on("click",".kkart-shipping-zone-postcodes-toggle",this.onTogglePostcodes)},onUpdateZone:function(e){var n=e.data.view,t=n.model,o=h(this).val(),i=h(e.target).data("attribute"),s={};e.preventDefault(),s[i]=o,t.set(i,o),t.logChanges(s),n.render()},block:function(){h(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){h(this.el).unblock()},render:function(){var e=_.indexBy(this.model.get("methods"),"instance_id"),n=this.model.get("zone_name"),o=this;h(".kkart-shipping-zone-name").text(n||c.strings.default_zone_name),this.$el.empty(),this.unblock(),_.size(e)?(e=_.sortBy(e,function(e){return parseInt(e.method_order,10)}),h.each(e,function(e,n){"yes"===n.enabled?n.enabled_icon='<span class="kkart-input-toggle kkart-input-toggle--enabled">'+c.strings.yes+"</span>":n.enabled_icon='<span class="kkart-input-toggle kkart-input-toggle--disabled">'+c.strings.no+"</span>",o.$el.append(o.rowTemplate(n));var t=o.$el.find('tr[data-id="'+n.instance_id+'"]');n.has_settings||(t.find(".kkart-shipping-zone-method-title > a").replaceWith("<span>"+t.find(".kkart-shipping-zone-method-title > a").text()+"</span>"),n=t.find(".kkart-shipping-zone-method-delete"),t.find(".kkart-shipping-zone-method-title .row-actions").empty().html(n))}),this.$el.find(".kkart-shipping-zone-method-delete").on("click",{view:this},this.onDeleteRow),this.$el.find(".kkart-shipping-zone-method-enabled a").on("click",{view:this},this.onToggleEnabled)):o.$el.append(i),this.initTooltips()},initTooltips:function(){h("#tiptip_holder").removeAttr("style"),h("#tiptip_arrow").removeAttr("style"),h(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:50})},onSubmit:function(e){e.data.view.block(),e.data.view.model.save(),e.preventDefault()},onDeleteRow:function(e){var n=e.data.view,t=n.model,o=_.indexBy(t.get("methods"),"instance_id"),i={},s=h(this).closest("tr").data("id");e.preventDefault(),delete o[s],i.methods=i.methods||{methods:{}},i.methods[s]=_.extend(i.methods[s]||{},{deleted:"deleted"}),t.set("methods",o),t.logChanges(i),n.render()},onToggleEnabled:function(e){var n=e.data.view,t=h(e.target),o=n.model,i=_.indexBy(o.get("methods"),"instance_id"),s=t.closest("tr").data("id"),d="yes"===t.closest("tr").data("enabled")?"no":"yes",t={};e.preventDefault(),i[s].enabled=d,t.methods=t.methods||{methods:{}},t.methods[s]=_.extend(t.methods[s]||{},{enabled:d}),o.set("methods",i),o.logChanges(t),n.render()},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,n.removeAttr("disabled")},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,n.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 n=e.data.view.model,t=h(e.target),o=t.closest("tr").data("id"),i=t.data("attribute"),s=t.val(),e=_.indexBy(n.get("methods"),"instance_id"),t={};e[o][i]!==s&&(t.methods[o]={},t.methods[o][i]=s,e[o][i]=s),n.logChanges(t)},updateModelOnSort:function(e){var e=e.data.view.model,o=_.indexBy(e.get("methods"),"instance_id"),i={};_.each(o,function(e){var n=parseInt(e.method_order,10),t=parseInt(s.find('tr[data-id="'+e.instance_id+'"]').index()+1,10);n!==t&&(o[e.instance_id].method_order=t,i.methods=i.methods||{methods:{}},i.methods[e.instance_id]=_.extend(i.methods[e.instance_id]||{},{method_order:t}))}),_.size(i)&&e.logChanges(i)},onConfigureShippingMethod:function(e){var n=h(this).closest("tr").data("id"),t=e.data.view.model,t=_.indexBy(t.get("methods"),"instance_id")[n];if(!t.settings_html)return!0;e.preventDefault(),h(this).KKARTBackboneModal({template:"kkart-modal-shipping-method-settings",variable:{instance_id:n,method:t},data:{instance_id:n,method:t}}),h(document.body).trigger("init_tooltips")},onConfigureShippingMethodSubmitted:function(e,n,t){"kkart-modal-shipping-method-settings"===n&&(a.block(),h.post(p+(0<p.indexOf("?")?"&":"?")+"action=kkart_shipping_zone_methods_save_settings",{kkart_shipping_zones_nonce:c.kkart_shipping_zones_nonce,instance_id:t.instance_id,data:t},function(e,n){"success"===n&&e.success?(h("table.kkart-shipping-zone-methods").parent().find("#kkart_errors").remove(),0<e.data.errors.length&&a.showErrors(e.data.errors),_.size(a.model.changes)?a.model.save():a.model.onSaveResponse(e,n)):(window.alert(c.strings.save_failed),a.unblock())},"json"))},showErrors:function(e){var t='<div id="kkart_errors" class="error notice is-dismissible">';h(e).each(function(e,n){t=t+"<p>"+n+"</p>"}),t+="</div>",h("table.kkart-shipping-zone-methods").before(t)},onAddShippingMethod:function(e){e.preventDefault(),h(this).KKARTBackboneModal({template:"kkart-modal-add-shipping-method",variable:{zone_id:c.zone_id}}),h(".kkart-shipping-zone-method-selector select").change()},onAddShippingMethodSubmitted:function(e,n,t){"kkart-modal-add-shipping-method"===n&&(a.block(),h.post(p+(0<p.indexOf("?")?"&":"?")+"action=kkart_shipping_zone_add_method",{kkart_shipping_zones_nonce:c.kkart_shipping_zones_nonce,method_id:t.add_method_id,zone_id:c.zone_id},function(e,n){"success"===n&&e.success&&(e.data.zone_id!==c.zone_id&&(c.zone_id=e.data.zone_id,window.history.pushState&&window.history.pushState({},"","admin.php?page=kkart-settings&tab=shipping&zone_id="+e.data.zone_id)),_.size(a.model.changes)?a.model.save():(a.model.set("methods",e.data.methods),a.model.trigger("change:methods"),a.model.changes={},a.model.trigger("saved:methods"))),a.unblock()},"json"))},onChangeShippingMethodSelector:function(){var e=h(this).find("option:selected").data("description");h(this).parent().find(".kkart-shipping-zone-method-description").remove(),h(this).after('<div class="kkart-shipping-zone-method-description">'+e+"</div>"),h(this).closest("article").height(h(this).parent().height())},onTogglePostcodes:function(e){e.preventDefault();e=h(this).closest("tr");e.find(".kkart-shipping-zone-postcodes").show(),e.find(".kkart-shipping-zone-postcodes-toggle").hide()}}),d=new o({methods:c.methods,zone_name:c.zone_name}),a=new t({model:d,el:e});a.render(),e.sortable({items:"tr",cursor:"move",axis:"y",handle:"td.kkart-shipping-zone-method-sort",scrollSensitivity:40})})}(jQuery,shippingZoneMethodsLocalizeScript,wp,ajaxurl);