From e8e2bda31c5f3e3e8f41656ac5dd4c5f4bcb0a90 Mon Sep 17 00:00:00 2001 From: Oleg Korshul Date: Mon, 1 Apr 2024 14:17:45 +0300 Subject: [PATCH] Fix work with external icons --- apps/common/main/lib/util/utils.js | 172 +++++++++++++++-------------- 1 file changed, 88 insertions(+), 84 deletions(-) diff --git a/apps/common/main/lib/util/utils.js b/apps/common/main/lib/util/utils.js index 7967866672..458b023a81 100644 --- a/apps/common/main/lib/util/utils.js +++ b/apps/common/main/lib/util/utils.js @@ -1213,99 +1213,103 @@ Common.UI.isRTL = function () { Common.UI.iconsStr2IconsObj = function(icons) { let result = icons; - if (typeof result === 'string' && result.indexOf('%') !== -1) { - /* - valid params: - theme-type - {string} theme type (light|dark|common) - theme-name - {string} the name of theme - state - {string} state of icons for different situations (normal|hover|active) - scale - {string} list of avaliable scales (100|125|150|175|200|default|extended) - extension - {string} use it after symbol "." (png|jpeg|svg) - - Example: "resources/%theme-type%(light|dark)/%state%(normal)icon%scale%(default).%extension%(png)" - */ - let scaleValue = { - '100%' : '.', - '125%' : '@1.25x.', - '150%' : '@1.5x.', - '175%' : '@1.75x.', - '200%' : '@2x.' - } - let arrParams = ['theme-type', 'theme-name' ,'state', 'scale', 'extension'], - start = result.indexOf('%'), - template = result.substring(start).replace(/[/.]/g, ('')), - commonPart = result.substring(0, start), - end = 0, - param = null, - values = null, - iconName = '', - tempObj = {}; - - result = []; - - for (let index = 0; index < arrParams.length; index++) { - param = arrParams[index]; - start = template.indexOf(param) - 1; - if (start < 0 ) - continue; - - end = param.length + 2; - template = template.substring(0, start) + template.substring(start + end); - start = template.indexOf('(', 0); - end = template.indexOf(')', 0); - values = template.substring((start + 1), end); - template = template.substring(0, start) + template.substring(++end); - tempObj[param] = values.split('|'); - } + if (typeof result === 'string') { + if (result.indexOf('%') !== -1) { + /* + valid params: + theme-type - {string} theme type (light|dark|common) + theme-name - {string} the name of theme + state - {string} state of icons for different situations (normal|hover|active) + scale - {string} list of avaliable scales (100|125|150|175|200|default|extended) + extension - {string} use it after symbol "." (png|jpeg|svg) + + Example: "resources/%theme-type%(light|dark)/%state%(normal)icon%scale%(default).%extension%(png)" + */ + let scaleValue = { + '100%' : '.', + '125%' : '@1.25x.', + '150%' : '@1.5x.', + '175%' : '@1.75x.', + '200%' : '@2x.' + } + let arrParams = ['theme-type', 'theme-name' ,'state', 'scale', 'extension'], + start = result.indexOf('%'), + template = result.substring(start).replace(/[/.]/g, ('')), + commonPart = result.substring(0, start), + end = 0, + param = null, + values = null, + iconName = '', + tempObj = {}; + + result = []; + + for (let index = 0; index < arrParams.length; index++) { + param = arrParams[index]; + start = template.indexOf(param) - 1; + if (start < 0 ) + continue; + + end = param.length + 2; + template = template.substring(0, start) + template.substring(start + end); + start = template.indexOf('(', 0); + end = template.indexOf(')', 0); + values = template.substring((start + 1), end); + template = template.substring(0, start) + template.substring(++end); + tempObj[param] = values.split('|'); + } - if (template.length) { - iconName = template; - } else { - let arr = commonPart.split('/'); - iconName = arr.pop().replace(/\./g, ''); - commonPart = arr.join('/') + '/'; - } + if (template.length) { + iconName = template; + } else { + let arr = commonPart.split('/'); + iconName = arr.pop().replace(/\./g, ''); + commonPart = arr.join('/') + '/'; + } - // we don't work with svg yet. Change it when we will work with it (extended variant). - if (tempObj['scale'] && (tempObj['scale'] == 'default' || tempObj['scale'] == 'extended') ) { - tempObj['scale'] = ['100', '125', '150', '175', '200']; - } else if (!tempObj['scale']) { - tempObj['scale'] = ['100']; - } + // we don't work with svg yet. Change it when we will work with it (extended variant). + if (tempObj['scale'] && (tempObj['scale'] == 'default' || tempObj['scale'] == 'extended') ) { + tempObj['scale'] = ['100', '125', '150', '175', '200']; + } else if (!tempObj['scale']) { + tempObj['scale'] = ['100']; + } - if (!tempObj['state']) { - tempObj['state'] = ['normal']; - } + if (!tempObj['state']) { + tempObj['state'] = ['normal']; + } - if (!iconName) { - iconName = 'icon'; - } + if (!iconName) { + iconName = 'icon'; + } - let bHasName = !!tempObj['theme-name']; - let bHasType = (tempObj['theme-type'] && tempObj['theme-type'][0] !== 'common'); - let arrThemes = bHasName ? tempObj['theme-name'] : (bHasType ? tempObj['theme-type'] : []); - let paramName = bHasName ? 'theme' : 'style'; - if (arrThemes.length) { - for (let thInd = 0; thInd < arrThemes.length; thInd++) { - let obj = {}; - obj[paramName] = arrThemes[thInd]; - result.push(obj); + let bHasName = !!tempObj['theme-name']; + let bHasType = (tempObj['theme-type'] && tempObj['theme-type'][0] !== 'common'); + let arrThemes = bHasName ? tempObj['theme-name'] : (bHasType ? tempObj['theme-type'] : []); + let paramName = bHasName ? 'theme' : 'style'; + if (arrThemes.length) { + for (let thInd = 0; thInd < arrThemes.length; thInd++) { + let obj = {}; + obj[paramName] = arrThemes[thInd]; + result.push(obj); + } + } else { + result.push({}); } - } else { - result.push({}); - } - for (let index = 0; index < result.length; index++) { - for (let scaleInd = 0; scaleInd < tempObj['scale'].length; scaleInd++) { - let themePath = (result[index][paramName] || 'img') + '/'; - let scale = tempObj['scale'][scaleInd] + '%'; - let obj = {}; - for (let stateInd = 0; stateInd < tempObj['state'].length; stateInd++) { - let state = tempObj['state'][stateInd]; - obj[state] = commonPart + themePath + (state == 'normal' ? '' : (state + '_')) + iconName + (scaleValue[scale] || '.') + tempObj['extension'][0]; + for (let index = 0; index < result.length; index++) { + for (let scaleInd = 0; scaleInd < tempObj['scale'].length; scaleInd++) { + let themePath = (result[index][paramName] || 'img') + '/'; + let scale = tempObj['scale'][scaleInd] + '%'; + let obj = {}; + for (let stateInd = 0; stateInd < tempObj['state'].length; stateInd++) { + let state = tempObj['state'][stateInd]; + obj[state] = commonPart + themePath + (state == 'normal' ? '' : (state + '_')) + iconName + (scaleValue[scale] || '.') + tempObj['extension'][0]; + } + result[index][scale] = obj; } - result[index][scale] = obj; } + } else { + return [icons]; } } return result;