Skip to content

Instantly share code, notes, and snippets.

@fritzmg
Last active August 27, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fritzmg/89356e72b3fe1d2c9a95 to your computer and use it in GitHub Desktop.
Save fritzmg/89356e72b3fe1d2c9a95 to your computer and use it in GitHub Desktop.
Contao Isotope cart AJAX actions
(function($)
{
// or use https://github.com/fczbkk/css-selector-generator instead
$.fn.getPath = function () {
if (this.length != 1) throw 'Requires one element.';
var path, node = this;
while (node.length) {
var realNode = node[0], name = realNode.localName;
if (!name) break;
name = name.toLowerCase();
if (realNode.id) {
// As soon as an id is found, there's no need to specify more.
return name + '#' + realNode.id + (path ? '>' + path : '');
} else if (realNode.className) {
name += '.' + realNode.className.split(/\s+/).join('.');
}
var parent = node.parent(), siblings = parent.children(name);
if (siblings.length > 1) name += ':eq(' + siblings.index(node) + ')';
path = name + (path ? '>' + path : '');
node = parent;
}
return path;
};
var setEventListeners = function()
{
// set event listeners to delete links
$('.mod_iso_cart td.remove a').click( function(e)
{
e.preventDefault();
$.get( $(this).attr('href'), function() { updateCart( window.location.href ); });
return false;
});
// set event listeners to the update buttons in all carts
$('.mod_iso_cart').each( function()
{
var $form = $(this);
$form.find('.button_update').click( function(e)
{
e.preventDefault();
$form.ajaxSubmit({ success: function() { updateCart( window.location.href ); } });
return false;
});
});
};
var updateCart = function( href )
{
// update each cart using the href
$('.mod_iso_cart').each( function() { $(this).load( href + ' ' + $(this).getPath() + ' > *' ); });
// set the event listeners again
setEventListeners();
};
$(document).ready( function()
{
setEventListeners();
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment