Skip to content

Commit

Permalink
Sorting in tabular view
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitaeverywhere committed Jan 22, 2017
1 parent d1bf982 commit 9631af8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iknow-entity-browser",
"version": "0.5.10",
"version": "0.5.11",
"description": "Visualizer for iKnow entities",
"main": "gulpfile.babel.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
</label>
</div>
<div class="wrapper">
<table>
<table id="tabular">
<thead>
<tr>
<th data-prop="id">ID</th>
Expand Down
42 changes: 39 additions & 3 deletions src/static/js/tabular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import * as model from "../model";
import { onSelectionUpdate, updateSelection, getSelection } from "../selection";

let sorting = {
enabled: false,
properties: ["entities", "0", "score"],
order: 1
};

let sorter = (a, b) => {
let i = 0;
while (i < sorting.properties.length && typeof (a = a[sorting.properties[i]]) !== "undefined"
&& typeof (b = b[sorting.properties[i]]) !== "undefined") { console.log(i); ++i }
return a > b ? -sorting.order : a === b ? 0 : sorting.order;
&& typeof (b = b[sorting.properties[i]]) !== "undefined") { ++i }
return a > b ? sorting.order : a === b ? 0 : -sorting.order;
};

function updateSelected () {
Expand All @@ -38,12 +37,42 @@ function updateOthers () {

}

function updateAll () {
updateSelected();
updateOthers();
}

onSelectionUpdate(() => {
if (!model.uiState.tabularToggled)
return;
updateSelected();
});

/**
* @this {HTMLElement} TH
*/
function columnClicked () {
let attr = this.getAttribute("data-prop"),
arr = attr.split(".");
if (attr === sorting.properties.join("."))
sorting.order = sorting.order === 1 ? -1 : sorting.order === -1 ? 0 : 1;
else
sorting.order = 1;
sorting.properties = arr;
updateAll();
updateHeaders(attr);
}

function updateHeaders (dataProp = undefined) {
[].slice.call(document.querySelectorAll("#tabular thead th")).forEach((th) => {
th.classList.remove("sort-up");
th.classList.remove("sort-down");
if (th.getAttribute("data-prop") !== dataProp)
return;
th.classList.toggle(`sort-${ sorting.order === 1 ? "up" : "down" }`, sorting.order !== 0);
});
}

export function init () {

d3.select("#tableToggle")
Expand All @@ -61,4 +90,11 @@ export function init () {
));
});

[].slice.call(document.querySelectorAll("#tabular thead th")).forEach((th) => {
if (!th.getAttribute("data-prop")) return;
th.addEventListener("click", columnClicked);
});

updateHeaders(sorting.properties.join("."));

}
12 changes: 12 additions & 0 deletions src/static/scss/tabular.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ $headerHeight: 36px;

}

}

#tabular th {

&.sort-up:after {
content: "";
}

&.sort-down:after {
content: "";
}

}

0 comments on commit 9631af8

Please sign in to comment.