mirror of
https://github.com/zadam/trilium.git
synced 2025-11-16 18:25:51 +01:00
cleanup of labels & relations frontend code
This commit is contained in:
@@ -22,7 +22,7 @@ const BUILTIN_ATTRIBUTES = [
|
||||
{ type: 'relation', name: 'runOnNoteTitleChange' }
|
||||
];
|
||||
|
||||
async function getNotesWithAttribute(name, value) {
|
||||
async function getNotesWithLabel(name, value) {
|
||||
let notes;
|
||||
|
||||
if (value !== undefined) {
|
||||
@@ -37,8 +37,8 @@ async function getNotesWithAttribute(name, value) {
|
||||
return notes;
|
||||
}
|
||||
|
||||
async function getNoteWithAttribute(name, value) {
|
||||
const notes = await getNotesWithAttribute(name, value);
|
||||
async function getNoteWithLabel(name, value) {
|
||||
const notes = await getNotesWithLabel(name, value);
|
||||
|
||||
return notes.length > 0 ? notes[0] : null;
|
||||
}
|
||||
@@ -70,10 +70,62 @@ async function getAttributeNames(type, nameLike) {
|
||||
return names;
|
||||
}
|
||||
|
||||
async function getEffectiveAttributes(noteId) {
|
||||
const attributes = await repository.getEntities(`
|
||||
WITH RECURSIVE tree(noteId, level) AS (
|
||||
SELECT ?, 0
|
||||
UNION
|
||||
SELECT branches.parentNoteId, tree.level + 1 FROM branches
|
||||
JOIN tree ON branches.noteId = tree.noteId
|
||||
JOIN notes ON notes.noteId = branches.parentNoteId
|
||||
WHERE notes.isDeleted = 0 AND branches.isDeleted = 0
|
||||
)
|
||||
SELECT attributes.* FROM attributes JOIN tree ON attributes.noteId = tree.noteId
|
||||
WHERE attributes.isDeleted = 0 AND (attributes.isInheritable = 1 OR attributes.noteId = ?)
|
||||
ORDER BY level, noteId, position`, [noteId, noteId]);
|
||||
// attributes are ordered so that "closest" attributes are first
|
||||
// we order by noteId so that attributes from same note stay together. Actual noteId ordering doesn't matter.
|
||||
|
||||
const filteredAttributes = attributes.filter((attr, index) => {
|
||||
if (attr.isDefinition()) {
|
||||
const firstDefinitionIndex = attributes.findIndex(el => el.type === attr.type && el.name === attr.name);
|
||||
|
||||
// keep only if this element is the first definition for this type & name
|
||||
return firstDefinitionIndex === index;
|
||||
}
|
||||
else {
|
||||
const definitionAttr = attributes.find(el => el.type === attr.type + '-definition' && el.name === attr.name);
|
||||
|
||||
if (!definitionAttr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const definition = definitionAttr.value;
|
||||
|
||||
if (definition.multiplicityType === 'multivalue') {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
const firstAttrIndex = attributes.findIndex(el => el.type === attr.type && el.name === attr.name);
|
||||
|
||||
// in case of single-valued attribute we'll keep it only if it's first (closest)
|
||||
return firstAttrIndex === index;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (const attr of filteredAttributes) {
|
||||
attr.isOwned = attr.noteId === noteId;
|
||||
}
|
||||
|
||||
return filteredAttributes;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getNotesWithAttribute,
|
||||
getNoteWithAttribute,
|
||||
getNotesWithLabel,
|
||||
getNoteWithLabel,
|
||||
createAttribute,
|
||||
getAttributeNames,
|
||||
getEffectiveAttributes,
|
||||
BUILTIN_ATTRIBUTES
|
||||
};
|
||||
@@ -3,7 +3,7 @@ const noteService = require('./notes');
|
||||
const sql = require('./sql');
|
||||
const utils = require('./utils');
|
||||
const dateUtils = require('./date_utils');
|
||||
const labelService = require('./labels');
|
||||
const attributeService = require('./attributes');
|
||||
const dateNoteService = require('./date_notes');
|
||||
const treeService = require('./tree');
|
||||
const config = require('./config');
|
||||
@@ -45,15 +45,14 @@ function ScriptApi(startNote, currentNote, workNote) {
|
||||
|
||||
this.getNote = repository.getNote;
|
||||
this.getBranch = repository.getBranch;
|
||||
this.getLabel = repository.getLabel;
|
||||
this.getRelation = repository.getRelation;
|
||||
this.getAttribute = repository.getAttribute;
|
||||
this.getImage = repository.getImage;
|
||||
this.getEntity = repository.getEntity;
|
||||
this.getEntities = repository.getEntities;
|
||||
|
||||
this.createLabel = labelService.createLabel;
|
||||
this.getNotesWithLabel = labelService.getNotesWithLabel;
|
||||
this.getNoteWithLabel = labelService.getNoteWithLabel;
|
||||
this.createAttribute = attributeService.createAttribute;
|
||||
this.getNotesWithLabel = attributeService.getNotesWithLabel;
|
||||
this.getNoteWithLabel = attributeService.getNoteWithLabel;
|
||||
|
||||
this.createNote = noteService.createNote;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user