Skip to content

Commit

Permalink
Add methods from zones screen
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Jan 13, 2016
1 parent f70b0a9 commit 8064b35
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 44 deletions.
2 changes: 1 addition & 1 deletion assets/css/admin.css

Large diffs are not rendered by default.

36 changes: 25 additions & 11 deletions assets/css/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2268,12 +2268,32 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
}
}
.wc-shipping-zone-methods {
color: #999;
.method_disabled {
text-decoration: line-through;
}
ul {
color: #999;
li {
display: inline;
margin: 0 0 0 0;
}
li:before {
content: ", ";
}
li:last-child:before, li:first-child:before {
content: "";
}
li:last-child {
display: block;
}
}
.button {
display: inline-block;
}
}
.wc-shipping-zone-actions, .wc-shipping-zone-method-actions {
width: 6em;
width: 4em;

a {
@include ir();
Expand All @@ -2293,16 +2313,6 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
}
}

a.wc-shipping-zone-edit {
&:before {
content: "\e603";
}
}
a.wc-shipping-zone-view {
&:before {
content: "\e010";
}
}
a.wc-shipping-zone-delete, a.wc-shipping-zone-method-delete {
&:before {
content: "\e013";
Expand Down Expand Up @@ -2342,6 +2352,10 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
}
}

.wc-backbone-modal .wc-shipping-zone-method-selector select {
width: 100%;
}

