PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.3.1
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.3.1
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 3.10.4 All 148 releases
visualizer / js / render-datatables.js

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

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