Skip to content

Commit

Permalink
Simplify updateEntity function (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentfretin authored Aug 29, 2024
1 parent 2669b8b commit 528d92a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
13 changes: 6 additions & 7 deletions src/components/components/PropertyRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ export default class PropertyRow extends React.Component {
entity: props.entity,
isSingle: props.isSingle,
name: props.name,
// Wrap updateEntity for tracking.
onChange: function (name, value) {
var propertyName = props.componentname;
if (!props.isSingle) {
propertyName += '.' + props.name;
}

updateEntity.apply(this, [props.entity, propertyName, value]);
updateEntity(
props.entity,
props.componentname,
!props.isSingle ? props.name : '',
value
);
},
value: value
};
Expand Down
33 changes: 12 additions & 21 deletions src/lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,30 @@ import { equal } from './utils';
* Update a component.
*
* @param {Element} entity - Entity to modify.
* @param {string} propertyName - component or component.property
* @param {string|number} value - New value.
* @param {string} component - component name
* @param {string} property - property name, use empty string if component is single property or if value is an object
* @param {string|number|object} value - New value.
*/
export function updateEntity(entity, propertyName, value) {
var splitName;

if (propertyName.indexOf('.') !== -1) {
// Multi-prop
splitName = propertyName.split('.');

export function updateEntity(entity, component, property, value) {
if (property) {
if (value === null || value === undefined) {
// Remove property.
entity.removeAttribute(splitName[0], splitName[1]);
entity.removeAttribute(component, property);
} else {
// Set property.
entity.setAttribute(splitName[0], splitName[1], value);
entity.setAttribute(component, property, value);
}
} else {
if (value === null || value === undefined) {
// Remove property.
entity.removeAttribute(propertyName);
// Remove component.
entity.removeAttribute(component);
} else {
// Set property.
entity.setAttribute(propertyName, value);
// Set component.
entity.setAttribute(component, value);
}
}

Events.emit('entityupdate', {
component: splitName ? splitName[0] : propertyName,
entity: entity,
property: splitName ? splitName[1] : '',
value: value
});
Events.emit('entityupdate', { entity, component, property, value });
}

/**
Expand Down

0 comments on commit 528d92a

Please sign in to comment.