2016-01-13 17:08:53 +00:00
|
|
|
/* global shippingClassesLocalizeScript, ajaxurl */
|
2016-01-13 16:52:28 +00:00
|
|
|
( function( $, data, wp, ajaxurl ) {
|
|
|
|
$( function() {
|
2016-05-12 11:18:35 +00:00
|
|
|
var $tbody = $( '.wc-shipping-class-rows' ),
|
2016-01-13 16:52:28 +00:00
|
|
|
$save_button = $( '.wc-shipping-class-save' ),
|
|
|
|
$row_template = wp.template( 'wc-shipping-class-row' ),
|
2016-01-13 17:08:53 +00:00
|
|
|
$blank_template = wp.template( 'wc-shipping-class-row-blank' ),
|
2016-01-13 16:52:28 +00:00
|
|
|
|
|
|
|
// Backbone model
|
2016-01-13 17:08:53 +00:00
|
|
|
ShippingClass = Backbone.Model.extend({
|
2016-01-13 16:52:28 +00:00
|
|
|
changes: {},
|
|
|
|
logChanges: function( changedRows ) {
|
|
|
|
var changes = this.changes || {};
|
|
|
|
|
|
|
|
_.each( changedRows, function( row, id ) {
|
|
|
|
changes[ id ] = _.extend( changes[ id ] || { term_id : id }, row );
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.changes = changes;
|
|
|
|
this.trigger( 'change:classes' );
|
|
|
|
},
|
|
|
|
save: function() {
|
|
|
|
if ( _.size( this.changes ) ) {
|
2016-03-24 19:06:51 +00:00
|
|
|
$.post( ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_classes_save_changes', {
|
2016-01-13 16:52:28 +00:00
|
|
|
wc_shipping_classes_nonce : data.wc_shipping_classes_nonce,
|
|
|
|
changes : this.changes
|
|
|
|
}, this.onSaveResponse, 'json' );
|
|
|
|
} else {
|
2016-01-13 17:08:53 +00:00
|
|
|
shippingClass.trigger( 'saved:classes' );
|
2016-01-13 16:52:28 +00:00
|
|
|
}
|
|
|
|
},
|
2016-05-12 11:18:35 +00:00
|
|
|
discardChanges: function( id ) {
|
|
|
|
var changes = this.changes || {};
|
|
|
|
|
|
|
|
// Delete all changes
|
|
|
|
delete changes[ id ];
|
|
|
|
|
|
|
|
// No changes? Disable save button.
|
|
|
|
if ( 0 === _.size( this.changes ) ) {
|
|
|
|
shippingClassView.clearUnloadConfirmation();
|
|
|
|
}
|
|
|
|
},
|
2016-01-13 16:52:28 +00:00
|
|
|
onSaveResponse: function( response, textStatus ) {
|
|
|
|
if ( 'success' === textStatus ) {
|
|
|
|
if ( response.success ) {
|
2016-01-13 17:08:53 +00:00
|
|
|
shippingClass.set( 'classes', response.data.shipping_classes );
|
|
|
|
shippingClass.trigger( 'change:classes' );
|
|
|
|
shippingClass.changes = {};
|
|
|
|
shippingClass.trigger( 'saved:classes' );
|
2016-01-13 16:52:28 +00:00
|
|
|
} else if ( response.data ) {
|
2016-05-12 11:18:35 +00:00
|
|
|
window.alert( response.data );
|
|
|
|
} else {
|
2016-01-13 16:52:28 +00:00
|
|
|
window.alert( data.strings.save_failed );
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 11:18:35 +00:00
|
|
|
shippingClassView.unblock();
|
2016-01-13 16:52:28 +00:00
|
|
|
}
|
|
|
|
} ),
|
|
|
|
|
|
|
|
// Backbone view
|
2016-01-13 17:08:53 +00:00
|
|
|
ShippingClassView = Backbone.View.extend({
|
2016-01-13 16:52:28 +00:00
|
|
|
rowTemplate: $row_template,
|
|
|
|
initialize: function() {
|
|
|
|
this.listenTo( this.model, 'change:classes', this.setUnloadConfirmation );
|
|
|
|
this.listenTo( this.model, 'saved:classes', this.clearUnloadConfirmation );
|
|
|
|
this.listenTo( this.model, 'saved:classes', this.render );
|
|
|
|
$tbody.on( 'change', { view: this }, this.updateModelOnChange );
|
|
|
|
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
|
|
|
|
$save_button.on( 'click', { view: this }, this.onSubmit );
|
|
|
|
$( document.body ).on( 'click', '.wc-shipping-class-add', { view: this }, this.onAddNewRow );
|
|
|
|
$( document.body ).on( 'click', '.wc-shipping-class-save-changes', { view: this }, this.onSubmit );
|
|
|
|
},
|
|
|
|
block: function() {
|
|
|
|
$( this.el ).block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
unblock: function() {
|
|
|
|
$( this.el ).unblock();
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
var classes = _.indexBy( this.model.get( 'classes' ), 'term_id' ),
|
|
|
|
view = this;
|
|
|
|
|
|
|
|
this.$el.empty();
|
|
|
|
this.unblock();
|
|
|
|
|
|
|
|
if ( _.size( classes ) ) {
|
2016-01-13 17:08:53 +00:00
|
|
|
// Sort classes
|
|
|
|
classes = _.sortBy( classes, function( shipping_class ) {
|
|
|
|
return shipping_class.name;
|
|
|
|
} );
|
|
|
|
|
2016-01-13 16:52:28 +00:00
|
|
|
// Populate $tbody with the current classes
|
|
|
|
$.each( classes, function( id, rowData ) {
|
2016-05-12 11:18:35 +00:00
|
|
|
view.renderRow( rowData );
|
2016-01-13 16:52:28 +00:00
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
view.$el.append( $blank_template );
|
|
|
|
}
|
|
|
|
},
|
2016-05-12 11:18:35 +00:00
|
|
|
renderRow: function( rowData ) {
|
|
|
|
var view = this;
|
|
|
|
view.$el.append( view.rowTemplate( rowData ) );
|
|
|
|
view.initRow( rowData );
|
|
|
|
},
|
|
|
|
initRow: function( rowData ) {
|
|
|
|
var view = this;
|
|
|
|
var $tr = view.$el.find( 'tr[data-id="' + rowData.term_id + '"]');
|
|
|
|
|
2016-05-19 11:38:28 +00:00
|
|
|
// Support select boxes
|
|
|
|
$tr.find( 'select' ).each( function() {
|
|
|
|
var attribute = $( this ).data( 'attribute' );
|
|
|
|
$( this ).find( 'option[value="' + rowData[ attribute ] + '"]' ).prop( 'selected', true );
|
|
|
|
} );
|
|
|
|
|
2016-05-12 11:18:35 +00:00
|
|
|
// Make the rows function
|
2016-05-19 11:38:28 +00:00
|
|
|
$tr.find( '.view' ).show();
|
|
|
|
$tr.find( '.edit' ).hide();
|
2016-05-12 11:18:35 +00:00
|
|
|
$tr.find( '.wc-shipping-class-edit' ).on( 'click', { view: this }, this.onEditRow );
|
|
|
|
$tr.find( '.wc-shipping-class-delete' ).on( 'click', { view: this }, this.onDeleteRow );
|
|
|
|
$tr.find( '.editing .wc-shipping-class-edit' ).trigger('click');
|
|
|
|
$tr.find( '.wc-shipping-class-cancel-edit' ).on( 'click', { view: this }, this.onCancelEditRow );
|
|
|
|
|
|
|
|
// Editing?
|
|
|
|
if ( true === rowData.editing ) {
|
|
|
|
$tr.addClass( 'editing' );
|
|
|
|
$tr.find( '.wc-shipping-class-edit' ).trigger( 'click' );
|
|
|
|
}
|
|
|
|
},
|
2016-01-13 16:52:28 +00:00
|
|
|
onSubmit: function( event ) {
|
|
|
|
event.data.view.block();
|
|
|
|
event.data.view.model.save();
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onAddNewRow: function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var view = event.data.view,
|
|
|
|
model = view.model,
|
|
|
|
classes = _.indexBy( model.get( 'classes' ), 'term_id' ),
|
|
|
|
changes = {},
|
|
|
|
size = _.size( classes ),
|
2017-11-22 05:49:03 +00:00
|
|
|
newRow = _.extend( {}, data.default_shipping_class, {
|
2016-01-13 16:52:28 +00:00
|
|
|
term_id: 'new-' + size + '-' + Date.now(),
|
|
|
|
editing: true,
|
2016-05-12 11:18:35 +00:00
|
|
|
newRow : true
|
2016-01-13 16:52:28 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
changes[ newRow.term_id ] = newRow;
|
|
|
|
|
|
|
|
model.logChanges( changes );
|
2016-05-12 11:18:35 +00:00
|
|
|
view.renderRow( newRow );
|
2016-05-24 11:49:29 +00:00
|
|
|
$( '.wc-shipping-classes-blank-state' ).remove();
|
2016-01-13 16:52:28 +00:00
|
|
|
},
|
|
|
|
onEditRow: function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
$( this ).closest('tr').addClass('editing');
|
|
|
|
$( this ).closest('tr').find('.view').hide();
|
|
|
|
$( this ).closest('tr').find('.edit').show();
|
|
|
|
event.data.view.model.trigger( 'change:classes' );
|
|
|
|
},
|
|
|
|
onDeleteRow: function( event ) {
|
|
|
|
var view = event.data.view,
|
|
|
|
model = view.model,
|
2016-05-12 11:18:35 +00:00
|
|
|
classes = _.indexBy( model.get( 'classes' ), 'term_id' ),
|
2016-01-13 16:52:28 +00:00
|
|
|
changes = {},
|
|
|
|
term_id = $( this ).closest('tr').data('id');
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
2016-05-12 11:18:35 +00:00
|
|
|
if ( classes[ term_id ] ) {
|
|
|
|
delete classes[ term_id ];
|
|
|
|
changes[ term_id ] = _.extend( changes[ term_id ] || {}, { deleted : 'deleted' } );
|
|
|
|
model.set( 'classes', classes );
|
|
|
|
model.logChanges( changes );
|
|
|
|
}
|
|
|
|
|
2016-05-24 11:49:29 +00:00
|
|
|
view.render();
|
2016-05-12 11:18:35 +00:00
|
|
|
},
|
|
|
|
onCancelEditRow: function( event ) {
|
|
|
|
var view = event.data.view,
|
|
|
|
model = view.model,
|
|
|
|
row = $( this ).closest('tr'),
|
|
|
|
term_id = $( this ).closest('tr').data('id'),
|
|
|
|
classes = _.indexBy( model.get( 'classes' ), 'term_id' );
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
model.discardChanges( term_id );
|
|
|
|
|
|
|
|
if ( classes[ term_id ] ) {
|
|
|
|
classes[ term_id ].editing = false;
|
|
|
|
row.after( view.rowTemplate( classes[ term_id ] ) );
|
|
|
|
view.initRow( classes[ term_id ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
row.remove();
|
2016-01-13 16:52:28 +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 ),
|
|
|
|
term_id = $target.closest( 'tr' ).data( 'id' ),
|
|
|
|
attribute = $target.data( 'attribute' ),
|
|
|
|
value = $target.val(),
|
|
|
|
classes = _.indexBy( model.get( 'classes' ), 'term_id' ),
|
|
|
|
changes = {};
|
|
|
|
|
2016-05-12 11:18:35 +00:00
|
|
|
if ( ! classes[ term_id ] || classes[ term_id ][ attribute ] !== value ) {
|
2016-01-13 16:52:28 +00:00
|
|
|
changes[ term_id ] = {};
|
|
|
|
changes[ term_id ][ attribute ] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
model.logChanges( changes );
|
|
|
|
}
|
|
|
|
} ),
|
2016-01-13 17:08:53 +00:00
|
|
|
shippingClass = new ShippingClass({
|
2016-01-13 16:52:28 +00:00
|
|
|
classes: data.classes
|
|
|
|
} ),
|
2016-01-13 17:08:53 +00:00
|
|
|
shippingClassView = new ShippingClassView({
|
|
|
|
model: shippingClass,
|
2016-01-13 16:52:28 +00:00
|
|
|
el: $tbody
|
|
|
|
} );
|
|
|
|
|
2016-01-13 17:08:53 +00:00
|
|
|
shippingClassView.render();
|
2016-01-13 16:52:28 +00:00
|
|
|
});
|
|
|
|
})( jQuery, shippingClassesLocalizeScript, wp, ajaxurl );
|