copyObj = (obj, name, id) => {
const trees = obj.trees;
const names = obj.names;
obj[name].map(item => {
const newId = uuid();
trees.map(tree => {
if (tree.tree_type === 'obj_tree') {
if (item.id === tree.obj_tree.parrent_element) {
tree.obj_tree.parrent_element = newId;
}
}
if (tree.tree_type === 'obj_feature_tree') {
if (item.id === tree.obj_feature_tree.parrent_element) {
tree.obj_feature_tree.parrent_element = newId;
}
}
if (tree.tree_type === 'regexp_tree') {
if (item.id === tree.regexp_tree.parrent_element) {
tree.regexp_tree.parrent_element = newId;
}
}
});
names.map(name => {
if (name.name_type === 'unmark_name') {
if (item.id === name.unmark_name.parrent_element) {
name.unmark_name.parrent_element = newId;
}
}
if (name.name_type === 'shift_name') {
if (item.id === name.shift_name.parrent_element) {
name.shift_name.parrent_element = newId;
}
if (item.id === name.shift_name.parrent_element_from) {
name.shift_name.parrent_element_from = newId;
}
if (item.id === name.shift_name.parrent_element_to) {
name.shift_name.parrent_element_to = newId;
}
}
if (name.name_type === 'fill_name') {
if (item.id === name.fill_name.parrent_element) {
name.fill_name.parrent_element = newId;
}
name.fill_name.feature_values.map(val => {
if (val.value_type === 'dynamic') {
if (item.id === val.dvalue.parrent_element) {
val.dvalue.parrent_element = newId;
}
}
});
}
});
});
return {
trees: trees,
names: names,
};
};
const treeFieldsByType = {
obj_tree: 'obj_tree',
obj_feature_tree: 'obj_feature_tree',
regexp_tree: 'regexp_tree',
};
trees.map((tree) => {
const fieldName = treeFieldsByType[tree.tree_type];
if (item.id === tree[fieldName].parrent_element) {
tree[fieldName].parrent_element = newId;
}
});
const copyObj = (obj, name, id) => {
const { trees, names } = obj;
const type_array_adder = ( array, suffix ) => array.map( type => type + '_' + suffix );
const types = {
tree: type_array_adder( [ 'obj', 'obj_feature', 'regexp' ], 'tree' ),
name: type_array_adder( [ 'unmark', 'shift', 'fill' ], 'name' )
};
const id_setter = ( item, obj, prop, ID ) => {
if ( item.id === obj[ prop ] ){
obj[ prop ] = ID;
}
}
const parent_id_setter = ( item, obj, type, ID ) => {
if (obj.name_type === type || obj.tree_type === type) {
id_setter( item, obj[ type ], 'parent_element', ID );
}
};
obj[name].map(item => {
const newId = uuid();
trees.map(tree => {
types['tree'].map( type => {
parent_id_setter( item, tree, type, newId );
});
});
names.map(name => {
types['name'].map( type => {
parent_id_setter( item, name, type, newId );
if (name.name_type === 'shift_name') {
id_setter( name[ type ], 'parent_element_from', newId );
id_setter( name[ type ], 'parent_element_to', newId );
}
if (name.name_type === 'fill_name') {
name[ type ].feature_values.map(val => {
if (val.value_type === 'dynamic') {
id_setter( item, val.dvalue, 'parent_element', newId );
}
});
}
});
});
});
return {
trees,
names
};
};
copyObj = (obj, name, id) => {
const trees = obj.trees;
const names = obj.names;
obj[name].forEach(item => {
const newId = uuid();
trees.forEach(tree => {
if (tree.tree_type !== 'obj_tree' || tree.tree_type !== 'obj_feature_tree' || tree.tree_type !== 'regexp_tree') { return; }
if (item.id === tree[tree.tree_type].parrent_element) {
tree[tree.tree_type].parrent_element = newId;
}
});
names.forEach(name => {
if (name.name_type !== 'unmark_name' || name.name_type !== 'shift_name' || name.name_type !== 'fill_name') { return; }
if (item.id === name[name.name_type].parrent_element) {
name[name.name_type].parrent_element = newId;
}
switch (name.name_type) {
case 'shift_name':
['parrent_element_from', 'parrent_element_to'].forEach(parrent_element => {
if (item.id === name.shift_name[parrent_element]) {
name.shift_name[parrent_element] = newId;
}
});
break;
case 'fill_name':
name.fill_name.feature_values.forEach(val => {
if (val.value_type === 'dynamic' && item.id === val.dvalue.parrent_element) {
val.dvalue.parrent_element = newId;
}
});
break;
}
});
});
return {
trees: trees,
names: names,
};
};