Skip to content

Commit

Permalink
Use common methods for parsing icons
Browse files Browse the repository at this point in the history
  • Loading branch information
JuliaRadzhabova committed Oct 21, 2024
1 parent 6447fbf commit 59349ab
Showing 1 changed file with 2 additions and 146 deletions.
148 changes: 2 additions & 146 deletions apps/common/main/lib/view/Plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,158 +237,14 @@ define([
if (typeof result === 'string') {
if (result.indexOf('%') === -1)
return [icons, icons];
/*
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('/') + '/';
}

// 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 (!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);
}
} 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];
}
result[index][scale] = obj;
}
}
return Common.UI.iconsStr2IconsObj(icons);
}
return result;
},

parseIcons: function(icons) {
icons = this.iconsStr2IconsObj(icons);
if (icons.length && typeof icons[0] !== 'string') {
var theme = Common.UI.Themes.currentThemeId().toLowerCase(),
style = Common.UI.Themes.isDarkTheme() ? 'dark' : 'light',
idx = -1;
for (var i=0; i<icons.length; i++) {
if (icons[i].theme && icons[i].theme.toLowerCase() == theme) {
idx = i;
break;
}
}
if (idx<0)
for (var i=0; i<icons.length; i++) {
if (icons[i].style && icons[i].style.toLowerCase() == style) {
idx = i;
break;
}
}
(idx<0) && (idx = 0);

var ratio = Common.Utils.applicationPixelRatio()*100,
current = icons[idx],
bestDistance = 10000,
currentDistance = 0,
defUrl,
bestUrl;
for (var key in current) {
if (current.hasOwnProperty(key)) {
if (key=='default') {
defUrl = current[key];
} else if (!isNaN(parseInt(key))) {
currentDistance = Math.abs(ratio-parseInt(key));
if (currentDistance < (bestDistance - 0.01))
{
bestDistance = currentDistance;
bestUrl = current[key];
}
}
}
}
(bestDistance>0.01 && defUrl) && (bestUrl = defUrl);
return {
'normal': bestUrl ? bestUrl['normal'] : '',
'hover': bestUrl ? bestUrl['hover'] || bestUrl['normal'] : '',
'active': bestUrl ? bestUrl['active'] || bestUrl['normal'] : ''
};
} else { // old version
var url = icons[((Common.Utils.applicationPixelRatio() > 1 && icons.length > 1) ? 1 : 0) + (icons.length > 2 ? 2 : 0)];
return {
'normal': url,
'hover': url,
'active': url
};
}
return Common.UI.getSuitableIcons(icons);
},

updatePluginIcons: function(model) {
Expand Down

0 comments on commit 59349ab

Please sign in to comment.