|
| 1 | +import { Color } from '@nativescript/core'; |
| 2 | + |
1 | 3 | function toCamelCase(s) {
|
2 | 4 | return s.replace(/([-_][a-z])/gi, ($1) => $1.toUpperCase().replace('-', '').replace('_', ''));
|
3 | 5 | }
|
| 6 | + |
| 7 | +const Expression = com.mapbox.mapboxsdk.style.expressions.Expression; |
| 8 | +function transformValue(key, value) { |
| 9 | + let nValue = value; |
| 10 | + if (Array.isArray(value)) { |
| 11 | + nValue = Expression.Converter.convert(JSON.stringify(value)); |
| 12 | + } |
| 13 | + if (key.indexOf('-color') !== -1 && !Array.isArray(value)) { |
| 14 | + const color = value instanceof Color ? value : new Color(value); |
| 15 | + nValue = color.android; |
| 16 | + } else if (typeof value === 'number') { |
| 17 | + nValue = new java.lang.Float(value); |
| 18 | + } else if (typeof value === 'boolean') { |
| 19 | + nValue = new java.lang.Boolean(value); |
| 20 | + } |
| 21 | + return nValue; |
| 22 | +} |
4 | 23 | export class PropertyParser {
|
5 | 24 | static parsePropertiesForLayer(propertiesObject) {
|
6 | 25 | const nProperties = [];
|
7 | 26 |
|
8 | 27 | const PropertyFactory = com.mapbox.mapboxsdk.style.layers.PropertyFactory;
|
9 |
| - const Expression = com.mapbox.mapboxsdk.style.expressions.Expression; |
10 | 28 | if (propertiesObject) {
|
11 | 29 | Object.keys(propertiesObject).forEach((k) => {
|
12 | 30 | const actualKey = toCamelCase(k);
|
13 | 31 | const value = propertiesObject[k];
|
14 |
| - let nValue = value; |
15 |
| - if (Array.isArray(value)) { |
16 |
| - nValue = Expression.Converter.convert(JSON.stringify(value)); |
17 |
| - } else if (typeof value === 'number') { |
18 |
| - nValue = new java.lang.Float(value); |
19 |
| - } |
| 32 | + const nValue = transformValue(k, value); |
20 | 33 | nProperties.push(PropertyFactory[actualKey](nValue));
|
21 | 34 | });
|
22 | 35 | }
|
|
0 commit comments