Skip to content

Instantly share code, notes, and snippets.

@ColinCampbell
Created May 17, 2010 23:30
Show Gist options
  • Save ColinCampbell/404376 to your computer and use it in GitHub Desktop.
Save ColinCampbell/404376 to your computer and use it in GitHub Desktop.
deleteVersion: function () {
var version = this.get('version'),
arrangedObjects = ContentManager.versionsController.get('arrangedObjects'),
idx = arrangedObjects.indexOf(version);
version.destroy();
version.commitRecord();
this.invokeLater(function () {
var object, test;
// start from the previous item
idx = idx - 1;
var indices = SC.IndexSet.create().addEach(arrangedObjects.map(function(item, index) {
return !item['isGroup'] ? index : null;
}));
// easy, previous object is selectable
if (indices.contains(idx)) {
object = arrangedObjects.objectAt(idx);
}
// previous object not in range
// check next object ahead for next idx
// will only return true if it was in same group
else if (indices.contains(idx + 1)) {
object = arrangedObjects.objectAt(idx + 1);
}
// prefer indices from previous groups
else if ((test = indices.indexBefore(idx)) > -1) {
object = arrangedObjects.objectAt(test);
}
// check for indices after, this is our last resort
else if ((test = indices.indexAfter(idx)) > -1) {
object = arrangedObjects.objectAt(test);
}
// nothing selectable
else {
object = null;
}
ContentManager.versionsController.selectObject(object);
});
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment