From 25138f4ce3218364c750785d841c44c5fa06834d Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 19 Apr 2016 18:26:57 +0100 Subject: [PATCH] Preserve values on add row --- assets/js/admin/wc-shipping-zones.js | 140 +++++++++++++---------- assets/js/admin/wc-shipping-zones.min.js | 2 +- 2 files changed, 83 insertions(+), 59 deletions(-) diff --git a/assets/js/admin/wc-shipping-zones.js b/assets/js/admin/wc-shipping-zones.js index e25a46f2e3e..20826b04c4d 100644 --- a/assets/js/admin/wc-shipping-zones.js +++ b/assets/js/admin/wc-shipping-zones.js @@ -82,11 +82,11 @@ $( this.el ).unblock(); }, render: function() { - var zones = _.indexBy( this.model.get( 'zones' ), 'zone_id' ), - view = this; + var zones = _.indexBy( this.model.get( 'zones' ), 'zone_id' ), + view = this; - this.$el.empty(); - this.unblock(); + view.$el.empty(); + view.unblock(); if ( _.size( zones ) ) { // Sort zones @@ -96,56 +96,86 @@ // Populate $tbody with the current zones $.each( zones, function( id, rowData ) { - view.$el.append( view.rowTemplate( rowData ) ); - - var $tr = view.$el.find( 'tr[data-id="' + rowData.zone_id + '"]'); - - // Editing? - if ( rowData.editing ) { - $tr.addClass( 'editing' ); - } - - // Select values in region select - _.each( rowData.zone_locations, function( location ) { - if ( 'postcode' === location.type ) { - var postcode_field = $tr.find( '.wc-shipping-zone-postcodes :input' ); - - if ( postcode_field.val() ) { - postcode_field.val( postcode_field.val() + '\n' + location.code ); - } else { - postcode_field.val( location.code ); - } - $tr.find( '.wc-shipping-zone-postcodes' ).show(); - $tr.find( '.wc-shipping-zone-postcodes-toggle' ).hide(); - } else { - $tr.find( 'option[value="' + location.type + ':' + location.code + '"]' ).prop( 'selected', true ); - } - } ); - - // List shipping methods - view.renderShippingMethods( rowData.zone_id, rowData.shipping_methods ); + view.renderRow( rowData ); } ); - // Make the rows function - this.$el.find('.view').show(); - this.$el.find('.edit').hide(); - this.$el.find('.wc-shipping-zone-save-changes-notice').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'); - - // Stripe - if ( 0 === _.size( zones ) % 2) { - $table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).addClass( 'odd' ); - } else { - $table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).removeClass( 'odd' ); - } } else { view.$el.append( $blank_template ); } - this.initTooltips(); + view.initRows( zones ); + }, + renderRow: function( rowData ) { + var view = this; + + view.$el.append( view.rowTemplate( rowData ) ); + + var $tr = view.$el.find( 'tr[data-id="' + rowData.zone_id + '"]'); + + // Editing? + if ( rowData.editing ) { + $tr.addClass( 'editing' ); + } + + // Select values in region select + _.each( rowData.zone_locations, function( location ) { + if ( 'string' === jQuery.type( location ) ) { + $tr.find( 'option[value="' + location + '"]' ).prop( 'selected', true ); + } else { + if ( 'postcode' === location.type ) { + var postcode_field = $tr.find( '.wc-shipping-zone-postcodes :input' ); + + if ( postcode_field.val() ) { + postcode_field.val( postcode_field.val() + '\n' + location.code ); + } else { + postcode_field.val( location.code ); + } + $tr.find( '.wc-shipping-zone-postcodes' ).show(); + $tr.find( '.wc-shipping-zone-postcodes-toggle' ).hide(); + } else { + $tr.find( 'option[value="' + location.type + ':' + location.code + '"]' ).prop( 'selected', true ); + } + } + } ); + + if ( rowData.zone_postcodes ) { + _.each( rowData.zone_postcodes, function( location ) { + var postcode_field = $tr.find( '.wc-shipping-zone-postcodes :input' ); + + if ( postcode_field.val() ) { + postcode_field.val( postcode_field.val() + '\n' + location.code ); + } else { + postcode_field.val( location.code ); + } + $tr.find( '.wc-shipping-zone-postcodes' ).show(); + $tr.find( '.wc-shipping-zone-postcodes-toggle' ).hide(); + } ); + } + + // List shipping methods + view.renderShippingMethods( rowData.zone_id, rowData.shipping_methods ); + }, + initRows: function( zones ) { + // Make the rows function + $table.find( '.view' ).show(); + $table.find( '.edit' ).hide(); + $table.find( '.wc-shipping-zone-save-changes-notice' ).hide(); + $table.find( '.wc-shipping-zone-edit' ).on( 'click', { view: this }, this.onEditRow ); + $table.find( '.wc-shipping-zone-delete' ).on( 'click', { view: this }, this.onDeleteRow ); + $table.find( '.wc-shipping-zone-postcodes-toggle' ).on( 'click', { view: this }, this.onTogglePostcodes ); + $table.find( '.editing .wc-shipping-zone-edit' ).trigger( 'click' ); + + // Stripe + if ( 0 === _.size( zones ) % 2 ) { + $table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).addClass( 'odd' ); + } else { + $table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).removeClass( 'odd' ); + } + + // Tooltips + $( '#tiptip_holder' ).removeAttr( 'style' ); + $( '#tiptip_arrow' ).removeAttr( 'style' ); + $( '.tips' ).tipTip({ 'attribute': 'data-tip', 'fadeIn': 50, 'fadeOut': 50, 'delay': 50 }); }, renderShippingMethods: function( zone_id, shipping_methods ) { var $tr = $( '.wc-shipping-zones tr[data-id="' + zone_id + '"]'); @@ -166,11 +196,6 @@ } }, - initTooltips: function() { - $( '#tiptip_holder' ).removeAttr( 'style' ); - $( '#tiptip_arrow' ).removeAttr( 'style' ); - $( '.tips' ).tipTip({ 'attribute': 'data-tip', 'fadeIn': 50, 'fadeOut': 50, 'delay': 50 }); - }, onSubmit: function( event ) { event.data.view.block(); event.data.view.model.save(); @@ -202,8 +227,8 @@ model.set( 'zones', zones ); model.logChanges( changes ); - - view.render(); + view.renderRow( newRow ); + view.initRows( zones ); }, onTogglePostcodes: function( event ) { event.preventDefault(); @@ -213,14 +238,13 @@ }, onEditRow: function( event ) { event.preventDefault(); - $( this ).closest('tr').addClass('editing'); + event.data.view.model.trigger( 'change:zones' ); + $( this ).closest('tr').addClass( 'editing' ); $( this ).closest('tr').find('.view').hide(); $( this ).closest('tr').find('.edit').show(); $( '.wc-shipping-zone-region-select:not(.enhanced)' ).select2( select2_args ); $( '.wc-shipping-zone-region-select:not(.enhanced)' ).addClass('enhanced'); $( this ).closest('tr').find('.add_shipping_method').attr( 'disabled', 'disabled' ).addClass( 'tips' ); - event.data.view.initTooltips(); - event.data.view.model.trigger( 'change:zones' ); }, onDeleteRow: function( event ) { var view = event.data.view, diff --git a/assets/js/admin/wc-shipping-zones.min.js b/assets/js/admin/wc-shipping-zones.min.js index 90dc8bc56cb..e6381ecf8c3 100644 --- a/assets/js/admin/wc-shipping-zones.min.js +++ b/assets/js/admin/wc-shipping-zones.min.js @@ -1 +1 @@ -!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+(d.indexOf("?")>0?"&":"?")+"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),a(document.body).on("change",".wc-shipping-zone-method-selector select",this.onChangeShippingMethodSelector)},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-save-changes-notice").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(b,c){var d=a('.wc-shipping-zones tr[data-id="'+b+'"]'),e=d.find(".wc-shipping-zone-methods ul");e.find(".wc-shipping-zone-method").remove(),_.size(c)&&_.each(c,function(a,b){var c="method_disabled";"yes"===a.enabled&&(c="method_enabled"),e.prepend('
  • '+a.title+"
  • ")})},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"),a(this).closest("tr").find(".add_shipping_method").attr("disabled","disabled").addClass("tips"),b.data.view.initTooltips(),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}}),a(".wc-shipping-zone-method-selector select").change()},onAddShippingMethodSubmitted:function(c,e,f){"wc-modal-add-shipping-method"===e&&(o.block(),a.post(d+(d.indexOf("?")>0?"&":"?")+"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"))},onChangeShippingMethodSelector:function(){var b=a(this).find("option:selected").data("description");a(this).parent().find(".wc-shipping-zone-method-description").remove(),a(this).after('

    '+b+"

    "),a(this).closest("article").height(a(this).parent().height())}}),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); \ No newline at end of file +!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+(d.indexOf("?")>0?"&":"?")+"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),a(document.body).on("change",".wc-shipping-zone-method-selector select",this.onChangeShippingMethodSelector)},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;c.$el.empty(),c.unblock(),_.size(b)?(b=_.sortBy(b,function(a){return parseInt(a.zone_order,10)}),a.each(b,function(a,b){c.renderRow(b)})):c.$el.append(j),c.initRows(b)},renderRow:function(a){var b=this;b.$el.append(b.rowTemplate(a));var c=b.$el.find('tr[data-id="'+a.zone_id+'"]');a.editing&&c.addClass("editing"),_.each(a.zone_locations,function(a){if("string"===jQuery.type(a))c.find('option[value="'+a+'"]').prop("selected",!0);else if("postcode"===a.type){var b=c.find(".wc-shipping-zone-postcodes :input");b.val()?b.val(b.val()+"\n"+a.code):b.val(a.code),c.find(".wc-shipping-zone-postcodes").show(),c.find(".wc-shipping-zone-postcodes-toggle").hide()}else c.find('option[value="'+a.type+":"+a.code+'"]').prop("selected",!0)}),a.zone_postcodes&&_.each(a.zone_postcodes,function(a){var b=c.find(".wc-shipping-zone-postcodes :input");b.val()?b.val(b.val()+"\n"+a.code):b.val(a.code),c.find(".wc-shipping-zone-postcodes").show(),c.find(".wc-shipping-zone-postcodes-toggle").hide()}),b.renderShippingMethods(a.zone_id,a.shipping_methods)},initRows:function(b){f.find(".view").show(),f.find(".edit").hide(),f.find(".wc-shipping-zone-save-changes-notice").hide(),f.find(".wc-shipping-zone-edit").on("click",{view:this},this.onEditRow),f.find(".wc-shipping-zone-delete").on("click",{view:this},this.onDeleteRow),f.find(".wc-shipping-zone-postcodes-toggle").on("click",{view:this},this.onTogglePostcodes),f.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"),a("#tiptip_holder").removeAttr("style"),a("#tiptip_arrow").removeAttr("style"),a(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:50})},renderShippingMethods:function(b,c){var d=a('.wc-shipping-zones tr[data-id="'+b+'"]'),e=d.find(".wc-shipping-zone-methods ul");e.find(".wc-shipping-zone-method").remove(),_.size(c)&&_.each(c,function(a,b){var c="method_disabled";"yes"===a.enabled&&(c="method_enabled"),e.prepend('
  • '+a.title+"
  • ")})},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.renderRow(h),c.initRows(e)},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(),b.data.view.model.trigger("change:zones"),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"),a(this).closest("tr").find(".add_shipping_method").attr("disabled","disabled").addClass("tips")},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}}),a(".wc-shipping-zone-method-selector select").change()},onAddShippingMethodSubmitted:function(c,e,f){"wc-modal-add-shipping-method"===e&&(o.block(),a.post(d+(d.indexOf("?")>0?"&":"?")+"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"))},onChangeShippingMethodSelector:function(){var b=a(this).find("option:selected").data("description");a(this).parent().find(".wc-shipping-zone-method-description").remove(),a(this).after('

    '+b+"

    "),a(this).closest("article").height(a(this).parent().height())}}),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); \ No newline at end of file