1
- function getTrainingsTable ( trainingScores , isPassedBack ) {
2
- const trainingsTable = { } ;
3
-
4
- Object . keys ( trainingScores ) . forEach ( trainingId => {
5
- if ( trainingsTable [ trainingId ] === undefined ) {
6
- trainingsTable [ trainingId ] = { } ;
7
- }
8
- trainingsTable [ trainingId ] . score = trainingScores [ trainingId ] ;
9
- } ) ;
10
-
11
- Object . keys ( isPassedBack ) . forEach ( trainingId => {
12
- if ( trainingsTable [ trainingId ] === undefined ) {
13
- trainingsTable [ trainingId ] = { } ;
14
- }
15
- trainingsTable [ trainingId ] . passedBackStatus = isPassedBack [ trainingId ] ;
16
- } ) ;
17
-
18
- return trainingsTable ;
1
+ const tableColomnHeaders = [
2
+ "№" , "training_id" , "start_timestamp" , "training_status" , "pass_back_status" , "score"
3
+ ] ;
4
+
5
+ function get_time_string ( timestampStr ) {
6
+ const timestamp = Date . parse ( timestampStr ) ;
7
+
8
+ options = { weekday : 'short' , year : 'numeric' , month : 'long' , day : 'numeric' , hour : 'numeric' , minute : '2-digit' , second : '2-digit' , timeZoneName : 'short' }
9
+ if ( ! isNaN ( timestamp ) ) {
10
+ processing_time = new Date ( timestamp ) ;
11
+ return processing_time . toLocaleString ( "ru-RU" , options ) ;
12
+ } else {
13
+ return "" ;
14
+ }
19
15
}
20
16
21
17
function createTableHeaderElement ( ) {
22
- const trainingIdHeaderElement = document . createElement ( "th" ) ;
23
- trainingIdHeaderElement . innerHTML = "training_id" ;
24
-
25
- const trainingScoreHeaderElement = document . createElement ( "th" ) ;
26
- trainingScoreHeaderElement . innerHTML = "score" ;
27
-
28
- const trainingStatusHeaderElement = document . createElement ( "th" ) ;
29
- trainingStatusHeaderElement . innerHTML = "pass_back_status" ;
30
-
31
18
const tableHeaderElement = document . createElement ( "tr" ) ;
32
19
33
- tableHeaderElement . append ( trainingIdHeaderElement , trainingScoreHeaderElement , trainingStatusHeaderElement ) ;
20
+ tableColomnHeaders . forEach ( colomnHeader => {
21
+ const tableColomnHeaderElement = document . createElement ( "th" ) ;
22
+ tableColomnHeaderElement . innerHTML = colomnHeader ;
23
+
24
+ tableHeaderElement . appendChild ( tableColomnHeaderElement ) ;
25
+ } ) ;
34
26
35
27
return tableHeaderElement ;
36
28
}
37
29
38
- function createTableRowElement ( trainingInfo , trainingId ) {
30
+ function createTableRowElement ( ) {
39
31
const tableRowElement = document . createElement ( "tr" ) ;
40
32
41
- const tableRowIdElement = document . createElement ( "td" ) ;
42
- tableRowIdElement . innerHTML = `<a href="/trainings/statistics/${ trainingId } /">${ trainingId } </a>` ;
33
+ const tableRow = { } ;
34
+
35
+ tableColomnHeaders . forEach ( columnHeader => {
36
+ const tableCellElement = document . createElement ( "td" ) ;
37
+
38
+ tableRow [ columnHeader ] = tableCellElement ;
39
+ tableRowElement . appendChild ( tableCellElement ) ;
40
+ } ) ;
43
41
44
- const tableRowScoreElement = document . createElement ( "td" ) ;
45
- tableRowScoreElement . innerHTML = trainingInfo . score ? trainingInfo . score . toFixed ( 2 ) : "none" ;
42
+ return [ tableRowElement , tableRow ] ;
43
+ }
46
44
47
- const tableRowStatusElement = document . createElement ( "td" ) ;
48
- tableRowStatusElement . innerHTML = trainingInfo . passedBackStatus || "none" ;
45
+ function TableRowFiller ( ) {
46
+ let pos = 1 ;
49
47
50
- tableRowElement . append ( tableRowIdElement , tableRowScoreElement , tableRowStatusElement ) ;
48
+ function fillTableRow ( tableRow , training ) {
49
+ tableRow [ "№" ] . innerHTML = pos ++ ;
50
+ tableRow [ "training_id" ] . innerHTML = `<a href="/trainings/statistics/${ training . id } /">${ training . id } </a>` ;
51
+ tableRow [ "start_timestamp" ] . innerHTML = get_time_string ( training . training_start_timestamp ) ;
52
+ tableRow [ "score" ] . innerHTML = training . score ? training . score . toFixed ( 2 ) : "none" ;
53
+ tableRow [ "training_status" ] . innerHTML = training . training_status ;
54
+ tableRow [ "pass_back_status" ] . innerHTML = training . passedBackStatus || "none" ;
55
+ }
51
56
52
- return tableRowElement ;
57
+ return fillTableRow ;
53
58
}
54
59
55
- function showRelatedTrainingsTable ( trainingScores , isPassedBack ) {
56
- const trainingsTable = getTrainingsTable ( trainingScores , isPassedBack ) ;
60
+ function getSortedTrainingsList ( trainings ) {
61
+ const trainingsList = [ ] ;
62
+
63
+ Object . keys ( trainings ) . forEach ( trainingId => {
64
+ const training = trainings [ trainingId ] ;
65
+ training [ "id" ] = trainingId ;
66
+
67
+ trainingsList . push ( training ) ;
68
+ } ) ;
69
+
70
+ trainingsList . sort ( ( a , b ) => {
71
+ return Date . parse ( a ) - Date . parse ( b ) ;
72
+ } ) ;
73
+
74
+ return trainingsList ;
75
+ }
57
76
77
+ function showRelatedTrainingsTable ( trainings ) {
58
78
const tableElement = document . getElementById ( "related-trainings-table" ) ;
59
79
60
80
tableElement . appendChild ( createTableHeaderElement ( ) ) ;
61
81
62
- Object . keys ( trainingsTable ) . forEach ( trainingId => {
63
- tableElement . appendChild ( createTableRowElement ( trainingsTable [ trainingId ] , trainingId ) ) ;
82
+ const trainingsList = getSortedTrainingsList ( trainings ) ;
83
+
84
+ const fillTableRow = TableRowFiller ( ) ;
85
+
86
+ trainingsList . forEach ( training => {
87
+ const [ tableRowElement , tableRow ] = createTableRowElement ( ) ;
88
+
89
+ fillTableRow ( tableRow , training ) ;
90
+
91
+ tableElement . appendChild ( tableRowElement ) ;
64
92
} ) ;
65
93
}
0 commit comments