added "sort by" dialog

This commit is contained in:
zadam 2021-02-28 23:40:15 +01:00
parent 1c59bc4d3c
commit 4aa27b6033
9 changed files with 84 additions and 39 deletions

View File

@ -2,7 +2,23 @@ import server from "../services/server.js";
import utils from "../services/utils.js";
const $dialog = $("#sort-child-notes-dialog");
const $form = $("#sort-child-notes-form");
let parentNoteId = null;
$form.on('submit', async () => {
const sortBy = $form.find("input[name='sort-by']:checked").val();
const sortDirection = $form.find("input[name='sort-direction']:checked").val();
await server.put(`notes/${parentNoteId}/sort-children`, {sortBy, sortDirection});
utils.closeActiveDialog();
});
export async function showDialog(noteId) {
parentNoteId = noteId;
export async function showDialog() {
utils.openDialog($dialog);
$form.find('input:first').focus();
}

View File

@ -1370,7 +1370,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
}
sortChildNotesCommand({node}) {
import("../dialogs/sort_child_notes.js").then(d => d.showDialog(node));
import("../dialogs/sort_child_notes.js").then(d => d.showDialog(node.data.noteId));
}
async recentChangesInSubtreeCommand({node}) {

View File

@ -4,6 +4,7 @@ const noteService = require('../../services/notes');
const treeService = require('../../services/tree');
const repository = require('../../services/repository');
const utils = require('../../services/utils');
const log = require('../../services/log');
const TaskContext = require('../../services/task_context');
function getNote(req) {
@ -85,10 +86,20 @@ function undeleteNote(req) {
taskContext.taskSucceeded();
}
function sortNotes(req) {
function sortChildNotes(req) {
const noteId = req.params.noteId;
const {sortBy, sortDirection} = req.body;
treeService.sortNotesAlphabetically(noteId);
log.info(`Sorting ${noteId} children with ${sortBy} ${sortDirection}`);
const reverse = sortDirection === 'desc';
if (sortBy === 'title') {
treeService.sortNotesByTitle(noteId, false, reverse);
}
else {
treeService.sortNotes(noteId, sortBy, reverse);
}
}
function protectNote(req) {
@ -215,7 +226,7 @@ module.exports = {
deleteNote,
undeleteNote,
createNote,
sortNotes,
sortChildNotes,
protectNote,
setNoteTypeMime,
getRelationMap,

View File

@ -150,7 +150,7 @@ function register(app) {
apiRoute(DELETE, '/api/notes/:noteId', notesApiRoute.deleteNote);
apiRoute(PUT, '/api/notes/:noteId/undelete', notesApiRoute.undeleteNote);
apiRoute(POST, '/api/notes/:parentNoteId/children', notesApiRoute.createNote);
apiRoute(PUT, '/api/notes/:noteId/sort', notesApiRoute.sortNotes);
apiRoute(PUT, '/api/notes/:noteId/sort-children', notesApiRoute.sortChildNotes);
apiRoute(PUT, '/api/notes/:noteId/protect/:isProtected', notesApiRoute.protectNote);
apiRoute(PUT, /\/api\/notes\/(.*)\/type\/(.*)\/mime\/(.*)/, notesApiRoute.setNoteTypeMime);
apiRoute(GET, '/api/notes/:noteId/revisions', noteRevisionsApiRoute.getNoteRevisions);

View File

@ -359,7 +359,7 @@ function BackendScriptApi(currentNote, apiParams) {
* @method
* @param {string} parentNoteId - this note's child notes will be sorted
*/
this.sortNotesAlphabetically = treeService.sortNotesAlphabetically;
this.sortNotesByTitle = treeService.sortNotesByTitle;
/**
* This method finds note by its noteId and prefix and either sets it to the given parentNoteId

View File

@ -31,7 +31,7 @@ eventService.subscribe(eventService.NOTE_TITLE_CHANGED, note => {
for (const parentNote of noteFromCache.parents) {
if (parentNote.hasLabel("sorted")) {
treeService.sortNotesAlphabetically(parentNote.noteId);
treeService.sortNotesByTitle(parentNote.noteId);
}
}
}
@ -84,14 +84,14 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
noteService.duplicateSubtreeWithoutRoot(templateNote.noteId, note.noteId);
}
else if (entity.type === 'label' && entity.name === 'sorted') {
treeService.sortNotesAlphabetically(entity.noteId);
treeService.sortNotesByTitle(entity.noteId);
if (entity.isInheritable) {
const note = noteCache.notes[entity.noteId];
if (note) {
for (const noteId of note.subtreeNoteIds) {
treeService.sortNotesAlphabetically(noteId);
treeService.sortNotesByTitle(noteId);
}
}
}

View File

@ -463,7 +463,7 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
if (!metaFile) {
// if there's no meta file then the notes are created based on the order in that tar file but that
// is usually quite random so we sort the notes in the way they would appear in the file manager
treeService.sortNotesAlphabetically(noteId, true);
treeService.sortNotesByTitle(noteId, true);
}
taskContext.increaseProgressCount();

View File

@ -106,7 +106,7 @@ function loadSubtreeNoteIds(parentNoteId, subtreeNoteIds) {
}
}
function sortNotesAlphabetically(parentNoteId, directoriesFirst = false) {
function sortNotesByTitle(parentNoteId, foldersFirst = false, reverse = false) {
sql.transactional(() => {
const notes = sql.getRows(
`SELECT branches.branchId, notes.noteId, title, isProtected,
@ -120,7 +120,7 @@ function sortNotesAlphabetically(parentNoteId, directoriesFirst = false) {
protectedSessionService.decryptNotes(notes);
notes.sort((a, b) => {
if (directoriesFirst && ((a.hasChildren && !b.hasChildren) || (!a.hasChildren && b.hasChildren))) {
if (foldersFirst && ((a.hasChildren && !b.hasChildren) || (!a.hasChildren && b.hasChildren))) {
// exactly one note of the two is a directory so the sorting will be done based on this status
return a.hasChildren ? -1 : 1;
}
@ -129,6 +129,10 @@ function sortNotesAlphabetically(parentNoteId, directoriesFirst = false) {
}
});
if (reverse) {
notes.reverse();
}
let position = 10;
for (const note of notes) {
@ -144,6 +148,33 @@ function sortNotesAlphabetically(parentNoteId, directoriesFirst = false) {
});
}
function sortNotes(parentNoteId, sortBy, reverse = false) {
sql.transactional(() => {
const notes = repository.getNote(parentNoteId).getChildNotes();
notes.sort((a, b) => a[sortBy] < b[sortBy] ? -1 : 1);
if (reverse) {
notes.reverse();
}
let position = 10;
for (const note of notes) {
const branch = note.getBranches().find(b => b.parentNoteId === parentNoteId);
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?",
[position, branch.branchId]);
noteCache.branches[branch.branchId].notePosition = position;
position += 10;
}
entityChangesService.addNoteReorderingEntityChange(parentNoteId);
});
}
/**
* @deprecated - this will be removed in the future
*/
@ -194,6 +225,7 @@ function setNoteToParent(noteId, prefix, parentNoteId) {
module.exports = {
getNotes,
validateParentChild,
sortNotesAlphabetically,
sortNotesByTitle,
sortNotes,
setNoteToParent
};

View File

@ -8,59 +8,45 @@
<span aria-hidden="true">&times;</span>
</button>
</div>
<form id="move-to-form">
<form id="sort-child-notes-form">
<div class="modal-body">
<h5>Sorting criteria</h5>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2">
<label class="form-check-label" for="defaultCheck2">
<input class="form-check-input" type="radio" name="sort-by" value="title" id="sort-by-title" checked>
<label class="form-check-label" for="sort-by-title">
title
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2">
<label class="form-check-label" for="defaultCheck2">
<input class="form-check-input" type="radio" name="sort-by" value="dateCreated" id="sort-by-date-created">
<label class="form-check-label" for="sort-by-date-created">
date created
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2">
<label class="form-check-label" for="defaultCheck2">
<input class="form-check-input" type="radio" name="sort-by" value="dateModified" id="sort-by-date-modified">
<label class="form-check-label" for="sort-by-date-modified">
date modified
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2">
<label class="form-check-label" for="defaultCheck2">
note content size
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2">
<label class="form-check-label" for="defaultCheck2">
note content size including revisions
</label>
</div>
<br/>
<h5>Sorting direction</h5>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2">
<label class="form-check-label" for="defaultCheck2">
<input class="form-check-input" type="radio" name="sort-direction" value="asc" id="sort-direction-asc" checked>
<label class="form-check-label" for="sort-direction-asc">
ascending
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2">
<label class="form-check-label" for="defaultCheck2">
<input class="form-check-input" type="radio" name="sort-direction" value="desc" id="sort-direction-desc">
<label class="form-check-label" for="sort-direction-desc">
descending
</label>
</div>