2015-12-11 14:11:12 +00:00
|
|
|
/* 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' ),
|
2015-12-11 14:11:12 +00:00
|
|
|
|
|
|
|
// Backbone model
|
2015-12-15 17:48:03 +00:00
|
|
|
ShippingMethod = Backbone.Model.extend({
|
2015-12-11 14:11:12 +00:00
|
|
|
changes: {},
|
|
|
|
logChanges: function( changedRows ) {
|
|
|
|
var changes = this.changes || {};
|
|
|
|
|
|
|
|
_.each( changedRows, function( row, id ) {
|
|
|
|
changes[ id ] = _.extend( changes[ id ] || { instance_id : id }, row );
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.changes = changes;
|
|
|
|
this.trigger( 'change:methods' );
|
|
|
|
},
|
|
|
|
save: function() {
|
|
|
|
if ( _.size( this.changes ) ) {
|
2015-12-15 17:48:03 +00:00
|
|
|
$.post( ajaxurl + '?action=woocommerce_shipping_zone_methods_save_changes', {
|
|
|
|
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
|
|
|
|
changes : this.changes,
|
|
|
|
zone_id : data.zone_id
|
2015-12-11 14:11:12 +00:00
|
|
|
}, this.onSaveResponse, 'json' );
|
|
|
|
} else {
|
2015-12-15 17:48:03 +00:00
|
|
|
shippingMethod.trigger( 'saved:methods' );
|
2015-12-11 14:11:12 +00:00
|
|
|
}
|
|
|
|
},
|
2015-12-16 15:16:52 +00:00
|
|
|
addMethod: function() {
|
2015-12-16 16:09:52 +00:00
|
|
|
if ( _.size( this.changes ) ) {
|
|
|
|
this.save();
|
|
|
|
}
|
2015-12-16 15:16:52 +00:00
|
|
|
$.post( ajaxurl + '?action=woocommerce_shipping_zone_add_method', {
|
|
|
|
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
|
|
|
|
method_id : $('select[name="add_method_id"]').val(),
|
|
|
|
zone_id : data.zone_id
|
|
|
|
}, this.onAddResponse, 'json' );
|
|
|
|
},
|
2015-12-11 14:11:12 +00:00
|
|
|
onSaveResponse: function( response, textStatus ) {
|
|
|
|
if ( 'success' === textStatus ) {
|
|
|
|
if ( response.success ) {
|
2015-12-15 17:48:03 +00:00
|
|
|
shippingMethod.set( 'methods', response.data.methods );
|
|
|
|
shippingMethod.trigger( 'change:methods' );
|
|
|
|
shippingMethod.changes = {};
|
|
|
|
shippingMethod.trigger( 'saved:methods' );
|
2015-12-11 14:11:12 +00:00
|
|
|
} else {
|
|
|
|
window.alert( data.strings.save_failed );
|
|
|
|
}
|
|
|
|
}
|
2015-12-16 15:16:52 +00:00
|
|
|
},
|
|
|
|
onAddResponse: function( response, textStatus ) {
|
|
|
|
if ( 'success' === textStatus ) {
|
|
|
|
if ( response.success && response.data.instance_id ) {
|
|
|
|
shippingMethod.set( 'methods', response.data.methods );
|
|
|
|
shippingMethod.trigger( 'change:methods' );
|
|
|
|
shippingMethod.changes = {};
|
|
|
|
shippingMethod.trigger( 'saved:methods' );
|
|
|
|
} else {
|
|
|
|
window.alert( data.strings.add_method_failed );
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 14:11:12 +00:00
|
|
|
}
|
|
|
|
} ),
|
|
|
|
|
|
|
|
// Backbone view
|
2015-12-15 17:48:03 +00:00
|
|
|
ShippingMethodView = Backbone.View.extend({
|
2015-12-11 14:11:12 +00:00
|
|
|
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 );
|
2015-12-16 15:16:52 +00:00
|
|
|
$('.wc-shipping-zone-add-method').on( 'click', { view: this }, this.onAdd );
|
|
|
|
},
|
|
|
|
block: function() {
|
|
|
|
$( this.el ).block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
unblock: function() {
|
|
|
|
$( this.el ).unblock();
|
2015-12-11 14:11:12 +00:00
|
|
|
},
|
|
|
|
render: function() {
|
2016-03-15 17:23:06 +00:00
|
|
|
var methods = _.indexBy( this.model.get( 'methods' ), 'instance_id' ),
|
2015-12-11 14:11:12 +00:00
|
|
|
view = this;
|
|
|
|
|
|
|
|
// Blank out the contents.
|
|
|
|
this.$el.empty();
|
2015-12-16 15:16:52 +00:00
|
|
|
this.unblock();
|
2015-12-11 14:11:12 +00:00
|
|
|
|
|
|
|
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 ) {
|
2016-03-15 17:23:06 +00:00
|
|
|
rowData.enabled_icon = '<span class="status-enabled">' + data.strings.yes + '</span>';
|
2015-12-16 16:24:58 +00:00
|
|
|
} else {
|
2016-03-15 17:23:06 +00:00
|
|
|
rowData.enabled_icon = '<span class="status-disabled">' + data.strings.no + '</span>';
|
2015-12-16 16:24:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 14:11:12 +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( $tr.find( '.wc-shipping-zone-method-title' ).text() );
|
|
|
|
$tr.find( '.wc-shipping-zone-method-settings' ).remove();
|
|
|
|
}
|
2015-12-11 14:11:12 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Make the rows function
|
|
|
|
this.$el.find( '.wc-shipping-zone-method-delete' ).on( 'click', { view: this }, this.onDeleteRow );
|
2016-03-15 17:23:06 +00:00
|
|
|
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-11 14:11:12 +00:00
|
|
|
}
|
2015-12-16 15:37:40 +00:00
|
|
|
|
|
|
|
this.initTooltips();
|
2015-12-11 14:11:12 +00:00
|
|
|
},
|
|
|
|
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();
|
2015-12-11 14:11:12 +00:00
|
|
|
event.data.view.model.save();
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
2015-12-16 15:16:52 +00:00
|
|
|
onAdd: function( event ) {
|
|
|
|
event.data.view.block();
|
|
|
|
event.data.view.model.addMethod();
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
2015-12-11 14:11:12 +00:00
|
|
|
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 ];
|
|
|
|
changes[ instance_id ] = _.extend( changes[ instance_id ] || {}, { deleted : 'deleted' } );
|
|
|
|
model.set( 'methods', methods );
|
|
|
|
model.logChanges( changes );
|
|
|
|
view.render();
|
|
|
|
},
|
2016-03-15 17:23:06 +00:00
|
|
|
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 = {};
|
2016-03-15 17:23:06 +00:00
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
methods[ instance_id ].enabled = enabled;
|
|
|
|
changes[ instance_id ] = _.extend( changes[ instance_id ] || {}, { enabled : enabled } );
|
|
|
|
model.set( 'methods', methods );
|
|
|
|
model.logChanges( changes );
|
|
|
|
view.render();
|
|
|
|
},
|
2015-12-11 14:11:12 +00:00
|
|
|
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 ) {
|
|
|
|
changes[ instance_id ] = {};
|
|
|
|
changes[ 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 ) {
|
2015-12-11 14:11:12 +00:00
|
|
|
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 );
|
2015-12-11 14:11:12 +00:00
|
|
|
|
|
|
|
if ( old_position !== new_position ) {
|
|
|
|
changes[ method.instance_id ] = _.extend( changes[ method.instance_id ] || {}, { method_order : new_position } );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( _.size( changes ) ) {
|
|
|
|
model.logChanges( changes );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} ),
|
2015-12-15 17:48:03 +00:00
|
|
|
shippingMethod = new ShippingMethod({
|
2015-12-11 14:11:12 +00:00
|
|
|
methods: data.methods
|
|
|
|
} ),
|
2015-12-15 17:48:03 +00:00
|
|
|
shippingMethodView = new ShippingMethodView({
|
|
|
|
model: shippingMethod,
|
2015-12-11 14:11:12 +00:00
|
|
|
el: $tbody
|
|
|
|
} );
|
|
|
|
|
2015-12-15 17:48:03 +00:00
|
|
|
shippingMethodView.render();
|
2015-12-11 14:11:12 +00:00
|
|
|
|
|
|
|
$tbody.sortable({
|
|
|
|
items: 'tr',
|
|
|
|
cursor: 'move',
|
|
|
|
axis: 'y',
|
|
|
|
handle: 'td.wc-shipping-zone-method-sort',
|
|
|
|
scrollSensitivity: 40
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})( jQuery, shippingZoneMethodsLocalizeScript, wp, ajaxurl );
|