Edition de l'arbre profile basique

This commit is contained in:
wpetit 2015-09-17 12:04:33 +02:00
parent a2f0a03671
commit 6a790cc5bd
3 changed files with 79 additions and 50 deletions

View File

@ -4,6 +4,7 @@ var ProfileTree = require('./profile-tree.jsx');
var DesktopAppList = require('./desktop-app-list.jsx'); var DesktopAppList = require('./desktop-app-list.jsx');
var ItemForm = require('./item-form.jsx'); var ItemForm = require('./item-form.jsx');
var IconThemeSelector = require('./icon-theme-selector.jsx'); var IconThemeSelector = require('./icon-theme-selector.jsx');
var tree = require('../../util/tree');
var actions = require('../../store/actions'); var actions = require('../../store/actions');
var DragDropContext = require('react-dnd').DragDropContext; var DragDropContext = require('react-dnd').DragDropContext;
@ -65,7 +66,7 @@ function select(state) {
desktopApps: state.desktopApps, desktopApps: state.desktopApps,
profile: state.profile, profile: state.profile,
theme: state.theme, theme: state.theme,
selectedItem: state.selectedItem selectedItem: tree.matches(state.profile, {selected: true})[0]
}; };
} }

View File

@ -1,12 +1,15 @@
var _ = require('lodash'); var _ = require('lodash');
var actions = require('../actions'); var actions = require('../actions');
var tree = require('../../util/tree');
module.exports = function(oldProfile, action) { module.exports = function(oldProfile, action) {
switch(action.type) { switch(action.type) {
case actions.launcher.LOAD_PROFILE_SUCCESS: case actions.launcher.LOAD_PROFILE_SUCCESS:
return _.cloneDeep(action.profile); var newProfile = _.cloneDeep(action.profile);
tree.walk(newProfile, ensureItemKey);
return newProfile;
case actions.edit.MOVE_PROFILE_ITEM: case actions.edit.MOVE_PROFILE_ITEM:
return moveProfileItem(oldProfile, action.movedItem, action.targetItem); return moveProfileItem(oldProfile, action.movedItem, action.targetItem);
@ -29,14 +32,19 @@ module.exports = function(oldProfile, action) {
function selectProfileItem(oldProfile, item) { function selectProfileItem(oldProfile, item) {
var newProfile = _.cloneDeep(oldProfile); var newProfile = _.cloneDeep(oldProfile);
tree.walk(newProfile, function(currentItem) {
delete currentItem.selected;
if( _.isEqual(currentItem, item) ) {
currentItem.selected = true;
}
});
return newProfile; return newProfile;
} }
function updateProfileItem(oldProfile, targetItem, key, value) { function updateProfileItem(oldProfile, targetItem, key, value) {
var newProfile = _.cloneDeep(oldProfile); var newProfile = _.cloneDeep(oldProfile);
var result = treeFind(newProfile, targetItem); var item = tree.find(newProfile, targetItem).item;
result.item[key] = value; item[key] = value;
return newProfile; return newProfile;
} }
@ -44,8 +52,8 @@ function updateProfileItem(oldProfile, targetItem, key, value) {
function moveProfileItem(oldProfile, movedItem, targetItem) { function moveProfileItem(oldProfile, movedItem, targetItem) {
var newProfile = _.cloneDeep(oldProfile); var newProfile = _.cloneDeep(oldProfile);
var previousParent = treeFind(newProfile, movedItem).parent; var previousParent = tree.find(newProfile, movedItem).parent;
var newParent = treeFind(newProfile, targetItem).item; var newParent = tree.find(newProfile, targetItem).item;
previousParent.items = _.reject(previousParent.items, function(item) { previousParent.items = _.reject(previousParent.items, function(item) {
return _.isEqual(item, movedItem); return _.isEqual(item, movedItem);
@ -61,54 +69,21 @@ function moveProfileItem(oldProfile, movedItem, targetItem) {
function addProfileItem(oldProfile, newItem, targetItem) { function addProfileItem(oldProfile, newItem, targetItem) {
var newProfile = _.cloneDeep(oldProfile); var newProfile = _.cloneDeep(oldProfile);
var newParent = treeFind(newProfile, targetItem).item; var newParent = tree.find(newProfile, targetItem).item;
newParent.items = newParent.items || []; newParent.items = newParent.items || [];
newParent.items.push(_.cloneDeep(newItem));
newItem = _.cloneDeep(newItem);
ensureItemKey(newItem);
newParent.items.push(newItem);
return newProfile; return newProfile;
} }
// Tree manipulation helpers var _inc = 0;
function ensureItemKey(item) {
function treeWalk(branch, func) { if( item && !('_key' in item) ) {
item._key = 'item_'+Date.now()+'_'+_inc++;
var items = branch.items;
if(!items) return;
for( var i = 0, item = items[i]; (item = items[i]); i++ ) {
var breakHere = func(item, parent);
if(breakHere) return breakHere;
if(item.items) {
breakHere = treeWalk(item, func);
if(breakHere) return breakHere;
} }
}
}
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;
}
}
} }

53
js/util/tree.js Normal file
View File

@ -0,0 +1,53 @@
var _ = require('lodash');
// Tree manipulation helpers
exports.walk = function(branch, func, parent) {
if(!branch) return;
var breakHere = func(branch, parent);
if(breakHere) return breakHere;
var items = branch.items;
if(!items) return;
for( var i = 0, item = items[i]; (item = items[i]); i++ ) {
breakHere = exports.walk(item, func, branch);
if(breakHere) return breakHere;
}
};
exports.find = function(tree, obj) {
var result;
exports.walk(tree, function(item, parent) {
if( _.isEqual(item, obj) ) {
result = {item: item, parent: parent};
return true;
}
});
return result;
};
exports.matches = function(tree, obj) {
var results = [];
var matches = _.matches(obj);
exports.walk(tree, function(item) {
if( matches(item) ) {
results.push(item);
}
});
return results;
};