Further zone tweaks to delete new rows on cancel

This commit is contained in:
Mike Jolley 2016-04-22 12:24:18 +01:00
parent a9b8101e48
commit 10f16f074c
4 changed files with 74 additions and 51 deletions

View File

@ -29,21 +29,29 @@
this.trigger( 'change:zones' ); this.trigger( 'change:zones' );
}, },
discardChanges: function( id ) { discardChanges: function( id ) {
var changes = this.changes || {}, var changes = this.changes || {},
position = null; set_position = null,
zones = _.indexBy( this.get( 'zones' ), 'zone_id' );
// Find current set position if it has moved since last save
if ( changes[ id ] && changes[ id ].zone_order !== undefined ) { if ( changes[ id ] && changes[ id ].zone_order !== undefined ) {
position = changes[ id ].zone_order; set_position = changes[ id ].zone_order;
} }
// Delete all changes
delete changes[ id ]; delete changes[ id ];
if ( position !== null ) { // If the position was set, and this zone does exist in DB, set the position again so the changes are not lost.
changes[ id ] = _.extend( changes[ id ] || {}, { zone_id : id, zone_order : position } ); if ( set_position !== null && zones[ id ] && zones[ id ].zone_order !== set_position ) {
changes[ id ] = _.extend( changes[ id ] || {}, { zone_id : id, zone_order : set_position } );
} }
this.changes = changes; this.changes = changes;
this.trigger( 'change:zones' );
// No changes? Disable save button.
if ( 0 === _.size( this.changes ) ) {
shippingZoneView.clearUnloadConfirmation();
}
}, },
save: function() { save: function() {
if ( _.size( this.changes ) ) { if ( _.size( this.changes ) ) {
@ -120,7 +128,7 @@
view.$el.append( $blank_template ); view.$el.append( $blank_template );
} }
view.initRows( zones ); view.initRows();
}, },
renderRow: function( rowData ) { renderRow: function( rowData ) {
var view = this; var view = this;
@ -131,11 +139,6 @@
var view = this; var view = this;
var $tr = view.$el.find( 'tr[data-id="' + rowData.zone_id + '"]'); var $tr = view.$el.find( 'tr[data-id="' + rowData.zone_id + '"]');
// Editing?
if ( rowData.editing ) {
$tr.addClass( 'editing' );
}
// Select values in region select // Select values in region select
_.each( rowData.zone_locations, function( location ) { _.each( rowData.zone_locations, function( location ) {
if ( 'string' === jQuery.type( location ) ) { if ( 'string' === jQuery.type( location ) ) {
@ -173,20 +176,24 @@
// List shipping methods // List shipping methods
view.renderShippingMethods( rowData.zone_id, rowData.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-cancel-edit' ).on( 'click', { view: this }, this.onCancelEditRow );
$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' );
// Make the row function
$tr.find( '.view' ).show();
$tr.find( '.edit' ).hide();
$tr.find( '.wc-shipping-zone-edit' ).on( 'click', { view: this }, this.onEditRow );
$tr.find( '.wc-shipping-zone-cancel-edit' ).on( 'click', { view: this }, this.onCancelEditRow );
$tr.find( '.wc-shipping-zone-delete' ).on( 'click', { view: this }, this.onDeleteRow );
$tr.find( '.wc-shipping-zone-postcodes-toggle' ).on( 'click', { view: this }, this.onTogglePostcodes );
// Editing?
if ( true === rowData.editing ) {
$tr.addClass( 'editing' );
$tr.find( '.wc-shipping-zone-edit' ).trigger( 'click' );
}
},
initRows: function() {
// Stripe // Stripe
if ( 0 === _.size( zones ) % 2 ) { if ( 0 === ( $( 'tbody.wc-shipping-zone-rows tr' ).length % 2 ) ) {
$table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).addClass( 'odd' ); $table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).addClass( 'odd' );
} else { } else {
$table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).removeClass( 'odd' ); $table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).removeClass( 'odd' );
@ -231,8 +238,9 @@
changes = {}, changes = {},
size = _.size( zones ), size = _.size( zones ),
newRow = _.extend( {}, data.default_zone, { newRow = _.extend( {}, data.default_zone, {
zone_id: 'new-' + size + '-' + Date.now(), zone_id : 'new-' + size + '-' + Date.now(),
editing: true zone_name: data.strings.default_zone_name,
editing : true
} ); } );
newRow.zone_order = 1 + _.max( newRow.zone_order = 1 + _.max(
@ -243,13 +251,11 @@
} }
); );
zones[ newRow.zone_id ] = newRow;
changes[ newRow.zone_id ] = newRow; changes[ newRow.zone_id ] = newRow;
model.set( 'zones', zones );
model.logChanges( changes ); model.logChanges( changes );
view.renderRow( newRow ); view.renderRow( newRow );
view.initRows( zones ); view.initRows();
}, },
onTogglePostcodes: function( event ) { onTogglePostcodes: function( event ) {
event.preventDefault(); event.preventDefault();
@ -280,35 +286,42 @@
event.preventDefault(); event.preventDefault();
model.discardChanges( zone_id ); model.discardChanges( zone_id );
// Remove row and re-render if ( zones[ zone_id ] ) {
row.after( view.rowTemplate( zones[ zone_id ] ) ); zones[ zone_id ].editing = false;
row.remove(); row.after( view.rowTemplate( zones[ zone_id ] ) );
view.initRow( zones[ zone_id ] );
}
view.initRow( zones[ zone_id ] ); row.remove();
view.initRows( zones ); view.initRows();
}, },
onDeleteRow: function( event ) { onDeleteRow: function( event ) {
var view = event.data.view, var view = event.data.view,
model = view.model, model = view.model,
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ), zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
changes = {}, changes = {},
row = $( this ).closest('tr'),
zone_id = $( this ).closest('tr').data('id'); zone_id = $( this ).closest('tr').data('id');
event.preventDefault(); event.preventDefault();
delete zones[ zone_id ]; if ( zones[ zone_id ] ) {
changes[ zone_id ] = _.extend( changes[ zone_id ] || {}, { deleted : 'deleted' } ); delete zones[ zone_id ];
model.set( 'zones', zones ); changes[ zone_id ] = _.extend( changes[ zone_id ] || {}, { deleted : 'deleted' } );
model.logChanges( changes ); model.set( 'zones', zones );
view.render(); model.logChanges( changes );
}
row.remove();
view.initRows();
}, },
setUnloadConfirmation: function() { setUnloadConfirmation: function() {
this.needsUnloadConfirm = true; this.needsUnloadConfirm = true;
$save_button.removeAttr( 'disabled' ); $save_button.prop( 'disabled', false );
}, },
clearUnloadConfirmation: function() { clearUnloadConfirmation: function() {
this.needsUnloadConfirm = false; this.needsUnloadConfirm = false;
$save_button.attr( 'disabled', 'disabled' ); $save_button.prop( 'disabled', true );
}, },
unloadConfirmation: function( event ) { unloadConfirmation: function( event ) {
if ( event.data.view.needsUnloadConfirm ) { if ( event.data.view.needsUnloadConfirm ) {
@ -326,7 +339,7 @@
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ), zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
changes = {}; changes = {};
if ( zones[ zone_id ][ attribute ] !== value ) { if ( ! zones[ zone_id ] || zones[ zone_id ][ attribute ] !== value ) {
changes[ zone_id ] = {}; changes[ zone_id ] = {};
changes[ zone_id ][ attribute ] = value; changes[ zone_id ][ attribute ] = value;
} }
@ -334,17 +347,24 @@
model.logChanges( changes ); model.logChanges( changes );
}, },
updateModelOnSort: function( event ) { updateModelOnSort: function( event ) {
var view = event.data.view, var view = event.data.view,
model = view.model, model = view.model,
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ), zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
changes = {}; rows = $( 'tbody.wc-shipping-zone-rows tr' ),
changes = {};
_.each( zones, function( zone ) { // Update sorted row position
var old_position = parseInt( zone.zone_order, 10 ); _.each( rows, function( row ) {
var new_position = parseInt( $table.find( 'tr[data-id="' + zone.zone_id + '"]').index(), 10 ); var zone_id = $( row ).data( 'id' ),
old_position = null,
new_position = parseInt( $( row ).index(), 10 );
if ( zones[ zone_id ] ) {
old_position = parseInt( zones[ zone_id ].zone_order, 10 );
}
if ( old_position !== new_position ) { if ( old_position !== new_position ) {
changes[ zone.zone_id ] = _.extend( changes[ zone.zone_id ] || {}, { zone_order : new_position } ); changes[ zone_id ] = _.extend( changes[ zone_id ] || {}, { zone_order : new_position } );
} }
} ); } );

File diff suppressed because one or more lines are too long

View File

@ -241,6 +241,7 @@ class WC_Settings_Shipping extends WC_Settings_Page {
'strings' => array( 'strings' => array(
'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ), 'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ), 'save_failed' => __( 'Your changes were not saved. Please retry.', 'woocommerce' ),
'default_zone_name' => __( 'Zone', 'woocommerce' ),
), ),
) ); ) );
wp_enqueue_script( 'wc-shipping-zones' ); wp_enqueue_script( 'wc-shipping-zones' );

View File

@ -47,6 +47,8 @@ class WC_Shipping_Zone extends WC_Data {
} elseif ( 0 === $zone ) { } elseif ( 0 === $zone ) {
$this->set_zone_name( __( 'Rest of the World', 'woocommerce' ) ); $this->set_zone_name( __( 'Rest of the World', 'woocommerce' ) );
$this->read_zone_locations( 0 ); $this->read_zone_locations( 0 );
} else {
$this->set_zone_name( __( 'Zone', 'woocommerce' ) );
} }
} }