Skip to content

Commit

Permalink
Merge pull request #3754 from penrosehill/js-expose-refresh-sortable-…
Browse files Browse the repository at this point in the history
…tables

Expose js function: Spree.SortableTable.refresh
  • Loading branch information
aldesantis authored Sep 18, 2020
2 parents a36a825 + d46d1bc commit 3c6f339
Showing 1 changed file with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
//= require solidus_admin/Sortable
/* eslint no-unused-vars: "off" */

Spree.ready(function() {
var sortable_tables = document.querySelectorAll('table.sortable');
Spree.SortableTable = {
refresh: function() {
var sortable_tables = document.querySelectorAll('table.sortable');

_.each(sortable_tables, function(table) {
var url = table.getAttribute('data-sortable-link');
var tbody = table.querySelector('tbody');
var sortable = Sortable.create(tbody,{
handle: ".handle",
onEnd: function(e) {
var positions = {};
_.each(e.to.querySelectorAll('tr'), function(el, index) {
var idAttr = el.id;
if (idAttr) {
var objId = idAttr.split('_').slice(-1);
if (!isNaN(objId)) {
positions['positions['+objId+']'] = index + 1;
_.each(sortable_tables, function(table) {
var url = table.getAttribute('data-sortable-link');
var tbody = table.querySelector('tbody');
var sortable = Sortable.create(tbody,{
handle: ".handle",
onEnd: function(e) {
var positions = {};
_.each(e.to.querySelectorAll('tr'), function(el, index) {
var idAttr = el.id;
if (idAttr) {
var objId = idAttr.split('_').slice(-1);
if (!isNaN(objId)) {
positions['positions['+objId+']'] = index + 1;
}
}
}
});
Spree.ajax({
type: 'POST',
dataType: 'json',
url: url,
data: positions,
});
}
});
Spree.ajax({
type: 'POST',
dataType: 'json',
url: url,
data: positions,
});
}
});
});
});
});
}
};

Spree.ready(Spree.SortableTable.refresh);

0 comments on commit 3c6f339

Please sign in to comment.