autocomplete respects hideInAutocomplete label

This commit is contained in:
azivner 2018-04-19 00:13:55 -04:00
parent df93cb09da
commit 5ffd621e9d

View File

@ -6,6 +6,7 @@ const repository = require('./repository');
let noteTitles; let noteTitles;
let noteIds; let noteIds;
const childToParent = {}; const childToParent = {};
const hideInAutocomplete = {};
async function load() { async function load() {
noteTitles = await sql.getMap(`SELECT noteId, LOWER(title) FROM notes WHERE isDeleted = 0 AND isProtected = 0`); noteTitles = await sql.getMap(`SELECT noteId, LOWER(title) FROM notes WHERE isDeleted = 0 AND isProtected = 0`);
@ -17,6 +18,12 @@ async function load() {
childToParent[rel.noteId] = childToParent[rel.noteId] || []; childToParent[rel.noteId] = childToParent[rel.noteId] || [];
childToParent[rel.noteId].push(rel.parentNoteId); childToParent[rel.noteId].push(rel.parentNoteId);
} }
const hiddenLabels = await sql.getColumn(`SELECT noteId FROM labels WHERE isDeleted = 0 AND name = 'hideInAutocomplete'`);
for (const noteId of hiddenLabels) {
hideInAutocomplete[noteId] = true;
}
} }
function getResults(query) { function getResults(query) {
@ -28,6 +35,10 @@ function getResults(query) {
const results = []; const results = [];
for (const noteId in noteTitles) { for (const noteId in noteTitles) {
if (hideInAutocomplete[noteId]) {
continue;
}
const title = noteTitles[noteId]; const title = noteTitles[noteId];
const foundTokens = []; const foundTokens = [];
@ -55,29 +66,17 @@ function search(noteIds, tokens, path, results) {
} }
if (tokens.length === 0) { if (tokens.length === 0) {
let curNoteId = noteIds[0]; const retPath = getSomePath(noteIds, path);
while (curNoteId !== 'root') { if (retPath) {
path.push(curNoteId); const noteTitle = getNoteTitle(retPath);
const parents = childToParent[curNoteId]; results.push({
title: noteTitle,
if (!parents || parents.length === 0) { path: retPath.join('/')
return; });
}
curNoteId = parents[0];
} }
path.reverse();
const noteTitle = getNoteTitle(path);
results.push({
title: noteTitle,
path: path.join('/')
});
return; return;
} }
@ -86,7 +85,7 @@ function search(noteIds, tokens, path, results) {
return; return;
} }
if (noteId === 'root') { if (noteId === 'root' || hideInAutocomplete[noteId]) {
continue; continue;
} }
@ -116,6 +115,34 @@ function getNoteTitle(path) {
return titles.join(' / '); return titles.join(' / ');
} }
function getSomePath(noteIds, path) {
for (const noteId of noteIds) {
if (noteId === 'root') {
path.reverse();
return path;
}
if (hideInAutocomplete[noteId]) {
continue;
}
const parents = childToParent[noteId];
if (!parents || parents.length === 0) {
continue;
}
const retPath = getSomePath(parents, path.concat([noteId]));
if (retPath) {
return retPath;
}
}
return false;
}
syncTableService.addListener(async (entityName, entityId) => { syncTableService.addListener(async (entityName, entityId) => {
if (entityName === 'notes') { if (entityName === 'notes') {
const note = await repository.getNote(entityId); const note = await repository.getNote(entityId);
@ -128,6 +155,22 @@ syncTableService.addListener(async (entityName, entityId) => {
noteTitles[note.noteId] = note.title; noteTitles[note.noteId] = note.title;
} }
} }
else if (entityName === 'labels') {
const label = await repository.getLabel(entityId);
if (label.name === 'hideInAutocomplete') {
// we're not using label object directly, since there might be other non-deleted hideInAutocomplete label
const hideLabel = await repository.getEntity(`SELECT * FROM labels WHERE isDeleted = 0
AND name = 'hideInAutocomplete' AND noteId = ?`, [label.noteId]);
if (hideLabel) {
hideInAutocomplete[label.noteId] = true;
}
else {
delete hideInAutocomplete[label.noteId];
}
}
}
}); });
sqlInit.dbReady.then(load); sqlInit.dbReady.then(load);