Skip to content

Commit

Permalink
tree view is now always sorted (on move, edit, and add)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniloNovakovic committed Oct 21, 2018
1 parent 0f23cc0 commit 8ea08d3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
36 changes: 34 additions & 2 deletions src/js/bookmarkManager/treeView.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ document.addEventListener('DOMContentLoaded', () => {
onDragover: allowDrop,
onDragstart: drag
});
parent.querySelector('ul').appendChild(newEl);

appendSorted(parent.querySelector('ul'), newEl);

// note: i wrapped this in timeout because storage is updated AFTER bookmark is created
setTimeout(() => {
Expand All @@ -82,6 +83,9 @@ document.addEventListener('DOMContentLoaded', () => {
chrome.bookmarks.onChanged.addListener((id, changeInfo) => {
if (changeInfo.title) {
let elem = document.getElementById(id);
elem.setAttribute('name', changeInfo.title);
appendSorted(elem.parentElement, elem);

if (elem.classList.contains('folder')) {
elem = elem.querySelector('.folder-header') || elem;
}
Expand All @@ -94,7 +98,7 @@ document.addEventListener('DOMContentLoaded', () => {
const elem = document.getElementById(id);
const parent = document.getElementById(moveInfo.parentId);
if (parent.classList.contains('folder')) {
parent.querySelector('ul').appendChild(elem);
appendSorted(parent.querySelector('ul'), elem);
}
setTimeout(() => {
if (elem.classList.contains('folder')) {
Expand All @@ -107,3 +111,31 @@ document.addEventListener('DOMContentLoaded', () => {
}, 100);
});
});

function appendSorted(parent, element) {
if (!parent) return console.warn('parent in appendSorted is undefined');
if (!element) return console.warn('element in appendSorted is undefined');
let appended = false;
const elemName = element.getAttribute('name').toLowerCase();
const isElemFolder = element.classList.contains('folder');
for (let child of parent.children) {
try {
const childName = child.getAttribute('name').toLowerCase();
const isChildFolder = child.classList.contains('folder');

if (
(isElemFolder && !isChildFolder) ||
(isElemFolder === isChildFolder && childName > elemName)
) {
parent.insertBefore(element, child);
appended = true;
break;
}
} catch (err) {
console.warn(err);
}
}
if (!appended) {
parent.appendChild(element);
}
}
2 changes: 1 addition & 1 deletion src/js/components/Folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Folder = (props, ...children) => {
const folderName = name || 'unknown';
const iconColor = folderIconColor || defaultFolderIconColor;
return div(
{ className: 'folder', ...(id && { id }) },
{ className: 'folder', name: folderName, ...(id && { id }) },
header(
{
...headerParams,
Expand Down

0 comments on commit 8ea08d3

Please sign in to comment.