2015-12-10 17:24:26 +00:00
|
|
|
/* global wc_enhanced_select_params, shippingZonesLocalizeScript, ajaxurl */
|
2015-12-10 11:55:03 +00:00
|
|
|
( function( $, data, wp, ajaxurl ) {
|
|
|
|
$( function() {
|
2015-12-16 15:37:40 +00:00
|
|
|
var $table = $( '.wc-shipping-zones' ),
|
|
|
|
$tbody = $( '.wc-shipping-zone-rows' ),
|
|
|
|
$save_button = $( '.wc-shipping-zone-save' ),
|
|
|
|
$row_template = wp.template( 'wc-shipping-zone-row' ),
|
|
|
|
$blank_template = wp.template( 'wc-shipping-zone-row-blank' ),
|
|
|
|
select2_args = $.extend({
|
2015-12-10 15:09:37 +00:00
|
|
|
minimumResultsForSearch: 10,
|
|
|
|
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
|
|
|
|
placeholder: $( this ).data( 'placeholder' ),
|
|
|
|
matcher: function( term, text, opt ) {
|
2015-12-10 17:24:26 +00:00
|
|
|
return text.toUpperCase().indexOf( term.toUpperCase() ) >= 0 || opt.attr( 'alt' ).toUpperCase().indexOf( term.toUpperCase() ) >= 0;
|
2015-12-10 15:09:37 +00:00
|
|
|
}
|
2015-12-10 17:24:26 +00:00
|
|
|
}, getEnhancedSelectFormatString() ),
|
2015-12-10 11:55:03 +00:00
|
|
|
|
2015-12-10 17:24:26 +00:00
|
|
|
// Backbone model
|
|
|
|
ShippingZone = Backbone.Model.extend({
|
|
|
|
changes: {},
|
2015-12-10 11:55:03 +00:00
|
|
|
logChanges: function( changedRows ) {
|
|
|
|
var changes = this.changes || {};
|
|
|
|
|
|
|
|
_.each( changedRows, function( row, id ) {
|
|
|
|
changes[ id ] = _.extend( changes[ id ] || { zone_id : id }, row );
|
|
|
|
} );
|
|
|
|
|
|
|
|
this.changes = changes;
|
|
|
|
this.trigger( 'change:zones' );
|
|
|
|
},
|
|
|
|
save: function() {
|
|
|
|
if ( _.size( this.changes ) ) {
|
|
|
|
$.post( ajaxurl + '?action=woocommerce_shipping_zones_save_changes', {
|
|
|
|
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
|
|
|
|
changes : this.changes
|
|
|
|
}, this.onSaveResponse, 'json' );
|
|
|
|
} else {
|
|
|
|
shippingZone.trigger( 'saved:zones' );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onSaveResponse: function( response, textStatus ) {
|
|
|
|
if ( 'success' === textStatus ) {
|
|
|
|
if ( response.success ) {
|
|
|
|
shippingZone.set( 'zones', response.data.zones );
|
|
|
|
shippingZone.trigger( 'change:zones' );
|
|
|
|
shippingZone.changes = {};
|
|
|
|
shippingZone.trigger( 'saved:zones' );
|
|
|
|
} else {
|
2015-12-10 17:24:26 +00:00
|
|
|
window.alert( data.strings.save_failed );
|
2015-12-10 11:55:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} ),
|
|
|
|
|
2015-12-10 17:24:26 +00:00
|
|
|
// Backbone view
|
2015-12-10 11:55:03 +00:00
|
|
|
ShippingZoneView = Backbone.View.extend({
|
2015-12-10 15:09:37 +00:00
|
|
|
rowTemplate: $row_template,
|
2015-12-10 11:55:03 +00:00
|
|
|
initialize: function() {
|
|
|
|
this.listenTo( this.model, 'change:zones', this.setUnloadConfirmation );
|
|
|
|
this.listenTo( this.model, 'saved:zones', this.clearUnloadConfirmation );
|
2015-12-10 13:09:39 +00:00
|
|
|
this.listenTo( this.model, 'saved:zones', this.render );
|
2015-12-10 11:55:03 +00:00
|
|
|
$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-10 18:33:59 +00:00
|
|
|
$( '.wc-shipping-zone-add' ).on( 'click', { view: this }, this.onAddNewRow );
|
2015-12-10 11:55:03 +00:00
|
|
|
},
|
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();
|
|
|
|
},
|
2015-12-10 11:55:03 +00:00
|
|
|
render: function() {
|
|
|
|
var zones = _.indexBy( this.model.get( 'zones' ), 'zone_id' ),
|
2015-12-16 13:38:03 +00:00
|
|
|
view = this;
|
2015-12-10 11:55:03 +00:00
|
|
|
|
2015-12-16 15:37:40 +00:00
|
|
|
this.$el.empty();
|
|
|
|
this.unblock();
|
2015-12-15 19:10:41 +00:00
|
|
|
|
2015-12-16 15:37:40 +00:00
|
|
|
if ( _.size( zones ) ) {
|
2015-12-10 12:31:03 +00:00
|
|
|
// Sort zones
|
|
|
|
zones = _.sortBy( zones, function( zone ) {
|
|
|
|
return parseInt( zone.zone_order, 10 );
|
|
|
|
} );
|
|
|
|
|
2015-12-10 11:55:03 +00:00
|
|
|
// Populate $tbody with the current zones
|
|
|
|
$.each( zones, function( id, rowData ) {
|
|
|
|
view.$el.append( view.rowTemplate( rowData ) );
|
2015-12-10 15:09:37 +00:00
|
|
|
|
2015-12-10 16:38:24 +00:00
|
|
|
var $tr = view.$el.find( 'tr[data-id="' + rowData.zone_id + '"]');
|
2015-12-10 15:09:37 +00:00
|
|
|
|
|
|
|
// Select values in region select
|
|
|
|
_.each( rowData.zone_locations, function( location ) {
|
2015-12-10 18:33:59 +00:00
|
|
|
if ( 'postcode' === location.type ) {
|
|
|
|
var postcode_field = $tr.find( '.wc-shipping-zone-postcodes :input' );
|
|
|
|
|
|
|
|
if ( postcode_field.val() ) {
|
2015-12-16 15:16:52 +00:00
|
|
|
postcode_field.val( postcode_field.val() + '\n' + location.code );
|
2015-12-10 18:33:59 +00:00
|
|
|
} 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 );
|
|
|
|
}
|
2015-12-10 15:09:37 +00:00
|
|
|
} );
|
2015-12-10 16:38:24 +00:00
|
|
|
|
2015-12-11 14:11:12 +00:00
|
|
|
// List shipping methods
|
2015-12-16 15:16:52 +00:00
|
|
|
var $method_list = $tr.find('.wc-shipping-zone-methods ul');
|
2015-12-11 14:11:12 +00:00
|
|
|
|
2015-12-15 16:35:33 +00:00
|
|
|
_.each( rowData.shipping_methods, function( shipping_method, instance_id ) {
|
2015-12-16 13:38:03 +00:00
|
|
|
$method_list.append( '<li><a href="admin.php?page=wc-shipping&instance_id=' + instance_id + '">' + shipping_method.title + '</a></li>' );
|
2015-12-11 14:11:12 +00:00
|
|
|
} );
|
|
|
|
|
2015-12-10 16:38:24 +00:00
|
|
|
// Editing?
|
|
|
|
if ( rowData.editing ) {
|
|
|
|
$tr.addClass( 'editing' );
|
|
|
|
}
|
2015-12-10 11:55:03 +00:00
|
|
|
} );
|
|
|
|
|
2015-12-10 17:24:26 +00:00
|
|
|
// Make the rows function
|
2015-12-10 11:55:03 +00:00
|
|
|
this.$el.find('.view').show();
|
|
|
|
this.$el.find('.edit').hide();
|
|
|
|
this.$el.find( '.wc-shipping-zone-edit' ).on( 'click', { view: this }, this.onEditRow );
|
2015-12-10 17:24:26 +00:00
|
|
|
this.$el.find( '.wc-shipping-zone-delete' ).on( 'click', { view: this }, this.onDeleteRow );
|
2015-12-10 18:33:59 +00:00
|
|
|
this.$el.find( '.wc-shipping-zone-postcodes-toggle' ).on( 'click', { view: this }, this.onTogglePostcodes );
|
2015-12-10 16:38:24 +00:00
|
|
|
this.$el.find('.editing .wc-shipping-zone-edit').trigger('click');
|
2015-12-10 11:55:03 +00:00
|
|
|
|
2015-12-10 17:24:26 +00:00
|
|
|
// 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' );
|
|
|
|
}
|
2015-12-16 15:37:40 +00:00
|
|
|
} else {
|
|
|
|
view.$el.append( $blank_template );
|
2015-12-10 17:24:26 +00:00
|
|
|
}
|
2015-12-16 15:37:40 +00:00
|
|
|
|
|
|
|
this.initTooltips();
|
2015-12-10 11:55:03 +00:00
|
|
|
},
|
2015-12-15 17:48:03 +00:00
|
|
|
initTooltips: function() {
|
|
|
|
$( '#tiptip_holder' ).removeAttr( 'style' );
|
|
|
|
$( '#tiptip_arrow' ).removeAttr( 'style' );
|
|
|
|
$( '.tips' ).tipTip({ 'attribute': 'data-tip', 'fadeIn': 50, 'fadeOut': 50, 'delay': 50 });
|
|
|
|
},
|
2015-12-10 11:55:03 +00:00
|
|
|
onSubmit: function( event ) {
|
2015-12-16 15:16:52 +00:00
|
|
|
event.data.view.block();
|
2015-12-10 11:55:03 +00:00
|
|
|
event.data.view.model.save();
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
onAddNewRow: function( event ) {
|
2015-12-10 18:33:59 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
2015-12-10 11:55:03 +00:00
|
|
|
var view = event.data.view,
|
|
|
|
model = view.model,
|
|
|
|
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
|
|
|
|
changes = {},
|
|
|
|
size = _.size( zones ),
|
|
|
|
newRow = _.extend( {}, data.default_zone, {
|
|
|
|
zone_id: 'new-' + size + '-' + Date.now(),
|
2015-12-10 16:38:24 +00:00
|
|
|
editing: true
|
2015-12-10 11:55:03 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
newRow.zone_order = 1 + _.max(
|
|
|
|
_.pluck( zones, 'zone_order' ),
|
|
|
|
function ( val ) {
|
|
|
|
// Cast them all to integers, because strings compare funky. Sighhh.
|
|
|
|
return parseInt( val, 10 );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
zones[ newRow.zone_id ] = newRow;
|
|
|
|
changes[ newRow.zone_id ] = newRow;
|
|
|
|
|
|
|
|
model.set( 'zones', zones );
|
|
|
|
model.logChanges( changes );
|
|
|
|
|
|
|
|
view.render();
|
2015-12-10 18:33:59 +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-10 11:55:03 +00:00
|
|
|
},
|
|
|
|
onEditRow: function( event ) {
|
|
|
|
event.preventDefault();
|
2015-12-10 16:38:24 +00:00
|
|
|
$( this ).closest('tr').addClass('editing');
|
2015-12-10 11:55:03 +00:00
|
|
|
$( this ).closest('tr').find('.view, .wc-shipping-zone-edit').hide();
|
|
|
|
$( this ).closest('tr').find('.edit').show();
|
2015-12-10 16:38:24 +00:00
|
|
|
$( '.wc-shipping-zone-region-select:not(.enhanced)' ).select2( select2_args );
|
|
|
|
$( '.wc-shipping-zone-region-select:not(.enhanced)' ).addClass('enhanced');
|
2015-12-10 11:55:03 +00:00
|
|
|
event.data.view.model.trigger( 'change:zones' );
|
|
|
|
},
|
|
|
|
onDeleteRow: function( event ) {
|
|
|
|
var view = event.data.view,
|
|
|
|
model = view.model,
|
|
|
|
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
|
|
|
|
changes = {},
|
|
|
|
zone_id = $( this ).closest('tr').data('id');
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
delete zones[ zone_id ];
|
|
|
|
changes[ zone_id ] = _.extend( changes[ zone_id ] || {}, { deleted : 'deleted' } );
|
|
|
|
model.set( 'zones', zones );
|
|
|
|
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 ),
|
|
|
|
zone_id = $target.closest( 'tr' ).data( 'id' ),
|
|
|
|
attribute = $target.data( 'attribute' ),
|
2015-12-10 12:31:03 +00:00
|
|
|
value = $target.val(),
|
|
|
|
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
|
2015-12-10 11:55:03 +00:00
|
|
|
changes = {};
|
|
|
|
|
|
|
|
if ( zones[ zone_id ][ attribute ] !== value ) {
|
|
|
|
changes[ zone_id ] = {};
|
|
|
|
changes[ zone_id ][ attribute ] = value;
|
|
|
|
zones[ zone_id ][ attribute ] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
model.logChanges( changes );
|
|
|
|
},
|
2015-12-10 17:47:14 +00:00
|
|
|
updateModelOnSort: function( event ) {
|
2015-12-10 11:55:03 +00:00
|
|
|
var view = event.data.view,
|
|
|
|
model = view.model,
|
|
|
|
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
|
2015-12-10 13:09:39 +00:00
|
|
|
changes = {};
|
2015-12-10 11:55:03 +00:00
|
|
|
|
2015-12-10 15:09:37 +00:00
|
|
|
_.each( zones, function( zone ) {
|
2015-12-10 13:09:39 +00:00
|
|
|
var old_position = parseInt( zone.zone_order, 10 );
|
|
|
|
var new_position = parseInt( $table.find( 'tr[data-id="' + zone.zone_id + '"]').index(), 10 );
|
2015-12-10 11:55:03 +00:00
|
|
|
|
2015-12-10 13:09:39 +00:00
|
|
|
if ( old_position !== new_position ) {
|
|
|
|
changes[ zone.zone_id ] = _.extend( changes[ zone.zone_id ] || {}, { zone_order : new_position } );
|
2015-12-10 11:55:03 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-12-10 13:09:39 +00:00
|
|
|
if ( _.size( changes ) ) {
|
2015-12-10 11:55:03 +00:00
|
|
|
model.logChanges( changes );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} ),
|
|
|
|
shippingZone = new ShippingZone({
|
|
|
|
zones: data.zones
|
|
|
|
} ),
|
|
|
|
shippingZoneView = new ShippingZoneView({
|
|
|
|
model: shippingZone,
|
|
|
|
el: $tbody
|
|
|
|
} );
|
|
|
|
|
|
|
|
shippingZoneView.render();
|
2015-12-10 15:09:37 +00:00
|
|
|
|
2015-12-10 17:24:26 +00:00
|
|
|
$tbody.sortable({
|
2015-12-10 11:55:03 +00:00
|
|
|
items: 'tr',
|
|
|
|
cursor: 'move',
|
|
|
|
axis: 'y',
|
|
|
|
handle: 'td.wc-shipping-zone-sort',
|
|
|
|
scrollSensitivity: 40
|
|
|
|
});
|
2015-12-10 15:09:37 +00:00
|
|
|
|
|
|
|
function getEnhancedSelectFormatString() {
|
|
|
|
var formatString = {
|
|
|
|
formatMatches: function( matches ) {
|
|
|
|
if ( 1 === matches ) {
|
|
|
|
return wc_enhanced_select_params.i18n_matches_1;
|
|
|
|
}
|
|
|
|
return wc_enhanced_select_params.i18n_matches_n.replace( '%qty%', matches );
|
|
|
|
},
|
|
|
|
formatNoMatches: function() {
|
|
|
|
return wc_enhanced_select_params.i18n_no_matches;
|
|
|
|
},
|
|
|
|
formatAjaxError: function() {
|
|
|
|
return wc_enhanced_select_params.i18n_ajax_error;
|
|
|
|
},
|
|
|
|
formatInputTooShort: function( input, min ) {
|
|
|
|
var number = min - input.length;
|
|
|
|
|
|
|
|
if ( 1 === number ) {
|
|
|
|
return wc_enhanced_select_params.i18n_input_too_short_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wc_enhanced_select_params.i18n_input_too_short_n.replace( '%qty%', number );
|
|
|
|
},
|
|
|
|
formatInputTooLong: function( input, max ) {
|
|
|
|
var number = input.length - max;
|
|
|
|
|
|
|
|
if ( 1 === number ) {
|
|
|
|
return wc_enhanced_select_params.i18n_input_too_long_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wc_enhanced_select_params.i18n_input_too_long_n.replace( '%qty%', number );
|
|
|
|
},
|
|
|
|
formatSelectionTooBig: function( limit ) {
|
|
|
|
if ( 1 === limit ) {
|
|
|
|
return wc_enhanced_select_params.i18n_selection_too_long_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return wc_enhanced_select_params.i18n_selection_too_long_n.replace( '%qty%', limit );
|
|
|
|
},
|
|
|
|
formatLoadMore: function() {
|
|
|
|
return wc_enhanced_select_params.i18n_load_more;
|
|
|
|
},
|
|
|
|
formatSearching: function() {
|
|
|
|
return wc_enhanced_select_params.i18n_searching;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return formatString;
|
|
|
|
}
|
2015-12-10 11:55:03 +00:00
|
|
|
});
|
|
|
|
})( jQuery, shippingZonesLocalizeScript, wp, ajaxurl );
|