Skip to content

Commit

Permalink
refactor(style): clean up style and test/style.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Jul 10, 2024
1 parent 565472b commit 982b908
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 135 deletions.
76 changes: 25 additions & 51 deletions src/Core/Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,31 @@ function baseAltitudeDefault(properties, ctx) {
}

export function readExpression(property, ctx) {
if (property != undefined) {
if (property.expression) {
return property.expression.evaluate(ctx);
} else if (property.stops) {
const stops = property.stops;
property = property.stops[0][1];
for (let i = stops.length - 1; i >= 0; i--) {
const stop = stops[i];

if (ctx.zoom >= stop[0]) {
property = stop[1];
break;
}
if (property.expression) {
return property.expression.evaluate(ctx);
}
if (property.stops) {
const stops = property.stops;
property = property.stops[0][1];
for (let i = stops.length - 1; i >= 0; i--) {
const stop = stops[i];

if (ctx.zoom >= stop[0]) {
property = stop[1];
break;
}
}
if (typeof property === 'string' || property instanceof String) {
property = property.replace(/\{(.+?)\}/g, (a, b) => (ctx.properties[b] || '')).trim();
}
if (property instanceof Function) {
// TOBREAK: Pass the current `context` as a unique parameter.
// In this proposal, metadata will be accessed in the callee by the
// `context.properties` property.
return property(ctx.properties, ctx);
}
return property;
}
if (typeof property === 'string' || property instanceof String) {
return property.replace(/\{(.+?)\}/g, (a, b) => (ctx.properties[b] || '')).trim();
}
if (property instanceof Function) {
// TOBREAK: Pass the current `context` as a unique parameter.
// In this proposal, metadata will be accessed in the callee by the
// `context.properties` property.
return property(ctx.properties, ctx);
}
return property;
}

function rgba2rgb(orig) {
Expand All @@ -55,16 +54,16 @@ function rgba2rgb(orig) {
return { color: orig };
} else if (typeof orig == 'string') {
const result = orig.match(/(?:((hsl|rgb)a? *\(([\d.%]+(?:deg|g?rad|turn)?)[ ,]*([\d.%]+)[ ,]*([\d.%]+)[ ,/]*([\d.%]*)\))|(#((?:[\d\w]{3}){1,2})([\d\w]{1,2})?))/i);
if (!result) {
if (result === null) {
return { color: orig, opacity: 1.0 };
} else if (result[7]) {
let opacity = 1.0;
if (result[9]) {
opacity = parseInt(result[9].length == 1 ? `${result[9]}${result[9]}` : result[9], 16) * inv255;
}
return { color: `#${result[8]}`, opacity };
} else if (result[0]) {
return { color: `${result[2]}(${result[3]},${result[4]},${result[5]})`, opacity: (Number(result[6]) || 1.0) };
} else if (result[1]) {
return { color: `${result[2]}(${result[3]},${result[4]},${result[5]})`, opacity: (result[6] ? Number(result[6]) : 1.0) };
}
}
}
Expand Down Expand Up @@ -706,31 +705,6 @@ class Style {
defineStyleProperty(this, 'icon', 'opacity', params.icon.opacity, 1.0);
}

/**
* Copies the content of the target style into this style.
* @param {Style} style - The style to copy.
*
* @return {Style} This style.
*/
copy(style) {
Object.assign(this.fill, style.fill);
Object.assign(this.stroke, style.stroke);
Object.assign(this.point, style.point);
Object.assign(this.text, style.text);
Object.assign(this.icon, style.icon);
return this;
}

/**
* Clones this style.
*
* @return {Style} The new style, cloned from this one.
*/
clone() {
const clone = new Style();
return clone.copy(this);
}

setContext(ctx) {
this.context = ctx;
}
Expand Down
Loading

0 comments on commit 982b908

Please sign in to comment.