pitaya-launcher/src/store/reducers/profile.js

112 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-09-11 16:25:45 +02:00
var _ = require('lodash');
var actions = require('../actions');
2015-09-17 12:04:33 +02:00
var tree = require('../../util/tree');
module.exports = function(oldProfile, action) {
var newProfile = oldProfile || { items: [] };
switch(action.type) {
2015-09-17 17:29:59 +02:00
case actions.common.LOAD_PROFILE_SUCCESS:
newProfile = _.cloneDeep(action.profile);
break;
2015-09-11 16:25:45 +02:00
case actions.edit.MOVE_PROFILE_ITEM:
newProfile = moveProfileItem(oldProfile, action.movedItem, action.targetItem);
break;
2015-09-11 16:25:45 +02:00
2015-10-10 18:44:31 +02:00
case actions.edit.REMOVE_PROFILE_ITEM:
newProfile = removeProfileItem(oldProfile, action.removedItem);
break;
2015-10-10 18:44:31 +02:00
2015-09-11 16:25:45 +02:00
case actions.edit.ADD_PROFILE_ITEM:
newProfile = addProfileItem(oldProfile, action.newItem, action.targetItem);
break;
2015-09-11 16:25:45 +02:00
2015-09-16 17:26:56 +02:00
case actions.edit.UPDATE_PROFILE_ITEM:
newProfile = updateProfileItem(oldProfile, action.item, action.key, action.value);
break;
2015-09-16 17:26:56 +02:00
case actions.edit.SELECT_PROFILE_ITEM:
newProfile = selectProfileItem(oldProfile, action.item);
break;
}
if(newProfile) tree.walk(newProfile, ensureItemKey);
return newProfile;
2015-09-11 16:25:45 +02:00
};
2015-09-16 17:26:56 +02:00
function selectProfileItem(oldProfile, item) {
var newProfile = _.cloneDeep(oldProfile);
2015-09-17 12:04:33 +02:00
tree.walk(newProfile, function(currentItem) {
delete currentItem.selected;
if( _.isEqual(currentItem, item) ) {
currentItem.selected = true;
}
});
2015-09-16 17:26:56 +02:00
return newProfile;
}
function updateProfileItem(oldProfile, targetItem, key, value) {
var newProfile = _.cloneDeep(oldProfile);
2015-09-17 12:04:33 +02:00
var item = tree.find(newProfile, targetItem).item;
item[key] = value;
2015-09-16 17:26:56 +02:00
return newProfile;
}
2015-10-10 18:44:31 +02:00
function removeProfileItem(oldProfile, removedItem) {
var newProfile = _.cloneDeep(oldProfile);
var parent = tree.find(newProfile, removedItem).parent;
parent.items = _.reject(parent.items, function(item) {
return _.isEqual(item, removedItem);
});
return newProfile;
}
2015-09-11 16:25:45 +02:00
function moveProfileItem(oldProfile, movedItem, targetItem) {
var newProfile = _.cloneDeep(oldProfile);
2015-09-17 12:04:33 +02:00
var previousParent = tree.find(newProfile, movedItem).parent;
var newParent = tree.find(newProfile, targetItem).item;
2015-09-11 16:25:45 +02:00
previousParent.items = _.reject(previousParent.items, function(item) {
return _.isEqual(item, movedItem);
});
newParent.items = newParent.items || [];
newParent.items.push(_.cloneDeep(movedItem));
return newProfile;
2015-09-11 16:25:45 +02:00
}
function addProfileItem(oldProfile, newItem, targetItem) {
var newProfile = _.cloneDeep(oldProfile);
2015-09-17 12:04:33 +02:00
var newParent = tree.find(newProfile, targetItem).item;
2015-09-11 16:25:45 +02:00
newParent.items = newParent.items || [];
2015-09-16 17:26:56 +02:00
2015-09-17 12:04:33 +02:00
newItem = _.cloneDeep(newItem);
ensureItemKey(newItem);
2015-09-16 17:26:56 +02:00
2015-09-17 12:04:33 +02:00
newParent.items.push(newItem);
2015-09-16 17:26:56 +02:00
2015-09-17 12:04:33 +02:00
return newProfile;
2015-09-16 17:26:56 +02:00
}
2015-09-17 12:04:33 +02:00
var _inc = 0;
function ensureItemKey(item) {
if( item && !('_key' in item) ) {
item._key = 'item_'+Date.now()+'_'+_inc++;
2015-09-11 16:25:45 +02:00
}
}