1 line
8.4 KiB
JavaScript
1 line
8.4 KiB
JavaScript
!function(e,n,o,t){e(function(){var i=e(".wc-shipping-zone-methods"),s=e(".wc-shipping-zone-method-rows"),d=e(".wc-shipping-zone-method-save"),a=o.template("wc-shipping-zone-method-row"),h=o.template("wc-shipping-zone-method-row-blank"),c=Backbone.Model.extend({changes:{},logChanges:function(e){var n=this.changes||{};_.each(e.methods,function(e,o){n.methods=n.methods||{methods:{}},n.methods[o]=_.extend(n.methods[o]||{instance_id:o},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(t+(t.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,o){"success"===o&&(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)),l.set("methods",e.data.methods),l.trigger("change:methods"),l.changes={},l.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 o=n.data.view,t=o.model,i=e(this).val(),s=e(n.target).data("attribute"),d={};n.preventDefault(),d[s]=i,t.set(s,i),t.logChanges(d),o.render()},block:function(){e(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){e(this.el).unblock()},render:function(){var o=_.indexBy(this.model.get("methods"),"instance_id"),t=this.model.get("zone_name"),i=this;e(".wc-shipping-zone-name").text(t||n.strings.default_zone_name),this.$el.empty(),this.unblock(),_.size(o)?(o=_.sortBy(o,function(e){return parseInt(e.method_order,10)}),e.each(o,function(e,o){"yes"===o.enabled?o.enabled_icon='<span class="woocommerce-input-toggle woocommerce-input-toggle--enabled">'+n.strings.yes+"</span>":o.enabled_icon='<span class="woocommerce-input-toggle woocommerce-input-toggle--disabled">'+n.strings.no+"</span>",i.$el.append(i.rowTemplate(o));var t=i.$el.find('tr[data-id="'+o.instance_id+'"]');o.has_settings||(t.find(".wc-shipping-zone-method-title a").replaceWith(t.find(".wc-shipping-zone-method-title").text()),t.find(".wc-shipping-zone-method-settings").remove())}),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 o=n.data.view,t=o.model,i=_.indexBy(t.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"}),t.set("methods",i),t.logChanges(s),o.render()},onToggleEnabled:function(n){var o=n.data.view,t=e(n.target),i=o.model,s=_.indexBy(i.get("methods"),"instance_id"),d=t.closest("tr").data("id"),a="yes"===t.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),o.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 o=n.data.view.model,t=e(n.target),i=t.closest("tr").data("id"),s=t.data("attribute"),d=t.val(),a=_.indexBy(o.get("methods"),"instance_id"),h={};a[i][s]!==d&&(h.methods[i]={},h.methods[i][s]=d,a[i][s]=d),o.logChanges(h)},updateModelOnSort:function(e){var n=e.data.view.model,o=_.indexBy(n.get("methods"),"instance_id"),t={};_.each(o,function(e){var n=parseInt(e.method_order,10),s=parseInt(i.find('tr[data-id="'+e.instance_id+'"]').index()+1,10);n!==s&&(o[e.instance_id].method_order=s,t.methods=t.methods||{methods:{}},t.methods[e.instance_id]=_.extend(t.methods[e.instance_id]||{},{method_order:s}))}),_.size(t)&&n.logChanges(t)},onConfigureShippingMethod:function(n){var o=e(this).closest("tr").data("id"),t=n.data.view.model,i=_.indexBy(t.get("methods"),"instance_id")[o];if(!i.settings_html)return!0;n.preventDefault(),e(this).WCBackboneModal({template:"wc-modal-shipping-method-settings",variable:{instance_id:o,method:i},data:{instance_id:o,method:i}}),e(document.body).trigger("init_tooltips")},onConfigureShippingMethodSubmitted:function(o,i,s){"wc-modal-shipping-method-settings"===i&&(p.block(),e.post(t+(t.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(o,t){"success"===t&&o.success?(e("table.wc-shipping-zone-methods").parent().find("#woocommerce_errors").remove(),o.data.errors.length>0&&this.showErrors(o.data.errors),_.size(p.model.changes)?p.model.save():p.model.onSaveResponse(o,t)):(window.alert(n.strings.save_failed),p.unblock())},"json"))},showErrors:function(n){var o='<div id="woocommerce_errors" class="error notice is-dismissible">';e(n).each(function(e,n){o=o+"<p>"+n+"</p>"}),o+="</div>",e("table.wc-shipping-zone-methods").before(o)},onAddShippingMethod:function(o){o.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(o,i,s){"wc-modal-add-shipping-method"===i&&(p.block(),e.post(t+(t.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,o){"success"===o&&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(p.model.changes)?p.model.save():(p.model.set("methods",e.data.methods),p.model.trigger("change:methods"),p.model.changes={},p.model.trigger("saved:methods"))),p.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 o=e(this).closest("tr");o.find(".wc-shipping-zone-postcodes").show(),o.find(".wc-shipping-zone-postcodes-toggle").hide()}}),l=new c({methods:n.methods,zone_name:n.zone_name}),p=new r({model:l,el:s});p.render(),s.sortable({items:"tr",cursor:"move",axis:"y",handle:"td.wc-shipping-zone-method-sort",scrollSensitivity:40})})}(jQuery,shippingZoneMethodsLocalizeScript,wp,ajaxurl); |