| 1 |
/* global prompt */ |
| 2 |
/* global visualizer */ |
| 3 |
/* global alert */ |
| 4 |
/* global ajaxurl */ |
| 5 |
/* global CodeMirror */ |
| 6 |
|
| 7 |
(function ($) { |
| 8 |
$(window).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 |
// open the correct source tab. |
| 17 |
var source = $('#visualizer-chart-id').attr('data-chart-source'); |
| 18 |
$('li.viz-group.' + source).addClass('open'); |
| 19 |
|
| 20 |
init_permissions(); |
| 21 |
|
| 22 |
if(typeof visualizer !== 'undefined' && visualizer.is_pro) { |
| 23 |
init_db_import(); |
| 24 |
} |
| 25 |
|
| 26 |
// update the manual configuation link to point to the correct chart type. |
| 27 |
var type = $('#visualizer-chart-id').attr('data-chart-type'); |
| 28 |
var chart_type_in_api_link = type + 'chart'; |
| 29 |
switch (type) { |
| 30 |
case "gauge": |
| 31 |
case "table": |
| 32 |
case "timeline": |
| 33 |
chart_type_in_api_link = type; |
| 34 |
break; |
| 35 |
} |
| 36 |
|
| 37 |
if($('span.viz-gvlink').length > 0) { |
| 38 |
$('span.viz-gvlink').html($('span.viz-gvlink').html().replace('?', chart_type_in_api_link)); |
| 39 |
} |
| 40 |
|
| 41 |
$('.type-radio').change(function () { |
| 42 |
$('.type-label-selected').removeClass('type-label-selected'); |
| 43 |
$(this).parent().addClass('type-label-selected'); |
| 44 |
}); |
| 45 |
|
| 46 |
$('#vz-chart-settings h2').click(function () { |
| 47 |
$("#vz-chart-source").hide(); |
| 48 |
$("#vz-chart-permissions").removeClass('open').addClass('bottom-fixed'); |
| 49 |
$(this).parent().removeClass('bottom-fixed').addClass('open'); |
| 50 |
$("#vz-chart-permissions .viz-group-header").hide(); |
| 51 |
return false; |
| 52 |
}); |
| 53 |
$('#vz-chart-settings .customize-section-back').click(function () { |
| 54 |
$("#vz-chart-source").show(); |
| 55 |
$(this).parent().parent().removeClass('open').addClass('bottom-fixed'); |
| 56 |
|
| 57 |
return false; |
| 58 |
}); |
| 59 |
$('.viz-group-title').click(function () { |
| 60 |
var parent = $(this).parent(); |
| 61 |
|
| 62 |
if (parent.hasClass('open')) { |
| 63 |
parent.removeClass('open'); |
| 64 |
} else { |
| 65 |
parent.parent().find('.viz-group.open').removeClass('open'); |
| 66 |
parent.addClass('open'); |
| 67 |
} |
| 68 |
}); |
| 69 |
$('#view-remote-file').click(function () { |
| 70 |
var url = $(this).parent().find('#remote-data').val(); |
| 71 |
|
| 72 |
if (url !== '') { |
| 73 |
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)) { |
| 74 |
if (url.substr(url.length - 8) === '/pubhtml') { |
| 75 |
url = url.substring(0, url.length - 8) + '/export?format=csv'; |
| 76 |
} |
| 77 |
|
| 78 |
$('#canvas').lock(); |
| 79 |
$(this).parent().submit(); |
| 80 |
} else { |
| 81 |
alert(visualizer.l10n.invalid_source); |
| 82 |
} |
| 83 |
} |
| 84 |
}); |
| 85 |
|
| 86 |
$('#vz-import-file').click(function (e) { |
| 87 |
e.preventDefault(); |
| 88 |
if ($.trim($(this).parent().find("#csv-file").val()) !== '') { |
| 89 |
$('#canvas').lock(); |
| 90 |
$(this).parent().submit(); |
| 91 |
} |
| 92 |
}); |
| 93 |
|
| 94 |
$('#thehole').load(function () { |
| 95 |
$('#canvas').unlock(); |
| 96 |
}); |
| 97 |
|
| 98 |
$('.viz-section-title').click(function () { |
| 99 |
$(this).toggleClass('open').parent().find('.viz-section-items').toggle(); |
| 100 |
}); |
| 101 |
|
| 102 |
$('.more-info').click(function () { |
| 103 |
$(this).parent().find('.viz-section-description:first').toggle(); |
| 104 |
return false; |
| 105 |
}); |
| 106 |
|
| 107 |
$('#cancel-button').click(function () { |
| 108 |
$('#cancel-form').submit(); |
| 109 |
}); |
| 110 |
|
| 111 |
}); |
| 112 |
|
| 113 |
function init_permissions(){ |
| 114 |
$('#vz-chart-permissions h2').click(function () { |
| 115 |
$("#vz-chart-source").hide(); |
| 116 |
$("#vz-chart-permissions .viz-group-header").show(); |
| 117 |
$("#vz-chart-settings").removeClass('open').addClass('bottom-fixed'); |
| 118 |
|
| 119 |
$('#settings-button').click(function(e) { |
| 120 |
e.preventDefault(); |
| 121 |
$('#permissions-form').submit(); |
| 122 |
$('#settings-form').submit(); |
| 123 |
}); |
| 124 |
|
| 125 |
$(this).parent().removeClass('bottom-fixed').addClass('open'); |
| 126 |
|
| 127 |
return false; |
| 128 |
}); |
| 129 |
$('#vz-chart-permissions .customize-section-back').click(function () { |
| 130 |
$("#vz-chart-source").show(); |
| 131 |
|
| 132 |
$("#vz-chart-permissions .viz-group-header").hide(); |
| 133 |
$('#settings-button').click(function(e) { |
| 134 |
e.preventDefault(); |
| 135 |
$('#settings-form').submit(); |
| 136 |
}); |
| 137 |
|
| 138 |
$(this).parent().parent().removeClass('open').addClass('bottom-fixed'); |
| 139 |
|
| 140 |
return false; |
| 141 |
}); |
| 142 |
|
| 143 |
$('.visualizer-permission').chosen({ |
| 144 |
width : '50%', |
| 145 |
search_contains : true |
| 146 |
}); |
| 147 |
|
| 148 |
$('.visualizer-permission-type').each(function(x, y){ |
| 149 |
var type = $(y).attr('data-visualizer-permission-type'); |
| 150 |
var child = $('.visualizer-permission-' + type + '-specific'); |
| 151 |
if($(y).val() === 'all'){ |
| 152 |
child.next('div.chosen-container').hide(); |
| 153 |
return; |
| 154 |
} |
| 155 |
}); |
| 156 |
|
| 157 |
$('.visualizer-permission-type').on('change', function(evt, params) { |
| 158 |
var type = $(this).attr('data-visualizer-permission-type'); |
| 159 |
var child = $('.visualizer-permission-' + type + '-specific'); |
| 160 |
child.empty(); |
| 161 |
if(params.selected === 'all'){ |
| 162 |
child.next('div.chosen-container').hide(); |
| 163 |
return; |
| 164 |
} else { |
| 165 |
child.next('div.chosen-container').show(); |
| 166 |
} |
| 167 |
child.append('<option value="">' + visualizer.l10n['loading'] + '</option>').trigger('chosen:updated'); |
| 168 |
$.ajax({ |
| 169 |
url : visualizer.ajax['url'], |
| 170 |
method : 'post', |
| 171 |
data : { |
| 172 |
'action' : visualizer.ajax['actions']['permissions'], |
| 173 |
'nonce' : visualizer.ajax['nonces']['permissions'], |
| 174 |
'type' : params.selected |
| 175 |
}, |
| 176 |
success : function(d, textStatus, XMLHttpRequest){ |
| 177 |
if(d.success) { |
| 178 |
child.empty(); |
| 179 |
$.each(d.data, function(k, v){ |
| 180 |
child.append('<option value="' + k + '">' + v + '</option>'); |
| 181 |
}); |
| 182 |
child.trigger('chosen:updated'); |
| 183 |
} |
| 184 |
} |
| 185 |
}); |
| 186 |
}); |
| 187 |
} |
| 188 |
|
| 189 |
// https://codemirror.net/ |
| 190 |
function init_db_import_component(){ |
| 191 |
var table_columns = visualizer.db_query.tables; |
| 192 |
var code_mirror = wp.CodeMirror || CodeMirror; |
| 193 |
var cm = code_mirror.fromTextArea($('.visualizer-db-query').get(0), { |
| 194 |
value: $('.visualizer-db-query').val(), |
| 195 |
autofocus: true, |
| 196 |
mode: 'text/x-mysql', |
| 197 |
lineWrapping: true, |
| 198 |
dragDrop: false, |
| 199 |
matchBrackets: true, |
| 200 |
autoCloseBrackets: true, |
| 201 |
extraKeys: {"Ctrl-Space": "autocomplete"}, |
| 202 |
hintOptions: { tables: table_columns } |
| 203 |
}); |
| 204 |
|
| 205 |
// force refresh so that the query shows on first time load. Otherwise you have to click on the editor for it to show. |
| 206 |
$('body').on('visualizer:db:query:focus', function(event, data){ |
| 207 |
cm.refresh(); |
| 208 |
}); |
| 209 |
|
| 210 |
cm.focus(); |
| 211 |
|
| 212 |
// update text area. |
| 213 |
cm.on('inputRead', function(x, y){ |
| 214 |
cm.save(); |
| 215 |
}); |
| 216 |
|
| 217 |
// backspace and delete do not register so the text box does not get empty if the entire query is deleted |
| 218 |
// from the editor. Let's force this. |
| 219 |
$('body').on('visualizer:db:query:update', function(event, data){ |
| 220 |
cm.save(); |
| 221 |
}); |
| 222 |
} |
| 223 |
|
| 224 |
function init_db_import(){ |
| 225 |
$( '#visualizer-db-query' ).css("z-index", "-1").hide(); |
| 226 |
|
| 227 |
init_db_import_component(); |
| 228 |
|
| 229 |
$('#visualizer-query-fetch').on('click', function(e){ |
| 230 |
|
| 231 |
$('body').trigger('visualizer:db:query:update', {}); |
| 232 |
if($('.visualizer-db-query').val() === ''){ |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
start_ajax($('#visualizer-db-query')); |
| 237 |
$('.db-wizard-results').empty(); |
| 238 |
$('.db-wizard-error').empty(); |
| 239 |
$.ajax({ |
| 240 |
url : ajaxurl, |
| 241 |
method : 'post', |
| 242 |
data : { |
| 243 |
'action' : visualizer.ajax['actions']['db_get_data'], |
| 244 |
'security' : visualizer.ajax['nonces']['db_get_data'], |
| 245 |
'params' : $('#db-query-form').serialize() |
| 246 |
}, |
| 247 |
success : function(data){ |
| 248 |
if(data.success){ |
| 249 |
$('.db-wizard-results').html(data.data.table); |
| 250 |
$('#results').DataTable({ |
| 251 |
"paging": false |
| 252 |
}); |
| 253 |
}else{ |
| 254 |
$('.db-wizard-error').html(data.data.msg); |
| 255 |
} |
| 256 |
}, |
| 257 |
complete: function(){ |
| 258 |
end_ajax($('#visualizer-db-query')); |
| 259 |
} |
| 260 |
}); |
| 261 |
}); |
| 262 |
|
| 263 |
$( '#db-chart-button' ).on( 'click', function(){ |
| 264 |
$('#content').css('width', 'calc(100% - 300px)'); |
| 265 |
if( $(this).attr( 'data-current' ) === 'chart'){ |
| 266 |
$(this).val( $(this).attr( 'data-t-filter' ) ); |
| 267 |
$(this).html( $(this).attr( 'data-t-filter' ) ); |
| 268 |
$(this).attr( 'data-current', 'filter' ); |
| 269 |
$( '.visualizer-editor-lhs' ).hide(); |
| 270 |
$( '#visualizer-db-query' ).css("z-index", "9999").show(); |
| 271 |
$('body').trigger('visualizer:db:query:focus', {}); |
| 272 |
$( '#canvas' ).hide(); |
| 273 |
}else{ |
| 274 |
var filter_button = $(this); |
| 275 |
$( '#visualizer-db-query' ).css("z-index", "-1").hide(); |
| 276 |
$('#canvas').lock(); |
| 277 |
filter_button.val( filter_button.attr( 'data-t-chart' ) ); |
| 278 |
filter_button.html( filter_button.attr( 'data-t-chart' ) ); |
| 279 |
filter_button.attr( 'data-current', 'chart' ); |
| 280 |
$( '#canvas' ).css("z-index", "1").show(); |
| 281 |
$( '#db-chart-save-button' ).trigger('click'); |
| 282 |
} |
| 283 |
} ); |
| 284 |
|
| 285 |
$( '#db-chart-save-button' ).on( 'click', function(){ |
| 286 |
$('#viz-db-wizard-params').val($('#db-query-form').serialize()); |
| 287 |
$('#vz-db-wizard').submit(); |
| 288 |
}); |
| 289 |
} |
| 290 |
|
| 291 |
function start_ajax(element){ |
| 292 |
element.lock(); |
| 293 |
} |
| 294 |
|
| 295 |
function end_ajax(element){ |
| 296 |
element.unlock(); |
| 297 |
} |
| 298 |
|
| 299 |
})(jQuery); |
| 300 |
|
| 301 |
(function ($) { |
| 302 |
$.fn.lock = function () { |
| 303 |
$(this).each(function () { |
| 304 |
var $this = $(this); |
| 305 |
var position = $this.css('position'); |
| 306 |
|
| 307 |
if (!position) { |
| 308 |
position = 'static'; |
| 309 |
} |
| 310 |
|
| 311 |
switch (position) { |
| 312 |
case 'absolute': |
| 313 |
case 'relative': |
| 314 |
break; |
| 315 |
default: |
| 316 |
$this.css('position', 'relative'); |
| 317 |
break; |
| 318 |
} |
| 319 |
$this.data('position', position); |
| 320 |
|
| 321 |
var width = $this.width(), |
| 322 |
height = $this.height(); |
| 323 |
|
| 324 |
var locker = $('<div class="locker"></div>'); |
| 325 |
locker.width(width).height(height); |
| 326 |
|
| 327 |
var loader = $('<div class="locker-loader"></div>'); |
| 328 |
loader.width(width).height(height); |
| 329 |
|
| 330 |
locker.append(loader); |
| 331 |
$this.append(locker); |
| 332 |
$(window).resize(function () { |
| 333 |
$this.find('.locker,.locker-loader').width($this.width()).height($this.height()); |
| 334 |
}); |
| 335 |
}); |
| 336 |
|
| 337 |
return $(this); |
| 338 |
}; |
| 339 |
|
| 340 |
$.fn.unlock = function () { |
| 341 |
$(this).each(function () { |
| 342 |
$(this).find('.locker').remove(); |
| 343 |
$(this).css('position', $(this).data('position')); |
| 344 |
}); |
| 345 |
|
| 346 |
return $(this); |
| 347 |
}; |
| 348 |
})(jQuery); |