Skip to content

Commit

Permalink
Add toolbar item for generic Point/Line/Area presets for simpler lega…
Browse files Browse the repository at this point in the history
…cy workflow compatibility (close #6458)
  • Loading branch information
quincylvania committed Sep 17, 2019
1 parent 4dd6d3f commit 966eecd
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 21 deletions.
2 changes: 2 additions & 0 deletions data/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ en:
favorites: Favorites
add_feature: Add Feature
finish: Finish
generic:
title: Base Types
geometry:
key: G
repeat:
Expand Down
3 changes: 3 additions & 0 deletions dist/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"favorites": "Favorites",
"add_feature": "Add Feature",
"finish": "Finish",
"generic": {
"title": "Base Types"
},
"geometry": {
"key": "G"
},
Expand Down
20 changes: 7 additions & 13 deletions modules/presets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,26 +377,20 @@ export function presetIndex(context) {
dispatch.call('favoritePreset');
}

all.getGenericRibbonItems = function() {
return ['point', 'line', 'area'].map(function(id) {
return RibbonItem(all.item(id), 'generic');
});
};

all.getFavorites = function() {
if (!_favorites) {

// fetch from local storage
var rawFavorites = JSON.parse(context.storage('preset_favorites'));

if (!rawFavorites) {
// no saved favorites

if (!context.isFirstSession) {
// assume existing user coming from iD 2, use the generic presets as defaults
rawFavorites = [
{ pID: 'point'},
{ pID: 'line'},
{ pID: 'area'}
];
} else {
// new user, no default favorites
rawFavorites = [];
}
rawFavorites = [];
context.storage('preset_favorites', JSON.stringify(rawFavorites));
}

Expand Down
4 changes: 2 additions & 2 deletions modules/ui/tools/quick_presets.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export function uiToolQuickPresets(context) {
var eventX = d3_event.x + draggingNode.parentNode.offsetLeft;
var origLeft = draggingNode.offsetLeft;

d3_selectAll('#bar button.add-preset')
d3_selectAll('#bar button.add-favorite, #bar button.add-recent')
.style('transform', function(d2) {

if (d.button === d2.button) {
Expand Down Expand Up @@ -237,7 +237,7 @@ export function uiToolQuickPresets(context) {
.classed('dragging', false)
.classed('removing', false);

d3_selectAll('#bar button.add-preset')
d3_selectAll('#bar button.add-favorite, #bar button.add-recent')
.style('transform', null);

var deltaY = d3_event.y - dragOrigin.y;
Expand Down
9 changes: 6 additions & 3 deletions modules/ui/tools/quick_presets_favorites.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ export function uiToolAddFavorite(context) {

var items = context.presets().getFavorites().slice(0, 10);

var precedingCount = context.storage('tool.add_generic.toggledOn') === 'true' ? 3 : 0;

items.forEach(function(item, index) {
var totalIndex = precedingCount + index;
var keyCode;
// use number row order: 1 2 3 4 5 6 7 8 9 0
// use the same for RTL even though the layout is backward: #6107
if (index === 9) {
if (totalIndex === 9) {
keyCode = 0;
} else if (index < 10) {
keyCode = index + 1;
} else if (totalIndex < 10) {
keyCode = totalIndex + 1;
}
if (keyCode !== undefined) {
item.key = keyCode.toString();
Expand Down
32 changes: 32 additions & 0 deletions modules/ui/tools/quick_presets_generic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { t } from '../../util/locale';
import { uiToolQuickPresets } from './quick_presets';

export function uiToolAddGeneric(context) {

var tool = uiToolQuickPresets(context);
tool.id = 'add_generic';
tool.label = t('toolbar.generic.title');
tool.iconName = 'iD-logo-features';
tool.iconClass = 'icon-30';

if (context.storage('tool.add_generic.toggledOn') === null) {
if (!context.isFirstSession) {
// assume existing user coming from iD 2, enable this item by default
tool.isToggledOn = true;
} else {
tool.isToggledOn = false;
}
context.storage('tool.add_generic.toggledOn', tool.isToggledOn);
}

tool.itemsToDraw = function() {
if (context.presets().getAddable().length) return [];
var items = context.presets().getGenericRibbonItems();
for (var i in items) {
items[i].key = (parseInt(i, 10) + 1).toString();
}
return items;
};

return tool;
}
7 changes: 4 additions & 3 deletions modules/ui/tools/quick_presets_recent.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ export function uiToolAddRecent(context) {

var maxShown = 10;
var maxRecents = 5;
var precedingCount = context.storage('tool.add_generic.toggledOn') === 'true' ? 3 : 0;

var favorites = context.presets().getFavorites().slice(0, maxShown);
var favoritesCount = favorites.length;
precedingCount += favorites.length;

function isAFavorite(recent) {
return favorites.some(function(favorite) {
return favorite.matches(recent.preset);
});
}

maxRecents = Math.min(maxRecents, maxShown - favoritesCount);
maxRecents = Math.min(maxRecents, maxShown - precedingCount);
var items = [];
if (maxRecents > 0) {
var recents = context.presets().getRecents().filter(function(recent) {
Expand All @@ -41,7 +42,7 @@ export function uiToolAddRecent(context) {
}

items.forEach(function(item, index) {
var totalIndex = favoritesCount + index;
var totalIndex = precedingCount + index;
var keyCode;
// use number row order: 1 2 3 4 5 6 7 8 9 0
// use the same for RTL even though the layout is backward: #6107
Expand Down
3 changes: 3 additions & 0 deletions modules/ui/top_toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { operationCircularize, operationContinue, operationDelete, operationDisc
operationReverse, operationSplit, operationStraighten } from '../operations';
import { uiToolAddFavorite, uiToolAddFeature, uiToolAddRecent, uiToolNotes, uiToolOperation, uiToolSave, uiToolUndoRedo } from './tools';
import { uiToolAddAddablePresets } from './tools/quick_presets_addable';
import { uiToolAddGeneric } from './tools/quick_presets_generic';
import { uiToolSimpleButton } from './tools/simple_button';
import { uiToolWaySegments } from './tools/way_segments';
import { uiToolRepeatAdd } from './tools/repeat_add';
Expand Down Expand Up @@ -41,6 +42,7 @@ export function uiTopToolbar(context) {
var toolbox = uiToolToolbox(context),
addAddable = uiToolAddAddablePresets(context),
addFeature = uiToolAddFeature(context),
addGeneric = uiToolAddGeneric(context),
addFavorite = uiToolAddFavorite(context),
addRecent = uiToolAddRecent(context),
notes = uiToolNotes(context),
Expand Down Expand Up @@ -151,6 +153,7 @@ export function uiTopToolbar(context) {
'spacer',
addFeature,
addAddable,
addGeneric,
addFavorite,
addRecent,
'spacer',
Expand Down

0 comments on commit 966eecd

Please sign in to comment.