Make recent presets geometry-specific

This commit is contained in:
Quincy Morgan
2019-03-07 12:14:18 -05:00
parent efac17810c
commit 7eb007d27a
4 changed files with 47 additions and 15 deletions

View File

@@ -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;
};

View File

@@ -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);
}
})

View File

@@ -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')

View File

@@ -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;