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

Update source prerequisite behavior #226

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 18 additions & 6 deletions modules/presets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function addHistoricalFields(fields) {

for (let i = 1; i < 4; i++){
let id = 'source:' + i.toString();
let previousId = 'source' + ((i-1) > 0 ? ':' + (i-1).toString() : '');
fields[id] = {
...fields.source,
key: id,
Expand All @@ -72,11 +71,24 @@ function addHistoricalFields(fields) {
baseKey: 'source',
index: i,
prerequisiteTag: {
keys: [
previousId,
previousId + ':url',
previousId + ':name',
previousId + ':date']}};
prerequisiteFunction: (tag) => {
if (tag.startsWith('source') ){
// allow index 1 if there are any source tags present
if (i === 1){
return true;
} else {
// for index > 1, get the index in the tag with a regex
let indexRegex = /(?<=\:)\d+/;
let regexMatch = parseInt(tag.match(indexRegex), 10);
// allow index i if the index directly before it is present OR if any index above it is present
if (regexMatch === i-1 || regexMatch > i){
return true;
}
}
}
return false;
}}
};
}
}

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

if (!entityIDs.every(function(entityID) {
var entity = context.graph().entity(entityID);
if (prerequisiteTag.prerequisiteFunction) {
// Return true if prerequisiteFunction returns true for any tag
return Object.keys(entity.tags).some(prerequisiteTag.prerequisiteFunction);
}
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
Expand Down
Loading