woocommerce/assets/js/admin/wc-shipping-zones.min.js

1 line
7.7 KiB
JavaScript

!function(a,b,c,d){a(function(){function e(){var a={formatMatches:function(a){return 1===a?wc_enhanced_select_params.i18n_matches_1:wc_enhanced_select_params.i18n_matches_n.replace("%qty%",a)},formatNoMatches:function(){return wc_enhanced_select_params.i18n_no_matches},formatAjaxError:function(){return wc_enhanced_select_params.i18n_ajax_error},formatInputTooShort:function(a,b){var c=b-a.length;return 1===c?wc_enhanced_select_params.i18n_input_too_short_1:wc_enhanced_select_params.i18n_input_too_short_n.replace("%qty%",c)},formatInputTooLong:function(a,b){var c=a.length-b;return 1===c?wc_enhanced_select_params.i18n_input_too_long_1:wc_enhanced_select_params.i18n_input_too_long_n.replace("%qty%",c)},formatSelectionTooBig:function(a){return 1===a?wc_enhanced_select_params.i18n_selection_too_long_1:wc_enhanced_select_params.i18n_selection_too_long_n.replace("%qty%",a)},formatLoadMore:function(){return wc_enhanced_select_params.i18n_load_more},formatSearching:function(){return wc_enhanced_select_params.i18n_searching}};return a}var f=a(".wc-shipping-zones"),g=a(".wc-shipping-zone-rows"),h=a(".wc-shipping-zone-save"),i=c.template("wc-shipping-zone-row"),j=c.template("wc-shipping-zone-row-blank"),k=a.extend({minimumResultsForSearch:10,allowClear:!!a(this).data("allow_clear"),placeholder:a(this).data("placeholder"),matcher:function(a,b,c){return b.toUpperCase().indexOf(a.toUpperCase())>=0||c.attr("alt").toUpperCase().indexOf(a.toUpperCase())>=0}},e()),l=Backbone.Model.extend({changes:{},logChanges:function(a){var b=this.changes||{};_.each(a,function(a,c){b[c]=_.extend(b[c]||{zone_id:c},a)}),this.changes=b,this.trigger("change:zones")},save:function(){_.size(this.changes)?a.post(d+"?action=woocommerce_shipping_zones_save_changes",{wc_shipping_zones_nonce:b.wc_shipping_zones_nonce,changes:this.changes},this.onSaveResponse,"json"):n.trigger("saved:zones")},onSaveResponse:function(a,c){"success"===c&&(a.success?(n.set("zones",a.data.zones),n.trigger("change:zones"),n.changes={},n.trigger("saved:zones")):window.alert(b.strings.save_failed))}}),m=Backbone.View.extend({rowTemplate:i,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),g.on("change",{view:this},this.updateModelOnChange),g.on("sortupdate",{view:this},this.updateModelOnSort),a(window).on("beforeunload",{view:this},this.unloadConfirmation),h.on("click",{view:this},this.onSubmit),a(document.body).on("click",".add_shipping_method",{view:this},this.onAddShippingMethod),a(document.body).on("click",".wc-shipping-zone-add",{view:this},this.onAddNewRow),a(document.body).on("click",".wc-shipping-zone-save-changes",{view:this},this.onSubmit),a(document.body).on("wc_backbone_modal_response",this.onAddShippingMethodSubmitted)},block:function(){a(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){a(this.el).unblock()},render:function(){var b=_.indexBy(this.model.get("zones"),"zone_id"),c=this;this.$el.empty(),this.unblock(),_.size(b)?(b=_.sortBy(b,function(a){return parseInt(a.zone_order,10)}),a.each(b,function(a,b){c.$el.append(c.rowTemplate(b));var d=c.$el.find('tr[data-id="'+b.zone_id+'"]');b.editing&&d.addClass("editing"),_.each(b.zone_locations,function(a){if("postcode"===a.type){var b=d.find(".wc-shipping-zone-postcodes :input");b.val()?b.val(b.val()+"\n"+a.code):b.val(a.code),d.find(".wc-shipping-zone-postcodes").show(),d.find(".wc-shipping-zone-postcodes-toggle").hide()}else d.find('option[value="'+a.type+":"+a.code+'"]').prop("selected",!0)}),c.renderShippingMethods(b.zone_id,b.shipping_methods)}),this.$el.find(".view").show(),this.$el.find(".edit").hide(),this.$el.find(".wc-shipping-zone-edit").on("click",{view:this},this.onEditRow),this.$el.find(".wc-shipping-zone-delete").on("click",{view:this},this.onDeleteRow),this.$el.find(".wc-shipping-zone-postcodes-toggle").on("click",{view:this},this.onTogglePostcodes),this.$el.find(".editing .wc-shipping-zone-edit").trigger("click"),0===_.size(b)%2?f.find("tbody.wc-shipping-zone-rows").next("tbody").find("tr").addClass("odd"):f.find("tbody.wc-shipping-zone-rows").next("tbody").find("tr").removeClass("odd")):c.$el.append(j),this.initTooltips()},renderShippingMethods:function(c,d){var e=a('.wc-shipping-zones tr[data-id="'+c+'"]'),f=e.find(".wc-shipping-zone-methods ul");f.empty(),_.size(d)?(_.each(d,function(a,b){var c="method_disabled";"yes"===a.enabled&&(c="method_enabled"),f.append('<li><a href="admin.php?page=wc-settings&amp;tab=shipping&amp;instance_id='+b+'" class="'+c+'">'+a.title+"</a></li>")}),f.append("<li>"+b.strings.add_another_method+"</li>")):f.append("<li>"+b.strings.no_methods+"</li>")},initTooltips:function(){a("#tiptip_holder").removeAttr("style"),a("#tiptip_arrow").removeAttr("style"),a(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:50})},onSubmit:function(a){a.data.view.block(),a.data.view.model.save(),a.preventDefault()},onAddNewRow:function(a){a.preventDefault();var c=a.data.view,d=c.model,e=_.indexBy(d.get("zones"),"zone_id"),f={},g=_.size(e),h=_.extend({},b.default_zone,{zone_id:"new-"+g+"-"+Date.now(),editing:!0});h.zone_order=1+_.max(_.pluck(e,"zone_order"),function(a){return parseInt(a,10)}),e[h.zone_id]=h,f[h.zone_id]=h,d.set("zones",e),d.logChanges(f),c.render()},onTogglePostcodes:function(b){b.preventDefault();var c=a(this).closest("tr");c.find(".wc-shipping-zone-postcodes").show(),c.find(".wc-shipping-zone-postcodes-toggle").hide()},onEditRow:function(b){b.preventDefault(),a(this).closest("tr").addClass("editing"),a(this).closest("tr").find(".view").hide(),a(this).closest("tr").find(".edit").show(),a(".wc-shipping-zone-region-select:not(.enhanced)").select2(k),a(".wc-shipping-zone-region-select:not(.enhanced)").addClass("enhanced"),b.data.view.model.trigger("change:zones")},onDeleteRow:function(b){var c=b.data.view,d=c.model,e=_.indexBy(d.get("zones"),"zone_id"),f={},g=a(this).closest("tr").data("id");b.preventDefault(),delete e[g],f[g]=_.extend(f[g]||{},{deleted:"deleted"}),d.set("zones",e),d.logChanges(f),c.render()},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,h.removeAttr("disabled")},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,h.attr("disabled","disabled")},unloadConfirmation:function(a){return a.data.view.needsUnloadConfirm?(a.returnValue=b.strings.unload_confirmation_msg,window.event.returnValue=b.strings.unload_confirmation_msg,b.strings.unload_confirmation_msg):void 0},updateModelOnChange:function(b){var c=b.data.view.model,d=a(b.target),e=d.closest("tr").data("id"),f=d.data("attribute"),g=d.val(),h=_.indexBy(c.get("zones"),"zone_id"),i={};h[e][f]!==g&&(i[e]={},i[e][f]=g,h[e][f]=g),c.logChanges(i)},updateModelOnSort:function(a){var b=a.data.view,c=b.model,d=_.indexBy(c.get("zones"),"zone_id"),e={};_.each(d,function(a){var b=parseInt(a.zone_order,10),c=parseInt(f.find('tr[data-id="'+a.zone_id+'"]').index(),10);b!==c&&(e[a.zone_id]=_.extend(e[a.zone_id]||{},{zone_order:c}))}),_.size(e)&&c.logChanges(e)},onAddShippingMethod:function(b){var c=a(this).closest("tr").data("id");b.preventDefault(),a(this).WCBackboneModal({template:"wc-modal-add-shipping-method",variable:{zone_id:c}})},onAddShippingMethodSubmitted:function(c,e,f){"wc-modal-add-shipping-method"===e&&(o.block(),a.post(d+"?action=woocommerce_shipping_zone_add_method",{wc_shipping_zones_nonce:b.wc_shipping_zones_nonce,method_id:f.add_method_id,zone_id:f.zone_id},function(a,b){"success"===b&&a.success&&o.renderShippingMethods(f.zone_id,a.data.methods),o.unblock()},"json"))}}),n=new l({zones:b.zones}),o=new m({model:n,el:g});o.render(),g.sortable({items:"tr",cursor:"move",axis:"y",handle:"td.wc-shipping-zone-sort",scrollSensitivity:40})})}(jQuery,shippingZonesLocalizeScript,wp,ajaxurl);