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

105 lines
2.6 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) {
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);
2015-10-10 18:44:31 +02:00
case actions.edit.REMOVE_PROFILE_ITEM:
return removeProfileItem(oldProfile, action.removedItem);
2015-09-11 16:25:45 +02:00
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-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
}
}