| 1 |
(function($) { |
| 2 |
'use strict'; |
| 3 |
function AysChartBuilder(element, options){ |
| 4 |
this.el = element; |
| 5 |
this.$el = $(element); |
| 6 |
this.ajaxAction = 'ays_chart_admin_ajax'; |
| 7 |
this.htmlClassPrefix = 'ays-chart-'; |
| 8 |
this.htmlNamePrefix = 'ays_'; |
| 9 |
this.dbOptions = undefined; |
| 10 |
this.chartSourceData = undefined; |
| 11 |
this.chartObj = undefined; |
| 12 |
this.chartOptions = null; |
| 13 |
this.chartData = null; |
| 14 |
this.chartTempData = null; |
| 15 |
this.chartType = 'pie_chart'; |
| 16 |
|
| 17 |
this.init(); |
| 18 |
|
| 19 |
return this; |
| 20 |
} |
| 21 |
|
| 22 |
AysChartBuilder.prototype.init = function() { |
| 23 |
var _this = this; |
| 24 |
_this.chartSourceData = window.ChartBuilderSourceData; |
| 25 |
_this.setEvents(); |
| 26 |
_this.initLibraries(); |
| 27 |
|
| 28 |
_this.initDbImport(); |
| 29 |
_this.initQuizDBImport(); |
| 30 |
}; |
| 31 |
|
| 32 |
// Set events |
| 33 |
AysChartBuilder.prototype.setEvents = function(e){ |
| 34 |
var _this = this; |
| 35 |
|
| 36 |
// Quiz maker integration |
| 37 |
$(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-data-query').on('change', function () { |
| 38 |
var option = $(this).find('option:selected'); |
| 39 |
if (option.attr('is-pro') == "true") { |
| 40 |
$(this).find('option[value=""]').prop('selected', true); |
| 41 |
window.open('https://ays-pro.com/wordpress/chart-builder/', '_blank'); |
| 42 |
} else { |
| 43 |
_this.$el.find('.ays-chart-quiz-query-tooltip').attr('title', _this.chartSourceData.quiz_query_tooltips[this.value]); |
| 44 |
_this.$el.find('.ays-chart-quiz-query-tooltip').attr('data-bs-original-title', _this.chartSourceData.quiz_query_tooltips[this.value]); |
| 45 |
_this.quizMakerIntegrationConfig(); |
| 46 |
_this.toggleQuizSelect(this); |
| 47 |
} |
| 48 |
}); |
| 49 |
|
| 50 |
$(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-quiz').on('change', function () { |
| 51 |
_this.quizMakerIntegrationConfig(); |
| 52 |
}); |
| 53 |
|
| 54 |
$(window).on('load', function () { |
| 55 |
_this.toggleQuizSelect(); |
| 56 |
}); |
| 57 |
} |
| 58 |
|
| 59 |
AysChartBuilder.prototype.toggleQuizSelect = function (e) { |
| 60 |
var _this = this; |
| 61 |
|
| 62 |
var select = $(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-data-query'); |
| 63 |
var option = $(select).find('option:selected').val(); |
| 64 |
var selectQuizSelect = $(document).find('.'+_this.htmlClassPrefix+'select-quiz-maker-quiz-container'); |
| 65 |
|
| 66 |
if (option == 'q1' || option == 'q3' || option == 'q6') { |
| 67 |
selectQuizSelect.slideDown(); |
| 68 |
} else { |
| 69 |
selectQuizSelect.slideUp(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
AysChartBuilder.prototype.quizMakerIntegrationConfig = function (e) { |
| 74 |
var _this = this; |
| 75 |
|
| 76 |
var input = $(document).find('input[name="ays_source_type"]'); |
| 77 |
if (input.val() !== 'quiz_maker') input.val('quiz_maker'); |
| 78 |
|
| 79 |
_this.$el.find('#ays-chart-quiz-maker-success').empty(); |
| 80 |
_this.$el.find('#ays-chart-quiz-maker-error').empty(); |
| 81 |
} |
| 82 |
|
| 83 |
AysChartBuilder.prototype.detectManualChange = function (e) { |
| 84 |
var input = $(document).find('input[name="ays_source_type"]'); |
| 85 |
if (input.val() !== 'manual') input.val('manual'); |
| 86 |
} |
| 87 |
|
| 88 |
AysChartBuilder.prototype.initLibraries = function (){ |
| 89 |
var _this = this; |
| 90 |
|
| 91 |
var googleSelect = _this.$el.find('.' + _this.htmlClassPrefix + 'google-sheet-select'); |
| 92 |
googleSelect.select2(); |
| 93 |
var fileTypeSelect = _this.$el.find('#' + _this.htmlClassPrefix + 'import-files-file-type'); |
| 94 |
fileTypeSelect.select2(); |
| 95 |
var quizQuerySelect = _this.$el.find('#' + _this.htmlClassPrefix + 'select-quiz-maker-data-query'); |
| 96 |
quizQuerySelect.select2(); |
| 97 |
var quizQuizSelect = _this.$el.find('#' + _this.htmlClassPrefix + 'select-quiz-maker-quiz'); |
| 98 |
quizQuizSelect.select2(); |
| 99 |
var woocommerceSelect = _this.$el.find('#' + _this.htmlClassPrefix + 'woocommerce-datas-select'); |
| 100 |
woocommerceSelect.select2(); |
| 101 |
|
| 102 |
_this.$el.find('#ays-chart-option-create-author').select2({ |
| 103 |
placeholder: aysChartBuilderAdmin.selectUser, |
| 104 |
minimumInputLength: 1, |
| 105 |
allowClear: true, |
| 106 |
language: { |
| 107 |
searching: function() { |
| 108 |
return aysChartBuilderAdmin.searching; |
| 109 |
}, |
| 110 |
inputTooShort: function () { |
| 111 |
return aysChartBuilderAdmin.pleaseEnterMore; |
| 112 |
} |
| 113 |
}, |
| 114 |
ajax: { |
| 115 |
url: ajaxurl, |
| 116 |
dataType: 'json', |
| 117 |
data: function (response) { |
| 118 |
var checkedUsers = _this.$el.find('#ays-chart-option-create-author').val(); |
| 119 |
return { |
| 120 |
action: _this.ajaxAction, |
| 121 |
function: window.aysChartBuilderChartSettings.ajax['actions']['author_user_search'], |
| 122 |
security: window.aysChartBuilderChartSettings.ajax['nonces']['author_user_search'], |
| 123 |
params: { |
| 124 |
search: response.term, |
| 125 |
val: checkedUsers |
| 126 |
} |
| 127 |
}; |
| 128 |
} |
| 129 |
} |
| 130 |
}); |
| 131 |
|
| 132 |
_this.$el.find('.' + _this.htmlClassPrefix + 'chart-source-data-content').sortable({ |
| 133 |
items: "> div." + _this.htmlClassPrefix + "chart-source-data-edit-block:not(:first)", |
| 134 |
handle: "." + _this.htmlClassPrefix + "chart-source-data-move-row", |
| 135 |
update: function (event, ui) { |
| 136 |
_this.$el.find('div.' + _this.htmlClassPrefix + 'chart-source-data-edit-block:not(:first)').each(function (index) { |
| 137 |
$(this).attr('data-source-id', index + 1); |
| 138 |
$(this).find("input.ays-text-input").attr('name', 'ays_chart_source_data[' + (index + 1) + '][]') |
| 139 |
}); |
| 140 |
} |
| 141 |
}); |
| 142 |
} |
| 143 |
|
| 144 |
AysChartBuilder.prototype.initDbImportComponent = function(){ |
| 145 |
var _this = this; |
| 146 |
var table_columns = window.aysChartBuilderChartSettings.db_query.tables; |
| 147 |
var code_mirror = wp.CodeMirror || CodeMirror; |
| 148 |
var cm = code_mirror.fromTextArea(_this.$el.find('.ays-chart-db-query').get(0), { |
| 149 |
value: _this.$el.find('.ays-chart-db-query').val(), |
| 150 |
autofocus: false, |
| 151 |
mode: 'text/x-mysql', |
| 152 |
lineWrapping: true, |
| 153 |
dragDrop: false, |
| 154 |
matchBrackets: true, |
| 155 |
autoCloseBrackets: true, |
| 156 |
extraKeys: {"Ctrl-Space": "autocomplete"}, |
| 157 |
hintOptions: { tables: table_columns } |
| 158 |
}); |
| 159 |
|
| 160 |
// force refresh so that the query shows on first time load. Otherwise you have to click on the editor for it to show. |
| 161 |
_this.$el.on('ays-chart:db:query:focus', function(event, data){ |
| 162 |
cm.refresh(); |
| 163 |
}); |
| 164 |
|
| 165 |
// cm.focus(); |
| 166 |
|
| 167 |
// update text area. |
| 168 |
cm.on('inputRead', function(x, y){ |
| 169 |
cm.save(); |
| 170 |
}); |
| 171 |
|
| 172 |
// backspace and delete do not register so the text box does not get empty if the entire query is deleted |
| 173 |
// from the editor. Let's force this. |
| 174 |
_this.$el.on('ays-chart:db:query:update', function(event, data){ |
| 175 |
cm.save(); |
| 176 |
}); |
| 177 |
|
| 178 |
// clear the editor. |
| 179 |
_this.$el.on('ays-chart:db:query:setvalue', function(event, data){ |
| 180 |
cm.setValue(data.value); |
| 181 |
cm.clearHistory(); |
| 182 |
cm.refresh(); |
| 183 |
}); |
| 184 |
|
| 185 |
// set an option at runtime? |
| 186 |
_this.$el.on('ays-chart:db:query:changeoption', function(event, data){ |
| 187 |
cm.setOption(data.name, data.value); |
| 188 |
}); |
| 189 |
} |
| 190 |
|
| 191 |
AysChartBuilder.prototype.initDbImport = function(){ |
| 192 |
var _this = this; |
| 193 |
_this.initDbImportComponent(); |
| 194 |
} |
| 195 |
|
| 196 |
AysChartBuilder.prototype.initQuizDBImport = function(){ |
| 197 |
var _this = this; |
| 198 |
|
| 199 |
_this.$el.find('#ays-chart-quiz-maker-fetch').on('click', function(e){ |
| 200 |
e.preventDefault(); |
| 201 |
|
| 202 |
var select_query = $(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-data-query').val(); |
| 203 |
if (select_query == "") { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
_this.fetchQuizData( true ); |
| 208 |
}); |
| 209 |
|
| 210 |
_this.$el.find('#ays-chart-quiz-maker-show-on-chart').on( 'click', function(e){ |
| 211 |
e.preventDefault(); |
| 212 |
|
| 213 |
var select_query = $(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-data-query').val(); |
| 214 |
if (select_query == "") { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
if( _this.chartTempData ){ |
| 219 |
_this.$el.find('#ays-chart-quiz-maker-success').empty(); |
| 220 |
_this.$el.find('#ays-chart-quiz-maker-error').empty(); |
| 221 |
if (_this.chartType == 'line_chart' || _this.chartType == 'bar_chart' || _this.chartType == 'column_chart') { |
| 222 |
_this.chartTempData = _this.multiColumnChartConvertData( _this.chartTempData ); |
| 223 |
} else if (_this.chartType == 'org_chart') { |
| 224 |
_this.chartTempData = _this.orgChartConvertData( _this.chartTempData ); |
| 225 |
} else { |
| 226 |
_this.chartTempData = _this.chartConvertData( _this.chartTempData ); |
| 227 |
} |
| 228 |
_this.chartData = window.google.visualization.arrayToDataTable( _this.chartTempData ); |
| 229 |
|
| 230 |
_this.updateChartData( _this.chartData ); |
| 231 |
|
| 232 |
_this.chartTempData = null; |
| 233 |
}else{ |
| 234 |
_this.fetchQuizData( false, true ); |
| 235 |
} |
| 236 |
|
| 237 |
}); |
| 238 |
|
| 239 |
_this.$el.find('#ays-chart-quiz-maker-save').on( 'click', function(e){ |
| 240 |
e.preventDefault(); |
| 241 |
|
| 242 |
var select_query = $(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-data-query').val(); |
| 243 |
if (select_query == "") { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
_this.$el.find('#ays-chart-quiz-maker-success').empty(); |
| 248 |
_this.$el.find('#ays-chart-quiz-maker-error').empty(); |
| 249 |
|
| 250 |
var chart_id = $(document).find('.' + _this.htmlClassPrefix + 'chart-id').val(); |
| 251 |
var query = $(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-data-query').find('option:selected').val(); |
| 252 |
var selectQuizSelectDisplay = $(document).find('.'+_this.htmlClassPrefix+'select-quiz-maker-quiz-container').css('display'); |
| 253 |
var quizID = null; |
| 254 |
if (selectQuizSelectDisplay != 'none') { |
| 255 |
quizID = $(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-quiz').find('option:selected').val(); |
| 256 |
if (quizID == 0) { |
| 257 |
_this.$el.find('#ays-chart-quiz-maker-error').html( 'Please select a quiz.' ); |
| 258 |
return; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
// submit only if a query has been provided. |
| 263 |
if( query.length > 0 ){ |
| 264 |
_this.startAjax( _this.$el.find('#ays-chart-quiz-maker-form') ); |
| 265 |
|
| 266 |
$.ajax({ |
| 267 |
url: ajaxurl, |
| 268 |
method: 'post', |
| 269 |
dataType: 'json', |
| 270 |
data: { |
| 271 |
'action': _this.ajaxAction, |
| 272 |
'function': window.aysChartBuilderChartSettings.ajax['actions']['quiz_maker_save_data'], |
| 273 |
'security': window.aysChartBuilderChartSettings.ajax['nonces']['quiz_maker_save_data'], |
| 274 |
'params': { |
| 275 |
query: query, |
| 276 |
quizID: quizID, |
| 277 |
chart_id: chart_id, |
| 278 |
} |
| 279 |
}, |
| 280 |
success: function( response ){ |
| 281 |
if( response.success ){ |
| 282 |
_this.$el.find('#ays-chart-quiz-maker-success').html( response.data.msg ); |
| 283 |
_this.$el.find('input[name="' + _this.htmlNamePrefix + 'source_type"]').val('quiz_maker'); |
| 284 |
}else{ |
| 285 |
_this.$el.find('#ays-chart-quiz-maker-error').html( response.data.msg ); |
| 286 |
_this.$el.find('input[name="' + _this.htmlNamePrefix + 'source_type"]').val('manual'); |
| 287 |
} |
| 288 |
}, |
| 289 |
complete: function(){ |
| 290 |
_this.endAjax( _this.$el.find('#ays-chart-quiz-maker-form') ); |
| 291 |
} |
| 292 |
}); |
| 293 |
}else{ |
| 294 |
_this.$el.find('#ays-chart-quiz-maker-form').unlock(); |
| 295 |
_this.$el.find('input[name="' + _this.htmlNamePrefix + 'source_type"]').val('manual'); |
| 296 |
} |
| 297 |
}); |
| 298 |
|
| 299 |
$(document).on('ays-chart:modal:close', function (){ |
| 300 |
_this.chartTempData = null; |
| 301 |
}); |
| 302 |
} |
| 303 |
|
| 304 |
AysChartBuilder.prototype.fetchQuizData = function( openModal = false, showOnChart = false ){ |
| 305 |
var _this = this; |
| 306 |
_this.$el.trigger('ays-chart:db:query:update', {}); |
| 307 |
|
| 308 |
var chart_id = $(document).find('.' + _this.htmlClassPrefix + 'chart-id').val(); |
| 309 |
var query = $(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-data-query').find('option:selected').val(); |
| 310 |
var selectQuizSelectDisplay = $(document).find('.'+_this.htmlClassPrefix+'select-quiz-maker-quiz-container').css('display'); |
| 311 |
var quizID = null; |
| 312 |
if (selectQuizSelectDisplay != 'none') { |
| 313 |
quizID = $(document).find('#'+_this.htmlClassPrefix+'select-quiz-maker-quiz').find('option:selected').val(); |
| 314 |
if (quizID == 0) { |
| 315 |
_this.$el.find('#ays-chart-quiz-maker-error').html( 'Please select a quiz.' ); |
| 316 |
return; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
if (query.length == 0) { |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
_this.startAjax( _this.$el.find('#ays-chart-quiz-maker-form') ); |
| 325 |
|
| 326 |
var modal = $(document).find('#ays-chart-db-query-results'); |
| 327 |
|
| 328 |
_this.$el.find('.db-wizard-results').empty(); |
| 329 |
_this.$el.find('#ays-chart-quiz-maker-success').empty(); |
| 330 |
_this.$el.find('#ays-chart-quiz-maker-error').empty(); |
| 331 |
|
| 332 |
$.ajax({ |
| 333 |
url: ajaxurl, |
| 334 |
method: 'post', |
| 335 |
dataType: 'json', |
| 336 |
data: { |
| 337 |
'action': _this.ajaxAction, |
| 338 |
'function': window.aysChartBuilderChartSettings.ajax['actions']['quiz_maker_get_data'], |
| 339 |
'security': window.aysChartBuilderChartSettings.ajax['nonces']['quiz_maker_get_data'], |
| 340 |
'params': { |
| 341 |
query: query, |
| 342 |
quizID: quizID, |
| 343 |
chart_id: chart_id, |
| 344 |
} |
| 345 |
}, |
| 346 |
success: function( response ){ |
| 347 |
if( response.success ){ |
| 348 |
modal.find('.db-wizard-results').html( response.data.table ); |
| 349 |
modal.find('#results').DataTable({ |
| 350 |
"paging": true |
| 351 |
}); |
| 352 |
modal.find('#results').parent().addClass('ays-db-results-wrap'); |
| 353 |
|
| 354 |
if( openModal === true ) { |
| 355 |
modal.aysModal('show'); |
| 356 |
} |
| 357 |
|
| 358 |
_this.chartTempData = response.data.data ? response.data.data : null; |
| 359 |
|
| 360 |
if( showOnChart === true ) { |
| 361 |
if (_this.chartType == 'line_chart' || _this.chartType == 'bar_chart' || _this.chartType == 'column_chart') { |
| 362 |
_this.chartTempData = _this.multiColumnChartConvertData( _this.chartTempData ); |
| 363 |
} else if (_this.chartType == 'org_chart') { |
| 364 |
_this.chartTempData = _this.orgChartConvertData( _this.chartTempData ); |
| 365 |
} else { |
| 366 |
_this.chartTempData = _this.chartConvertData( _this.chartTempData ); |
| 367 |
} |
| 368 |
_this.chartData = window.google.visualization.arrayToDataTable(_this.chartTempData); |
| 369 |
|
| 370 |
_this.updateChartData(_this.chartData); |
| 371 |
|
| 372 |
_this.chartTempData = null; |
| 373 |
} |
| 374 |
}else{ |
| 375 |
modal.find('ays-close').trigger('click'); |
| 376 |
_this.$el.find('#ays-chart-quiz-maker-error').html( response.data.msg ); |
| 377 |
_this.$el.find('input[name="' + _this.htmlNamePrefix + 'source_type"]').val('manual'); |
| 378 |
} |
| 379 |
}, |
| 380 |
complete: function(){ |
| 381 |
_this.endAjax( _this.$el.find('#ays-chart-quiz-maker-form') ); |
| 382 |
} |
| 383 |
}); |
| 384 |
} |
| 385 |
|
| 386 |
AysChartBuilder.prototype.startAjax = function( element ){ |
| 387 |
element.lock(); |
| 388 |
} |
| 389 |
|
| 390 |
AysChartBuilder.prototype.endAjax = function( element ){ |
| 391 |
element.unlock(); |
| 392 |
} |
| 393 |
|
| 394 |
AysChartBuilder.prototype.quickSaveHotKeys = function() { |
| 395 |
$(document).on('keydown', function(e){ |
| 396 |
var saveButton = $(document).find('input#ays-button-apply'); |
| 397 |
if ( saveButton.length > 0 ) { |
| 398 |
if (!(e.which == 83 && e.ctrlKey) && !(e.which == 19)){ |
| 399 |
return true; |
| 400 |
} |
| 401 |
saveButton.trigger("click"); |
| 402 |
e.preventDefault(); |
| 403 |
return false; |
| 404 |
} |
| 405 |
}); |
| 406 |
} |
| 407 |
|
| 408 |
AysChartBuilder.prototype.htmlDecode = function (input) { |
| 409 |
if (!input) return input; |
| 410 |
|
| 411 |
var e = document.createElement('div'); |
| 412 |
e.innerHTML = input; |
| 413 |
return e.childNodes[0].nodeValue; |
| 414 |
} |
| 415 |
|
| 416 |
$.fn.AysChartBuilderMain = function(options) { |
| 417 |
return this.each(function() { |
| 418 |
if (!$.data(this, 'AysChartBuilderMain')) { |
| 419 |
$.data(this, 'AysChartBuilderMain', new AysChartBuilder(this, options)); |
| 420 |
} else { |
| 421 |
try { |
| 422 |
$(this).data('AysChartBuilderMain').init(); |
| 423 |
} catch (err) { |
| 424 |
console.error('AysChartBuilderMain has not initiated properly'); |
| 425 |
} |
| 426 |
} |
| 427 |
}); |
| 428 |
}; |
| 429 |
$(document).find('#ays-charts-form').AysChartBuilderMain(); |
| 430 |
|
| 431 |
})(jQuery); |
| 432 |
|
| 433 |
(function ($) { |
| 434 |
|
| 435 |
$.fn.serializeFormJSON = function () { |
| 436 |
let o = {}, |
| 437 |
a = this.serializeArray(); |
| 438 |
$.each(a, function () { |
| 439 |
if (o[this.name]) { |
| 440 |
if (!o[this.name].push) { |
| 441 |
o[this.name] = [o[this.name]]; |
| 442 |
} |
| 443 |
o[this.name].push(this.value || ''); |
| 444 |
} else { |
| 445 |
o[this.name] = this.value || ''; |
| 446 |
} |
| 447 |
}); |
| 448 |
return o; |
| 449 |
}; |
| 450 |
|
| 451 |
$.fn.lock = function () { |
| 452 |
$(this).each(function () { |
| 453 |
var $this = $(this); |
| 454 |
var position = $this.css('position'); |
| 455 |
|
| 456 |
if (!position) { |
| 457 |
position = 'static'; |
| 458 |
} |
| 459 |
|
| 460 |
switch (position) { |
| 461 |
case 'absolute': |
| 462 |
case 'relative': |
| 463 |
break; |
| 464 |
default: |
| 465 |
$this.css('position', 'relative'); |
| 466 |
break; |
| 467 |
} |
| 468 |
$this.data('position', position); |
| 469 |
|
| 470 |
var width = $this.width(), |
| 471 |
height = $this.height(); |
| 472 |
|
| 473 |
var locker = $('<div class="locker"></div>'); |
| 474 |
locker.width(width).height(height); |
| 475 |
|
| 476 |
var loader = $('<div class="locker-loader"></div>'); |
| 477 |
loader.width(width).height(height); |
| 478 |
|
| 479 |
locker.append(loader); |
| 480 |
$this.append(locker); |
| 481 |
$(window).resize(function () { |
| 482 |
$this.find('.locker,.locker-loader').width($this.width()).height($this.height()); |
| 483 |
}); |
| 484 |
}); |
| 485 |
|
| 486 |
return $(this); |
| 487 |
}; |
| 488 |
|
| 489 |
$.fn.unlock = function () { |
| 490 |
$(this).each(function () { |
| 491 |
$(this).find('.locker').remove(); |
| 492 |
$(this).css('position', $(this).data('position')); |
| 493 |
}); |
| 494 |
|
| 495 |
return $(this); |
| 496 |
}; |
| 497 |
})(jQuery); |