@@ -3,16 +3,15 @@ import * as model from "../model";
3
3
import { onSelectionUpdate , updateSelection , getSelection } from "../selection" ;
4
4
5
5
let sorting = {
6
- enabled : false ,
7
6
properties : [ "entities" , "0" , "score" ] ,
8
7
order : 1
9
8
} ;
10
9
11
10
let sorter = ( a , b ) => {
12
11
let i = 0 ;
13
12
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 ;
16
15
} ;
17
16
18
17
function updateSelected ( ) {
@@ -38,12 +37,42 @@ function updateOthers () {
38
37
39
38
}
40
39
40
+ function updateAll ( ) {
41
+ updateSelected ( ) ;
42
+ updateOthers ( ) ;
43
+ }
44
+
41
45
onSelectionUpdate ( ( ) => {
42
46
if ( ! model . uiState . tabularToggled )
43
47
return ;
44
48
updateSelected ( ) ;
45
49
} ) ;
46
50
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
+
47
76
export function init ( ) {
48
77
49
78
d3 . select ( "#tableToggle" )
@@ -61,4 +90,11 @@ export function init () {
61
90
) ) ;
62
91
} ) ;
63
92
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
+
64
100
}
0 commit comments