woocommerce/assets/js/admin/wc-shipping-zone-methods.mi...

1 line
8.5 KiB
JavaScript

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