PluginProbe
M Chart / 1.2.1
M Chart v1.2.1
2.3.2 2.3.1 2.3 2.2.2 2.2.1 2.2 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.10 1.10.1 1.11 1.11.1 1.11.2 1.12 1.2 1.2.1 1.3 1.3.1 1.3.2 All 53 releases
m-chart / components / js / m-chart-admin.js

m-chart-admin.js in M Chart 1.2.1, at components/js/m-chart-admin.js

341 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function( $ ) {
2 'use strict';
3
4 // Start things up
5 m_chart_admin.init = function() {
6 this.post_id = $( document.getElementById( 'post_ID' ) ).attr( 'value' );
7 this.nonce = $( 'input[name="m-chart[nonce]"]' ).attr( 'value' );
8
9 this.create_spreadsheet();
10
11 // Store the setting inputs and title input for use later
12 this.$setting_inputs = $( document.getElementById( 'm-chart' ) ).find( '.settings input, .settings select' );
13 this.$title_input = $( document.getElementById( 'titlediv' ) ).find( 'input' );
14
15 // Only show fields/inputs that are appropriate for the current chart type
16 var $chart_type_select = $( document.getElementById( 'm-chart-type' ) );
17 $chart_type_select.on( 'load, change', this.handle_chart_type );
18 $chart_type_select.trigger( 'change' );
19
20 // Set the form encoding type to multipart/form-data so that the CSV import will work
21 var $form = $( 'form#post' );
22 $form.attr( 'enctype', 'multipart/form-data' );
23
24 // Watch form submissions and stop them if necessary or update data value
25 $form.on( 'submit', function( event ) {
26 if ( false === m_chart_admin.allow_form_submission ) {
27 event.preventDefault();
28 } else {
29 $( document.getElementById( 'm-chart-spreadsheet' ) ).find( '.data' ).val(
30 JSON.stringify( m_chart_admin.$spreadsheet.getData() )
31 );
32 }
33 })
34
35 // Store these for later
36 this.$form_buttons = $( '#save-post, #wp-preview, #post-preview, #publish' );
37
38 // Watch for a new chart to be built
39 if ( 'default' === this.performance ) {
40 $( '.m-chart' ).on( 'render_done', this.generate_image_from_chart );
41 }
42
43 // Watch for clicks on the shortcode input
44 $( document.getElementById( 'm-chart-shortcode' ) ).on( 'click', function () {
45 $( this ).select();
46 });
47
48 // Watch for clicks on the image input
49 $( document.getElementById( 'm-chart-image' ) ).on( 'click', function () {
50 $( this ).select();
51 });
52
53 // Watch for clicks on the CSV tools
54 this.handle_csv_import();
55 this.handle_csv_export();
56
57 // Do instant preview unless it's been turned off
58 if ( 'no-preview' !== this.performance ) {
59 this.watch_for_chart_changes();
60 }
61 };
62
63 // Instantiate the spreedsheet
64 m_chart_admin.create_spreadsheet = function() {
65 var $spreadsheet_div = document.getElementById( 'hands-on-table-sheet-' + this.post_id );
66
67 this.$spreadsheet = new Handsontable( $spreadsheet_div, {
68 data: hands_on_table_data,
69 colHeaders: true,
70 rowHeaders: true,
71 height: 350,
72 minRows: 17,
73 minCols: 37,
74 minSpareRows: 1,
75 minSpareCols: 1,
76 contextMenu: true,
77 afterChange: function() {
78 // If a value changes update the chart
79 m_chart_admin.refresh_chart();
80 }
81 });
82 }
83
84 // Handle chart type input changes so the settings UI only reflects appropriate options
85 m_chart_admin.handle_chart_type = function( event ) {
86 var chart_type = $( this ).attr( 'value' );
87 var $chart_meta_box = $( document.getElementById( 'm-chart' ) );
88
89 // Show everything before hiding the options we don't want
90 $chart_meta_box.find( '.row' ).removeClass( 'hide' );
91
92 if (
93 'area' === chart_type
94 || 'column' === chart_type
95 || 'bar' === chart_type
96 ) {
97 $chart_meta_box.find( '.row.y-min' ).addClass( 'hide' );
98 }
99
100 if ( 'pie' === chart_type ) {
101 $chart_meta_box.find( '.row.vertical-axis, .row.horizontal-axis, .row.y-min' ).addClass( 'hide' );
102 }
103 };
104
105 // Handle CSV import functionality
106 m_chart_admin.handle_csv_import = function() {
107 var $csv_meta_box = $( document.getElementById( 'm-chart-csv' ) );
108 var $select = $csv_meta_box.find( '.import .select.button' );
109 var $import = $csv_meta_box.find( '.import .import.button' );
110 var $import_form = $( document.getElementById( 'm-chart-csv-import-form' ) );
111 var $file_input = $import_form.find( 'input[type=file]' );
112 var $file_info = $csv_meta_box.find( '.file-info' );
113 var $file_error = $csv_meta_box.find( '.file.error' );
114 var $file_import = $csv_meta_box.find( '.import.in-progress' );
115 var $import_error = $csv_meta_box.find( '.import.error' );
116 var $cancel = $csv_meta_box.find( '.dashicons-dismiss' );
117
118 // Watch for clicks on the select button
119 $select.on( 'click', function( event ) {
120 event.preventDefault();
121 $file_error.addClass( 'hide' );
122 $import_error.addClass( 'hide' );
123 $file_input.trigger( 'click' );
124 });
125
126 // Watch for changes to the file input
127 $file_input.on( 'change', function( event ) {
128 var file_name = $( this ).attr( 'value' ).replace( /C:\\fakepath\\/i, '' );
129
130 if ( -1 === file_name.search( /.+(\.csv)$/ ) ) {
131 $file_error.removeClass( 'hide' );
132 return;
133 }
134
135 $file_info.find( '.file-name' ).text( file_name );
136
137 $select.addClass( 'hide' );
138 $import.removeClass( 'hide' );
139 $file_info.removeClass( 'hide' );
140 });
141
142 // Watch for clicks on the cancel button
143 $cancel.on( 'click', function( event ) {
144 event.preventDefault();
145 $file_info.addClass( 'hide' );
146 $file_input.attr( 'value', '' );
147 $select.removeClass( 'hide' );
148 $import.addClass( 'hide' );
149 });
150
151 // Watch for clicks on import button
152 $import.on( 'click', function( event ) {
153 event.preventDefault();
154
155 $file_info.addClass( 'hide' );
156 $import.addClass( 'hide' );
157 $import_error.addClass( 'hide' );
158 $file_import.removeClass( 'hide' );
159
160 $import_form.trigger( 'submit' );
161 });
162
163 // Watch for CSV import form submission
164 $import_form.on( 'submit', function( event ) {
165 event.preventDefault();
166
167 var $form_data = new FormData( this );
168
169 $form_data.append( 'post_id', m_chart_admin.post_id );
170 $form_data.append( 'nonce', m_chart_admin.nonce );
171
172 var request = $.ajax({
173 url: 'admin-ajax.php?action=m_chart_import_csv',
174 type: 'POST',
175 data: $form_data,
176 cache: false,
177 dataType: 'json',
178 // Don't process the files
179 processData: false,
180 // Set content type to false as jQuery will tell the server its a query string request
181 contentType: false
182 });
183
184 request.done( function( response ) {
185 if ( false == response.success ) {
186 console.log(response)
187 $import_error.text( response.data );
188 $import_error.removeClass( 'hide' );
189
190 $file_input.attr( 'value', '' );
191 $select.removeClass( 'hide' );
192 $file_import.addClass( 'hide' );
193
194 return false;
195 }
196
197 // Update the spreadsheet with the new data
198 m_chart_admin.$spreadsheet.loadData( response.data );
199
200 $file_input.attr( 'value', '' );
201 $select.removeClass( 'hide' );
202 $file_import.addClass( 'hide' );
203 });
204 });
205 };
206
207 // Handle CSV export functionality
208 m_chart_admin.handle_csv_export = function() {
209 $( document.getElementById( 'm-chart-csv' ) ).find( '.export a' ).on( 'click', function( event ) {
210 event.preventDefault();
211
212 var $form = $( document.getElementById( 'm-chart-csv-export-form' ) );
213
214 $( document.getElementById( 'm-chart-csv-post-id' ) ).val( m_chart_admin.post_id );
215 $( document.getElementById( 'm-chart-csv-data' ) ).val( JSON.stringify( m_chart_admin.$spreadsheet.getData() ) );
216 $( document.getElementById( 'm-chart-csv-title' ) ).val( m_chart_admin.$title_input.attr( 'value' ) );
217
218 $form.trigger( 'submit' );
219 });
220 };
221
222 // Generate a PNG image out of a rendered chart
223 m_chart_admin.generate_image_from_chart = function( event ) {
224 var svg = event.chart.getSVG();
225 var width = svg.match(/^<svg[^>]*width\s*=\s*\"?(\d+)\"?[^>]*>/)[1];
226 var height = svg.match(/^<svg[^>]*height\s*=\s*\"?(\d+)\"?[^>]*>/)[1];
227
228 // Double the width/height values in SVG
229 svg = svg.replace( 'width="' + width + '"', 'width="' + ( width * 2 ) + '"' );
230 svg = svg.replace( 'height="' + height + '"', 'height="' + ( height * 2 ) + '"' );
231
232 // Scale the SVG object
233 svg = svg.replace( '<svg ', '<svg transform="scale(2, 2)" ' );
234
235 // Create a Canvas object out of the SVG
236 var $canvas = $( '#m-chart-canvas-render-' + event.post_id );
237 m_chart_admin.canvas = $canvas.get( 0 );
238
239 canvg( m_chart_admin.canvas, svg );
240
241 // Create Canvas context so we can play with it before saving
242 m_chart_admin.canvas_context = m_chart_admin.canvas.getContext( '2d' );
243
244 $( '.m-chart' ).trigger({
245 type: 'canvas_done'
246 });
247
248 var img = m_chart_admin.canvas.toDataURL( 'image/png' );
249
250 // Save the image string to the text area so we can save it on update/publish
251 $( document.getElementById( 'm-chart-img' ) ).attr( 'value', img );
252
253 // Allow form submission now that we've got a valid img value set
254 m_chart_admin.form_submission( true );
255 };
256
257 // Watch for changes to the chart settings or title
258 m_chart_admin.watch_for_chart_changes = function() {
259 this.$setting_inputs.on( 'change', function() {
260 m_chart_admin.refresh_chart();
261 });
262
263 this.$title_input.on( 'change', function() {
264 m_chart_admin.refresh_chart();
265 });
266 };
267
268 // Refresh chart
269 m_chart_admin.refresh_chart = function() {
270 m_chart_admin.refresh_counter++;
271
272 // Handsontable calls afterChange on the first render for some silly reason
273 if ( 1 === m_chart_admin.refresh_counter ) {
274 return false;
275 }
276
277 // Stop any existing requests so we don't just pile them up
278 if ( this.request ) {
279 this.request.abort();
280 }
281
282 // Stop form submission while we wait for the chart to refresh and a new image to generate
283 m_chart_admin.form_submission( false );
284
285 // Build an object with all fo the post_meta values
286 var $post_meta = {};
287
288 this.$setting_inputs.each( function() {
289 // Don't record unselected/unchecked radio/checkboxes
290 if (
291 'radio' !== $( this ).attr( 'type' )
292 && 'checkbox' !== $( this ).attr( 'type' )
293 || true === $( this ).is( ':checked' )
294 ) {
295 $post_meta[ $( this ).attr( 'name' ).replace( /^m-chart\[|\]$/g , '' ) ] = $( this ).attr( 'value' );
296 }
297 });
298
299 $post_meta.data = JSON.stringify( m_chart_admin.$spreadsheet.getData() );
300
301 // Request a new chart_args object so we can rerender the chart with the changes
302 this.request = $.ajax({
303 url: 'admin-ajax.php?action=m_chart_get_chart_args',
304 type: 'POST',
305 data: {
306 post_id: m_chart_admin.post_id,
307 nonce: m_chart_admin.nonce,
308 library: 'highcharts',
309 title: this.$title_input.attr( 'value' ),
310 post_meta: $post_meta
311 },
312 cache: false,
313 dataType: 'json'
314 });
315
316 this.request.done( function( response ) {
317 console.log( response );
318 if ( true !== response.success ) {
319 return false;
320 }
321
322 // Update active chart args and then rerender the chart
323 window[ 'm_chart_highcharts_' + m_chart_admin.post_id ].chart_args = response.data;
324 window[ 'm_chart_highcharts_' + m_chart_admin.post_id ].render_chart();
325 });
326 };
327
328 m_chart_admin.form_submission = function( enable ) {
329 m_chart_admin.allow_form_submission = enable;
330
331 if ( false === enable ) {
332 m_chart_admin.$form_buttons.addClass( 'disabled' );
333 } else {
334 m_chart_admin.$form_buttons.removeClass( 'disabled' );
335 }
336 };
337
338 $( function() {
339 m_chart_admin.init();
340 } );
341 })( jQuery );