Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jsdialog: move sorting to separate function
Browse files Browse the repository at this point in the history
no functional change

Signed-off-by: Szymon Kłos <szymon.klos@collabora.com>
Change-Id: I79f1b7714130b9d632f4fed32815e5245e6afe2e
eszkadev committed Jan 8, 2025
1 parent b868496 commit 8da4259
Showing 1 changed file with 71 additions and 71 deletions.
142 changes: 71 additions & 71 deletions browser/src/control/jsdialog/Widget.TreeView.ts
Original file line number Diff line number Diff line change
@@ -992,6 +992,75 @@ class TreeViewControl {
return isRealTreeView;
}

getSortComparator(columnIndex: number, up: boolean) {
return (a: HTMLElement, b: HTMLElement) => {
if (!a || !b) return 0;

var tda = a.querySelectorAll('div').item(columnIndex);
var tdb = b.querySelectorAll('div').item(columnIndex);

if (tda.querySelector('input')) {
if (
tda.querySelector('input').checked ===
tdb.querySelector('input').checked
)
return 0;
if (up) {
if (
tda.querySelector('input').checked >
tdb.querySelector('input').checked
)
return 1;
else return -1;
} else if (
tdb.querySelector('input').checked >
tda.querySelector('input').checked
)
return 1;
else return -1;
}

if (up)
return tdb.innerText
.toLowerCase()
.localeCompare(tda.innerText.toLowerCase());
else
return tda.innerText
.toLowerCase()
.localeCompare(tdb.innerText.toLowerCase());
};
}

sortByColumn(columnIndex: number, up: boolean) {
this.clearSorting();

var toSort: Array<HTMLDivElement> = [];

const container = this._container;
container
.querySelectorAll(
':not(.ui-treeview-expanded-content) .ui-treeview-entry',
)
.forEach((item: HTMLDivElement) => {
toSort.push(item);
container.removeChild(item);
});

toSort.sort(this.getSortComparator(columnIndex, up));

toSort.forEach((item) => {
container.insertBefore(item, container.lastChild.nextSibling);
});
}

clearSorting() {
var icons = this._thead.querySelectorAll('.ui-treeview-header-sort-icon');
icons.forEach((icon) => {
L.DomUtil.removeClass(icon, 'down');
L.DomUtil.removeClass(icon, 'up');
});
}

fillHeaders(headers: Array<TreeHeaderJSON>, builder: any) {
if (!headers) return;

@@ -1015,83 +1084,14 @@ class TreeViewControl {
for (const index in headers) {
this.fillHeader(headers[index], builder);

var sortByColumn = (columnIndex: number, up: boolean) => {
var compareFunction = (a: HTMLElement, b: HTMLElement) => {
if (!a || !b) return 0;

var tda = a.querySelectorAll('div').item(columnIndex);
var tdb = b.querySelectorAll('div').item(columnIndex);

if (tda.querySelector('input')) {
if (
tda.querySelector('input').checked ===
tdb.querySelector('input').checked
)
return 0;
if (up) {
if (
tda.querySelector('input').checked >
tdb.querySelector('input').checked
)
return 1;
else return -1;
} else if (
tdb.querySelector('input').checked >
tda.querySelector('input').checked
)
return 1;
else return -1;
}

if (up)
return tdb.innerText
.toLowerCase()
.localeCompare(tda.innerText.toLowerCase());
else
return tda.innerText
.toLowerCase()
.localeCompare(tdb.innerText.toLowerCase());
};

var toSort: Array<HTMLDivElement> = [];

const container = this._container;
container
.querySelectorAll(
':not(.ui-treeview-expanded-content) .ui-treeview-entry',
)
.forEach((item: HTMLDivElement) => {
toSort.push(item);
container.removeChild(item);
});

toSort.sort(compareFunction);

toSort.forEach((item) => {
container.insertBefore(item, container.lastChild.nextSibling);
});
};

var clickFunction = (columnIndex: number, icon: HTMLSpanElement) => {
var clearSorting = () => {
var icons = this._thead.querySelectorAll(
'.ui-treeview-header-sort-icon',
);
icons.forEach((icon) => {
L.DomUtil.removeClass(icon, 'down');
L.DomUtil.removeClass(icon, 'up');
});
};

return () => {
if (L.DomUtil.hasClass(icon, 'down')) {
clearSorting();
L.DomUtil.addClass(icon, 'up');
sortByColumn(columnIndex + dummyCells, true);
this.sortByColumn(columnIndex + dummyCells, true);
} else {
clearSorting();
L.DomUtil.addClass(icon, 'down');
sortByColumn(columnIndex + dummyCells, false);
this.sortByColumn(columnIndex + dummyCells, false);
}
};
};

0 comments on commit 8da4259

Please sign in to comment.