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 3 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
2 changes: 2 additions & 0 deletions data/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,8 @@ en:
terms: copyleft, copyright
source:
label: Source
source_multiple:
label: Source {localizerOption}
presets:
type/chronology:
name: Chronology
Expand Down
25 changes: 15 additions & 10 deletions modules/presets/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export function presetField(fieldID, field, allFields) {
allFields = allFields || {};
let _this = Object.assign({}, field); // shallow copy

// This handles fieldIDs that contain ':', like 'source:1'
let localizerFieldID = fieldID.includes(':') ? fieldID.split(':')[0] + '_multiple': fieldID;
// This is what comes after the ':' in the fieldID.
let localizerOption = fieldID.includes(':') ? fieldID.split(':')[1]: 0;
1ec5 marked this conversation as resolved.
Show resolved Hide resolved
1ec5 marked this conversation as resolved.
Show resolved Hide resolved

_this.id = fieldID;

// for use in classes, element ids, css selectors
Expand All @@ -21,14 +26,14 @@ export function presetField(fieldID, field, allFields) {
return !_this.geometry || geometries.every(geom => _this.geometry.indexOf(geom) !== -1);
};

_this.t = (scope, options) => t(localizer.coalesceStringIds([`custom_presets.fields.${fieldID}.${scope}`,
`_tagging.presets.fields.${fieldID}.${scope}`]), options);
_this.t.html = (scope, options) => t.html(localizer.coalesceStringIds([`custom_presets.fields.${fieldID}.${scope}`,
`_tagging.presets.fields.${fieldID}.${scope}`]), options);
_this.t.append = (scope, options) => t.append(localizer.coalesceStringIds([`custom_presets.fields.${fieldID}.${scope}`,
`_tagging.presets.fields.${fieldID}.${scope}`]), options);
_this.hasTextForStringId = (scope) => localizer.hasTextForStringId(`custom_presets.fields.${fieldID}.${scope}`) ||
localizer.hasTextForStringId(`_tagging.presets.fields.${fieldID}.${scope}`);
_this.t = (scope, options) => t(localizer.coalesceStringIds([`custom_presets.fields.${localizerFieldID}.${scope}`,
`_tagging.presets.fields.${localizerFieldID}.${scope}`]), options);
_this.t.html = (scope, options) => t.html(localizer.coalesceStringIds([`custom_presets.fields.${localizerFieldID}.${scope}`,
`_tagging.presets.fields.${localizerFieldID}.${scope}`]), options);
_this.t.append = (scope, options) => t.append(localizer.coalesceStringIds([`custom_presets.fields.${localizerFieldID}.${scope}`,
`_tagging.presets.fields.${localizerFieldID}.${scope}`]), options);
_this.hasTextForStringId = (scope) => localizer.hasTextForStringId(`custom_presets.fields.${localizerFieldID}.${scope}`) ||
localizer.hasTextForStringId(`_tagging.presets.fields.${localizerFieldID}.${scope}`);

_this.resolveReference = which => {
const referenceRegex = /^\{(.*)\}$/;
Expand All @@ -43,10 +48,10 @@ export function presetField(fieldID, field, allFields) {
return _this;
};

_this.title = () => _this.overrideLabel || _this.resolveReference('label').t('label', { 'default': fieldID });
_this.title = () => _this.overrideLabel || _this.resolveReference('label').t('label', { 'default': fieldID, 'localizerOption': localizerOption });
_this.label = () => _this.overrideLabel ?
selection => selection.text(_this.overrideLabel) :
_this.resolveReference('label').t.append('label', { 'default': fieldID });
_this.resolveReference('label').t.append('label', { 'default': fieldID, 'localizerOption': localizerOption });
1ec5 marked this conversation as resolved.
Show resolved Hide resolved

_this.placeholder = () => _this.resolveReference('placeholder').t('placeholder', { 'default': '' });

Expand Down
15 changes: 15 additions & 0 deletions modules/presets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ 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'],
prerequisiteTag: {
keys: [
previousId,
previousId + ':url',
previousId + ':name',
previousId + ':date']}};
}
}

fields.license = {
Expand Down
10 changes: 10 additions & 0 deletions modules/ui/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ export function uiField(context, presetField, entityIDs, options) {
referenceKey = referenceKey.replace(/:$/, '');
}

if (d.type === 'source') { // lookup key without the trailing ':'
referenceKey = referenceKey.split(':')[0];
}

var referenceOptions = d.reference || {
key: referenceKey,
value: _tags[referenceKey]
Expand Down Expand Up @@ -350,6 +354,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