Edition via drag & drop des items
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
var actions = require('../../actions');
|
||||
var actions = require('../actions');
|
||||
|
||||
module.exports = function(state, action) {
|
||||
|
||||
var desktopApps = [];
|
||||
var desktopApps = state || [];
|
||||
|
||||
if( action.type === actions.edit.LOAD_DESKTOP_APPS_SUCCESS ) {
|
||||
desktopApps = action.desktopApps;
|
||||
|
@ -1,17 +1,72 @@
|
||||
var actions = require('../../actions');
|
||||
var _ = require('lodash');
|
||||
var actions = require('../actions');
|
||||
|
||||
module.exports = function(oldProfile, action) {
|
||||
|
||||
var newProfile = oldProfile || null;
|
||||
|
||||
switch(action.type) {
|
||||
|
||||
case actions.launcher.LOAD_PROFILE_SUCCESS:
|
||||
newProfile = action.profile;
|
||||
break;
|
||||
return _.cloneDeep(action.profile);
|
||||
|
||||
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);
|
||||
|
||||
default:
|
||||
return oldProfile || null;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
function moveProfileItem(oldProfile, movedItem, targetItem) {
|
||||
|
||||
var newProfile = _.cloneDeep(oldProfile);
|
||||
var previousParent = treeFind(newProfile, movedItem).parent;
|
||||
var newParent = treeFind(newProfile, targetItem).item;
|
||||
|
||||
previousParent.items = _.reject(previousParent.items, function(item) {
|
||||
return _.isEqual(item, movedItem);
|
||||
});
|
||||
|
||||
newParent.items = newParent.items || [];
|
||||
newParent.items.push(_.cloneDeep(movedItem));
|
||||
|
||||
return newProfile;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
function addProfileItem(oldProfile, newItem, targetItem) {
|
||||
|
||||
var newProfile = _.cloneDeep(oldProfile);
|
||||
var newParent = treeFind(newProfile, targetItem).item;
|
||||
|
||||
newParent.items = newParent.items || [];
|
||||
newParent.items.push(_.cloneDeep(newItem));
|
||||
|
||||
return newProfile;
|
||||
}
|
||||
|
||||
function treeFind(branch, obj) {
|
||||
|
||||
var items = branch.items;
|
||||
|
||||
if(!items) return;
|
||||
|
||||
for( var i = 0, item = items[i]; (item = items[i]); i++ ) {
|
||||
|
||||
if( _.isEqual(item, obj) ) {
|
||||
return {item: item, parent: branch};
|
||||
}
|
||||
|
||||
if(item.items) {
|
||||
var result = treeFind(item, obj);
|
||||
if(result) return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user