first attempt at a working sortable list

This commit is contained in:
Avital Oliver
2013-08-01 16:27:59 -07:00
parent a1404b7a70
commit 11feb8800a
2 changed files with 38 additions and 19 deletions

View File

@@ -1,8 +1,3 @@
Items = new Meteor.Collection(null);
Items.insert({ text: 'Foo Greenspan' });
Items.insert({ text: 'Bar Oliver' });
Items.insert({ text: 'Beef Tofu' });
Meteor.startup(function () {
/*
Meteor.setTimeout(function () {
@@ -15,24 +10,37 @@ Meteor.startup(function () {
UI.body.name = 'David';
UI.body.items = Items.find({}, { sort: { text: 1 }});
UI.body.items = Items.find({}, { sort: { rank: 1 }});
// xcxc rendered didn't work and was surprising.
UI.body.attached = function () {
$('#list').sortable();
};
$('#list').sortable({
stop: function (event, ui) {
// xcxc use jQuery .prev() instead of previousElementSibling
// TO REPRO FAILURE:
// 1. Move "Bar Oliver" to the bottom of the list
// 2. Call `funkyReorder()`.
var funkyIteration = 0;
funkyReorder = function () {
var chars = "THEMAGICALMACHINETHATDOESNTBREAKNOMATTERWHATYOUDOWITHIT";
Items.find({}, {sort: {text: 1}}).forEach(function (item) {
Items.update(item._id, {
text: chars[funkyIteration++] + item.text.slice(1)
});
el = ui.item[0]; // unbox jQuery array
var before = el.previousElementSibling;
var after = el.nextElementSibling;
var doc = el.$ui.data();
// xcxc improve rank generation.
var newRank;
if (before === null) { // moving to the top of the list
var firstDoc = after.$ui.data();
newRank = firstDoc.rank-1;
} else if (after === null) { // moving to the bottom of the list
var lastDoc = before.$ui.data();
newRank = lastDoc.rank+1;
} else {
var beforeDoc = before.$ui.data();
var afterDoc = after.$ui.data();
newRank = (beforeDoc.rank + afterDoc.rank) / 2;
}
Items.update(doc._id, {$set: {rank: newRank}});
}
});
};
};

View File

@@ -0,0 +1,11 @@
Items = new Meteor.Collection("items");
if (Meteor.isServer) {
Meteor.startup(function () {
if (Items.find().count() === 0) {
Items.insert({ text: 'Foo Greenspan', rank: 1 });
Items.insert({ text: 'Bar Oliver', rank: 2 });
Items.insert({ text: 'Beef Tofu', rank: 3 });
}
});
}