@@ -30,8 +30,8 @@ window.onkeyup = (e) => {
3030function makeRequest ( verb = 'GET' , url , body ) {
3131 const xmlHttp = new XMLHttpRequest ( ) ;
3232 // Return it as a Promise
33- return new Promise ( function ( resolve , reject ) {
34- xmlHttp . onreadystatechange = function ( ) {
33+ return new Promise ( function ( resolve , reject ) {
34+ xmlHttp . onreadystatechange = function ( ) {
3535 // Only run if the request is complete
3636 if ( xmlHttp . readyState !== 4 ) {
3737 return ;
@@ -76,7 +76,12 @@ function closeTab() {
7676
7777function setQueryResult ( result ) {
7878 if ( result . status === 200 ) {
79- queryResult = result . body ;
79+ queryResult = { truncatedByLimit : result . body . truncatedByLimit } ;
80+ if ( pluginConfiguration . entityType && pluginConfiguration . entityType === 'edge' ) {
81+ queryResult . result = result . body . edges . filter ( edge => edge . data . type === pluginConfiguration . itemType ) ;
82+ } else {
83+ queryResult . result = result . body . nodes . filter ( node => node . data . categories . includes ( pluginConfiguration . itemType ) ) ;
84+ }
8085 } else {
8186 handleError ( result ) ;
8287 }
@@ -85,7 +90,7 @@ function setQueryResult(result) {
8590async function runQueryByID ( query ) {
8691 const result = await makeRequest (
8792 'POST' ,
88- `/api/ plugins/table/runQueryByIDPlugin` ,
93+ `/plugins/table/api /runQueryByIDPlugin` ,
8994 { query, queryParams}
9095 ) ;
9196 setQueryResult ( JSON . parse ( result . response ) ) ;
@@ -183,7 +188,7 @@ function setSchema(result) {
183188async function getSchema ( ) {
184189 const result = await makeRequest (
185190 'GET' ,
186- `/api/ plugins/table/getSchema?sourceKey=${ queryParams . global . sourceKey } ` ,
191+ `/plugins/table/api /getSchema?sourceKey=${ queryParams . global . sourceKey } ` ,
187192 null
188193 ) ;
189194 setSchema ( JSON . parse ( result . response ) ) ;
@@ -193,13 +198,12 @@ async function validatePluginConfiguration() {
193198 try {
194199 const result = await makeRequest (
195200 'POST' ,
196- `/api/ plugins/table/checkPluginsConfiguration` ,
201+ `/plugins/table/api /checkPluginsConfiguration` ,
197202 { results : schema }
198203 ) ;
199204 pluginConfiguration = JSON . parse ( result . response ) ;
200- console . log ( pluginConfiguration ) ;
201205 return true ;
202- } catch ( e ) {
206+ } catch ( e ) {
203207 handleError ( e ) ;
204208 }
205209}
@@ -217,50 +221,87 @@ function truncateText(text, maxLength = 50) {
217221}
218222
219223function getFilteredSchema ( schemaStructure ) {
220- if ( queryResult . nodes . length > 0 ) {
221- return schemaStructure . filter ( result => {
222- return queryResult . nodes [ 0 ] . data . categories . includes ( result . itemType ) ;
224+ const properties = schemaStructure . find ( item => item . itemType === pluginConfiguration . itemType ) . properties || [ ] ;
225+ let sortedProperties = [ ] ;
226+ if ( pluginConfiguration . properties ) {
227+ pluginConfiguration . properties . forEach ( propertyName => {
228+ const property = properties . find ( property => property . propertyKey === propertyName ) ;
229+ if ( property ) {
230+ sortedProperties . push ( property ) ;
231+ }
223232 } ) ;
224233 } else {
225- return schemaStructure . filter ( result => {
226- return queryResult . edges [ 0 ] . data . type === result . itemType ;
227- } ) ;
234+ sortedProperties = sortAlphabetically ( properties , 'propertyKey' ) ;
228235 }
236+ return sortedProperties ;
237+ }
238+
239+ function sortAlphabetically ( arr , objKey ) {
240+ return arr . sort ( ( a , b ) => {
241+ const aK = objKey ? a [ objKey ] : a ;
242+ const bK = objKey ? b [ objKey ] : b ;
243+ const sanitizedA = typeof aK === 'string' ? aK . toLowerCase ( ) : aK ;
244+ const sanitizedB = typeof bK === 'string' ? bK . toLowerCase ( ) : bK ;
245+ return sanitizedA < sanitizedB ? - 1 : sanitizedA > sanitizedB ? 1 : 0 ;
246+ } ) ;
229247}
230248
231249function getTableStructure ( schemaStructure ) {
232- const filteredSchema = getFilteredSchema ( schemaStructure ) ;
233- const sanitizedData = filteredSchema [ 0 ] . properties . map ( property => {
250+ const properties = getFilteredSchema ( schemaStructure ) ;
251+ const sanitizedData = properties . map ( property => {
252+ let align = 'left' ;
253+ if (
254+ property . propertyType &&
255+ ( property . propertyType . name === 'number' ||
256+ property . propertyType . name === 'date' ||
257+ property . propertyType . name === 'datetime' )
258+ ) {
259+ align = 'right' ;
260+ }
234261 return {
235262 title : property . propertyKey ,
236263 field : property . propertyKey ,
237- align : 'left' ,
264+ align : align ,
238265 titleFormatter : truncateColumnTitle
239266 } ;
240267 } ) ;
241- return [ { title : 'id' , field : 'id' , align : 'center ' } , ...sanitizedData ] ;
268+ return [ { title : 'Row' , field : 'row' , align : 'right' } , { title : ' id', field : 'id' , align : 'right ' } , ...sanitizedData ] ;
242269}
243270
244271function getTableData ( queryResult ) {
245- return ( queryResult . nodes . length > 0 ) ?
246- queryResult . nodes . map ( node => {
247- return { ...node . data . properties , 'id' : node . id } ;
248- } ) :
249- queryResult . edges . map ( edge => edge . data . properties ) ;
272+ return queryResult . result . map ( ( item , index ) => {
273+ for ( let [ key , value ] of Object . entries ( item . data . properties ) ) {
274+ if ( typeof value === 'object' ) {
275+ if ( value . value && ( value . type === 'date' || value . type === 'datetime' ) ) {
276+ item . data . properties [ key ] = new Date ( value . value ) . toISOString ( ) ;
277+ } else {
278+ item . data . properties [ key ] = value . value || value . original ;
279+ }
280+ }
281+ }
282+ return { ...item . data . properties , 'id' : item . id , 'row' : index + 1 } ;
283+ } ) ;
250284}
251285
252286function getTooltipsHeader ( column ) {
253287 return column . getDefinition ( ) . title ;
254288}
255289
256290function setTableTitle ( ) {
291+ if ( queryResult . truncatedByLimit && queryParams . global . limit === undefined ) {
292+ document . getElementById ( 'warning' ) . classList . remove ( 'hide' ) ;
293+ }
257294 document . getElementById ( 'table_title' ) . innerText = query . name ;
258295 if ( pluginConfiguration . entityType === undefined || pluginConfiguration . entityType === 'node' ) {
296+ document . getElementById ( 'warning_text' ) . innerText = `The query returned too many results. The ${
297+ queryResult . result . length } first results are displayed below.`;
259298 document . getElementById ( 'item_type' ) . innerText = `Node type : ${ pluginConfiguration . itemType } ` ;
260- document . getElementById ( 'item_count' ) . innerText = `Number of nodes : ${ queryResult . nodes . length } ` ;
261- } else if ( pluginConfiguration . entityType === 'node' ) {
299+ document . getElementById ( 'item_count' ) . innerText = `Number of nodes : ${ queryResult . result . length } ` ;
300+ } else if ( pluginConfiguration . entityType === 'edge' ) {
301+ document . getElementById ( 'warning_text' ) . innerText = `The query returned too many results. The ${
302+ queryResult . result . length } first results are displayed below.`;
262303 document . getElementById ( 'item_type' ) . innerText = `Edge type : ${ pluginConfiguration . itemType } ` ;
263- document . getElementById ( 'item_count' ) . innerText = `Number of edges : ${ queryResult . edges . length } ` ;
304+ document . getElementById ( 'item_count' ) . innerText = `Number of edges : ${ queryResult . result . length } ` ;
264305 }
265306}
266307
@@ -284,7 +325,6 @@ function clearFilter() {
284325function filterTableColumns ( ) {
285326 const list = document . getElementsByTagName ( 'input' ) ;
286327 for ( let i = 0 ; i < list . length ; i ++ ) {
287- console . log ( list [ i ] ) ;
288328 if ( list [ i ] . checked ) {
289329 table . showColumn ( list [ i ] . id ) ;
290330 } else {
@@ -360,28 +400,31 @@ function addButtons() {
360400}
361401
362402function fillDataTable ( ) {
363- loaderElement . classList . remove ( 'active' ) ;
364- setTableTitle ( ) ;
365403 tableStructure = getTableStructure ( schema ) ;
366404 const tableData = getTableData ( { ...queryResult } ) ;
405+ console . log ( tableData ) ;
406+ console . log ( tableStructure ) ;
407+ if ( tableData . length === 0 ) {
408+ return handleError ( { body : { message : 'No result was returned.' } } ) ;
409+ }
410+ loaderElement . classList . remove ( 'active' ) ;
411+ setTableTitle ( ) ;
367412 // create Tabulator on DOM element with id "example-table"
368413 table = new Tabulator ( '#table' , {
369414 tooltipsHeader : getTooltipsHeader ,
370415 layoutColumnsOnNewData : true ,
371416 resizableColumns : false ,
372417 pagination : 'local' ,
373- paginationSize : 15 ,
374- paginationSizeSelector : [ 8 , 12 , 16 , 20 ] ,
418+ paginationSize : 10 ,
375419 movableColumns : true ,
376- placeholder : 'There was not matches for the filter ' ,
420+ placeholder : 'No result was returned. ' ,
377421 data : tableData , //assign data to table
378422 layout : 'fitDataFill' , //fit columns to width of table
379423 columns : tableStructure ,
380- rowClick : function ( e , row ) { //trigger an alert message when the row is clicked
424+ rowClick : function ( e , row ) { //trigger an alert message when the row is clicked
381425 alert ( 'Row ' + row . getData ( ) . id + ' Clicked!!!!' ) ;
382426 }
383427 } ) ;
384- // addFilter();
385428 fillModalColumns ( ) ;
386429 addButtons ( ) ;
387430}
@@ -390,13 +433,13 @@ async function getQuery() {
390433 try {
391434 return await makeRequest (
392435 'POST' ,
393- `/api/ plugins/table/getQuery` ,
436+ `/plugins/table/api /getQuery` ,
394437 {
395438 id : queryParams . global . queryId ,
396439 sourceKey : queryParams . global . sourceKey
397440 }
398441 ) ;
399- } catch ( e ) {
442+ } catch ( e ) {
400443 handleError ( e ) ;
401444 }
402445}
@@ -417,7 +460,7 @@ async function main() {
417460 await runQueryByID ( query ) ;
418461 fillDataTable ( ) ;
419462 }
420- } catch ( e ) {
463+ } catch ( e ) {
421464 handleError ( e ) ;
422465 }
423466
0 commit comments