PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.3
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.3
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 / classes / Visualizer / Gutenberg / src / Components / DataTable.js

DataTable.js in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.4.3, at classes/Visualizer/Gutenberg/src/Components/DataTable.js

206 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * External dependencies
3 */
4 import uuidv4 from 'uuid';
5
6 /**
7 * WordPress dependencies
8 */
9 const {
10 Component,
11 Fragment
12 } = wp.element;
13
14 class DataTables extends Component {
15 constructor() {
16 super( ...arguments );
17
18 this.initDataTable = this.initDataTable.bind( this );
19 this.dataRenderer = this.dataRenderer.bind( this );
20
21 this.table;
22 this.uniqueId = uuidv4();
23 }
24
25 componentDidMount() {
26 this.initDataTable( this.props.columns, this.props.rows );
27 };
28
29 componentWillUnmount() {
30 this.table.destroy();
31 }
32
33 componentDidUpdate( prevProps ) {
34 if ( this.props !== prevProps ) {
35 if ( this.props.options.responsive_bool !== prevProps.options.responsive_bool ) {
36 if ( 'true' === prevProps.options.responsive_bool ) {
37 document.getElementById( `dataTable-instances-${ this.props.id }-${ this.uniqueId }` ).classList.remove( 'collapsed' );
38 }
39 }
40
41 this.table.destroy();
42 document.getElementById( `dataTable-instances-${ this.props.id }-${ this.uniqueId }` ).innerHTML = '';
43 this.initDataTable( this.props.columns, this.props.rows );
44 }
45 }
46
47 initDataTable( tableColumns, tableRow ) {
48 const settings = this.props.options;
49
50 const columns = tableColumns.map( ( i, index ) => {
51 let type = i.type;
52
53 switch ( i.type ) {
54 case 'number':
55 type = 'num';
56 break;
57 case 'date':
58 case 'datetime':
59 case 'timeofday':
60 type = 'date';
61 break;
62 }
63
64 return {
65 title: i.label,
66 data: i.label,
67 type: type,
68 render: this.dataRenderer( data, type, index )
69 };
70 });
71
72 const data = tableRow.map( i => {
73 const row = {};
74
75 columns.forEach( ( j, n ) => {
76 var datum = i[n];
77
78 // datum could be undefined for dynamic data (e.g. through json).
79 if ( 'undefined' === typeof datum ) {
80 datum = i[j.data];
81 }
82 row[j.data] = datum;
83 });
84
85 return row;
86 });
87
88 this.table = jQuery( `#dataTable-instances-${ this.props.id }-${ this.uniqueId }` ).DataTable({
89 destroy: true,
90 data: data,
91 columns: columns,
92 paging: 'true' === settings.paging_bool ? true : false,
93 pageLength: settings.pageLength_int || 10,
94 pagingType: settings.pagingType,
95 ordering: 'false' === settings.ordering_bool ? false : true,
96 fixedHeader: 'true' === settings.fixedHeader_bool ? true : false,
97 scrollCollapse: this.props.chartsScreen && true || 'true' === settings.scrollCollapse_bool ? true : false,
98 scrollY: this.props.chartsScreen && 180 || ( 'true' === settings.scrollCollapse_bool && Number( settings.scrollY_int ) || false ),
99 responsive: this.props.chartsScreen && true || 'true' === settings.responsive_bool ? true : false,
100 searching: false,
101 select: false,
102 lengthChange: false,
103 bFilter: false,
104 bInfo: false
105 });
106 }
107
108 dataRenderer( data, type, index ) {
109 const settings = this.props.options;
110
111 if ( undefined === settings.series[index]) {
112 return data;
113 }
114
115 switch ( type ) {
116 case 'date':
117 case 'datetime':
118 case 'timeofday':
119 if ( settings.series[index].format && settings.series[index].format.from && settings.series[index].format.to ) {
120 return jQuery.fn.dataTable.render.moment( settings.series[index].format.from, settings.series[index].format.to );
121 }
122 return jQuery.fn.dataTable.render.moment( 'MM-DD-YYYY' );
123 break;
124 case 'num':
125 const parts = [ '', '', '', '', '' ];
126
127 if ( settings.series[index].format.thousands ) {
128 parts[0] = settings.series[index].format.thousands;
129 }
130
131 if ( settings.series[index].format.decimal ) {
132 parts[1] = settings.series[index].format.decimal;
133 }
134
135 if ( settings.series[index].format.precision ) {
136 parts[2] = settings.series[index].format.precision;
137 }
138
139 if ( settings.series[index].format.prefix ) {
140 parts[3] = settings.series[index].format.prefix;
141 }
142
143 if ( settings.series[index].format.suffix ) {
144 parts[4] = settings.series[index].format.suffix;
145 }
146 return jQuery.fn.dataTable.render.number( ...parts );
147 break;
148 case 'boolean':
149 jQuery.fn.dataTable.render.extra = function( data, type, row ) {
150 if ( ( true === data || 'true' === data ) && 'undefined' !== typeof settings.series[index].format && '' !== settings.series[index].format.truthy ) {
151 return settings.series[index].format.truthy.replace( /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '' );
152 }
153 if ( ( false === data || 'false' === data ) && 'undefined' !== typeof settings.series[index].format && '' !== settings.series[index].format.falsy ) {
154 return settings.series[index].format.falsy.replace( /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '' );
155 }
156 return data;
157 };
158 return jQuery.fn.dataTable.render.extra;
159 break;
160 }
161
162 return data;
163 }
164
165 render() {
166 const settings = this.props.options;
167
168 return (
169 <Fragment>
170 { settings.customcss && (
171 <style>
172 { settings.customcss.oddTableRow && (
173 `#dataTable-instances-${ this.props.id }-${ this.uniqueId } tr.odd {
174 ${ settings.customcss.oddTableRow.color ? `color: ${ settings.customcss.oddTableRow.color } !important;` : '' }
175 ${ settings.customcss.oddTableRow['background-color'] ? `background-color: ${ settings.customcss.oddTableRow['background-color'] } !important;` : '' }
176 ${ settings.customcss.oddTableRow.transform ? `transform: rotate( ${ settings.customcss.oddTableRow.transform }deg ) !important;` : '' }
177 }`
178 )}
179
180 { settings.customcss.evenTableRow && (
181 `#dataTable-instances-${ this.props.id }-${ this.uniqueId } tr.even {
182 ${ settings.customcss.evenTableRow.color ? `color: ${ settings.customcss.evenTableRow.color } !important;` : '' }
183 ${ settings.customcss.evenTableRow['background-color'] ? `background-color: ${ settings.customcss.evenTableRow['background-color'] } !important;` : '' }
184 ${ settings.customcss.evenTableRow.transform ? `transform: rotate( ${ settings.customcss.evenTableRow.transform }deg ) !important;` : '' }
185 }`
186 )}
187
188 { settings.customcss.tableCell && (
189 `#dataTable-instances-${ this.props.id }-${ this.uniqueId } tr td,
190 #dataTable-instances-${ this.props.id }-${ this.uniqueId }_wrapper tr th {
191 ${ settings.customcss.tableCell.color ? `color: ${ settings.customcss.tableCell.color } !important;` : '' }
192 ${ settings.customcss.tableCell['background-color'] ? `background-color: ${ settings.customcss.tableCell['background-color'] } !important;` : '' }
193 ${ settings.customcss.tableCell.transform ? `transform: rotate( ${ settings.customcss.tableCell.transform }deg ) !important;` : '' }
194 }`
195 )}
196 </style>
197 ) }
198
199 <table id={ `dataTable-instances-${ this.props.id }-${ this.uniqueId }` }></table>
200 </Fragment>
201 );
202 }
203 }
204
205 export default DataTables;
206