1
- const schema = {
2
- 'material' : {
3
- 'name' : { 'type' : 'string' , 'color' : 'rgba(0, 0, 255, 0.1)' } ,
4
- 'source' : { 'type' : 'string' , 'color' : 'rgba(0, 0, 255, 0.1)' } ,
5
- 'lot_number' : { 'type' : 'string' , 'color' : 'rgba(0, 0, 255, 0.1)' } ,
6
- 'notes' : { 'type' : 'longstring' , 'color' : 'rgba(0, 255, 0, 0.1)' } ,
7
- 'keywords' : { 'type' : 'list' , 'color' : 'rgba(0, 0, 255, 0.1)' } ,
8
- 'hazards' : { 'type' : 'list' , 'color' : 'rgba(0, 0, 255, 0.1)' } ,
9
- 'properties' : { 'type' : 'list' , 'color' : 'rgba(0, 0, 255, 0.1)' } ,
10
- } ,
11
- 'property' : {
12
- 'name' : { 'type' : 'string' , } ,
13
- 'value' : { 'type' : 'float' , } ,
14
- 'units' : { 'type' : 'string' , } ,
15
- 'notes' : { 'type' : 'string' , } ,
16
- } ,
17
- } ;
18
-
19
-
20
-
1
+ // import current data as single large JSON
2
+ // copy it using var d = d0.slice();
3
+ // make adjustments to d
4
+ // save or cancel at the end
21
5
22
6
23
7
24
8
const tablediv = document . getElementById ( 'tablediv' ) ;
25
- var matcols = Object . keys ( schema [ 'material' ] ) ;
26
-
9
+ var udata = { } ;
10
+ var matkeys = [ ] ;
11
+
12
+ // retrieve the consolidated data
13
+ getUserData ( ) ;
14
+
15
+ // retrieve data from the server
16
+ function getUserData ( ) {
17
+ $ . ajax ( {
18
+ url : "/userdata" ,
19
+ type : "get" ,
20
+ success : function ( r ) {
21
+ udata = r ;
22
+ updateTable ( ) ;
23
+ } ,
24
+ } ) ;
25
+ } ;
27
26
28
27
29
28
function updateTable ( ) {
30
29
30
+ // get material keys
31
+ var matkeys = Object . keys ( udata [ 'materials' ] [ 0 ] ) ;
32
+
31
33
// create table
32
- const table = document . createElement ( 'table' ) ;
34
+ var table = document . createElement ( 'table' ) ;
33
35
table . id = 'ingesttable' ;
34
36
table . classList . add ( 'table' , 'table-hover' ) ;
35
37
36
38
// loop over each row
37
39
// this should happen before table headers are created
38
- // so that the so that the tbody is created automatically
39
- for ( let r = 0 ; r < 3 ; r ++ ) {
40
+ // so that the tbody is created automatically
41
+ for ( let r = 0 ; r < udata [ 'materials' ] . length ; r ++ ) {
40
42
const tr = table . insertRow ( ) ;
41
43
tr . id = r ;
42
44
@@ -48,33 +50,38 @@ function updateTable() {
48
50
49
51
50
52
// loop over each cell in the row
51
- for ( mc of matcols ) {
53
+ for ( mk of matkeys ) {
52
54
const td = tr . insertCell ( ) ;
53
55
54
56
var el = document . createTextNode ( '' ) ;
55
- var datatype = schema [ 'material '] [ mc ] [ 'type' ] ;
57
+ var dataval = udata [ 'materials '] [ 0 ] [ mk ] ;
56
58
57
- if ( datatype === 'string' ) {
58
- el = createInput ( id = 'id' + r + mc , placeholder = formatLabel ( mc ) , value = '' ) ;
59
- el . addEventListener ( 'input' , parseTable ) ;
59
+ if ( typeof dataval === 'string' ) {
60
60
61
- } else if ( datatype === 'longstring' ) {
62
- el = createButton ( trigger = 'notesmodal' , id = 'id' + r + mc , )
61
+ if ( mk === 'notes' ) {
62
+ el = createButton ( trigger = 'notesmodal' , id = 'id' + r + mk , )
63
+ } else {
64
+ el = createInput ( id = 'id' + r + mk , placeholder = formatLabel ( mk ) , value = '' ) ;
65
+ el . addEventListener ( 'input' , parseTable ) ;
66
+ } ;
63
67
64
- } else if ( datatype === 'list' ) {
68
+ } else if ( dataval . constructor === Array ) {
65
69
66
- if ( mc === 'keywords' ) {
67
- el = createButton ( trigger = 'keywordsmodal' , id = 'id' + r + mc , )
70
+ if ( mk === 'keywords' ) {
71
+ el = createButton ( trigger = 'keywordsmodal' , id = 'id' + r + mk , )
68
72
} ;
69
73
70
- if ( mc === 'hazards' ) {
71
- el = createButton ( trigger = 'hazardsmodal' , id = 'id' + r + mc , )
74
+ if ( mk === 'hazards' ) {
75
+ el = createButton ( trigger = 'hazardsmodal' , id = 'id' + r + mk , )
72
76
} ;
73
77
74
- if ( mc === 'properties' ) {
75
- el = createButton ( trigger = 'propertiesmodal' , id = 'id' + r + mc , )
78
+ if ( mk === 'properties' ) {
79
+ el = createButton ( trigger = 'propertiesmodal' , id = 'id' + r + mk , )
76
80
} ;
77
81
82
+ if ( mk === 'data' ) {
83
+ el = createButton ( trigger = 'datamodal' , id = 'id' + r + mk , )
84
+ } ;
78
85
79
86
} ;
80
87
@@ -89,7 +96,7 @@ function updateTable() {
89
96
// create table headers
90
97
const thead = table . createTHead ( ) ;
91
98
var row = thead . insertRow ( 0 ) ;
92
- for ( mc of [ '' ] . concat ( matcols ) ) {
99
+ for ( mc of [ '' ] . concat ( matkeys ) ) {
93
100
var cell = row . insertCell ( ) ;
94
101
cell . innerHTML = formatLabel ( mc ) ;
95
102
} ;
@@ -99,15 +106,16 @@ function updateTable() {
99
106
100
107
101
108
// build the table
102
- var table = updateTable ( ) ;
109
+ // var table = updateTable();
103
110
104
111
105
112
106
113
107
114
// parse the table contents into JSON format
108
115
function parseTable ( ) {
116
+ var table = document . getElementById ( 'ingesttable' ) ;
109
117
// create object to hold data from table
110
- var data = { 'materials' : [ ] } ;
118
+ var dataexport = { 'materials' : [ ] } ;
111
119
// iterate over table rows
112
120
for ( var r = 0 , row ; row = table . rows [ r ] ; r ++ ) {
113
121
// skip the headers row
@@ -117,18 +125,17 @@ function parseTable() {
117
125
// iterate over table columns
118
126
for ( var c = 1 , cell ; cell = row . cells [ c ] ; c ++ ) {
119
127
// get name of current column (subtract 1 to ignore the 'delete row' button in 1st column)
120
- var colname = matcols [ c - 1 ] ;
121
- console . log ( colname ) ;
128
+ var colname = matkeys [ c - 1 ] ;
122
129
// if cell contains a text input, add its value to the data object
123
- if ( schema [ 'material' ] [ colname ] [ 'type' ] === 'string' ) {
130
+ if ( typeof cell . children [ 0 ] . value === 'string' ) {
124
131
mat [ colname ] = cell . children [ 0 ] . value ;
125
132
} ;
126
133
} ;
127
134
// add material to the dataset
128
- data [ 'materials' ] . push ( mat ) ;
135
+ dataexport [ 'materials' ] . push ( mat ) ;
129
136
} ;
130
137
// display the dataset on the UI
131
- document . getElementById ( 'results' ) . innerHTML = '<pre><b>Result:</b><br>' + JSON . stringify ( data , null , 4 ) + '</pre>' ;
138
+ document . getElementById ( 'results' ) . innerHTML = '<pre><b>Result:</b><br>' + JSON . stringify ( dataexport , null , 4 ) + '</pre>' ;
132
139
} ;
133
140
134
141
0 commit comments