Skip to content
This repository was archived by the owner on Aug 30, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions assets/ajax-cart.js.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,30 @@ ShopifyAPI.onCartUpdate = function(cart) {
};

ShopifyAPI.updateCartNote = function(note, callback) {
var params = {
var $body = $(document.body),
params = {
type: 'POST',
url: '/cart/update.js',
data: 'note=' + attributeToString(note),
dataType: 'json',
beforeSend: function() {
$body.trigger('beforeUpdateCartNote.ajaxCart', note);
},
success: function(cart) {
if ((typeof callback) === 'function') {
callback(cart);
}
else {
ShopifyAPI.onCartUpdate(cart);
}
$body.trigger('afterUpdateCartNote.ajaxCart', [note, cart]);
},
error: function(XMLHttpRequest, textStatus) {
$body.trigger('errorUpdateCartNote.ajaxCart', [XMLHttpRequest, textStatus]);
ShopifyAPI.onError(XMLHttpRequest, textStatus);
},
complete: function(jqxhr, text) {
$body.trigger('completeUpdateCartNote.ajaxCart', [this, jqxhr, text]);
}
};
jQuery.ajax(params);
Expand All @@ -72,18 +81,23 @@ ShopifyAPI.onError = function(XMLHttpRequest, textStatus) {
- Allow custom error callback
==============================================================================*/
ShopifyAPI.addItemFromForm = function(form, callback, errorCallback) {
var params = {
var $body = $(document.body),
params = {
type: 'POST',
url: '/cart/add.js',
data: jQuery(form).serialize(),
dataType: 'json',
beforeSend: function(jqxhr, settings) {
$body.trigger('beforeAddItem.ajaxCart', form);
},
success: function(line_item) {
if ((typeof callback) === 'function') {
callback(line_item, form);
}
else {
ShopifyAPI.onItemAdded(line_item, form);
}
$body.trigger('afterAddItem.ajaxCart', [line_item, form]);
},
error: function(XMLHttpRequest, textStatus) {
if ((typeof errorCallback) === 'function') {
Expand All @@ -92,40 +106,55 @@ ShopifyAPI.addItemFromForm = function(form, callback, errorCallback) {
else {
ShopifyAPI.onError(XMLHttpRequest, textStatus);
}
$body.trigger('errorAddItem.ajaxCart', [XMLHttpRequest, textStatus]);
},
complete: function(jqxhr, text) {
$body.trigger('completeAddItem.ajaxCart', [this, jqxhr, text]);
}
};
jQuery.ajax(params);
};

// Get from cart.js returns the cart in JSON
ShopifyAPI.getCart = function(callback) {
$(document.body).trigger('beforeGetCart.ajaxCart');
jQuery.getJSON('/cart.js', function (cart, textStatus) {
if ((typeof callback) === 'function') {
callback(cart);
}
else {
ShopifyAPI.onCartUpdate(cart);
}
$(document.body).trigger('afterGetCart.ajaxCart', cart);
});
};

// POST to cart/change.js returns the cart in JSON
ShopifyAPI.changeItem = function(line, quantity, callback) {
var params = {
var $body = $(document.body),
params = {
type: 'POST',
url: '/cart/change.js',
data: 'quantity=' + quantity + '&line=' + line,
dataType: 'json',
beforeSend: function() {
$body.trigger('beforeChangeItem.ajaxCart', [line, quantity]);
},
success: function(cart) {
if ((typeof callback) === 'function') {
callback(cart);
}
else {
ShopifyAPI.onCartUpdate(cart);
}
$body.trigger('afterChangeItem.ajaxCart', [line, quantity, cart]);
},
error: function(XMLHttpRequest, textStatus) {
$body.trigger('errorChangeItem.ajaxCart', [XMLHttpRequest, textStatus]);
ShopifyAPI.onError(XMLHttpRequest, textStatus);
},
complete: function(jqxhr, text) {
$body.trigger('completeChangeItem.ajaxCart', [this, jqxhr, text]);
}
};
jQuery.ajax(params);
Expand Down Expand Up @@ -178,7 +207,7 @@ var ajaxCart = (function(module, $) {
$cartCostSelector = $(settings.cartCostSelector);

// General Selectors
$body = $('body');
$body = $(document.body);

// Track cart activity status
isUpdating = false;
Expand Down Expand Up @@ -325,7 +354,7 @@ var ajaxCart = (function(module, $) {

cartCallback = function(cart) {
$body.removeClass('drawer--is-loading');
$body.trigger('ajaxCart.afterCartLoad', cart);
$body.trigger('afterCartLoad.ajaxCart', cart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this name. It's the only breaking change I can see in the PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that. I'll update the version number differently instead. All good

};

adjustCart = function () {
Expand All @@ -336,7 +365,7 @@ var ajaxCart = (function(module, $) {
if (isUpdating) {
return;
}

var $el = $(this),
line = $el.data('line'),
$qtySelector = $el.siblings('.ajaxcart__qty-num'),
Expand Down Expand Up @@ -366,7 +395,7 @@ var ajaxCart = (function(module, $) {
if (isUpdating) {
return;
}

var $el = $(this),
line = $el.data('line'),
qty = parseInt($el.val().replace(/\D/g, ''));
Expand Down
14 changes: 13 additions & 1 deletion assets/timber.js.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ timber.cacheSelectors = function () {
timber.cache = {
// General
$html : $('html'),
$body : $('body'),
$body : $(document.body),

// Navigation
$navigation : $('#AccessibleNav'),
Expand Down Expand Up @@ -401,6 +401,9 @@ timber.Drawers = (function () {
return this.close();
}

// Notify the drawer is going to open
timber.cache.$body.trigger('beforeDrawerOpen.timber', this);

// Add is-transitioning class to moved elements on open so drawer can have
// transition for close animation
this.$nodes.moved.addClass('is-transitioning');
Expand Down Expand Up @@ -432,13 +435,19 @@ timber.Drawers = (function () {
this.close();
return false;
}, this));

// Notify the drawer has opened
timber.cache.$body.trigger('afterDrawerOpen.timber', this);
};

Drawer.prototype.close = function () {
if (!this.drawerIsOpen) { // don't close a closed drawer
return;
}

// Notify the drawer is going to close
timber.cache.$body.trigger('beforeDrawerClose.timber', this);

// deselect any focused form elements
$(document.activeElement).trigger('blur');

Expand All @@ -454,6 +463,9 @@ timber.Drawers = (function () {
this.removeTrapFocus(this.$drawer, 'drawer_focus');

this.$nodes.page.off('.drawer');

// Notify the drawer is closed now
timber.cache.$body.trigger('afterDrawerClose.timber', this);
};

Drawer.prototype.trapFocus = function ($container, eventNamespace) {
Expand Down
4 changes: 2 additions & 2 deletions layout/theme.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,8 @@
});
});

jQuery('body').on('ajaxCart.afterCartLoad', function(evt, cart) {
// Bind to 'ajaxCart.afterCartLoad' to run any javascript after the cart has loaded in the DOM
jQuery(document.body).on('afterCartLoad.ajaxCart', function(evt, cart) {
// Bind to 'afterCartLoad.ajaxCart' to run any javascript after the cart has loaded in the DOM
timber.RightDrawer.open();
});
</script>
Expand Down