Skip to content

Commit

Permalink
https://github.com/ioBroker/ioBroker.admin/issues/1658
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Aug 29, 2022
1 parent 6845d3a commit 3f0cf42
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src-rx/src/components/Object/ObjectCustomEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class ObjectCustomEditor extends Component {
}

getCustomTemplate(adapter) {
const ad = this.props.objects['system.adapter.' + adapter] ? JSON.parse(JSON.stringify(this.props.objects['system.adapter.' + adapter])) : null;
const ad = this.props.objects[`system.adapter.${adapter}`] ? JSON.parse(JSON.stringify(this.props.objects[`system.adapter.${adapter}`])) : null;

if (!ad) {
console.error(`Cannot find adapter "${ad}"`);
Expand Down Expand Up @@ -264,8 +264,7 @@ class ObjectCustomEditor extends Component {
try {
// eslint-disable-next-line no-new-func
const f = new Function('data', 'originalData', '_system', 'instanceObj', 'customObj', '_socket', func.includes('return') ? func : 'return ' + func);
const result = f(data || this.props.data, this.props.originalData, this.props.systemConfig, instanceObj, customObj, this.props.socket);
data[attr] = result;
data[attr] = f(data || this.props.data, this.props.originalData, this.props.systemConfig, instanceObj, customObj, this.props.socket);
} catch (e) {
console.error(`Cannot execute ${func}: ${e}`);
data[attr] = !items[attr] || items[attr].default === undefined ? null: items[attr].default;
Expand Down Expand Up @@ -302,6 +301,7 @@ class ObjectCustomEditor extends Component {
defaultValues[attr] = items[attr].default;
}
});

// now init default that must be calculated
attrs.forEach(async attr => {
if (items[attr].defaultFunc) {
Expand Down Expand Up @@ -333,6 +333,10 @@ class ObjectCustomEditor extends Component {

if (custom) {
Object.keys(custom).forEach(_attr => {
// remove all temporary values
if (_attr.startsWith('_')) {
return;
}
if (commons[inst][_attr] === undefined) {
commons[inst][_attr] = custom[_attr];
} else if (commons[inst][_attr] !== custom[_attr]) {
Expand All @@ -351,6 +355,9 @@ class ObjectCustomEditor extends Component {
_default.enabled = false;

Object.keys(_default).forEach(_attr => {
if (_attr.startsWith('_')) {
return;
}
if (commons[inst][_attr] === undefined) {
commons[inst][_attr] = _default[_attr];
} else if (commons[inst][_attr] !== _default[_attr]) {
Expand Down Expand Up @@ -583,11 +590,11 @@ class ObjectCustomEditor extends Component {
// save all objects
const keys = Object.keys(_objects);
if (!keys.length) {
this.setState({maxOids: null}, () =>
this.setState({ maxOids: null }, () =>
this.props.onProgress(false));
cb && cb();
} else {
this.setState({progress: Math.round(((this.state.maxOids - keys.length) / this.state.maxOids) * 50) + 50});
this.setState({ progress: Math.round(((this.state.maxOids - keys.length) / this.state.maxOids) * 50) + 50 });
const id = keys.shift();
if (JSON.stringify(_objects[id].common) !== JSON.stringify(_oldObjects[id].common)) {
!this.changedIds.includes(id) && this.changedIds.push(id);
Expand All @@ -613,12 +620,12 @@ class ObjectCustomEditor extends Component {
} else {
const maxOids = this.state.maxOids || ids.length;
if (this.state.maxOids === null) {
this.setState({maxOids: ids.length}, () =>
this.setState({ maxOids: ids.length }, () =>
this.props.onProgress(true));
}

// 0 - 50
this.setState({progress: Math.round(((maxOids - ids.length) / maxOids) * 50)});
this.setState({ progress: Math.round(((maxOids - ids.length) / maxOids) * 50) });

const id = ids.shift();
this.getObject(_objects, _oldObjects, id)
Expand Down Expand Up @@ -665,14 +672,22 @@ class ObjectCustomEditor extends Component {
// provide defaults
let _default = this.getDefaultValues(instance, obj);
obj.common.custom[instance] = JSON.parse(JSON.stringify(_default || {}));
// remove all temporary values
Object.keys(obj.common.custom[instance]).forEach(attr => {
if (attr.startsWith('_')) {
delete obj.common.custom[instance][attr];
}
});
}

obj.common.custom[instance].enabled = true;

Object.keys(newValues).forEach(attr => {
// if not different
if (!Array.isArray(newValues[attr]) || (newValues[attr][0] && typeof newValues[attr][0] === 'object')) {
obj.common.custom[instance][attr] = newValues[attr];
if (!attr.startsWith('_')) {
obj.common.custom[instance][attr] = newValues[attr];
}
}
});
}
Expand Down Expand Up @@ -710,7 +725,7 @@ class ObjectCustomEditor extends Component {
onSave = cb => {
if (this.props.objectIDs.length > 10 && !this.state.confirmed) {
this.cb = cb;
return this.setState({showConfirmation: true});
return this.setState({ showConfirmation: true });
}

this.saveOneState([...this.props.objectIDs], () => {
Expand Down

0 comments on commit 3f0cf42

Please sign in to comment.