Improve sorting behavior by adding delay
This commit is contained in:
1 parent
7941d929d2
commit
dabf1b2f43
10 files changed
+75
-87
No files matched your search
@@ -0,0 +1,16 @@
|
|||||||
|
export default function debounce<T>(
|
||||||
|
func: (args: T[]) => void,
|
||||||
|
timeout: number
|
||||||
|
): (arg: T) => void {
|
||||||
|
let timer: ReturnType<typeof setTimeout>;
|
||||||
|
let args: T[] = [];
|
||||||
|
return (arg: T) => {
|
||||||
|
args.push(arg);
|
||||||
|
clearTimeout(timer);
|
||||||
|
timer = setTimeout(() => {
|
||||||
|
const argscopy = args;
|
||||||
|
args = [];
|
||||||
|
func(argscopy);
|
||||||
|
}, timeout);
|
||||||
|
};
|
||||||
|
}
|
||||||
+7
-11
@@ -268,18 +268,14 @@ export const component: ClosureComponent = (): Component => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onSortChange(sortedAttrs): void {
|
function onSortChange(sortedAttrs): void {
|
||||||
let _sort = Object.assign({}, sort);
|
const _sort = {};
|
||||||
for (const [index, direction] of Object.entries(sortedAttrs)) {
|
for (const index of sortedAttrs) {
|
||||||
const param = memoizedGetSortable(attributes[index].parameter);
|
const param = memoizedGetSortable(
|
||||||
if (param) {
|
attributes[Math.abs(index) - 1].parameter
|
||||||
// Changing the priority of columns
|
);
|
||||||
delete _sort[param];
|
_sort[param] = Math.sign(index);
|
||||||
if (direction) _sort = Object.assign({ [param]: direction }, _sort);
|
|
||||||
}
|
}
|
||||||
}
|
const ops = { sort: JSON.stringify(_sort) };
|
||||||
|
|
||||||
const ops = {};
|
|
||||||
if (Object.keys(_sort).length) ops["sort"] = JSON.stringify(_sort);
|
|
||||||
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
||||||
m.route.set("/devices", ops);
|
m.route.set("/devices", ops);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-10
@@ -107,16 +107,10 @@ export const component: ClosureComponent = (): Component => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onSortChange(sortAttrs): void {
|
function onSortChange(sortAttrs): void {
|
||||||
let _sort = Object.assign({}, sort);
|
const _sort = {};
|
||||||
for (const [index, direction] of Object.entries(sortAttrs)) {
|
for (const index of sortAttrs)
|
||||||
// Changing the priority of columns
|
_sort[attributes[Math.abs(index) - 1].id] = Math.sign(index);
|
||||||
delete _sort[attributes[index].id];
|
const ops = { sort: JSON.stringify(_sort) };
|
||||||
if (direction)
|
|
||||||
_sort = Object.assign({ [attributes[index].id]: direction }, _sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ops = {};
|
|
||||||
if (Object.keys(_sort).length) ops["sort"] = JSON.stringify(_sort);
|
|
||||||
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
||||||
m.route.set("/faults", ops);
|
m.route.set("/faults", ops);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-10
@@ -151,16 +151,10 @@ export const component: ClosureComponent = (): Component => {
|
|||||||
sortAttributes[i] = sort[attributes[i].id] || 0;
|
sortAttributes[i] = sort[attributes[i].id] || 0;
|
||||||
|
|
||||||
function onSortChange(sortAttrs): void {
|
function onSortChange(sortAttrs): void {
|
||||||
let _sort = Object.assign({}, sort);
|
const _sort = {};
|
||||||
for (const [index, direction] of Object.entries(sortAttrs)) {
|
for (const index of sortAttrs)
|
||||||
// Changing the priority of columns
|
_sort[attributes[Math.abs(index) - 1].id] = Math.sign(index);
|
||||||
delete _sort[attributes[index].id];
|
const ops = { sort: JSON.stringify(_sort) };
|
||||||
if (direction)
|
|
||||||
_sort = Object.assign({ [attributes[index].id]: direction }, _sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ops = {};
|
|
||||||
if (Object.keys(_sort).length) ops["sort"] = JSON.stringify(_sort);
|
|
||||||
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
||||||
m.route.set("/admin/files", ops);
|
m.route.set("/admin/files", ops);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
import { ClosureComponent, Component, Children } from "mithril";
|
import { ClosureComponent, Component, Children } from "mithril";
|
||||||
import { m } from "./components";
|
import { m } from "./components";
|
||||||
import { getIcon } from "./icons";
|
import { getIcon } from "./icons";
|
||||||
|
import debounce from "../lib/common/debounce";
|
||||||
|
|
||||||
interface Attribute {
|
interface Attribute {
|
||||||
id?: string;
|
id?: string;
|
||||||
@@ -36,7 +37,7 @@ function renderTable(
|
|||||||
showMoreCallback: () => void,
|
showMoreCallback: () => void,
|
||||||
selected: Set<string>,
|
selected: Set<string>,
|
||||||
sortAttributes: Record<string, any>,
|
sortAttributes: Record<string, any>,
|
||||||
onSortChange: (obj: Record<string, number>) => void,
|
onSort: (i: number) => void,
|
||||||
downloadUrl?: string,
|
downloadUrl?: string,
|
||||||
valueCallback?: (attr: Attribute, record: Record<string, any>) => Children,
|
valueCallback?: (attr: Attribute, record: Record<string, any>) => Children,
|
||||||
actionsCallback?: Children | ((sel: Set<string>) => Children),
|
actionsCallback?: Children | ((sel: Set<string>) => Children),
|
||||||
@@ -86,9 +87,9 @@ function renderTable(
|
|||||||
const sortable = m(
|
const sortable = m(
|
||||||
"button",
|
"button",
|
||||||
{
|
{
|
||||||
onclick: () => {
|
onclick: (e) => {
|
||||||
const direction = ((sortAttributes[i] + 2) % 3) - 1;
|
e.redraw = false;
|
||||||
return onSortChange({ [i]: direction });
|
onSort(i);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
symbol
|
symbol
|
||||||
@@ -213,7 +214,10 @@ function renderTable(
|
|||||||
|
|
||||||
const component: ClosureComponent = (): Component => {
|
const component: ClosureComponent = (): Component => {
|
||||||
let selected = new Set<string>();
|
let selected = new Set<string>();
|
||||||
|
let sortingfunction: (events: number[]) => void;
|
||||||
|
const onSort = debounce((events: number[]) => {
|
||||||
|
sortingfunction(events);
|
||||||
|
}, 500);
|
||||||
return {
|
return {
|
||||||
view: (vnode) => {
|
view: (vnode) => {
|
||||||
const attributes = vnode.attrs["attributes"];
|
const attributes = vnode.attrs["attributes"];
|
||||||
@@ -232,6 +236,20 @@ const component: ClosureComponent = (): Component => {
|
|||||||
const id = record["_id"] || record["DeviceID.ID"].value[0];
|
const id = record["_id"] || record["DeviceID.ID"].value[0];
|
||||||
if (selected.has(id)) _selected.add(id);
|
if (selected.has(id)) _selected.add(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sortingfunction = (events) => {
|
||||||
|
const sortArray = new Set(
|
||||||
|
Object.keys(sortAttributes)
|
||||||
|
.map((x) => (parseInt(x) + 1) * sortAttributes[x])
|
||||||
|
.filter((x) => x)
|
||||||
|
);
|
||||||
|
for (const num of events) {
|
||||||
|
if (sortArray.delete(num + 1)) sortArray.add(-(num + 1));
|
||||||
|
else if (!sortArray.delete((num + 1) * -1)) sortArray.add(num + 1);
|
||||||
|
}
|
||||||
|
onSortChange(Array.from(sortArray).reverse());
|
||||||
|
};
|
||||||
|
|
||||||
selected = _selected;
|
selected = _selected;
|
||||||
|
|
||||||
return renderTable(
|
return renderTable(
|
||||||
@@ -241,7 +259,7 @@ const component: ClosureComponent = (): Component => {
|
|||||||
showMoreCallback,
|
showMoreCallback,
|
||||||
selected,
|
selected,
|
||||||
sortAttributes,
|
sortAttributes,
|
||||||
onSortChange,
|
onSort,
|
||||||
downloadUrl,
|
downloadUrl,
|
||||||
valueCallback,
|
valueCallback,
|
||||||
actionsCallback,
|
actionsCallback,
|
||||||
|
|||||||
+4
-10
@@ -205,16 +205,10 @@ export const component: ClosureComponent = (): Component => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onSortChange(sortAttrs): void {
|
function onSortChange(sortAttrs): void {
|
||||||
let _sort = Object.assign({}, sort);
|
const _sort = {};
|
||||||
for (const [index, direction] of Object.entries(sortAttrs)) {
|
for (const index of sortAttrs)
|
||||||
// Changing the priority of columns
|
_sort[attributes[Math.abs(index) - 1].id] = Math.sign(index);
|
||||||
delete _sort[attributes[index].id];
|
const ops = { sort: JSON.stringify(_sort) };
|
||||||
if (direction)
|
|
||||||
_sort = Object.assign({ [attributes[index].id]: direction }, _sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ops = {};
|
|
||||||
if (Object.keys(_sort).length) ops["sort"] = JSON.stringify(_sort);
|
|
||||||
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
||||||
m.route.set("/admin/permissions", ops);
|
m.route.set("/admin/permissions", ops);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-10
@@ -190,16 +190,10 @@ export const component: ClosureComponent = (): Component => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onSortChange(sortAttrs): void {
|
function onSortChange(sortAttrs): void {
|
||||||
let _sort = Object.assign({}, sort);
|
const _sort = {};
|
||||||
for (const [index, direction] of Object.entries(sortAttrs)) {
|
for (const index of sortAttrs)
|
||||||
// Changing the priority of columns
|
_sort[attributes[Math.abs(index) - 1].id] = Math.sign(index);
|
||||||
delete _sort[attributes[index].id];
|
const ops = { sort: JSON.stringify(_sort) };
|
||||||
if (direction)
|
|
||||||
_sort = Object.assign({ [attributes[index].id]: direction }, _sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ops = {};
|
|
||||||
if (Object.keys(_sort).length) ops["sort"] = JSON.stringify(_sort);
|
|
||||||
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
||||||
m.route.set("/admin/presets", ops);
|
m.route.set("/admin/presets", ops);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-10
@@ -173,16 +173,10 @@ export const component: ClosureComponent = (): Component => {
|
|||||||
sortAttributes[i] = sort[attributes[i].id] || 0;
|
sortAttributes[i] = sort[attributes[i].id] || 0;
|
||||||
|
|
||||||
function onSortChange(sortAttrs): void {
|
function onSortChange(sortAttrs): void {
|
||||||
let _sort = Object.assign({}, sort);
|
const _sort = {};
|
||||||
for (const [index, direction] of Object.entries(sortAttrs)) {
|
for (const index of sortAttrs)
|
||||||
// Changing the priority of columns
|
_sort[attributes[Math.abs(index) - 1].id] = Math.sign(index);
|
||||||
delete _sort[attributes[index].id];
|
const ops = { sort: JSON.stringify(_sort) };
|
||||||
if (direction)
|
|
||||||
_sort = Object.assign({ [attributes[index].id]: direction }, _sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ops = {};
|
|
||||||
if (Object.keys(_sort).length) ops["sort"] = JSON.stringify(_sort);
|
|
||||||
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
||||||
m.route.set("/admin/provisions", ops);
|
m.route.set("/admin/provisions", ops);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-10
@@ -186,16 +186,10 @@ export const component: ClosureComponent = (): Component => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onSortChange(sortAttrs): void {
|
function onSortChange(sortAttrs): void {
|
||||||
let _sort = Object.assign({}, sort);
|
const _sort = {};
|
||||||
for (const [index, direction] of Object.entries(sortAttrs)) {
|
for (const index of sortAttrs)
|
||||||
// Changing the priority of columns
|
_sort[attributes[Math.abs(index) - 1].id] = Math.sign(index);
|
||||||
delete _sort[attributes[index].id];
|
const ops = { sort: JSON.stringify(_sort) };
|
||||||
if (direction)
|
|
||||||
_sort = Object.assign({ [attributes[index].id]: direction }, _sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ops = {};
|
|
||||||
if (Object.keys(_sort).length) ops["sort"] = JSON.stringify(_sort);
|
|
||||||
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
||||||
m.route.set("/admin/users", ops);
|
m.route.set("/admin/users", ops);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,16 +173,10 @@ export const component: ClosureComponent = (): Component => {
|
|||||||
sortAttributes[i] = sort[attributes[i].id] || 0;
|
sortAttributes[i] = sort[attributes[i].id] || 0;
|
||||||
|
|
||||||
function onSortChange(sortAttrs): void {
|
function onSortChange(sortAttrs): void {
|
||||||
let _sort = Object.assign({}, sort);
|
const _sort = {};
|
||||||
for (const [index, direction] of Object.entries(sortAttrs)) {
|
for (const index of sortAttrs)
|
||||||
// Changing the priority of columns
|
_sort[attributes[Math.abs(index) - 1].id] = Math.sign(index);
|
||||||
delete _sort[attributes[index].id];
|
const ops = { sort: JSON.stringify(_sort) };
|
||||||
if (direction)
|
|
||||||
_sort = Object.assign({ [attributes[index].id]: direction }, _sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
const ops = {};
|
|
||||||
if (Object.keys(_sort).length) ops["sort"] = JSON.stringify(_sort);
|
|
||||||
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
if (vnode.attrs["filter"]) ops["filter"] = vnode.attrs["filter"];
|
||||||
m.route.set("/admin/virtualParameters", ops);
|
m.route.set("/admin/virtualParameters", ops);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user