img.help_tip {
margin: 0 0 0 9px;
vertical-align: middle;
Expand Down
150 changes: 150 additions & 0 deletions assets/js/admin/backbone-modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*global jQuery, Backbone, _ */
( function( $, Backbone, _ ) {
'use strict';

/**
* WooCommerce Backbone Modal plugin
*
* @param {object} options
*/
$.fn.WCBackboneModal = function( options ) {
return this.each( function() {
( new $.WCBackboneModal( $( this ), options ) );
});
};

/**
* Initialize the Backbone Modal
*
* @param {object} element [description]
* @param {object} options [description]
*/
$.WCBackboneModal = function( element, options ) {
// Set settings
var settings = $.extend( {}, $.WCBackboneModal.defaultOptions, options );

if ( settings.template ) {
new $.WCBackboneModal.View({
target: settings.template,
string: settings.variable
});
}
};

/**
* Set default options
*
* @type {object}
*/
$.WCBackboneModal.defaultOptions = {
template: '',
variable: {}
};

/**
* Create the Backbone Modal
*
* @return {null}
*/
$.WCBackboneModal.View = Backbone.View.extend({
tagName: 'div',
id: 'wc-backbone-modal-dialog',
_target: undefined,
_string: undefined,
events: {
'click .modal-close': 'closeButton',
'click #btn-ok': 'addButton',
'touchstart #btn-ok': 'addButton',
'keydown': 'keyboardActions'
},
initialize: function( data ) {
this._target = data.target;
this._string = data.string;
_.bindAll( this, 'render' );
this.render();
},
render: function() {
var template = wp.template( this._target );

this.$el.attr( 'tabindex' , '0' ).append(
template( this._string )
);

$( document.body ).css({
'overflow': 'hidden'
}).append( this.$el );

var $content = $( '.wc-backbone-modal-content' ).find( 'article' );
var content_h = ( $content.height() < 90 ) ? 90 : $content.height();
var max_h = $( window ).height() - 200;

if ( max_h > 400 ) {
max_h = 400;
}

if ( content_h > max_h ) {
$content.css({
'overflow': 'auto',
height: max_h + 'px'
});
} else {
$content.css({
'overflow': 'visible',
height: ( content_h > 90 ) ? 'auto' : content_h + 'px'
});
}

$( '.wc-backbone-modal-content' ).css({
'margin-top': '-' + ( $( '.wc-backbone-modal-content' ).height() / 2 ) + 'px',
'margin-left': '-' + ( $( '.wc-backbone-modal-content' ).width() / 2 ) + 'px'
});

$( document.body ).trigger( 'wc_backbone_modal_loaded', this._target );
},
closeButton: function( e ) {
e.preventDefault();
$( document.body ).trigger( 'wc_backbone_modal_before_remove', this._target );
this.undelegateEvents();
$( document ).off( 'focusin' );
$( document.body ).css({
'overflow': 'auto'
});
this.remove();
$( document.body ).trigger( 'wc_backbone_modal_removed', this._target );
},
addButton: function( e ) {
$( document.body ).trigger( 'wc_backbone_modal_response', [ this._target, this.getFormData() ] );
this.closeButton( e );
},
getFormData: function() {
var data = {};

$( document.body ).trigger( 'wc_backbone_modal_before_update', this._target );

$.each( $( 'form', this.$el ).serializeArray(), function( index, item ) {
if ( data.hasOwnProperty( item.name ) ) {
data[ item.name ] = $.makeArray( data[ item.name ] );
data[ item.name ].push( item.value );
} else {
data[ item.name ] = item.value;
}
});

return data;
},
keyboardActions: function( e ) {
var button = e.keyCode || e.which;

// Enter key
if ( 13 === button && ! ( e.target.tagName && ( e.target.tagName.toLowerCase() === 'input' || e.target.tagName.toLowerCase() === 'textarea' ) ) ) {
this.addButton( e );
}

// ESC key
if ( 27 === button ) {
this.closeButton( e );
}
}
});

}( jQuery, Backbone, _ ));
1 change: 1 addition & 0 deletions assets/js/admin/backbone-modal.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 60 additions & 20 deletions assets/js/admin/wc-shipping-zones.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@
$tbody.on( 'sortupdate', { view: this }, this.updateModelOnSort );
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
$save_button.on( 'click', { view: this }, this.onSubmit );
$( document.body ).on( 'click', '.add_shipping_method', { view: this }, this.onAddShippingMethod );
$( document.body ).on( 'click', '.wc-shipping-zone-add', { view: this }, this.onAddNewRow );
$( document.body ).on( 'click', '.wc-shipping-zone-save-changes', { view: this }, this.onSubmit );
$( document.body ).on( 'wc_backbone_modal_response', this.onAddShippingMethodSubmitted );
},
block: function() {
$( this.el ).block({
Expand Down Expand Up @@ -96,6 +99,11 @@

var $tr = view.$el.find( 'tr[data-id="' + rowData.zone_id + '"]');

// Editing?
if ( rowData.editing ) {
$tr.addClass( 'editing' );
}

// Select values in region select
_.each( rowData.zone_locations, function( location ) {
if ( 'postcode' === location.type ) {
Expand All @@ -114,26 +122,7 @@
} );

// List shipping methods
var $method_list = $tr.find('.wc-shipping-zone-methods ul');

if ( _.size( rowData.shipping_methods ) ) {
_.each( rowData.shipping_methods, function( shipping_method, instance_id ) {
var class_name = 'method_disabled';

if ( 'yes' === shipping_method.enabled ) {
class_name = 'method_enabled';
}

$method_list.append( '<li><a href="admin.php?page=wc-settings&amp;tab=shipping&amp;instance_id=' + instance_id + '" class="' + class_name + '">' + shipping_method.title + '</a></li>' );
} );
} else {
$method_list.append( '<li>' + data.strings.no_methods + '</li>' );
}

// Editing?
if ( rowData.editing ) {
$tr.addClass( 'editing' );
}
view.renderShippingMethods( rowData.zone_id, rowData.shipping_methods );
} );

// Make the rows function
Expand All @@ -156,6 +145,27 @@

this.initTooltips();
},
renderShippingMethods: function( zone_id, shipping_methods ) {
var $tr = $( '.wc-shipping-zones tr[data-id="' + zone_id + '"]');
var $method_list = $tr.find('.wc-shipping-zone-methods ul');

$method_list.empty();

if ( _.size( shipping_methods ) ) {
_.each( shipping_methods, function( shipping_method, instance_id ) {
var class_name = 'method_disabled';

if ( 'yes' === shipping_method.enabled ) {
class_name = 'method_enabled';
}

$method_list.append( '<li><a href="admin.php?page=wc-settings&amp;tab=shipping&amp;instance_id=' + instance_id + '" class="' + class_name + '">' + shipping_method.title + '</a></li>' );
} );
$method_list.append( '<li>' + data.strings.add_another_method + '</li>' );
} else {
$method_list.append( '<li>' + data.strings.no_methods + '</li>' );
}
},
initTooltips: function() {
$( '#tiptip_holder' ).removeAttr( 'style' );
$( '#tiptip_arrow' ).removeAttr( 'style' );
Expand Down Expand Up @@ -275,6 +285,36 @@
if ( _.size( changes ) ) {
model.logChanges( changes );
}
},
onAddShippingMethod: function( event ) {
var zone_id = $( this ).closest('tr').data('id');

event.preventDefault();

$( this ).WCBackboneModal({
template : 'wc-modal-add-shipping-method',
variable : {
zone_id : zone_id
}
});
},
onAddShippingMethodSubmitted: function( event, target, posted_data ) {
if ( 'wc-modal-add-shipping-method' === target ) {
shippingZoneView.block();

// Add method to zone via ajax call
$.post( ajaxurl + '?action=woocommerce_shipping_zone_add_method', {
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
method_id : posted_data.add_method_id,
zone_id : posted_data.zone_id
}, function( response, textStatus ) {
if ( 'success' === textStatus && response.success ) {
// Method was added. Render methods.
shippingZoneView.renderShippingMethods( posted_data.zone_id, response.data.methods );
}
shippingZoneView.unblock();
}, 'json' );
}
}
} ),
shippingZone = new ShippingZone({
Expand Down

0 comments on commit 8064b35

Please sign in to comment.