Skip to content

Commit

Permalink
Add multiple source fields
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromml committed Aug 22, 2024
1 parent 846e2f2 commit da502df
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions data/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2542,6 +2542,12 @@ en:
terms: copyleft, copyright
source:
label: Source
source:2:
label: Source 2
source:3:
label: Source 3
source:4:
label: Source 4
presets:
type/chronology:
name: Chronology
Expand Down
17 changes: 17 additions & 0 deletions modules/presets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ function addHistoricalFields(fields) {
fields.source.type = 'source';
fields.source.source = false;
fields.source.keys = ['source', 'source:url', 'source:name', 'source:date'];

const multipleSourceIds = ['', ':2', ':3', ':4'];

for (var i = 1; i < multipleSourceIds.length; i++){
var id = multipleSourceIds[i];
var previousId = multipleSourceIds[i-1];
fields['source' + id] = {
...fields.source,
key: 'source' + id,
keys: ['source' + id, 'source' + id + ':url', 'source' + id + ':name', 'source' + id + ':date'],
prerequisiteTag: {
keys: [
'source' + previousId,
'source' + previousId + ':url',
'source' + previousId + ':name',
'source' + previousId + ':date']}};
}
}

fields.license = {
Expand Down
8 changes: 8 additions & 0 deletions modules/ui/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,14 @@ 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
for (var i = 0; i < prerequisiteTag.keys.length; i++){
if (entity.tags[prerequisiteTag.keys[i]]) return true;
}
return false;
}
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:2', 'source:3', 'source:4'];
optionalCoreKeys.forEach(key => {
let field = presetsManager.field(key);
if (field && _fieldsArr.indexOf(field) === -1) {
_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

0 comments on commit da502df

Please sign in to comment.