Notes/src/services/search/expressions/attribute_exists.js

44 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
const NoteSet = require('../note_set');
const becca = require('../../../becca/becca');
const Expression = require('./expression');
2020-05-17 10:11:19 +02:00
2020-05-22 09:38:30 +02:00
class AttributeExistsExp extends Expression {
constructor(attributeType, attributeName, prefixMatch) {
2020-05-22 09:38:30 +02:00
super();
2020-05-16 23:12:29 +02:00
this.attributeType = attributeType;
this.attributeName = attributeName;
// template attr is used as a marker for templates, but it's not meant to be inherited
this.isTemplateLabel = this.attributeType === 'label' && (this.attributeName === 'template' || this.attributeName === 'workspacetemplate');
this.prefixMatch = prefixMatch;
2020-05-16 23:12:29 +02:00
}
execute(inputNoteSet, executionContext, searchContext) {
const attrs = this.prefixMatch
2021-04-16 23:00:08 +02:00
? becca.findAttributesWithPrefix(this.attributeType, this.attributeName)
: becca.findAttributes(this.attributeType, this.attributeName);
2020-05-16 23:12:29 +02:00
const resultNoteSet = new NoteSet();
for (const attr of attrs) {
const note = attr.note;
if (attr.isInheritable && !this.isTemplateLabel) {
resultNoteSet.addAll(note.getSubtreeNotesIncludingTemplated());
}
else if (note.isInherited() && !this.isTemplateLabel) {
2023-01-06 20:31:55 +01:00
resultNoteSet.addAll(note.getInheritingNotes());
}
else {
resultNoteSet.add(note);
2020-05-16 23:12:29 +02:00
}
}
return resultNoteSet.intersection(inputNoteSet);
2020-05-16 23:12:29 +02:00
}
}
2020-05-17 09:48:24 +02:00
2020-05-20 00:03:33 +02:00
module.exports = AttributeExistsExp;