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