Skip to content

Commit

Permalink
icon-image fallback
Browse files Browse the repository at this point in the history
If icon-image is specified as a selection function, choose the first string value that refers to an available image.
  • Loading branch information
1ec5 committed Jul 11, 2016
1 parent 27b724d commit baf963b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
16 changes: 12 additions & 4 deletions js/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,14 @@ SymbolBucket.prototype.populateBuffers = function(collisionTile, stacks, icons)
}

if (layout['icon-image']) {
var iconName = resolveStringValue(features[k].properties, layout['icon-image']);
var iconNames = resolveStringValue(features[k].properties, layout['icon-image'], true);
var iconName = '';
for (var i = 0; i < iconNames.length; i++) {
if (iconNames[i] in icons) {
iconName = iconNames[i];
break;
}
}
var image = icons[iconName];
shapedIcon = shapeIcon(image, layout);

Expand Down Expand Up @@ -505,9 +512,10 @@ SymbolBucket.prototype.updateIcons = function(icons) {
if (!iconValue) return;

for (var i = 0; i < this.features.length; i++) {
var iconName = resolveStringValue(this.features[i].properties, iconValue);
if (iconName)
icons[iconName] = true;
var iconNames = resolveStringValue(this.features[i].properties, iconValue, true);
for (var j = 0; j < iconNames.length; j++) {
icons[iconNames[j]] = true;
}
}
};

Expand Down
11 changes: 7 additions & 4 deletions js/util/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module.exports = resolveStringValue;
*
* @param {Object} properties a key/value relationship between tokens and replacements
* @param {string|Object} value a template string or selection function object
* @returns {string} the template with tokens replaced
* @param {boolean=false} all true to return each valid case in the selection function, with tokens replaced, instead of only the first one
* @returns {string|Array<string>} the string with tokens replaced; if `all`, then an array of such strings
* @private
*/
function resolveStringValue(properties, value) {
function resolveStringValue(properties, value, all) {
var results = [];
if (typeof value === 'string') {
return value.replace(/{([^{}]+)}/g, function(match, ref) {
return ref in properties ? properties[ref] : '';
Expand All @@ -20,11 +22,12 @@ function resolveStringValue(properties, value) {
for (var i = 0; i < cases.length; i++) {
var resolvedCase = resolveStringTemplate(properties, cases[i]);
if (resolvedCase !== undefined) {
return resolvedCase;
results.push(resolvedCase);
if (!all) break;
}
}
}
return '';
return all ? results : (results[0] ? results[0] : '');
}

/**
Expand Down
11 changes: 10 additions & 1 deletion test/js/util/token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,18 @@ test('token', function(t) {
cases: [
[{ref: 'name'}, ' (', {ref: ''}, ')'],
[{ref: 'name'}, ' (', {ref: 'name_fr'}, ')'],
['⚜', {ref: 'name'}]
[{ref: 'name'}]
]
}));
t.deepEqual(['⚜Louisiane', '⚜Louisiana'], resolveStringValue({name: 'Louisiana', 'name_fr': 'Louisiane'}, {
type: 'selection',
cases: [
['⚜', {ref: 'name_en'}],
['⚜', {ref: 'name_fr'}],
['⚜', {ref: 'name_lou'}],
['⚜', {ref: 'name'}]
]
}, true));

t.end();
});

0 comments on commit baf963b

Please sign in to comment.