| @@ -1,12 +1,51 @@ | ||
| 1 | 1 | /* global prompt */ |
| 2 | 2 | /* global visualizer */ |
| 3 | 3 | /* global alert */ |
| 4 | +/* global ajaxurl */ | |
| 5 | +/* global CodeMirror */ | |
| 4 | 6 | |
| 5 | 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 | + | |
| 6 | 15 | $(document).ready(function () { |
| 16 | + // open the correct source tab/sub-tab. | |
| 17 | + var source = $('#visualizer-chart-id').attr('data-chart-source'); | |
| 18 | + $('li.viz-group.' + source).addClass('open'); | |
| 19 | + $('li.viz-group.' + source + ' span.viz-section-title.' + source).addClass('open'); | |
| 20 | + $('li.viz-group.' + source + ' span.viz-section-title.' + source + '.open').parent().find('div.viz-section-items').show(); | |
| 21 | + | |
| 7 | 22 | init_permissions(); |
| 8 | 23 | |
| 24 | + if(typeof visualizer !== 'undefined' && visualizer.is_pro) { | |
| 25 | + init_db_import(); | |
| 26 | + init_filter_import(); | |
| 27 | + } | |
| 28 | + | |
| 29 | + init_json_import(); | |
| 30 | + | |
| 31 | + init_editor_table(); | |
| 32 | + | |
| 33 | + // update the manual configuation link to point to the correct chart type. | |
| 34 | + var type = $('#visualizer-chart-id').attr('data-chart-type'); | |
| 35 | + var chart_type_in_api_link = type + 'chart'; | |
| 36 | + switch (type) { | |
| 37 | + case "gauge": | |
| 38 | + case "table": | |
| 39 | + case "timeline": | |
| 40 | + chart_type_in_api_link = type; | |
| 41 | + break; | |
| 42 | + } | |
| 43 | + | |
| 44 | + if($('span.viz-gvlink').length > 0) { | |
| 45 | + $('span.viz-gvlink').html($('span.viz-gvlink').html().replace('?', chart_type_in_api_link)); | |
| 46 | + } | |
| 47 | + | |
| 9 | 48 | $('.type-radio').change(function () { |
| 10 | 49 | $('.type-label-selected').removeClass('type-label-selected'); |
| 11 | 50 | $(this).parent().addClass('type-label-selected'); |
| 12 | 51 | }); |
| @@ -71,8 +110,12 @@ | ||
| 71 | 110 | $(this).parent().find('.viz-section-description:first').toggle(); |
| 72 | 111 | return false; |
| 73 | 112 | }); |
| 74 | 113 | |
| 114 | + $('#cancel-button').click(function () { | |
| 115 | + $('#cancel-form').submit(); | |
| 116 | + }); | |
| 117 | + | |
| 75 | 118 | }); |
| 76 | 119 | |
| 77 | 120 | function init_permissions(){ |
| 78 | 121 | $('#vz-chart-permissions h2').click(function () { |
| @@ -148,8 +191,318 @@ | ||
| 148 | 191 | } |
| 149 | 192 | }); |
| 150 | 193 | }); |
| 151 | 194 | } |
| 195 | + | |
| 196 | + // https://codemirror.net/ | |
| 197 | + function init_db_import_component(){ | |
| 198 | + var table_columns = visualizer.db_query.tables; | |
| 199 | + var code_mirror = wp.CodeMirror || CodeMirror; | |
| 200 | + var cm = code_mirror.fromTextArea($('.visualizer-db-query').get(0), { | |
| 201 | + value: $('.visualizer-db-query').val(), | |
| 202 | + autofocus: true, | |
| 203 | + mode: 'text/x-mysql', | |
| 204 | + lineWrapping: true, | |
| 205 | + dragDrop: false, | |
| 206 | + matchBrackets: true, | |
| 207 | + autoCloseBrackets: true, | |
| 208 | + extraKeys: {"Ctrl-Space": "autocomplete"}, | |
| 209 | + hintOptions: { tables: table_columns } | |
| 210 | + }); | |
| 211 | + | |
| 212 | + // force refresh so that the query shows on first time load. Otherwise you have to click on the editor for it to show. | |
| 213 | + $('body').on('visualizer:db:query:focus', function(event, data){ | |
| 214 | + cm.refresh(); | |
| 215 | + }); | |
| 216 | + | |
| 217 | + cm.focus(); | |
| 218 | + | |
| 219 | + // update text area. | |
| 220 | + cm.on('inputRead', function(x, y){ | |
| 221 | + cm.save(); | |
| 222 | + }); | |
| 223 | + | |
| 224 | + // backspace and delete do not register so the text box does not get empty if the entire query is deleted | |
| 225 | + // from the editor. Let's force this. | |
| 226 | + $('body').on('visualizer:db:query:update', function(event, data){ | |
| 227 | + cm.save(); | |
| 228 | + }); | |
| 229 | + } | |
| 230 | + | |
| 231 | + function init_filter_import() { | |
| 232 | + $( '#db-filter-save-button' ).on( 'click', function(){ | |
| 233 | + $('#vz-filter-wizard').submit(); | |
| 234 | + }); | |
| 235 | + } | |
| 236 | + | |
| 237 | + function init_db_import(){ | |
| 238 | + $( '#visualizer-db-query' ).css("z-index", "-1").hide(); | |
| 239 | + | |
| 240 | + init_db_import_component(); | |
| 241 | + | |
| 242 | + $('#visualizer-query-fetch').on('click', function(e){ | |
| 243 | + | |
| 244 | + $('body').trigger('visualizer:db:query:update', {}); | |
| 245 | + if($('.visualizer-db-query').val() === ''){ | |
| 246 | + return; | |
| 247 | + } | |
| 248 | + | |
| 249 | + start_ajax($('#visualizer-db-query')); | |
| 250 | + $('.db-wizard-results').empty(); | |
| 251 | + $('.db-wizard-error').empty(); | |
| 252 | + $.ajax({ | |
| 253 | + url : ajaxurl, | |
| 254 | + method : 'post', | |
| 255 | + data : { | |
| 256 | + 'action' : visualizer.ajax['actions']['db_get_data'], | |
| 257 | + 'security' : visualizer.ajax['nonces']['db_get_data'], | |
| 258 | + 'params' : $('#db-query-form').serialize() | |
| 259 | + }, | |
| 260 | + success : function(data){ | |
| 261 | + if(data.success){ | |
| 262 | + $('.db-wizard-results').html(data.data.table); | |
| 263 | + $('#results').DataTable({ | |
| 264 | + "paging": false | |
| 265 | + }); | |
| 266 | + }else{ | |
| 267 | + $('.db-wizard-error').html(data.data.msg); | |
| 268 | + } | |
| 269 | + }, | |
| 270 | + complete: function(){ | |
| 271 | + end_ajax($('#visualizer-db-query')); | |
| 272 | + } | |
| 273 | + }); | |
| 274 | + }); | |
| 275 | + | |
| 276 | + $( '#db-chart-button' ).on( 'click', function(){ | |
| 277 | + $('#content').css('width', 'calc(100% - 300px)'); | |
| 278 | + if( $(this).attr( 'data-current' ) === 'chart'){ | |
| 279 | + $(this).val( $(this).attr( 'data-t-filter' ) ); | |
| 280 | + $(this).html( $(this).attr( 'data-t-filter' ) ); | |
| 281 | + $(this).attr( 'data-current', 'filter' ); | |
| 282 | + $( '.visualizer-editor-lhs' ).hide(); | |
| 283 | + $( '#visualizer-db-query' ).css("z-index", "9999").show(); | |
| 284 | + $('body').trigger('visualizer:db:query:focus', {}); | |
| 285 | + $( '#canvas' ).hide(); | |
| 286 | + }else{ | |
| 287 | + var filter_button = $(this); | |
| 288 | + $( '#visualizer-db-query' ).css("z-index", "-1").hide(); | |
| 289 | + $('#canvas').lock(); | |
| 290 | + filter_button.val( filter_button.attr( 'data-t-chart' ) ); | |
| 291 | + filter_button.html( filter_button.attr( 'data-t-chart' ) ); | |
| 292 | + filter_button.attr( 'data-current', 'chart' ); | |
| 293 | + $( '#canvas' ).css("z-index", "1").show(); | |
| 294 | + $( '#db-chart-save-button' ).trigger('click'); | |
| 295 | + } | |
| 296 | + } ); | |
| 297 | + | |
| 298 | + $( '#db-chart-save-button' ).on( 'click', function(){ | |
| 299 | + $('#viz-db-wizard-params').val($('#db-query-form').serialize()); | |
| 300 | + $('#vz-db-wizard').submit(); | |
| 301 | + }); | |
| 302 | + } | |
| 303 | + | |
| 304 | + function init_json_import(){ | |
| 305 | + var regex = new RegExp(visualizer.json_tag_separator, 'g'); | |
| 306 | + | |
| 307 | + $( '#visualizer-json-screen' ).css("z-index", "-1").hide(); | |
| 308 | + $('.visualizer-json-form').accordion({ | |
| 309 | + heightStyle: 'content', | |
| 310 | + active: 0 | |
| 311 | + }); | |
| 312 | + | |
| 313 | + // toggle between chart and create/modify parameters | |
| 314 | + $( '#json-chart-button' ).on( 'click', function(){ | |
| 315 | + $('#content').css('width', 'calc(100% - 300px)'); | |
| 316 | + if( $(this).attr( 'data-current' ) === 'chart'){ | |
| 317 | + $(this).val( $(this).attr( 'data-t-filter' ) ); | |
| 318 | + $(this).html( $(this).attr( 'data-t-filter' ) ); | |
| 319 | + $(this).attr( 'data-current', 'filter' ); | |
| 320 | + $( '.visualizer-editor-lhs' ).hide(); | |
| 321 | + $( '#visualizer-json-screen' ).css("z-index", "9999").show(); | |
| 322 | + $( '#canvas' ).hide(); | |
| 323 | + }else{ | |
| 324 | + var filter_button = $(this); | |
| 325 | + $( '#visualizer-json-screen' ).css("z-index", "-1").hide(); | |
| 326 | + $('#canvas').lock(); | |
| 327 | + filter_button.val( filter_button.attr( 'data-t-chart' ) ); | |
| 328 | + filter_button.html( filter_button.attr( 'data-t-chart' ) ); | |
| 329 | + filter_button.attr( 'data-current', 'chart' ); | |
| 330 | + $( '#canvas' ).css("z-index", "1").show(); | |
| 331 | + $('#canvas').unlock(); | |
| 332 | + } | |
| 333 | + } ); | |
| 334 | + | |
| 335 | + // fetch the roots for the provided endpoint | |
| 336 | + $( '#visualizer-json-fetch' ).on( 'click', function(e){ | |
| 337 | + e.preventDefault(); | |
| 338 | + $('.visualizer-json-form').accordion( 'option', 'active', 0 ); | |
| 339 | + $('.visualizer-json-form h3.viz-step:not(.step1)').addClass('ui-state-disabled'); | |
| 340 | + $('.json-table').html(''); | |
| 341 | + start_ajax( $( '#visualizer-json-screen' ) ); | |
| 342 | + $.ajax({ | |
| 343 | + url : ajaxurl, | |
| 344 | + method : 'post', | |
| 345 | + data : { | |
| 346 | + 'action' : visualizer.ajax['actions']['json_get_roots'], | |
| 347 | + 'security' : visualizer.ajax['nonces']['json_get_roots'], | |
| 348 | + 'params' : $('#json-endpoint-form').serialize() | |
| 349 | + }, | |
| 350 | + success : function(data){ | |
| 351 | + if(data.success){ | |
| 352 | + $('#json-root-form [name="url"]').val(data.data.url); | |
| 353 | + $('#vz-import-json-root').empty(); | |
| 354 | + $.each(data.data.roots, function(i, name){ | |
| 355 | + $('#vz-import-json-root').append('<option value="' + name + '">' + name.replace(regex, visualizer.json_tag_separator_view) + '</option>'); | |
| 356 | + }); | |
| 357 | + $('#json-root-form').fadeIn('medium'); | |
| 358 | + json_accordion_activate(1, true); | |
| 359 | + }else{ | |
| 360 | + alert(visualizer.l10n.json_error); | |
| 361 | + } | |
| 362 | + }, | |
| 363 | + complete: function(){ | |
| 364 | + end_ajax($('#visualizer-json-screen')); | |
| 365 | + } | |
| 366 | + }); | |
| 367 | + }); | |
| 368 | + | |
| 369 | + // fetch the data for the chosen root | |
| 370 | + $( '#visualizer-json-parse' ).on( 'click', function(e){ | |
| 371 | + e.preventDefault(); | |
| 372 | + $('.visualizer-json-form h3.viz-step:not(.step1):not(.step2)').addClass('ui-state-disabled'); | |
| 373 | + $('.json-table').html(''); | |
| 374 | + start_ajax( $( '#visualizer-json-screen' ) ); | |
| 375 | + $.ajax({ | |
| 376 | + url : ajaxurl, | |
| 377 | + method : 'post', | |
| 378 | + data : { | |
| 379 | + 'action' : visualizer.ajax['actions']['json_get_data'], | |
| 380 | + 'security' : visualizer.ajax['nonces']['json_get_data'], | |
| 381 | + 'params' : $('#json-root-form').serialize() | |
| 382 | + }, | |
| 383 | + success : function(data){ | |
| 384 | + if(data.success){ | |
| 385 | + $('#vz-import-json-paging option:not(.static)').remove(); | |
| 386 | + if(data.data.paging.length > 0){ | |
| 387 | + var $template = $('#vz-import-json-paging').attr('data-template'); | |
| 388 | + $.each(data.data.paging, function(i, name){ | |
| 389 | + var display = name.replace(regex, visualizer.json_tag_separator_view); | |
| 390 | + display = $template.replace('?', display); | |
| 391 | + $('#vz-import-json-paging').append('<option value="' + name + '">' + display + '</option>'); | |
| 392 | + }); | |
| 393 | + $('.json-pagination').show(); | |
| 394 | + } | |
| 395 | + $('#json-conclude-form [name="url"]').val(data.data.url); | |
| 396 | + $('#json-conclude-form [name="root"]').val(data.data.root); | |
| 397 | + $('#json-conclude-form .json-table').html(data.data.table); | |
| 398 | + | |
| 399 | + var $table = create_editor_table( '#json-conclude-form' ); | |
| 400 | + | |
| 401 | + json_accordion_activate(3, true); | |
| 402 | + json_accordion_activate(2, false); | |
| 403 | + $table.columns.adjust().draw(); | |
| 404 | + }else{ | |
| 405 | + alert(visualizer.l10n.json_error); | |
| 406 | + } | |
| 407 | + }, | |
| 408 | + complete: function(){ | |
| 409 | + end_ajax($('#visualizer-json-screen')); | |
| 410 | + } | |
| 411 | + }); | |
| 412 | + }); | |
| 413 | + | |
| 414 | + // when the data is set and the chart is updated, toggle the screen so that the chart is shown | |
| 415 | + $('#json-conclude-form').on( 'submit', function(e){ | |
| 416 | + // populate the form elements that are in the misc tab. | |
| 417 | + $('#json-conclude-form-helper .json-form-element').each(function(x, y){ | |
| 418 | + $('#json-conclude-form').append('<input type="hidden" name="' + y.name + '" value="' + y.value + '">'); | |
| 419 | + }); | |
| 420 | + $( '#json-chart-button' ).trigger('click'); | |
| 421 | + $('#canvas').lock(); | |
| 422 | + }); | |
| 423 | + | |
| 424 | + // update the schedule | |
| 425 | + $('#json-chart-save-button').on('click', function(e){ | |
| 426 | + e.preventDefault(); | |
| 427 | + $('#canvas').lock(); | |
| 428 | + $.ajax({ | |
| 429 | + url : ajaxurl, | |
| 430 | + method : 'post', | |
| 431 | + data : { | |
| 432 | + 'action' : visualizer.ajax['actions']['json_set_schedule'], | |
| 433 | + 'security' : visualizer.ajax['nonces']['json_set_schedule'], | |
| 434 | + 'chart' : $('#vz-json-time').attr('data-chart'), | |
| 435 | + 'time' : $('#vz-json-time').val() | |
| 436 | + }, | |
| 437 | + success : function(data){ | |
| 438 | + // do nothing. | |
| 439 | + }, | |
| 440 | + complete: function(){ | |
| 441 | + $('#canvas').unlock(); | |
| 442 | + } | |
| 443 | + }); | |
| 444 | + }); | |
| 445 | + | |
| 446 | + } | |
| 447 | + | |
| 448 | + function init_editor_table() { | |
| 449 | + $('body').on('visualizer:db:editor:table:init', function(event, data){ | |
| 450 | + var $table = create_editor_table('.viz-table-editor'); | |
| 451 | + $('body').on('visualizer:db:editor:table:redraw', function(event, data){ | |
| 452 | + $table.draw(); | |
| 453 | + }); | |
| 454 | + }); | |
| 455 | + } | |
| 456 | + | |
| 457 | + function create_editor_table(element) { | |
| 458 | + var settings = { | |
| 459 | + paging: false, | |
| 460 | + searching: false, | |
| 461 | + ordering: false, | |
| 462 | + select: false, | |
| 463 | + scrollX: "100%", | |
| 464 | + scrollY: "400px", | |
| 465 | + info: false, | |
| 466 | + colReorder: { | |
| 467 | + fixedColumnsLeft: 1 | |
| 468 | + } | |
| 469 | + }; | |
| 470 | + | |
| 471 | + // show column visibility button only when more than 6 columns are found (including the Label column) | |
| 472 | + if($(element + ' table.viz-editor-table thead tr th').length > 6){ | |
| 473 | + $.extend( settings, { | |
| 474 | + dom: 'Bt', | |
| 475 | + buttons: [ | |
| 476 | + { | |
| 477 | + extend: 'colvis', | |
| 478 | + columns: ':gt(0)', | |
| 479 | + collectionLayout: 'four-column' | |
| 480 | + } | |
| 481 | + ] | |
| 482 | + } ); | |
| 483 | + } | |
| 484 | + | |
| 485 | + $.extend( $.fn.dataTable.defaults, settings ); | |
| 486 | + var $table = $(element + ' .viz-editor-table').DataTable(); | |
| 487 | + return $table; | |
| 488 | + } | |
| 489 | + | |
| 490 | + function json_accordion_activate($step, $activate){ | |
| 491 | + $('.visualizer-json-form h3.viz-step.step' + ( $step + 1 )).removeClass('ui-state-disabled'); | |
| 492 | + if($activate){ | |
| 493 | + $('.visualizer-json-form').accordion( 'option', 'active', $step ); | |
| 494 | + } | |
| 495 | + } | |
| 496 | + | |
| 497 | + function start_ajax(element){ | |
| 498 | + element.lock(); | |
| 499 | + } | |
| 500 | + | |
| 501 | + function end_ajax(element){ | |
| 502 | + element.unlock(); | |
| 503 | + } | |
| 504 | + | |
| 152 | 505 | })(jQuery); |
| 153 | 506 | |
| 154 | 507 | (function ($) { |
| 155 | 508 | $.fn.lock = function () { |