Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple source fields #223

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions modules/presets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ function addHistoricalFields(fields) {
fields.source.type = 'source';
fields.source.source = false;
fields.source.keys = ['source', 'source:url', 'source:name', 'source:date'];

for (let i = 1; i < 4; i++){
let id = 'source' + (i > 0 ? ':' + i.toString() : '');
let previousId = 'source' + ((i-1) > 0 ? ':' + (i-1).toString() : '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: String.prototype.toString() is suitable for Western languages, but not for some other languages we support, like Persian. Number.prototype.toLocaleString automatically adapts the number to the given writing system. Fortunately, you can just pass a number into the t function and it’ll do the right thing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This String.prototype.toString() is being used to define the source:2 key. If the "2" is localized, couldn't that cause problems? With the key ending up like source:٢

Copy link
Member

@1ec5 1ec5 Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you’re right, you’re only appending i to “Source” below. So as long as you pass i into the t() call for that field name, it should work fine.

fields[id] = {
...fields.source,
key: id,
keys: [id, id + ':url', id + ':name', id + ':date'],
overrideLabel: 'Source ' + i,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be localized. You can define a format string in the localization and pass in the number as a parameter to replace the token in the format string:

iD/data/core.yaml

Lines 766 to 767 in cb779ea

# format for the label of a feature that has a name and date range
dated: "{name} [{dateRange}]"
import { t, localizer } from '../core/localizer';
return dateRange ? t('inspector.display_name.dated', {dateRange: dateRange, name: name}) : name;

/ref #224

prerequisiteTag: {
keys: [
previousId,
previousId + ':url',
previousId + ':name',
previousId + ':date']}};
}
}

fields.license = {
Expand Down
6 changes: 6 additions & 0 deletions modules/ui/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ export function uiField(context, presetField, entityIDs, options) {

if (!entityIDs.every(function(entityID) {
var entity = context.graph().entity(entityID);
if (prerequisiteTag.keys) {
// Return true if any key in prerequisiteTag.keys is present, return false otherwise
// If prerequisiteTag.keys is present, prerequisiteTag.key will be ignored
const inEntityTags = (e) => e in entity.tags;
return prerequisiteTag.keys.some(inEntityTags);
}
if (prerequisiteTag.key) {
var value = entity.tags[prerequisiteTag.key];
if (!value) return false;
Expand Down
7 changes: 6 additions & 1 deletion modules/ui/fields/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function uiFieldSources(field, context) {
let _selection = d3_select(null);
let _pendingChange;

const mainKey = 'source';
const mainKey = field.key;
const sourceHeader = mainKey + ':';

// Pre-selected subkeys to show
Expand Down Expand Up @@ -130,5 +130,10 @@ export function uiFieldSources(field, context) {
_selection.call(sources);
};

sources.focus = function() {
var node = _selection.selectAll('input').node();
if (node) node.focus();
};

return utilRebind(sources, dispatch, 'on');
}
12 changes: 11 additions & 1 deletion modules/ui/sections/preset_fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,17 @@ export function uiSectionPresetFields(context) {
}
});

let optionalCoreKeys = ['source:1', 'source:2', 'source:3'];
optionalCoreKeys.forEach(key => {
let field = presetsManager.field(key);
if (field && !_fieldsArr.includes(field)) {
_fieldsArr.push(uiField(context, field, _entityIDs, { show: false }));
}
});


sharedFields.forEach(function(field) {
if (!coreKeys.includes(field.id) && field.matchAllGeometry(geometries)) {
if (!coreKeys.includes(field.id) && !optionalCoreKeys.includes(field.id) && field.matchAllGeometry(geometries)) {
_fieldsArr.push(
uiField(context, field, _entityIDs)
);
Expand All @@ -101,6 +110,7 @@ export function uiSectionPresetFields(context) {
additionalFields.forEach(function(field) {
if (sharedFields.indexOf(field) === -1 &&
!coreKeys.includes(field.id) &&
!optionalCoreKeys.includes(field.id) &&
field.matchAllGeometry(geometries)) {
_fieldsArr.push(
uiField(context, field, _entityIDs, { show: false })
Expand Down
Loading