PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 4.0.7
Visualizer – Tables & Charts Manager with Built-in AI Generator v4.0.7
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
visualizer / js / render-datatables.js

render-datatables.js in Visualizer – Tables & Charts Manager with Built-in AI Generator 4.0.7, at js/render-datatables.js

453 lines 15.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global console */
2 /* global visualizer */
3
4 (function($) {
5 var all_charts;
6 // so that we know which charts belong to our library.
7 var rendered_charts = [];
8
9 // save the settings corresponding to cssClassNames so that when a table is being edited, they stripes etc. don't get reset
10 var cssClassNames;
11
12 function renderChart(id, v) {
13 var chart = all_charts[id];
14 if(chart.library !== 'datatables'){
15 return;
16 }
17 cssClassNames = null;
18 renderSpecificChart(id, chart, v);
19 }
20
21 function renderSpecificChart(id, chart, v) {
22 var render, container, series, data, table, settings, i, j, row, date, axis, property, format, formatter, type, rows, cols;
23
24 if(chart.library !== 'datatables'){
25 return;
26 }
27
28 // Bail if the chart is already rendered or is being rendered.
29 if ( ! window.isResizeRequest && ( $('#' + id).hasClass('visualizer-chart-loaded') || ( 'canvas' !== id && $('#' + id).children( ':not(.loader, style)' ).length > 0 ) ) ) {
30 return;
31 }
32 rendered_charts[id] = 'yes';
33
34 series = chart.series;
35 data = chart.data;
36
37 container = document.getElementById(id);
38 if (container == null) {
39 return;
40 }
41
42 if($('#' + id).find('table.visualizer-data-table').length > 0){
43 $('#' + id).empty();
44 }
45
46 settings = {
47 destroy: true,
48 paging: false,
49 searching: false,
50 ordering: true,
51 select: false,
52 lengthChange: false,
53 buttons: {
54 buttons: [{
55 extend: 'print',
56 text: '',
57 title: ''
58 }]
59 },
60 dom: 'Bfrtip',
61 };
62
63 var $classes = 'dataTable visualizer-data-table table table-striped';
64
65 if(typeof v.page_type !== 'undefined'){
66 switch(v.page_type){
67 case 'post':
68 // fall-through.
69 case 'library':
70 // for smaller screens...
71 if(window.innerWidth < 1500){
72 delete chart.settings['scrollX'];
73 delete chart.settings['scrollY'];
74 delete chart.settings['scrollY_int'];
75 $.extend( settings, {
76 scrollX: '100%',
77 scrollY: (( chart.settings['responsive_bool'] === 'true' ? 0.8 : 0.5 ) * parseInt($(container).css('height').replace('px',''))),
78 scrollCollapse: true
79 } );
80 }else{
81 if(parseInt(chart.settings['scrollY_int']) > 180){
82 chart.settings['scrollY_int'];
83 }
84 delete chart.settings['scrollX'];
85 $.extend( settings, {
86 scrollX: '100%',
87 scrollY: 180,
88 } );
89 }
90 break;
91 case 'chart':
92 delete chart.settings['scrollX']; // jshint ignore:line
93 // fall-through.
94 case 'frontend':
95 // empty.
96 break;
97 }
98 }
99
100 if(typeof chart.settings['scrollX'] !== 'undefined'){
101 if(chart.settings['scrollX'] == 'true'){ // jshint ignore:line
102 $classes = $classes + ' nowrap';
103 }
104 }
105
106 $('#' + id).append($('<table class="' + $classes + '"></table>'));
107
108 var select = {
109 info: false
110 };
111 // we cannot use { select } below because https://github.com/Codeinwp/visualizer/issues/357
112 $.extend( settings, { info: false } ); // jshint ignore:line
113
114 var stripe = ['', ''];
115
116 if(cssClassNames !== null){
117 chart.settings['cssClassNames'] = cssClassNames;
118 }
119
120 // in preview mode, cssClassNames will not exist when a color is changed.
121 if(typeof chart.settings['cssClassNames'] !== 'undefined'){
122 cssClassNames = chart.settings['cssClassNames'];
123 if(typeof chart.settings['cssClassNames']['oddTableRow'] !== 'undefined'){
124 stripe[0] = chart.settings['cssClassNames']['oddTableRow'];
125 }
126
127 if(typeof chart.settings['cssClassNames']['evenTableRow'] !== 'undefined'){
128 stripe[1] = chart.settings['cssClassNames']['evenTableRow'];
129 }
130
131 if(typeof chart.settings['cssClassNames']['selectedTableItem'] !== 'undefined'){
132 $.extend( select, {
133 className: chart.settings['cssClassNames']['selectedTableItem']
134 } );
135 }
136 }
137
138 // remove this so that the eval does not fail.
139 delete chart.settings['internal_title'];
140
141 var additional = [];
142 for (i in chart.settings) {
143 var valoo = chart.settings[i];
144
145 // remove the type suffix to get the name of the setting.
146 i = i.replace(/_bool/g, '').replace(/_int/g, '');
147
148 switch(valoo){
149 case 'true':
150 valoo = true;
151 break;
152 case 'false':
153 valoo = false;
154 break;
155 default:
156 if(parseInt(valoo) > 0){
157 valoo = parseInt(valoo);
158 }
159 if(i === 'pageLength' && (valoo === '' || parseInt(valoo) < 0)){
160 valoo = 1;
161 }
162 }
163
164 // if the setting name has an '_' this means it is a sub-setting e.g. select_items means { select: { items: ... } }.
165 var array = i.split('_');
166 if(array.length === 2){
167 i = eval( array[0] ); // jshint ignore:line
168 i[ array[1] ] = valoo;
169 additional[ array[0] ] = i;
170 if(array[0] === 'select' && array[1] === 'info' ){
171 // enable main info or the selection info will not show.
172 $.extend( settings, {info: true} );
173 }
174 } else {
175 settings[i] = valoo;
176 }
177 }
178 $.extend( settings, additional );
179
180 cols = [];
181 for (j = 0; j < series.length; j++) {
182 type = series[j].type;
183 switch(type){
184 case 'number':
185 type = 'num';
186 break;
187 case 'date':
188 case 'datetime':
189 case 'timeofday':
190 type = 'date';
191 break;
192 }
193 render = addRenderer(series[j].type, settings.series, j);
194
195 var col = { title: series[j].label, data: series[j].label, type: type, render: render };
196 if(typeof chart.settings['cssClassNames'] !== 'undefined' && typeof chart.settings['cssClassNames']['tableCell'] !== 'undefined'){
197 col['className'] = chart.settings['cssClassNames']['tableCell'];
198 }
199 cols.push(col);
200 }
201
202 rows = [];
203 for (i = 0; i < data.length; i++) {
204 row = [];
205 for (j = 0; j < series.length; j++) {
206 var datum = data[i][j];
207 // datum could be undefined for dynamic data (e.g. through json).
208 if(typeof datum === 'undefined'){
209 datum = data[i][series[j].label];
210 }
211 row[ series[j].label ] = datum;
212 }
213 rows.push(row);
214 }
215
216 $.extend( settings, {
217 data: rows,
218 columns: cols,
219 stripeClasses: stripe,
220 } );
221
222 // allow user to extend the settings.
223 $('body').trigger('visualizer:chart:settings:extend', {id: id, chart: chart, settings: settings});
224
225 table = $('#' + id + ' table.visualizer-data-table');
226 table.DataTable( settings );
227
228 // header row is handled here as the class is added dynamically to it (after the table is rendered).
229 if(typeof chart.settings['cssClassNames'] !== 'undefined'){
230 if(typeof chart.settings['cssClassNames']['headerRow'] !== 'undefined'){
231 $('#' + id + ' table thead tr').addClass( chart.settings['cssClassNames']['headerRow'] );
232 }
233 }
234
235 $('.loader').remove();
236 }
237
238 function addRenderer(type, series, index){
239 var render = null;
240 if(typeof series === 'undefined' || typeof series[index] === 'undefined' || typeof series[index].format === 'undefined' ){
241 return render;
242 }
243
244 series = series[index];
245
246 /* jshint ignore:start */
247 switch(type){
248 case 'number':
249 var parts = ['', '', '', '', ''];
250 if(typeof series.format.thousands !== ''){
251 parts[0] = series.format.thousands;
252 }
253 if(typeof series.format.decimal !== ''){
254 parts[1] = series.format.decimal;
255 if ( '' === series.format.precision ) {
256 series.format.precision = 2;
257 }
258 }
259 if(typeof series.format.precision !== '' && parseInt(series.format.precision) > 0){
260 parts[2] = series.format.precision;
261 }
262 if(typeof series.format.prefix !== ''){
263 parts[3] = series.format.prefix;
264 }
265 if(typeof series.format.suffix !== ''){
266 parts[4] = series.format.suffix;
267 }
268 render = $.fn.dataTable.render.number(parts[0], parts[1], parts[2], parts[3], parts[4]);
269 break;
270 case 'date':
271 case 'datetime':
272 case 'timeofday':
273 if(typeof series.format.to !== 'undefined' && typeof series.format.from !== 'undefined' && series.format.from != '' && series.format.to !== ''){
274 render = $.fn.dataTable.render.moment(series.format.from, series.format.to);
275 }
276 break;
277 default:
278 render = $.fn.dataTable.render.extra = function ( data, type, row ) {
279 if((data === true || data === 'true') && typeof series.format !== 'undefined' && series.format.truthy !== ''){
280 data = series.format.truthy.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
281 }
282 if((data === false || data === 'false') && typeof series.format !== 'undefined' && series.format.falsy !== ''){
283 data = series.format.falsy.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '');
284 }
285 return data;
286 }
287 }
288 /* jshint ignore:end */
289
290 return render;
291 }
292
293 function render(v) {
294 for (var id in (all_charts || {})) {
295 renderChart(id, v);
296 }
297 }
298
299 $('body').on('visualizer:render:chart:start', function(event, v){
300 all_charts = v.charts;
301
302 if(v.is_front == true && typeof v.id !== 'undefined'){ // jshint ignore:line
303 renderChart(v.id, v);
304 } else {
305 render(v);
306 }
307 });
308
309 $('body').on('visualizer:render:specificchart:start', function(event, v){
310 renderSpecificChart(v.id, v.chart, v.v);
311 });
312
313 $('body').on('visualizer:render:currentchart:update', function(event, v){
314 var data = v || event.detail;
315 renderSpecificChart('canvas', all_charts['canvas'], data.visualizer);
316 });
317
318 // front end actions
319 $('body').on('visualizer:action:specificchart', function(event, v){
320 switch(v.action){
321 case 'print':
322 var id = v.id;
323 if(typeof rendered_charts[id] === 'undefined'){
324 return;
325 }
326 $('#' + id + ' .buttons-print').trigger('click');
327 break;
328 }
329 });
330
331 })(jQuery);
332
333
334 /* jshint ignore:start */
335
336 /**
337 * Date / time formats often from back from server APIs in a format that you
338 * don't wish to display to your end users (ISO8601 for example). This rendering
339 * helper can be used to transform any source date / time format into something
340 * which can be easily understood by your users when reading the table, and also
341 * by DataTables for sorting the table.
342 *
343 * The [MomentJS library](http://momentjs.com/) is used to accomplish this and
344 * you simply need to tell it which format to transfer from, to and specify a
345 * locale if required.
346 *
347 * This function should be used with the `dt-init columns.render` configuration
348 * option of DataTables.
349 *
350 * It accepts one, two or three parameters:
351 *
352 * $.fn.dataTable.render.moment( to );
353 * $.fn.dataTable.render.moment( from, to );
354 * $.fn.dataTable.render.moment( from, to, locale );
355 *
356 * Where:
357 *
358 * * `to` - the format that will be displayed to the end user
359 * * `from` - the format that is supplied in the data (the default is ISO8601 -
360 * `YYYY-MM-DD`)
361 * * `locale` - the locale which MomentJS should use - the default is `en`
362 * (English).
363 *
364 * @name datetime
365 * @summary Convert date / time source data into one suitable for display
366 * @author [Allan Jardine](http://datatables.net)
367 * @requires DataTables 1.10+
368 *
369 * @example
370 * // Convert ISO8601 dates into a simple human readable format
371 * $('#example').DataTable( {
372 * columnDefs: [ {
373 * targets: 1,
374 * render: $.fn.dataTable.render.moment( 'Do MMM YYYYY' )
375 * } ]
376 * } );
377 *
378 * @example
379 * // Specify a source format - in this case a unix timestamp
380 * $('#example').DataTable( {
381 * columnDefs: [ {
382 * targets: 2,
383 * render: $.fn.dataTable.render.moment( 'X', 'Do MMM YY' )
384 * } ]
385 * } );
386 *
387 * @example
388 * // Specify a source format and locale
389 * $('#example').DataTable( {
390 * columnDefs: [ {
391 * targets: 2,
392 * render: $.fn.dataTable.render.moment( 'YYYY/MM/DD', 'Do MMM YY', 'fr' )
393 * } ]
394 * } );
395 */
396 (function( factory ) {
397 "use strict";
398
399 if ( typeof define === 'function' && define.amd ) {
400 // AMD
401 define( ['jquery'], function ( $ ) {
402 return factory( $, window, document );
403 } );
404 }
405 else if ( typeof exports === 'object' ) {
406 // CommonJS
407 module.exports = function (root, $) {
408 if ( ! root ) {
409 root = window;
410 }
411
412 if ( ! $ ) {
413 $ = typeof window !== 'undefined' ?
414 require('jquery') :
415 require('jquery')( root );
416 }
417
418 return factory( $, root, root.document );
419 };
420 }
421 else {
422 // Browser
423 factory( jQuery, window, document );
424 }
425 }
426 (function( $, window, document ) {
427
428
429 $.fn.dataTable.render.moment = function ( from, to, locale ) {
430 // Argument shifting
431 if ( arguments.length === 1 ) {
432 locale = 'en';
433 to = from;
434 from = 'YYYY-MM-DD';
435 }
436 else if ( arguments.length === 2 ) {
437 locale = 'en';
438 }
439
440 return function ( d, type, row ) {
441 if (!d) return null;
442 var m = window.moment( d, from, locale, true );
443
444 // Order and type get a number value from Moment, everything else
445 // sees the rendered value
446 return m.format( type === 'sort' || type === 'type' ? 'x' : to );
447 };
448 };
449
450
451 }));
452
453 /* jshint ignore:end */