@@ -3,16 +3,15 @@ import * as model from "../model";
33import { onSelectionUpdate , updateSelection , getSelection } from "../selection" ;
44
55let sorting = {
6- enabled : false ,
76 properties : [ "entities" , "0" , "score" ] ,
87 order : 1
98} ;
109
1110let sorter = ( a , b ) => {
1211 let i = 0 ;
1312 while ( i < sorting . properties . length && typeof ( a = a [ sorting . properties [ i ] ] ) !== "undefined"
14- && typeof ( b = b [ sorting . properties [ i ] ] ) !== "undefined" ) { console . log ( i ) ; ++ i }
15- return a > b ? - sorting . order : a === b ? 0 : sorting . order ;
13+ && typeof ( b = b [ sorting . properties [ i ] ] ) !== "undefined" ) { ++ i }
14+ return a > b ? sorting . order : a === b ? 0 : - sorting . order ;
1615} ;
1716
1817function updateSelected ( ) {
@@ -38,12 +37,42 @@ function updateOthers () {
3837
3938}
4039
40+ function updateAll ( ) {
41+ updateSelected ( ) ;
42+ updateOthers ( ) ;
43+ }
44+
4145onSelectionUpdate ( ( ) => {
4246 if ( ! model . uiState . tabularToggled )
4347 return ;
4448 updateSelected ( ) ;
4549} ) ;
4650
51+ /**
52+ * @this {HTMLElement} TH
53+ */
54+ function columnClicked ( ) {
55+ let attr = this . getAttribute ( "data-prop" ) ,
56+ arr = attr . split ( "." ) ;
57+ if ( attr === sorting . properties . join ( "." ) )
58+ sorting . order = sorting . order === 1 ? - 1 : sorting . order === - 1 ? 0 : 1 ;
59+ else
60+ sorting . order = 1 ;
61+ sorting . properties = arr ;
62+ updateAll ( ) ;
63+ updateHeaders ( attr ) ;
64+ }
65+
66+ function updateHeaders ( dataProp = undefined ) {
67+ [ ] . slice . call ( document . querySelectorAll ( "#tabular thead th" ) ) . forEach ( ( th ) => {
68+ th . classList . remove ( "sort-up" ) ;
69+ th . classList . remove ( "sort-down" ) ;
70+ if ( th . getAttribute ( "data-prop" ) !== dataProp )
71+ return ;
72+ th . classList . toggle ( `sort-${ sorting . order === 1 ? "up" : "down" } ` , sorting . order !== 0 ) ;
73+ } ) ;
74+ }
75+
4776export function init ( ) {
4877
4978 d3 . select ( "#tableToggle" )
@@ -61,4 +90,11 @@ export function init () {
6190 ) ) ;
6291 } ) ;
6392
93+ [ ] . slice . call ( document . querySelectorAll ( "#tabular thead th" ) ) . forEach ( ( th ) => {
94+ if ( ! th . getAttribute ( "data-prop" ) ) return ;
95+ th . addEventListener ( "click" , columnClicked ) ;
96+ } ) ;
97+
98+ updateHeaders ( sorting . properties . join ( "." ) ) ;
99+
64100}
0 commit comments