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');
|
2015-09-04 12:10:08 +02:00
|
|
|
|
|
|
|
module.exports = function(oldProfile, action) {
|
|
|
|
|
|
|
|
switch(action.type) {
|
|
|
|
|
2015-09-17 17:29:59 +02:00
|
|
|
case actions.common.LOAD_PROFILE_SUCCESS:
|
2015-09-17 12:04:33 +02:00
|
|
|
var newProfile = _.cloneDeep(action.profile);
|
|
|
|
tree.walk(newProfile, ensureItemKey);
|
|
|
|
return newProfile;
|
2015-09-11 16:25:45 +02:00
|
|
|
|
|
|
|
case actions.edit.MOVE_PROFILE_ITEM:
|
|
|
|
return moveProfileItem(oldProfile, action.movedItem, action.targetItem);
|
|
|
|
|
|
|
|
case actions.edit.ADD_PROFILE_ITEM:
|
|
|
|
return addProfileItem(oldProfile, action.newItem, action.targetItem);
|
|
|
|
|
2015-09-16 17:26:56 +02:00
|
|
|
case actions.edit.UPDATE_PROFILE_ITEM:
|
|
|
|
return updateProfileItem(oldProfile, action.item, action.key, action.value);
|
|
|
|
|
|
|
|
case actions.edit.SELECT_PROFILE_ITEM:
|
|
|
|
return selectProfileItem(oldProfile, action.item);
|
|
|
|
|
2015-09-11 16:25:45 +02:00
|
|
|
default:
|
|
|
|
return oldProfile || null;
|
2015-09-04 12:10:08 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
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-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));
|
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|