From 7eb007d27a67489eb35a553d56bca2179facfb0f Mon Sep 17 00:00:00 2001 From: Quincy Morgan Date: Thu, 7 Mar 2019 12:14:18 -0500 Subject: [PATCH] Make recent presets geometry-specific --- modules/presets/index.js | 29 +++++++++++++++++++++-------- modules/ui/modes.js | 3 +++ modules/ui/preset_list.js | 2 +- modules/ui/search_add.js | 28 ++++++++++++++++++++++------ 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/modules/presets/index.js b/modules/presets/index.js index 37a8b9338..e92039222 100644 --- a/modules/presets/index.js +++ b/modules/presets/index.js @@ -3,6 +3,7 @@ import _forEach from 'lodash-es/forEach'; import _isEmpty from 'lodash-es/isEmpty'; import _reject from 'lodash-es/reject'; import _uniq from 'lodash-es/uniq'; +import _uniqWith from 'lodash-es/uniqWith'; import { json as d3_json } from 'd3-request'; @@ -26,7 +27,7 @@ export function presetIndex() { var _defaults = { area: all, line: all, point: all, vertex: all, relation: all }; var _fields = {}; var _universal = []; - var _recent = presetCollection([]); + var _recentWithGeometry = []; // Index of presets by (geometry, tag key). var _index = { @@ -213,7 +214,7 @@ export function presetIndex() { all.init = function() { all.collection = []; - _recent.collection = []; + _recentWithGeometry = []; _fields = {}; _universal = []; _index = { point: {}, vertex: {}, line: {}, area: {}, relation: {} }; @@ -227,7 +228,7 @@ export function presetIndex() { _defaults = { area: all, line: all, point: all, vertex: all, relation: all }; _fields = {}; _universal = []; - _recent = presetCollection([]); + _recentWithGeometry = []; // Index of presets by (geometry, tag key). _index = { @@ -263,18 +264,30 @@ export function presetIndex() { }; all.defaults = function(geometry, n) { - var rec = _recent.matchGeometry(geometry).collection.slice(0, 4); + var rec = all.recent().matchGeometry(geometry).collection.slice(0, 4); var def = _uniq(rec.concat(_defaults[geometry].collection)).slice(0, n - 1); return presetCollection(_uniq(rec.concat(def).concat(all.item(geometry)))); }; - all.recent = function(geometries, n) { - return presetCollection(_recent.matchAnyGeometry(geometries).collection.slice(0, n - 1)); + all.recent = function() { + return presetCollection(_uniq(_recentWithGeometry.map(function(d) { + return d.preset; + }))); }; - all.choose = function(preset) { + all.recentWithGeometry = function() { + return _recentWithGeometry; + }; + + all.choose = function(preset, geometry) { if (preset.searchable !== false) { - _recent = presetCollection(_uniq([preset].concat(_recent.collection))); + var newWithGeometry = { + preset: preset, + geometry: geometry + }; + _recentWithGeometry = _uniqWith([newWithGeometry].concat(_recentWithGeometry), function(d1, d2) { + return d1.preset === d2.preset && d1.geometry === d2.geometry; + }); } return all; }; diff --git a/modules/ui/modes.js b/modules/ui/modes.js index 238581e8e..0f2147d3c 100644 --- a/modules/ui/modes.js +++ b/modules/ui/modes.js @@ -168,6 +168,9 @@ export function uiModes(context) { if (d.id === currMode) { context.enter(modeBrowse(context)); } else { + if (d.preset) { + context.presets().choose(d.preset, d.geometry); + } context.enter(d); } }) diff --git a/modules/ui/preset_list.js b/modules/ui/preset_list.js index 1e8ab2d46..8e89c01ce 100644 --- a/modules/ui/preset_list.js +++ b/modules/ui/preset_list.js @@ -388,7 +388,7 @@ export function uiPresetList(context) { item.choose = function() { if (d3_select(this).classed('disabled')) return; - context.presets().choose(preset); + context.presets().choose(preset, context.geometry(_entityID)); context.perform( actionChangePreset(_entityID, _currentPreset, preset), t('operations.change_tags.annotation') diff --git a/modules/ui/search_add.js b/modules/ui/search_add.js index c6c3075a9..27fe49094 100644 --- a/modules/ui/search_add.js +++ b/modules/ui/search_add.js @@ -205,9 +205,13 @@ export function uiSearchAdd(context) { var value = search.property('value'); var results; if (value.length) { - results = presets.search(value, shownGeometry); + results = presets.search(value, shownGeometry).collection; } else { - results = context.presets().recent(shownGeometry, 36); + var recents = context.presets().recentWithGeometry(); + recents = recents.filter(function(d) { + return shownGeometry.indexOf(d.geometry) !== -1; + }); + results = recents.slice(0, 35); } list.call(drawList, results); @@ -244,6 +248,9 @@ export function uiSearchAdd(context) { if (preset.members) { return CategoryItem(preset); } + if (preset.preset && preset.geometry) { + return AddablePresetItem(preset.preset, preset.geometry); + } var supportedGeometry = preset.geometry.filter(function(geometry) { return shownGeometry.indexOf(geometry) !== -1; }).sort(); @@ -258,16 +265,16 @@ export function uiSearchAdd(context) { return MultiGeometryPresetItem(preset, supportedGeometry); } - function drawList(list, presets) { + function drawList(list, data) { list.selectAll('.subsection').remove(); - var collection = presets.collection.map(function(preset) { + var dataItems = data.map(function(preset) { return itemForPreset(preset); }); var items = list.selectAll('.list-item') - .data(collection, function(d) { return d.preset.id; }); + .data(dataItems, function(d) { return d.id(); }); items.order(); @@ -426,6 +433,9 @@ export function uiSearchAdd(context) { function CategoryItem(preset) { var item = {}; + item.id = function() { + return preset.id; + }; item.subsection = d3_select(null); item.preset = preset; item.choose = function() { @@ -444,6 +454,9 @@ export function uiSearchAdd(context) { function MultiGeometryPresetItem(preset, geometries) { var item = {}; + item.id = function() { + return preset.id + geometries; + }; item.subsection = d3_select(null); item.preset = preset; item.geometries = geometries; @@ -462,6 +475,9 @@ export function uiSearchAdd(context) { function AddablePresetItem(preset, geometry, isSubitem) { var item = {}; + item.id = function() { + return preset.id + geometry + isSubitem; + }; item.isSubitem = isSubitem; item.preset = preset; item.geometry = geometry; @@ -488,7 +504,7 @@ export function uiSearchAdd(context) { mode = modeAddArea(context, modeInfo); } search.node().blur(); - context.presets().choose(preset); + context.presets().choose(preset, geometry); context.enter(mode); }; return item;