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 / frame.js

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

614 lines 24.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global prompt */
2 /* global visualizer */
3 /* global alert */
4 /* global ajaxurl */
5 /* global CodeMirror */
6
7 (function ($) {
8 $(window).on('load', function(){
9 // scroll to the selected chart type.
10 if($('label.type-label.type-label-selected').length > 0) {
11 $('label.type-label.type-label-selected')[0].scrollIntoView();
12 }
13 });
14
15 $(document).ready(function () {
16 onReady();
17 });
18
19 function onReady() {
20 // open the correct source tab/sub-tab.
21 var source = $('#visualizer-chart-id').attr('data-chart-source');
22 $('li.viz-group.' + source).addClass('open');
23 $('li.viz-group.' + source + ' span.viz-section-title.' + source).addClass('open');
24 $('li.viz-group.' + source + ' span.viz-section-title.' + source + '.open').parent().find('div.viz-section-items').show();
25
26 init_permissions();
27
28 if(typeof visualizer !== 'undefined' && visualizer.is_pro) {
29 init_db_import();
30 init_filter_import();
31 }
32
33 init_json_import();
34
35 init_editor_table();
36
37 // update the manual configuation link to point to the correct chart type.
38 var type = $('#visualizer-chart-id').attr('data-chart-type');
39 var chart_type_in_api_link = type + 'chart';
40 switch (type) {
41 case "gauge":
42 case "table":
43 case "timeline":
44 chart_type_in_api_link = type;
45 break;
46 }
47
48 if($('span.viz-gvlink').length > 0) {
49 $('span.viz-gvlink').html($('span.viz-gvlink').html().replace('?', chart_type_in_api_link));
50 }
51
52 $('.type-radio').change(function () {
53 $('.type-label-selected').removeClass('type-label-selected');
54 $(this).parent().addClass('type-label-selected');
55 });
56
57 $('#vz-chart-settings h2').click(function () {
58 $("#vz-chart-source").hide();
59 $("#vz-chart-permissions").removeClass('open').addClass('bottom-fixed');
60 $(this).parent().removeClass('bottom-fixed').addClass('open');
61 $("#vz-chart-permissions .viz-group-header").hide();
62 return false;
63 });
64 $('#vz-chart-settings .customize-section-back').click(function () {
65 $("#vz-chart-source").show();
66 $(this).parent().parent().removeClass('open').addClass('bottom-fixed');
67
68 return false;
69 });
70
71 // collapse other open sections of this group
72 $('.viz-group-title').click(function () {
73 var parent = $(this).parent();
74
75 if (parent.hasClass('open')) {
76 parent.removeClass('open');
77 } else {
78 parent.parent().find('.viz-group.open').removeClass('open');
79 parent.addClass('open');
80 }
81
82 // if the user wanted to perform an action
83 // and the chart is no longer showing because that particular screen is showing
84 // e.g. create parameters
85 // and then the user decided to click on another action
86 // let's invoke the show chart of the previous action so that the chart shows
87 // and the user does not get confused
88 $('input[type="button"][data-current!="chart"].show-chart-toggle').trigger('click');
89 });
90
91 // collapse other open subsections of this section
92 $('.viz-section-title').click(function () {
93 var grandparent = $(this).parent().parent();
94 grandparent.find('.viz-section-title.open ~ .viz-section-items').hide();
95 grandparent.find('.viz-section-title.open').removeClass('open');
96 });
97
98 $('#view-remote-file').click(function () {
99 var url = $(this).parent().find('#remote-data').val();
100
101 if (url !== '') {
102 if (/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url)) {
103 if (url.substr(url.length - 8) === '/pubhtml') {
104 url = url.substring(0, url.length - 8) + '/export?format=csv';
105 }
106
107 $('#canvas').lock();
108 $(this).parent().submit();
109 } else {
110 alert(visualizer.l10n.invalid_source);
111 }
112 }
113 });
114
115 $('#vz-import-file').click(function (e) {
116 e.preventDefault();
117 if ($.trim($(this).parent().find("#csv-file").val()) !== '') {
118 $('#canvas').lock();
119 $(this).parent().submit();
120 }
121 });
122
123 $('#thehole').load(function () {
124 $('#canvas').unlock();
125 });
126
127 $('.viz-section-title').click(function () {
128 $(this).toggleClass('open').parent().find('.viz-section-items').toggle();
129 });
130
131 $('.more-info').click(function () {
132 $(this).parent().find('.viz-section-description:first').toggle();
133 return false;
134 });
135
136 $('#cancel-button').click(function () {
137 $('#cancel-form').submit();
138 });
139
140 init_type_vs_library();
141
142 $('.viz-abort').on('click', function(e){
143 e.preventDefault();
144 window.parent.postMessage('visualizer:mediaframe:close', '*');
145 });
146
147 }
148
149 function init_type_vs_library() {
150 var $data = $('select.viz-select-library').attr('data-type-vs-library');
151 if(typeof $data === 'undefined' || $data.length === 0){
152 return;
153 }
154 var $typeVsLibrary = JSON.parse( $('select.viz-select-library').attr('data-type-vs-library') );
155 // disable all unsupported libraries for the chart type.
156 $('input.type-radio').on('click', function(){
157 enable_libraries_for($(this).val(), $typeVsLibrary);
158 });
159
160 enable_libraries_for($('input.type-radio:checked').val(), $typeVsLibrary);
161 }
162
163 function enable_libraries_for($type, $typeVsLibrary) {
164 $('select.viz-select-library option').addClass('disabled').attr('disabled', 'disabled');
165 var $libs = $typeVsLibrary[$type];
166 $.each($libs, function( i, $lib ) {
167 $('select.viz-select-library option[value="' + $lib + '"]').removeClass('disabled').removeAttr('disabled');
168 });
169 $('select.viz-select-library').val( $('select.viz-select-library option:not(.disabled)').val() );
170 }
171
172 function init_permissions(){
173 $('#vz-chart-permissions h2').click(function () {
174 $("#vz-chart-source").hide();
175 $("#vz-chart-permissions .viz-group-header").show();
176 $("#vz-chart-settings").removeClass('open').addClass('bottom-fixed');
177
178 $('#settings-button').click(function(e) {
179 e.preventDefault();
180 $('#permissions-form').submit();
181 $('#settings-form').submit();
182 });
183
184 $(this).parent().removeClass('bottom-fixed').addClass('open');
185
186 return false;
187 });
188 $('#vz-chart-permissions .customize-section-back').click(function () {
189 $("#vz-chart-source").show();
190
191 $("#vz-chart-permissions .viz-group-header").hide();
192 $('#settings-button').click(function(e) {
193 e.preventDefault();
194 $('#settings-form').submit();
195 });
196
197 $(this).parent().parent().removeClass('open').addClass('bottom-fixed');
198
199 return false;
200 });
201
202 $('.visualizer-permission').chosen({
203 width : '50%',
204 search_contains : true
205 });
206
207 $('.visualizer-permission-type').each(function(x, y){
208 var type = $(y).attr('data-visualizer-permission-type');
209 var child = $('.visualizer-permission-' + type + '-specific');
210 if($(y).val() === 'all'){
211 child.next('div.chosen-container').hide();
212 return;
213 }
214 });
215
216 $('.visualizer-permission-type').on('change', function(evt, params) {
217 var type = $(this).attr('data-visualizer-permission-type');
218 var child = $('.visualizer-permission-' + type + '-specific');
219 child.empty();
220 if(params.selected === 'all'){
221 child.next('div.chosen-container').hide();
222 return;
223 } else {
224 child.next('div.chosen-container').show();
225 }
226 child.append('<option value="">' + visualizer.l10n['loading'] + '</option>').trigger('chosen:updated');
227 $.ajax({
228 url : visualizer.ajax['url'],
229 method : 'post',
230 data : {
231 'action' : visualizer.ajax['actions']['permissions'],
232 'nonce' : visualizer.ajax['nonces']['permissions'],
233 'type' : params.selected
234 },
235 success : function(d, textStatus, XMLHttpRequest){
236 if(d.success) {
237 child.empty();
238 $.each(d.data, function(k, v){
239 child.append('<option value="' + k + '">' + v + '</option>');
240 });
241 child.trigger('chosen:updated');
242 }
243 }
244 });
245 });
246 }
247
248 // https://codemirror.net/
249 function init_db_import_component(){
250 var table_columns = visualizer.db_query.tables;
251 var code_mirror = wp.CodeMirror || CodeMirror;
252 var cm = code_mirror.fromTextArea($('.visualizer-db-query').get(0), {
253 value: $('.visualizer-db-query').val(),
254 autofocus: true,
255 mode: 'text/x-mysql',
256 lineWrapping: true,
257 dragDrop: false,
258 matchBrackets: true,
259 autoCloseBrackets: true,
260 extraKeys: {"Ctrl-Space": "autocomplete"},
261 hintOptions: { tables: table_columns }
262 });
263
264 // force refresh so that the query shows on first time load. Otherwise you have to click on the editor for it to show.
265 $('body').on('visualizer:db:query:focus', function(event, data){
266 cm.refresh();
267 });
268
269 cm.focus();
270
271 // update text area.
272 cm.on('inputRead', function(x, y){
273 cm.save();
274 });
275
276 // backspace and delete do not register so the text box does not get empty if the entire query is deleted
277 // from the editor. Let's force this.
278 $('body').on('visualizer:db:query:update', function(event, data){
279 cm.save();
280 });
281 }
282
283 function init_filter_import() {
284 $( '#db-filter-save-button' ).on( 'click', function(){
285 $('#vz-filter-wizard').submit();
286 });
287 }
288
289 function init_db_import(){
290 $( '#visualizer-db-query' ).css("z-index", "-1").hide();
291
292 init_db_import_component();
293
294 $('#visualizer-query-fetch').on('click', function(e){
295
296 $('body').trigger('visualizer:db:query:update', {});
297 if($('.visualizer-db-query').val() === ''){
298 return;
299 }
300
301 start_ajax($('#visualizer-db-query'));
302 $('.db-wizard-results').empty();
303 $('.db-wizard-error').empty();
304 $.ajax({
305 url : ajaxurl,
306 method : 'post',
307 data : {
308 'action' : visualizer.ajax['actions']['db_get_data'],
309 'security' : visualizer.ajax['nonces']['db_get_data'],
310 'params' : $('#db-query-form').serialize()
311 },
312 success : function(data){
313 if(data.success){
314 $('.db-wizard-results').html(data.data.table);
315 $('#results').DataTable({
316 "paging": false
317 });
318 }else{
319 $('.db-wizard-error').html(data.data.msg);
320 }
321 },
322 complete: function(){
323 end_ajax($('#visualizer-db-query'));
324 }
325 });
326 });
327
328 $( '#db-chart-button' ).on( 'click', function(){
329 $('#content').css('width', 'calc(100% - 300px)');
330 if( $(this).attr( 'data-current' ) === 'chart'){
331 $(this).val( $(this).attr( 'data-t-filter' ) );
332 $(this).html( $(this).attr( 'data-t-filter' ) );
333 $(this).attr( 'data-current', 'filter' );
334 $( '.visualizer-editor-lhs' ).hide();
335 $( '#visualizer-db-query' ).css("z-index", "9999").show();
336 $('body').trigger('visualizer:db:query:focus', {});
337 $( '#canvas' ).hide();
338 }else{
339 var filter_button = $(this);
340 $( '#visualizer-db-query' ).css("z-index", "-1").hide();
341 $('#canvas').lock();
342 filter_button.val( filter_button.attr( 'data-t-chart' ) );
343 filter_button.html( filter_button.attr( 'data-t-chart' ) );
344 filter_button.attr( 'data-current', 'chart' );
345 $( '#canvas' ).css("z-index", "1").show();
346 $( '#db-chart-save-button' ).trigger('click');
347 }
348 } );
349
350 $( '#db-chart-save-button' ).on( 'click', function(){
351 // submit only if a query has been provided.
352 if($('#db-query-form .visualizer-db-query').val().length > 0){
353 $('#viz-db-wizard-params').val($('#db-query-form').serialize());
354 $('#vz-db-wizard').submit();
355 }else{
356 $('#canvas').unlock();
357 }
358 });
359 }
360
361 function init_json_import(){
362 if(typeof visualizer === 'undefined'){
363 return;
364 }
365
366 var regex = new RegExp(visualizer.json_tag_separator, 'g');
367
368 $( '#visualizer-json-screen' ).css("z-index", "-1").hide();
369 $('.visualizer-json-form').accordion({
370 heightStyle: 'content',
371 active: 0
372 });
373
374 // toggle between chart and create/modify parameters
375 $( '#json-chart-button' ).on( 'click', function(){
376 $('#content').css('width', 'calc(100% - 300px)');
377 if( $(this).attr( 'data-current' ) === 'chart'){
378 $(this).val( $(this).attr( 'data-t-filter' ) );
379 $(this).html( $(this).attr( 'data-t-filter' ) );
380 $(this).attr( 'data-current', 'filter' );
381 $( '.visualizer-editor-lhs' ).hide();
382 $( '#visualizer-json-screen' ).css("z-index", "9999").show();
383 $( '#canvas' ).hide();
384 }else{
385 var filter_button = $(this);
386 $( '#visualizer-json-screen' ).css("z-index", "-1").hide();
387 $('#canvas').lock();
388 filter_button.val( filter_button.attr( 'data-t-chart' ) );
389 filter_button.html( filter_button.attr( 'data-t-chart' ) );
390 filter_button.attr( 'data-current', 'chart' );
391 $( '#canvas' ).css("z-index", "1").show();
392 $('#canvas').unlock();
393 }
394 } );
395
396 // fetch the roots for the provided endpoint
397 $( '#visualizer-json-fetch' ).on( 'click', function(e){
398 e.preventDefault();
399 $('.visualizer-json-form').accordion( 'option', 'active', 0 );
400 $('.visualizer-json-form h3.viz-step:not(.step1)').addClass('ui-state-disabled');
401 $('.json-table').html('');
402 start_ajax( $( '#visualizer-json-screen' ) );
403 $.ajax({
404 url : ajaxurl,
405 method : 'post',
406 data : {
407 'action' : visualizer.ajax['actions']['json_get_roots'],
408 'security' : visualizer.ajax['nonces']['json_get_roots'],
409 'params' : $('#json-endpoint-form').serialize()
410 },
411 success : function(data){
412 if(data.success){
413 $('#json-root-form [name="url"]').val(data.data.url);
414 $('#vz-import-json-root').empty();
415 $.each(data.data.roots, function(i, name){
416 $('#vz-import-json-root').append('<option value="' + name + '">' + name.replace(regex, visualizer.json_tag_separator_view) + '</option>');
417 });
418 $('#json-root-form').fadeIn('medium');
419 json_accordion_activate(1, true);
420 }else{
421 alert(data.data.msg);
422 }
423 },
424 complete: function(){
425 end_ajax($('#visualizer-json-screen'));
426 }
427 });
428 });
429
430 // fetch the data for the chosen root
431 $( '#visualizer-json-parse' ).on( 'click', function(e){
432 e.preventDefault();
433 $('.visualizer-json-form h3.viz-step:not(.step1):not(.step2)').addClass('ui-state-disabled');
434 $('.json-table').html('');
435 start_ajax( $( '#visualizer-json-screen' ) );
436 $.ajax({
437 url : ajaxurl,
438 method : 'post',
439 data : {
440 'action' : visualizer.ajax['actions']['json_get_data'],
441 'security' : visualizer.ajax['nonces']['json_get_data'],
442 'params' : $('#json-root-form').serialize()
443 },
444 success : function(data){
445 if(data.success){
446 $('#vz-import-json-paging option:not(.static)').remove();
447 if(data.data.paging.length > 0){
448 var $template = $('#vz-import-json-paging').attr('data-template');
449 $.each(data.data.paging, function(i, name){
450 var display = name.replace(regex, visualizer.json_tag_separator_view);
451 display = $template.replace('?', display);
452 $('#vz-import-json-paging').append('<option value="' + name + '">' + display + '</option>');
453 });
454 $('.json-pagination').show();
455 }
456 $('#json-conclude-form [name="url"]').val(data.data.url);
457 $('#json-conclude-form [name="root"]').val(data.data.root);
458 $('#json-conclude-form .json-table').html(data.data.table);
459
460 var $table = create_editor_table( '#json-conclude-form' );
461
462 json_accordion_activate(3, true);
463 json_accordion_activate(2, false);
464 $table.columns.adjust().draw();
465 }else{
466 alert(data.data.msg);
467 }
468 },
469 complete: function(){
470 end_ajax($('#visualizer-json-screen'));
471 }
472 });
473 });
474
475 // when the data is set and the chart is updated, toggle the screen so that the chart is shown
476 $('#json-conclude-form').on( 'submit', function(e){
477 // populate the form elements that are in the misc tab.
478 $('#json-conclude-form-helper .json-form-element').each(function(x, y){
479 $('#json-conclude-form').append('<input type="hidden" name="' + y.name + '" value="' + y.value + '">');
480 });
481 $( '#json-chart-button' ).trigger('click');
482 $('#canvas').lock();
483 });
484
485 // update the schedule
486 $('#json-chart-save-button').on('click', function(e){
487 e.preventDefault();
488 $('#canvas').lock();
489 $.ajax({
490 url : ajaxurl,
491 method : 'post',
492 data : {
493 'action' : visualizer.ajax['actions']['json_set_schedule'],
494 'security' : visualizer.ajax['nonces']['json_set_schedule'],
495 'chart' : $('#vz-json-time').attr('data-chart'),
496 'time' : $('#vz-json-time').val()
497 },
498 success : function(data){
499 // do nothing.
500 },
501 complete: function(){
502 $('#canvas').unlock();
503 }
504 });
505 });
506
507 }
508
509 function init_editor_table() {
510 $('body').on('visualizer:db:editor:table:init', function(event, data){
511 var $table = create_editor_table('.viz-table-editor');
512 $('body').on('visualizer:db:editor:table:redraw', function(event, data){
513 $table.draw();
514 });
515 });
516 }
517
518 function create_editor_table(element) {
519 var settings = {
520 paging: false,
521 searching: false,
522 ordering: false,
523 select: false,
524 scrollX: "100%",
525 scrollY: "400px",
526 info: false,
527 colReorder: {
528 fixedColumnsLeft: 1
529 }
530 };
531
532 // show column visibility button only when more than 6 columns are found (including the Label column)
533 if($(element + ' table.viz-editor-table thead tr th').length > 6){
534 $.extend( settings, {
535 dom: 'Bt',
536 buttons: [
537 {
538 extend: 'colvis',
539 columns: ':gt(0)',
540 collectionLayout: 'four-column'
541 }
542 ]
543 } );
544 }
545
546 var $table = $(element + ' .viz-editor-table').DataTable(settings);
547 return $table;
548 }
549
550 function json_accordion_activate($step, $activate){
551 $('.visualizer-json-form h3.viz-step.step' + ( $step + 1 )).removeClass('ui-state-disabled');
552 if($activate){
553 $('.visualizer-json-form').accordion( 'option', 'active', $step );
554 }
555 }
556
557 function start_ajax(element){
558 element.lock();
559 }
560
561 function end_ajax(element){
562 element.unlock();
563 }
564
565 })(jQuery);
566
567 (function ($) {
568 $.fn.lock = function () {
569 $(this).each(function () {
570 var $this = $(this);
571 var position = $this.css('position');
572
573 if (!position) {
574 position = 'static';
575 }
576
577 switch (position) {
578 case 'absolute':
579 case 'relative':
580 break;
581 default:
582 $this.css('position', 'relative');
583 break;
584 }
585 $this.data('position', position);
586
587 var width = $this.width(),
588 height = $this.height();
589
590 var locker = $('<div class="locker"></div>');
591 locker.width(width).height(height);
592
593 var loader = $('<div class="locker-loader"></div>');
594 loader.width(width).height(height);
595
596 locker.append(loader);
597 $this.append(locker);
598 $(window).resize(function () {
599 $this.find('.locker,.locker-loader').width($this.width()).height($this.height());
600 });
601 });
602
603 return $(this);
604 };
605
606 $.fn.unlock = function () {
607 $(this).each(function () {
608 $(this).find('.locker').remove();
609 $(this).css('position', $(this).data('position'));
610 });
611
612 return $(this);
613 };
614 })(jQuery);