-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathii.sparql.format.table.js
238 lines (214 loc) · 7.4 KB
/
ii.sparql.format.table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
spqlib.table = ( function () {
/**
* Entry point
*/
spqlib.sparql2Table = function ( config ) {
if ( !config.sparqlWithPrefixes && config.sparql && config.queryPrefixes ) {
config.sparqlWithPrefixes = spqlib.util.addPrefixes( config.sparql, config.queryPrefixes );
}
this.util.doQuery( config.endpoint, config.endpointName, config.sparqlWithPrefixes, spqlib.table.renderTable, config, spqlib.table.preQuery, spqlib.table.failQuery );
};
var my = { };
my.preQuery = function ( configuration ) {
if ( configuration.spinnerImagePath ) {
$( '#' + configuration.divId ).html( "<img src='" + configuration.spinnerImagePath + "' style='vertical-align:middle;'>" );
} else {
$( '#' + configuration.divId ).html( 'Loading...' );
}
};
my.failQuery = function ( configuration, jqXHR, textStatus ) {
$( '#' + configuration.divId ).html( '' );
$( '#' + configuration.divId ).html( spqlib.util.generateErrorBox( textStatus ) );
throw new Error( 'Error invoking sparql endpoint ' + textStatus + ' ' + JSON.stringify( jqXHR ) );
};
/**
* funzione di callback di default dopo la chiamata ajax all'endpoint sparql.
*/
my.renderTable = function ( json, config ) {
var head = json.head.vars,
data = json.results.bindings;
if ( data.length == 0 ) {
$( '#' + config.divId ).html( "<div class'ii-sprf-table-no-result warning'>" + config.noResultMessage + '</div>' );
return;
}
var colTitles = config.columnTitles || config.columnConfiguration,
colTitleMapping = {};
if ( colTitles ) {
for ( var i = 0; i < colTitles.length; i++ ) {
colTitleMapping[ colTitles[ i ].queryField ] = {
label: colTitles[ i ].label,
visible: ((typeof colTitles[i].visible !== 'undefined') ? colTitles[i].visible : true) ,
showLink: colTitles[ i ].showLink,
cellValuePattern: colTitles[ i ].cellValuePattern,
cellLinkPattern: colTitles[ i ].cellLinkPattern
};
}
}
// tableHeader
var headers = [],
thead = '<thead>';
for ( var i = 0; i < head.length; i++ ) {
var mappedColumnInfo = mapColumnTitle( head[ i ], colTitleMapping ),
colTitle = '';
var isVisible=true;
if ( mappedColumnInfo == null ) {
colTitle = head[ i ];
} else {
colTitle = mappedColumnInfo.label;
isVisible = mappedColumnInfo.visible;
}
if (isVisible){
headers.push( head[ i ] );
thead += '<th>' + colTitle + '</th>';
}
}
thead += '</thead>';
// tableRows
var tbody = '<tbody>';
for ( var i = 0; i < data.length; i++ ) {
var row = "<tr class='" + assignRowCssClass( i, config ) + "'>";
for ( var j = 0; j < headers.length; j++ ) {
var cellValue = '';
if ( data[ i ][ headers[ j ] ] ) {
cellValue = data[ i ][ headers[ j ] ].value;
}
var mappedColumnInfo = mapColumnTitle( headers[ j ], colTitleMapping ),
linkCellValue = '';
if ( mappedColumnInfo ) {
if ( mappedColumnInfo.showLink == 'true' ) {
linkCellValue = getCellLinkValue( config, cellValue, mappedColumnInfo,data[ i ] );
} else {
linkCellValue = getCellValue( cellValue, mappedColumnInfo,config );
}
} else {
linkCellValue = cellValue;
}
var cell = "<td class='" + assignCellCssClass( i, config ) + "'>" + linkCellValue + '</td>';
row += cell;
}
row += '</tr>';
tbody += row;
}
tbody += '</tbody>';
var table = "<table class='" + config.tableClass + "'>" + thead + tbody + '</table>';
$( '#' + config.divId ).html( table );
var csvExport = config.csvExport == 'true';
if ( spqlib.util.exportTableToCSV && csvExport ) {
// aggiungo il link per l'export csv delle pagine
var filename = config.csvFileName || 'export.csv',
label = config.csvLinkLabel || 'Export as CSV',
csvFormAction = config.csvFormAction,
formId = config.divId + '-form';
if ( !csvFormAction ) {
throw ( "csvFormAction vuota -> l'export csv non funzionerà" );
}
var tableContainer = $( '#' + config.divId ).find( 'table' ),
exporter = $( "<span class='export-table-csv' table-container-id='" + config.divId + "'> </span>" )
.html( "<a class='export'>" + label + "</a> \
<form action='" + csvFormAction + "' method ='post' id='" + formId + "'> \
<input type='hidden' id='csv_text' name='csv_text' /> \
<input type='hidden' id='csv_file_name' name='csv_file_name' value='" + filename + "'/> \
</form>" );
exporter.insertBefore( tableContainer );
exporter.on( 'click', function ( event ) {
var divId = $( event.currentTarget ).attr( 'table-container-id' ),
table = $( '#' + divId ).find( 'table' ),
csv = spqlib.util.exportTableToCSV( table ),
form = $( '#' + formId );
form.find( "input[id='csv_text']" ).val( csv );
form.submit();
} );
}
$( '#' + config.divId ).trigger( 'done' );
};
function getCellLinkValue( config, cellValue, columnConfiguration,rowData ) {
var link = '';
if ( columnConfiguration.cellLinkPattern ) {
link = formatString( columnConfiguration.cellLinkPattern, cellValue,config,rowData );
} else {
link = cellValue;
}
var formattedCellValue = getCellValue( cellValue, columnConfiguration,config,rowData );
return "<a href='" + spqlib.util.htmlEncode( link ).replace( /'/g, ''' ) + "'>" + formattedCellValue + '</a>';
}
function getCellValue( cellValue, columnConfiguration,config,rowData ) {
if ( columnConfiguration.cellValuePattern ) {
return formatString( columnConfiguration.cellValuePattern, cellValue,config,rowData );
}
return cellValue;
}
function formatString( format, param,config,rowData ) {
// {%n[<format>]@<locale>}
var placeholderWithOtherField = /{%s\[[^}]*\]}/gm;
var otherFieldNameRegex = /(?<={%s\[).*(?=\])/gm;
var regex = /{%n\[.*\](@[a-z-]+)?}/gm;
var numberLocaleRegex = /(?<={%n\[.*]@).*(?=})/gm;
var numberFormatRegex = /(?<={%n\[).*(?=\])/gm;
var output = format;
if (format.includes("{%s}")){
output = output.replace( '{%s}', param );
}
var temp = output;
if (placeholderWithOtherField.test(temp)){
var res = temp.match(placeholderWithOtherField);
for (var i=0;i<res.length;i++){
var m = res[i];
var fieldName=m.match(otherFieldNameRegex)[0];
var fieldValue=rowData[fieldName].value;
output=output.replace(m,fieldValue);
}
}
if (output.match(regex)){
//devo formattare il numero
if (output.match(numberLocaleRegex)){
var numberLocale=numberLocaleRegex.exec(output)[0];
numeral.locale(numberLocale);
} else {
numeral.locale(config.numberFormatDefaultLocale);
}
var numberFormat = numberFormatRegex.exec(output)[0];
var formattedNumber=numeral(param).format(numberFormat);
output = output.replace(regex,formattedNumber);
}
return output;
}
function isEven( i ) {
return ( i % 2 == 0 );
}
function assignRowCssClass( i, config ) {
if ( isEven( i ) ) {
if ( config.cssEvenRowClass ) {
return config.cssEvenRowClass;
}
} else {
if ( config.cssOddRowClass ) {
return config.cssOddRowClass;
}
}
return '';
}
function assignCellCssClass( i, config ) {
if ( isEven( i ) ) {
if ( config.cssEvenTdClass ) {
return config.cssEvenTdClass;
}
} else {
if ( config.cssOddTdClass ) {
return config.cssOddTdClass;
}
}
return '';
}
function mapColumnTitle( field, mapping ) {
if ( !mapping ) {
return null;
} else {
if ( mapping[ field ] ) {
return mapping[ field ];
} else {
return null;
}
}
}
return my;
}() );