2020-10-23 00:11:44 +02:00
|
|
|
import server from './server.js';
|
2021-08-25 22:49:24 +02:00
|
|
|
import froca from './froca.js';
|
2024-12-19 20:51:47 +02:00
|
|
|
import FNote from '../entities/fnote.js';
|
2020-10-23 00:11:44 +02:00
|
|
|
|
2024-12-19 20:51:47 +02:00
|
|
|
async function addLabel(noteId: string, name: string, value: string = "") {
|
2020-10-23 00:11:44 +02:00
|
|
|
await server.put(`notes/${noteId}/attribute`, {
|
|
|
|
|
type: 'label',
|
|
|
|
|
name: name,
|
|
|
|
|
value: value
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 20:51:47 +02:00
|
|
|
async function setLabel(noteId: string, name: string, value: string = "") {
|
2020-10-24 23:50:32 +02:00
|
|
|
await server.put(`notes/${noteId}/set-attribute`, {
|
|
|
|
|
type: 'label',
|
|
|
|
|
name: name,
|
|
|
|
|
value: value
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 20:51:47 +02:00
|
|
|
async function removeAttributeById(noteId: string, attributeId: string) {
|
2020-10-23 00:11:44 +02:00
|
|
|
await server.remove(`notes/${noteId}/attributes/${attributeId}`);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 22:49:24 +02:00
|
|
|
/**
|
2023-01-05 23:38:41 +01:00
|
|
|
* @returns {boolean} - returns true if this attribute has the potential to influence the note in the argument.
|
2021-08-25 22:49:24 +02:00
|
|
|
* That can happen in multiple ways:
|
|
|
|
|
* 1. attribute is owned by the note
|
|
|
|
|
* 2. attribute is owned by the template of the note
|
|
|
|
|
* 3. attribute is owned by some note's ancestor and is inheritable
|
|
|
|
|
*/
|
2024-12-19 20:51:47 +02:00
|
|
|
function isAffecting(this: { isInheritable: boolean }, attrRow: { noteId: string }, affectedNote: FNote) {
|
2021-08-25 22:49:24 +02:00
|
|
|
if (!affectedNote || !attrRow) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const attrNote = froca.notes[attrRow.noteId];
|
|
|
|
|
|
|
|
|
|
if (!attrNote) {
|
2023-01-15 21:04:17 +01:00
|
|
|
// the note (owner of the attribute) is not even loaded into the cache, so it should not affect anything else
|
2021-08-25 22:49:24 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-06 20:31:55 +01:00
|
|
|
const owningNotes = [affectedNote, ...affectedNote.getNotesToInheritAttributesFrom()];
|
2021-08-25 22:49:24 +02:00
|
|
|
|
|
|
|
|
for (const owningNote of owningNotes) {
|
|
|
|
|
if (owningNote.noteId === attrNote.noteId) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-19 20:51:47 +02:00
|
|
|
// TODO: This doesn't seem right.
|
2021-08-25 22:49:24 +02:00
|
|
|
if (this.isInheritable) {
|
|
|
|
|
for (const owningNote of owningNotes) {
|
2023-02-01 22:55:31 +01:00
|
|
|
if (owningNote.hasAncestor(attrNote.noteId, true)) {
|
2021-08-25 22:49:24 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 00:11:44 +02:00
|
|
|
export default {
|
|
|
|
|
addLabel,
|
2020-10-24 23:50:32 +02:00
|
|
|
setLabel,
|
2021-08-25 22:49:24 +02:00
|
|
|
removeAttributeById,
|
|
|
|
|
isAffecting
|
2020-10-23 00:11:44 +02:00
|
|
|
}
|