src-rx/src/components/Utils.js
/**
     * Update the smartname of a state.
     * @param {ioBroker.StateObject} obj
     * @param {string} newSmartName
     * @param {string | undefined} byON
     * @param {string | undefined} smartType
     * @param {string} instanceId
     * @param {boolean} [noCommon]
     */
Update the smartname of a state.
Update the smartname of a state .
static updateSmartName(obj, newSmartName, byON, smartType, instanceId, noCommon) {
        const language = I18n.getLanguage();
        if (typeof obj.common.smartName === 'string') {
            const nnn = obj.common.smartName;
            obj.common.smartName = {};
            obj.common.smartName[language] = nnn;
        }
        if (obj.native && obj.native.byON) {
            delete obj.native.byON;
            let _smartName = obj.common.smartName;
            if (!_smartName || typeof _smartName !== 'object') {
                _smartName = {en: _smartName};
                _smartName[language] = _smartName.en;
            }
            obj.common.smartName = _smartName;
        }
        if (smartType !== undefined) {
            if (noCommon) {
                obj.common.custom = obj.common.custom || {};
                obj.common.custom[instanceId] = obj.common.custom[instanceId] || {};
                obj.common.custom[instanceId].smartName = obj.common.custom[instanceId].smartName || {};
                if (!smartType) {
                    delete obj.common.custom[instanceId].smartName.smartType;
                } else {
                    obj.common.custom[instanceId].smartName.smartType = smartType;
                }
            } else {
                obj.common.smartName = obj.common.smartName || {};
                if (!smartType) {
                    delete obj.common.smartName.smartType;
                } else {
                    obj.common.smartName.smartType = smartType;
                }
            }
        }
        if (byON !== undefined) {
            if (noCommon) {
                obj.common.custom = obj.common.custom || {};
                obj.common.custom[instanceId] = obj.common.custom[instanceId] || {};
                obj.common.custom[instanceId].smartName = obj.common.custom[instanceId].smartName || {};
                obj.common.custom[instanceId].smartName.byON = byON;
            } else {
                obj.common.smartName = obj.common.smartName || {};
                obj.common.smartName.byON = byON;
            }
        }
        if (newSmartName !== undefined) {
            let smartName;
            if (noCommon) {
                obj.common.custom = obj.common.custom || {};
                obj.common.custom[instanceId] = obj.common.custom[instanceId] || {};
                obj.common.custom[instanceId].smartName = obj.common.custom[instanceId].smartName || {};
                smartName = obj.common.custom[instanceId].smartName;
            } else {
                obj.common.smartName = obj.common.smartName || {};
                smartName = obj.common.smartName;
            }
            smartName[language] = newSmartName;
            if (smartName && (!smartName[language] ||
                (smartName[language] === obj.common.name &&
                    (!obj.common.role || obj.common.role.indexOf('button') >= 0)))) {
                delete smartName[language];
                let empty = true;
                for (const key in smartName) {
                    if (smartName.hasOwnProperty(key)) {
                        empty = false;
                        break;
                    }
                }
                if (empty) {
                    if (noCommon) {
                        if (obj.common.custom[instanceId].smartName.byON === undefined) {
                            delete obj.common.custom[instanceId];
                        } else {
                            delete obj.common.custom[instanceId].en;
                            delete obj.common.custom[instanceId].de;
                            delete obj.common.custom[instanceId].ru;
                            delete obj.common.custom[instanceId].nl;
                            delete obj.common.custom[instanceId].pl;
                            delete obj.common.custom[instanceId].it;
                            delete obj.common.custom[instanceId].fr;
                            delete obj.common.custom[instanceId].pt;
                            delete obj.common.custom[instanceId].es;
                            delete obj.common.custom[instanceId]['zh-cn'];
                        }
                    } else {
                        if (obj.common.smartName.byON !== undefined) {
                            delete obj.common.smartName.en;
                            delete obj.common.smartName.de;
                            delete obj.common.smartName.ru;
                            delete obj.common.smartName.nl;
                            delete obj.common.smartName.pl;
                            delete obj.common.smartName.it;
                            delete obj.common.smartName.fr;
                            delete obj.common.smartName.pt;
                            delete obj.common.smartName.es;
                            delete obj.common.smartName['zh-cn'];
                        } else {
                            obj.common.smartName = null;
                        }
                    }
                }
            }
        }
    }
