Edition de l'arbre profile basique
This commit is contained in:
parent
a2f0a03671
commit
6a790cc5bd
|
@ -4,6 +4,7 @@ var ProfileTree = require('./profile-tree.jsx');
|
|||
var DesktopAppList = require('./desktop-app-list.jsx');
|
||||
var ItemForm = require('./item-form.jsx');
|
||||
var IconThemeSelector = require('./icon-theme-selector.jsx');
|
||||
var tree = require('../../util/tree');
|
||||
|
||||
var actions = require('../../store/actions');
|
||||
var DragDropContext = require('react-dnd').DragDropContext;
|
||||
|
@ -65,7 +66,7 @@ function select(state) {
|
|||
desktopApps: state.desktopApps,
|
||||
profile: state.profile,
|
||||
theme: state.theme,
|
||||
selectedItem: state.selectedItem
|
||||
selectedItem: tree.matches(state.profile, {selected: true})[0]
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
var _ = require('lodash');
|
||||
var actions = require('../actions');
|
||||
var tree = require('../../util/tree');
|
||||
|
||||
module.exports = function(oldProfile, action) {
|
||||
|
||||
switch(action.type) {
|
||||
|
||||
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:
|
||||
return moveProfileItem(oldProfile, action.movedItem, action.targetItem);
|
||||
|
@ -29,14 +32,19 @@ module.exports = function(oldProfile, action) {
|
|||
|
||||
function selectProfileItem(oldProfile, item) {
|
||||
var newProfile = _.cloneDeep(oldProfile);
|
||||
|
||||
tree.walk(newProfile, function(currentItem) {
|
||||
delete currentItem.selected;
|
||||
if( _.isEqual(currentItem, item) ) {
|
||||
currentItem.selected = true;
|
||||
}
|
||||
});
|
||||
return newProfile;
|
||||
}
|
||||
|
||||
function updateProfileItem(oldProfile, targetItem, key, value) {
|
||||
var newProfile = _.cloneDeep(oldProfile);
|
||||
var result = treeFind(newProfile, targetItem);
|
||||
result.item[key] = value;
|
||||
var item = tree.find(newProfile, targetItem).item;
|
||||
item[key] = value;
|
||||
return newProfile;
|
||||
}
|
||||
|
||||
|
@ -44,8 +52,8 @@ function updateProfileItem(oldProfile, targetItem, key, value) {
|
|||
function moveProfileItem(oldProfile, movedItem, targetItem) {
|
||||
|
||||
var newProfile = _.cloneDeep(oldProfile);
|
||||
var previousParent = treeFind(newProfile, movedItem).parent;
|
||||
var newParent = treeFind(newProfile, targetItem).item;
|
||||
var previousParent = tree.find(newProfile, movedItem).parent;
|
||||
var newParent = tree.find(newProfile, targetItem).item;
|
||||
|
||||
previousParent.items = _.reject(previousParent.items, function(item) {
|
||||
return _.isEqual(item, movedItem);
|
||||
|
@ -61,54 +69,21 @@ function moveProfileItem(oldProfile, movedItem, targetItem) {
|
|||
function addProfileItem(oldProfile, newItem, targetItem) {
|
||||
|
||||
var newProfile = _.cloneDeep(oldProfile);
|
||||
var newParent = treeFind(newProfile, targetItem).item;
|
||||
var newParent = tree.find(newProfile, targetItem).item;
|
||||
|
||||
newParent.items = newParent.items || [];
|
||||
newParent.items.push(_.cloneDeep(newItem));
|
||||
|
||||
newItem = _.cloneDeep(newItem);
|
||||
ensureItemKey(newItem);
|
||||
|
||||
newParent.items.push(newItem);
|
||||
|
||||
return newProfile;
|
||||
}
|
||||
|
||||
// Tree manipulation helpers
|
||||
|
||||
function treeWalk(branch, func) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
var _inc = 0;
|
||||
function ensureItemKey(item) {
|
||||
if( item && !('_key' in item) ) {
|
||||
item._key = 'item_'+Date.now()+'_'+_inc++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
};
|
Loading…
Reference in New Issue