Check for valid indices when reordering favorites

This commit is contained in:
Quincy Morgan
2019-03-07 11:22:19 -05:00
parent c3d1dc43d4
commit efac17810c
+10 -1
View File
@@ -312,10 +312,13 @@ export function coreContext() {
/* Presets */
var presets;
context.presets = function() { return presets; };
//get favorites from local storage
context.getFavoritePresets = function() {
// get favorites from local storage
var favs = JSON.parse(context.storage('favorite_presets')) || [];
return favs.filter(function(d) {
// iD's presets could have changed since this favorite was saved,
// so make sure it's still valid.
var preset = presets.item(d.id);
if (preset === null) {
return false;
@@ -358,7 +361,13 @@ export function coreContext() {
});
};
context.moveFavoritePreset = function(fromIndex, toIndex) {
if (fromIndex === toIndex) return;
var favs = context.getFavoritePresets();
if (fromIndex < 0 || toIndex < 0 ||
fromIndex >= favs.length || toIndex >= favs.length) return;
favs.splice(toIndex, 0, favs.splice(fromIndex, 1)[0]);
setFavoritePresets(favs);
};