static updateSmartName ( obj , newSmartName , byON , smartType , instanceId , noCommon ) { const language = I18n . getLanguage ( ) ; if ( typeof obj . common . smartName === 'string' ) { const nnn = obj . common . smartName ; obj . common . smartName = { } ; obj . common . smartName [ language ] = nnn ; } if ( obj . native && obj . native . byON ) { delete obj . native . byON ; let _smartName = obj . common . smartName ; if ( ! _smartName || typeof _smartName !== 'object' ) { _smartName = { en : _smartName } ; _smartName [ language ] = _smartName . en ; } obj . common . smartName = _smartName ; } if ( smartType !== undefined ) { if ( noCommon ) { obj . common . custom = obj . common . custom || { } ; obj . common . custom [ instanceId ] = obj . common . custom [ instanceId ] || { } ; obj . common . custom [ instanceId ] . smartName = obj . common . custom [ instanceId ] . smartName || { } ; if ( ! smartType ) { delete obj . common . custom [ instanceId ] . smartName . smartType ; } else { obj . common . custom [ instanceId ] . smartName . smartType = smartType ; } } else { obj . common . smartName = obj . common . smartName || { } ; if ( ! smartType ) { delete obj . common . smartName . smartType ; } else { obj . common . smartName . smartType = smartType ; } } } if ( byON !== undefined ) { if ( noCommon ) { obj . common . custom = obj . common . custom || { } ; obj . common . custom [ instanceId ] = obj . common . custom [ instanceId ] || { } ; obj . common . custom [ instanceId ] . smartName = obj . common . custom [ instanceId ] . smartName || { } ; obj . common . custom [ instanceId ] . smartName . byON = byON ; } else { obj . common . smartName = obj . common . smartName || { } ; obj . common . smartName . byON = byON ; } } if ( newSmartName !== undefined ) { let smartName ; if ( noCommon ) { obj . common . custom = obj . common . custom || { } ; obj . common . custom [ instanceId ] = obj . common . custom [ instanceId ] || { } ; obj . common . custom [ instanceId ] . smartName = obj . common . custom [ instanceId ] . smartName || { } ; smartName = obj . common . custom [ instanceId ] . smartName ; } else { obj . common . smartName = obj . common . smartName || { } ; smartName = obj . common . smartName ; } smartName [ language ] = newSmartName ; if ( smartName && ( ! smartName [ language ] || ( smartName [ language ] === obj . common . name && ( ! obj . common . role || obj . common . role . indexOf ( 'button' ) >= 0 ) ) ) ) { delete smartName [ language ] ; let empty = true ; for ( const key in smartName ) { if ( smartName . hasOwnProperty ( key ) ) { empty = false ; break ; } } if ( empty ) { if ( noCommon ) { if ( obj . common . custom [ instanceId ] . smartName . byON === undefined ) { delete obj . common . custom [ instanceId ] ; } else { delete obj . common . custom [ instanceId ] . en ; delete obj . common . custom [ instanceId ] . de ; delete obj . common . custom [ instanceId ] . ru ; delete obj . common . custom [ instanceId ] . nl ; delete obj . common . custom [ instanceId ] . pl ; delete obj . common . custom [ instanceId ] . it ; delete obj . common . custom [ instanceId ] . fr ; delete obj . common . custom [ instanceId ] . pt ; delete obj . common . custom [ instanceId ] . es ; delete obj . common . custom [ instanceId ] [ 'zh-cn' ] ; } } else { if ( obj . common . smartName . byON !== undefined ) { delete obj . common . smartName . en ; delete obj . common . smartName . de ; delete obj . common . smartName . ru ; delete obj . common . smartName . nl ; delete obj . common . smartName . pl ; delete obj . common . smartName . it ; delete obj . common . smartName . fr ; delete obj . common . smartName . pt ; delete obj . common . smartName . es ; delete obj . common . smartName [ 'zh-cn' ] ; } else { obj . common . smartName = null ; } } } } } }
Update the smartname of a state.
Update the smartname of a state .
// convert Old format // convert old settings // If smart name deleted // Check if structure has any definitions // If empty => delete smartName completely