diff --git a/modules/presets/index.js b/modules/presets/index.js index 9860918fc..2bf6c8571 100644 --- a/modules/presets/index.js +++ b/modules/presets/index.js @@ -314,13 +314,23 @@ export function presetIndex() { _this.defaults = (geometry, n, startWithRecents) => { - let rec = []; + let recents = []; if (startWithRecents) { - rec = _this.recent().matchGeometry(geometry).collection.slice(0, 4); + recents = _this.recent().matchGeometry(geometry).collection.slice(0, 4); } - const def = utilArrayUniq(rec.concat(_defaults[geometry].collection)).slice(0, n - 1); + let defaults; + if (_addablePresetIDs) { + defaults = Array.from(_addablePresetIDs).map(function(id) { + var preset = _this.item(id); + if (preset && preset.matchGeometry(geometry)) return preset; + return null; + }).filter(Boolean); + } else { + defaults = _defaults[geometry].collection.concat(_this.fallback(geometry)); + } + return presetCollection( - utilArrayUniq(rec.concat(def).concat(_this.fallback(geometry))) + utilArrayUniq(recents.concat(defaults)).slice(0, n - 1) ); }; diff --git a/test/spec/presets/index.js b/test/spec/presets/index.js index e832ff2e7..cbec7863e 100644 --- a/test/spec/presets/index.js +++ b/test/spec/presets/index.js @@ -181,7 +181,8 @@ describe('iD.presetIndex', function () { describe('#addablePresetIDs', function () { var testPresets = { residential: { tags: { highway: 'residential' }, geometry: ['line'] }, - park: { tags: { leisure: 'park' }, geometry: ['point', 'area'] } + park: { tags: { leisure: 'park' }, geometry: ['point', 'area'] }, + bench: { tags: { amenity: 'bench' }, geometry: ['point', 'line'] } }; it('addablePresetIDs is initially null', function (done) { @@ -210,6 +211,29 @@ describe('iD.presetIndex', function () { done(); }); }); + + it('addablePresetIDs are default presets', function (done) { + iD.fileFetcher.cache().preset_presets = testPresets; + var presets = iD.presetIndex(); + presets.ensureLoaded().then(function() { + var ids = new Set(['bench', 'residential']); // can only add presets with these IDs + presets.addablePresetIDs(ids); + + var areaDefaults = presets.defaults('area', 10).collection; + expect(areaDefaults.length).to.eql(0); + + var pointDefaults = presets.defaults('point', 10).collection; + expect(pointDefaults.length).to.eql(1); + expect(pointDefaults[0].id).to.eql('bench'); + + var lineDefaults = presets.defaults('line', 10).collection; + expect(lineDefaults.length).to.eql(2); + expect(lineDefaults[0].id).to.eql('bench'); + expect(lineDefaults[1].id).to.eql('residential'); + + done(); + }); + }); });