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

427 lines
15 KiB
JavaScript
Raw Normal View History

/* global shippingZoneMethodsLocalizeScript, ajaxurl */
( function( $, data, wp, ajaxurl ) {
$( function() {
2015-12-16 15:37:40 +00:00
var $table = $( '.wc-shipping-zone-methods' ),
$tbody = $( '.wc-shipping-zone-method-rows' ),
$save_button = $( '.wc-shipping-zone-method-save' ),
$row_template = wp.template( 'wc-shipping-zone-method-row' ),
$blank_template = wp.template( 'wc-shipping-zone-method-row-blank' ),
// Backbone model
2015-12-15 17:48:03 +00:00
ShippingMethod = Backbone.Model.extend({
changes: {},
logChanges: function( changedRows ) {
var changes = this.changes || {};
2016-10-10 16:50:29 +00:00
_.each( changedRows.methods, function( row, id ) {
changes.methods = changes.methods || { methods : {} };
changes.methods[ id ] = _.extend( changes.methods[ id ] || { instance_id : id }, row );
} );
2016-10-14 12:06:06 +00:00
if ( typeof changedRows.zone_name !== 'undefined' ) {
2016-10-10 17:41:07 +00:00
changes.zone_name = changedRows.zone_name;
}
2016-10-14 12:06:06 +00:00
if ( typeof changedRows.zone_locations !== 'undefined' ) {
2016-10-10 17:41:07 +00:00
changes.zone_locations = changedRows.zone_locations;
}
2016-10-14 12:06:06 +00:00
if ( typeof changedRows.zone_postcodes !== 'undefined' ) {
2016-10-10 17:41:07 +00:00
changes.zone_postcodes = changedRows.zone_postcodes;
}
this.changes = changes;
this.trigger( 'change:methods' );
},
save: function() {
$.post(
ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_zone_methods_save_changes',
{
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
changes : this.changes,
zone_id : data.zone_id
},
this.onSaveResponse,
'json'
);
},
onSaveResponse: function( response, textStatus ) {
if ( 'success' === textStatus ) {
if ( response.success ) {
2016-09-24 02:22:57 +00:00
if ( response.data.zone_id !== data.zone_id ) {
data.zone_id = response.data.zone_id;
if ( window.history.pushState ) {
window.history.pushState(
{},
'',
'admin.php?page=wc-settings&tab=shipping&zone_id=' + response.data.zone_id
);
2016-09-24 02:22:57 +00:00
}
}
2015-12-15 17:48:03 +00:00
shippingMethod.set( 'methods', response.data.methods );
shippingMethod.trigger( 'change:methods' );
shippingMethod.changes = {};
shippingMethod.trigger( 'saved:methods' );
} else {
window.alert( data.strings.save_failed );
}
}
2016-05-12 11:18:35 +00:00
}
} ),
// Backbone view
2015-12-15 17:48:03 +00:00
ShippingMethodView = Backbone.View.extend({
rowTemplate: $row_template,
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 );
$tbody.on( 'change', { view: this }, this.updateModelOnChange );
$tbody.on( 'sortupdate', { view: this }, this.updateModelOnSort );
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
$save_button.on( 'click', { view: this }, this.onSubmit );
2016-03-24 17:26:40 +00:00
$( document.body ).on(
'input change',
'#zone_name, #zone_locations, #zone_postcodes',
{ view: this },
this.onUpdateZone
);
2016-03-24 17:26:40 +00:00
$( document.body ).on( 'click', '.wc-shipping-zone-method-settings', { view: this }, this.onConfigureShippingMethod );
$( document.body ).on( 'click', '.wc-shipping-zone-add-method', { view: this }, this.onAddShippingMethod );
2016-03-24 17:26:40 +00:00
$( document.body ).on( 'wc_backbone_modal_response', this.onConfigureShippingMethodSubmitted );
$( document.body ).on( 'wc_backbone_modal_response', this.onAddShippingMethodSubmitted );
$( document.body ).on( 'change', '.wc-shipping-zone-method-selector select', this.onChangeShippingMethodSelector );
2016-09-24 02:22:57 +00:00
$( document.body ).on( 'click', '.wc-shipping-zone-postcodes-toggle', this.onTogglePostcodes );
},
2016-10-10 17:41:07 +00:00
onUpdateZone: function( event ) {
2016-10-10 16:50:29 +00:00
var view = event.data.view,
model = view.model,
2016-10-10 17:41:07 +00:00
value = $( this ).val(),
$target = $( event.target ),
attribute = $target.data( 'attribute' ),
2016-10-10 16:50:29 +00:00
changes = {};
event.preventDefault();
2016-10-10 17:41:07 +00:00
changes[ attribute ] = value;
model.set( attribute, value );
2016-10-10 16:50:29 +00:00
model.logChanges( changes );
view.render();
2015-12-16 15:16:52 +00:00
},
block: function() {
$( this.el ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
},
unblock: function() {
$( this.el ).unblock();
},
render: function() {
var methods = _.indexBy( this.model.get( 'methods' ), 'instance_id' ),
2016-10-10 16:50:29 +00:00
zone_name = this.model.get( 'zone_name' ),
view = this;
2016-10-10 16:50:29 +00:00
// Set name.
$('.wc-shipping-zone-name').text( zone_name ? zone_name : data.strings.default_zone_name );
2016-10-10 16:50:29 +00:00
// Blank out the contents.
this.$el.empty();
2015-12-16 15:16:52 +00:00
this.unblock();
if ( _.size( methods ) ) {
// Sort methods
methods = _.sortBy( methods, function( method ) {
return parseInt( method.method_order, 10 );
} );
// Populate $tbody with the current methods
$.each( methods, function( id, rowData ) {
2015-12-16 16:24:58 +00:00
if ( 'yes' === rowData.enabled ) {
rowData.enabled_icon = '<span class="woocommerce-input-toggle woocommerce-input-toggle--enabled">' +
data.strings.yes +
'</span>';
2015-12-16 16:24:58 +00:00
} else {
rowData.enabled_icon = '<span class="woocommerce-input-toggle woocommerce-input-toggle--disabled">' +
data.strings.no +
'</span>';
2015-12-16 16:24:58 +00:00
}
view.$el.append( view.rowTemplate( rowData ) );
2015-12-18 13:12:20 +00:00
var $tr = view.$el.find( 'tr[data-id="' + rowData.instance_id + '"]');
if ( ! rowData.has_settings ) {
$tr
.find( '.wc-shipping-zone-method-title > a' )
.replaceWith('<span>' + $tr.find( '.wc-shipping-zone-method-title > a' ).text() + '</span>' );
var $del = $tr.find( '.wc-shipping-zone-method-delete' );
$tr.find( '.wc-shipping-zone-method-title .row-actions' ).empty().html($del);
2015-12-18 13:12:20 +00:00
}
} );
// Make the rows function
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 );
2015-12-16 15:37:40 +00:00
} else {
view.$el.append( $blank_template );
}
2015-12-16 15:37:40 +00:00
this.initTooltips();
},
initTooltips: function() {
$( '#tiptip_holder' ).removeAttr( 'style' );
$( '#tiptip_arrow' ).removeAttr( 'style' );
$( '.tips' ).tipTip({ 'attribute': 'data-tip', 'fadeIn': 50, 'fadeOut': 50, 'delay': 50 });
},
onSubmit: function( event ) {
2015-12-16 15:16:52 +00:00
event.data.view.block();
event.data.view.model.save();
event.preventDefault();
},
onDeleteRow: function( event ) {
var view = event.data.view,
model = view.model,
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
changes = {},
instance_id = $( this ).closest('tr').data('id');
event.preventDefault();
delete methods[ instance_id ];
2016-10-10 17:45:20 +00:00
changes.methods = changes.methods || { methods : {} };
2016-10-10 16:50:29 +00:00
changes.methods[ instance_id ] = _.extend( changes.methods[ instance_id ] || {}, { deleted : 'deleted' } );
model.set( 'methods', methods );
model.logChanges( changes );
view.render();
},
onToggleEnabled: function( event ) {
var view = event.data.view,
$target = $( event.target ),
model = view.model,
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
instance_id = $target.closest( 'tr' ).data( 'id' ),
enabled = $target.closest( 'tr' ).data( 'enabled' ) === 'yes' ? 'no' : 'yes',
2016-03-18 20:43:58 +00:00
changes = {};
event.preventDefault();
methods[ instance_id ].enabled = enabled;
2016-10-10 16:50:29 +00:00
changes.methods = changes.methods || { methods : {} };
changes.methods[ instance_id ] = _.extend( changes.methods[ instance_id ] || {}, { enabled : enabled } );
model.set( 'methods', methods );
model.logChanges( changes );
view.render();
},
setUnloadConfirmation: function() {
this.needsUnloadConfirm = true;
$save_button.removeAttr( 'disabled' );
},
clearUnloadConfirmation: function() {
this.needsUnloadConfirm = false;
$save_button.attr( 'disabled', 'disabled' );
},
unloadConfirmation: function( event ) {
if ( event.data.view.needsUnloadConfirm ) {
event.returnValue = data.strings.unload_confirmation_msg;
window.event.returnValue = data.strings.unload_confirmation_msg;
return data.strings.unload_confirmation_msg;
}
},
updateModelOnChange: function( event ) {
var model = event.data.view.model,
$target = $( event.target ),
instance_id = $target.closest( 'tr' ).data( 'id' ),
attribute = $target.data( 'attribute' ),
value = $target.val(),
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
changes = {};
if ( methods[ instance_id ][ attribute ] !== value ) {
2016-10-10 16:50:29 +00:00
changes.methods[ instance_id ] = {};
changes.methods[ instance_id ][ attribute ] = value;
methods[ instance_id ][ attribute ] = value;
}
model.logChanges( changes );
},
updateModelOnSort: function( event ) {
var view = event.data.view,
model = view.model,
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
changes = {};
2015-12-15 17:48:03 +00:00
_.each( methods, function( method ) {
var old_position = parseInt( method.method_order, 10 );
2015-12-16 16:09:52 +00:00
var new_position = parseInt( $table.find( 'tr[data-id="' + method.instance_id + '"]').index() + 1, 10 );
if ( old_position !== new_position ) {
2016-10-10 13:45:15 +00:00
methods[ method.instance_id ].method_order = new_position;
2016-10-10 16:50:29 +00:00
changes.methods = changes.methods || { methods : {} };
changes.methods[ method.instance_id ] = _.extend(
changes.methods[ method.instance_id ] || {}, { method_order : new_position }
);
}
} );
if ( _.size( changes ) ) {
model.logChanges( changes );
}
2016-03-24 17:26:40 +00:00
},
onConfigureShippingMethod: function( event ) {
var instance_id = $( this ).closest( 'tr' ).data( 'id' ),
model = event.data.view.model,
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
method = methods[ instance_id ];
2016-03-24 18:31:39 +00:00
// Only load modal if supported
if ( ! method.settings_html ) {
return true;
}
2016-03-24 17:26:40 +00:00
event.preventDefault();
$( this ).WCBackboneModal({
template : 'wc-modal-shipping-method-settings',
variable : {
instance_id : instance_id,
method : method
},
data : {
instance_id : instance_id,
method : method
}
});
$( document.body ).trigger( 'init_tooltips' );
},
onConfigureShippingMethodSubmitted: function( event, target, posted_data ) {
if ( 'wc-modal-shipping-method-settings' === target ) {
shippingMethodView.block();
// Save method settings via ajax call
$.post(
ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_zone_methods_save_settings',
{
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
instance_id : posted_data.instance_id,
data : posted_data
},
function( response, textStatus ) {
if ( 'success' === textStatus && response.success ) {
$( 'table.wc-shipping-zone-methods' ).parent().find( '#woocommerce_errors' ).remove();
// If there were errors, prepend the form.
if ( response.data.errors.length > 0 ) {
shippingMethodView.showErrors( response.data.errors );
}
2016-03-24 17:26:40 +00:00
// Method was saved. Re-render.
if ( _.size( shippingMethodView.model.changes ) ) {
shippingMethodView.model.save();
} else {
shippingMethodView.model.onSaveResponse( response, textStatus );
}
2016-03-24 19:06:51 +00:00
} else {
window.alert( data.strings.save_failed );
shippingMethodView.unblock();
2016-03-24 19:06:51 +00:00
}
},
'json'
);
2016-03-24 17:26:40 +00:00
}
2016-03-31 14:18:35 +00:00
},
showErrors: function( errors ) {
var error_html = '<div id="woocommerce_errors" class="error notice is-dismissible">';
$( errors ).each( function( index, value ) {
error_html = error_html + '<p>' + value + '</p>';
} );
error_html = error_html + '</div>';
$( 'table.wc-shipping-zone-methods' ).before( error_html );
},
onAddShippingMethod: function( event ) {
event.preventDefault();
$( this ).WCBackboneModal({
template : 'wc-modal-add-shipping-method',
variable : {
zone_id : data.zone_id
}
});
2021-01-07 22:08:36 +00:00
$( '.wc-shipping-zone-method-selector select' ).trigger( 'change' );
},
onAddShippingMethodSubmitted: function( event, target, posted_data ) {
if ( 'wc-modal-add-shipping-method' === target ) {
shippingMethodView.block();
// Add method to zone via ajax call
$.post( ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_zone_add_method', {
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
method_id : posted_data.add_method_id,
zone_id : data.zone_id
}, function( response, textStatus ) {
if ( 'success' === textStatus && response.success ) {
2016-09-24 02:22:57 +00:00
if ( response.data.zone_id !== data.zone_id ) {
data.zone_id = response.data.zone_id;
if ( window.history.pushState ) {
window.history.pushState(
{},
'',
'admin.php?page=wc-settings&tab=shipping&zone_id=' + response.data.zone_id
);
2016-09-24 02:22:57 +00:00
}
}
// Trigger save if there are changes, or just re-render
if ( _.size( shippingMethodView.model.changes ) ) {
shippingMethodView.model.save();
} else {
shippingMethodView.model.set( 'methods', response.data.methods );
shippingMethodView.model.trigger( 'change:methods' );
shippingMethodView.model.changes = {};
shippingMethodView.model.trigger( 'saved:methods' );
}
}
shippingMethodView.unblock();
}, 'json' );
}
},
onChangeShippingMethodSelector: function() {
var description = $( this ).find( 'option:selected' ).data( 'description' );
$( this ).parent().find( '.wc-shipping-zone-method-description' ).remove();
2017-01-17 15:50:19 +00:00
$( this ).after( '<div class="wc-shipping-zone-method-description">' + description + '</div>' );
$( this ).closest( 'article' ).height( $( this ).parent().height() );
2016-09-24 02:22:57 +00:00
},
onTogglePostcodes: function( event ) {
event.preventDefault();
var $tr = $( this ).closest( 'tr');
$tr.find( '.wc-shipping-zone-postcodes' ).show();
$tr.find( '.wc-shipping-zone-postcodes-toggle' ).hide();
}
} ),
2015-12-15 17:48:03 +00:00
shippingMethod = new ShippingMethod({
2016-10-10 16:50:29 +00:00
methods: data.methods,
zone_name: data.zone_name
} ),
2015-12-15 17:48:03 +00:00
shippingMethodView = new ShippingMethodView({
model: shippingMethod,
el: $tbody
} );
2015-12-15 17:48:03 +00:00
shippingMethodView.render();
$tbody.sortable({
items: 'tr',
cursor: 'move',
axis: 'y',
handle: 'td.wc-shipping-zone-method-sort',
scrollSensitivity: 40
});
});
})( jQuery, shippingZoneMethodsLocalizeScript, wp, ajaxurl );