PluginProbe
Chartify – WordPress Chart Plugin / 3.2.7
Chartify – WordPress Chart Plugin v3.2.7
3.8.0 3.7.9 3.7.8 3.7.7 3.7.6 3.7.5 trunk 1.0.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 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.1.4 3.1.5 3.1.6 All 83 releases
chart-builder / admin / js / chart-builder-admin-chartjs.js

chart-builder-admin-chartjs.js in Chartify – WordPress Chart Plugin 3.2.7, at admin/js/chart-builder-admin-chartjs.js

968 lines 35.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function($) {
2 'use strict';
3 function ChartBuilderChartsJs(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 this.chartSourceType = 'chart-js';
17 this.chartObject = null;
18
19 this.chartSources = {
20 'pie_chart' : 'Pie Chart',
21 'bar_chart' : 'Bar Chart',
22 'line_chart' : 'Line Chart',
23 }
24
25 this.init();
26
27 return this;
28 }
29
30 ChartBuilderChartsJs.prototype.init = function() {
31 var _this = this;
32 _this.chartSourceData = window.ChartBuilderSourceData;
33 _this.setEvents();
34 _this.initLibraries();
35 _this.setAccordionEvents();
36 };
37
38 // Set events
39 ChartBuilderChartsJs.prototype.setEvents = function(e){
40 var _this = this;
41
42 _this.chartType = _this.chartSourceData.chartType;
43 _this.chartSourceType = _this.chartSourceData.chartSourceType;
44 _this.chartData = _this.chartSourceData.source;
45 _this.loadChartBySource();
46 _this.configureOptions();
47 _this.resizeChart();
48
49 /* == Tabulation == */
50 _this.$el.on('click', '.nav-tab-wrapper a.nav-tab' , _this.changeTabs);
51 /* */
52
53 /* == Notifications dismiss button == */
54 _this.$el.on('click', '.notice-dismiss', function (e) {
55 _this.changeCurrentUrl('status');
56 });
57
58 _this.$el.on('click', '.toggle_ddmenu' , _this.toggleDDmenu);
59 /* */
60
61 /* Add Manual data */
62 _this.$el.on("click" , '.'+_this.htmlClassPrefix+'add-new-row-box', function(){
63 _this.addChartDataRow();
64 });
65
66 _this.$el.on("click" , '.'+_this.htmlClassPrefix+'add-new-column-box', function(){
67 _this.addChartDataColumn();
68 });
69
70 _this.$el.on('click', '.'+_this.htmlClassPrefix+'show-on-chart-bttn', function(e){
71 e.preventDefault();
72 _this.showOnChart();
73 });
74
75 /* */
76
77 /* Delete data */
78 _this.$el.on("click" , '.'+_this.htmlClassPrefix+'chart-source-data-remove-row', function(){
79 _this.deleteChartDataRow($(this));
80 _this.detectManualChange();
81 });
82 _this.$el.on("click" , '.'+_this.htmlClassPrefix+'chart-source-data-remove-col', function(){
83 _this.deleteChartDataColumn($(this));
84 _this.detectManualChange();
85 });
86 _this.$el.on('mouseenter', '.'+_this.htmlClassPrefix+'chart-source-data-remove-block', function() {
87 $(this).find('path').css('fill', '#ff0000');
88 });
89 _this.$el.on('mouseleave', '.'+_this.htmlClassPrefix+'chart-source-data-remove-block', function() {
90 $(this).find('path').css('fill', '#b8b8b8');
91 });
92 /* */
93
94 /* Save with Ctrl + S */
95 _this.$el.on('keydown', $(document), _this.quickSaveHotKeys);
96 /* */
97
98 // Submit buttons disabling
99 _this.$el.on('click', '.'+_this.htmlClassPrefix+'loader-banner', _this.submitOnce);
100 /* */
101
102 // Disabling submit when press enter button on inputing
103 $(document).on("keypress", '.ays-text-input', _this.keyBoardConfig.bind(_this));
104 /* */
105
106 // Modal close
107 $(document).on('click', '.ays-close', function () {
108 $(this).parents('.ays-modal').aysModal('hide');
109 });
110
111 // Changing source type to manual
112 _this.$el.on('input', '.'+_this.htmlClassPrefix+'chart-source-data-content input', function () {
113 _this.detectManualChange();
114 });
115
116 // Shortcode text for editor tooltip
117 $(document).find('strong.ays-chart-shortcode-box').on('mouseleave', function(){
118 var _this = $(this);
119
120 _this.attr('data-original-title', aysChartBuilderAdmin.clickForCopy);
121 _this.attr("data-bs-original-title", aysChartBuilderAdmin.clickForCopy);
122 _this.attr("title", aysChartBuilderAdmin.clickForCopy);
123 });
124
125 _this.$el.find('.'+_this.htmlClassPrefix+'toggle-hidden-option').on('change', function () {
126 // var animationOptions = _this.$el.find('.'+_this.htmlClassPrefix+'hidden-options-section');
127 var currentSettings = $(this).parents('.' + _this.htmlClassPrefix + 'settings-data-main-wrap')
128 var hiddenSection = currentSettings.find('.' + _this.htmlClassPrefix + 'hidden-options-section');
129 var notHiddenSection = currentSettings.find('.' + _this.htmlClassPrefix + 'not-hidden-options-section');
130
131 if ($(this).is(':checked')) {
132 hiddenSection.removeClass('display_none');
133 notHiddenSection.addClass('display_none');
134 } else {
135 hiddenSection.addClass('display_none');
136 notHiddenSection.removeClass('display_none');
137 }
138 });
139
140 _this.$el.on("click", '.'+_this.htmlClassPrefix+'chart-source-data-sort', function(){
141 _this.sortDataByColumn($(this));
142 _this.detectManualChange();
143 });
144
145 // Live preview
146 _this.liveSettingsPreview();
147 }
148
149 ChartBuilderChartsJs.prototype.liveSettingsPreview = function () {
150 var _this = this;
151
152 // title
153 _this.$el.find('#ays-title').on('input', function () {
154 _this.$el.find('.'+_this.htmlClassPrefix+'charts-title').text($(this).val());
155 });
156
157 // description
158 _this.$el.find('#'+_this.htmlClassPrefix+'description').on('input', function () {
159 _this.$el.find('.'+_this.htmlClassPrefix+'charts-description').text($(this).val());
160 });
161
162 // chart styles
163 _this.$el.find('#'+_this.htmlClassPrefix+'option-border-width').on('input', function () {
164 _this.chartSourceData.settings.border_width = $(this).val();
165 _this.$el.find('.'+_this.htmlClassPrefix+'charts-main-container').css('border-width', $(this).val() + 'px');
166 _this.chartObject.update();
167 });
168
169 _this.$el.find('#'+_this.htmlClassPrefix+'option-border-radius').on('input', function () {
170 _this.chartSourceData.settings.border_radius = $(this).val();
171 _this.$el.find('.'+_this.htmlClassPrefix+'charts-main-container').css('border-radius', $(this).val() + 'px');
172 _this.chartObject.update();
173 });
174
175 _this.$el.find('#'+_this.htmlClassPrefix+'option-border-color').on('input', function () {
176 _this.chartSourceData.settings.border_color = $(this).val();
177 _this.$el.find('.'+_this.htmlClassPrefix+'charts-main-container').css('border-color', $(this).val());
178 _this.chartObject.update();
179 });
180
181 _this.$el.find('#'+_this.htmlClassPrefix+'option-border-style').on('change', function () {
182 _this.chartSourceData.settings.border_style = $(this).val();
183 _this.$el.find('.'+_this.htmlClassPrefix+'charts-main-container').css('border-style', $(this).val());
184 _this.chartObject.update();
185 });
186
187 _this.$el.find('#'+_this.htmlClassPrefix+'option-box-shadow').on('change', function () {
188 console.log($(this).is(':checked'));
189 if ($(this).is(':checked')) {
190 _this.$el.find('.'+_this.htmlClassPrefix+'charts-main-container').css('box-shadow', '2px 2px 10px 2px #000000');
191 } else {
192 _this.$el.find('.'+_this.htmlClassPrefix+'charts-main-container').css('box-shadow', 'unset');
193 }
194 _this.chartObject.update();
195 });
196
197 // advanced options
198 _this.$el.find('#'+_this.htmlClassPrefix+'option-outer-radius').on('input', function () {
199 _this.chartSourceData.settings.outer_radius = $(this).val();
200 _this.chartObject.options.radius = _this.chartSourceData.settings.outer_radius;
201 _this.chartObject.update();
202 });
203
204 _this.$el.find('#'+_this.htmlClassPrefix+'option-slice-spacing').on('input', function () {
205 _this.chartSourceData.settings.slice_spacing = $(this).val();
206 _this.chartObject.options.spacing = _this.chartSourceData.settings.slice_spacing;
207 _this.chartObject.update();
208 });
209
210 _this.$el.find('#'+_this.htmlClassPrefix+'option-circumference').on('input', function () {
211 _this.chartSourceData.settings.circumference = $(this).val();
212 _this.chartObject.options.circumference = _this.chartSourceData.settings.circumference;
213 _this.chartObject.update();
214 });
215
216 _this.$el.find('#'+_this.htmlClassPrefix+'option-start-angle').on('input', function () {
217 _this.chartSourceData.settings.start_angle = $(this).val();
218 _this.chartObject.options.rotation = _this.chartSourceData.settings.start_angle;
219 _this.chartObject.update();
220 });
221 }
222
223 ChartBuilderChartsJs.prototype.detectManualChange = function (e) {
224 var input = $(document).find('input[name="ays_source_type"]');
225 if (input.val() !== 'manual') input.val('manual');
226 }
227
228 ChartBuilderChartsJs.prototype.keyBoardConfig = function(e) {
229 if (e.which == 13) {
230 if ($(document).find("#ays-charts-form-chart-js").length !== 0 || $(document).find("#ays-settings-form").length !== 0) {
231 var parent = $(e.target).parents('.ays-chart-chart-source-data-edit-block');
232 var index = $(e.target).parents('.ays-chart-chart-source-data-input-box').index();
233 index = parent.index() == 0 ? index - 1 : index - 2;
234
235 if (e.shiftKey) {
236 var prevRow;
237 if (parent.prev('.ays-chart-chart-source-data-edit-block').length === 0) {
238 return false;
239 }
240 prevRow = parent.prev('.ays-chart-chart-source-data-edit-block');
241
242 prevRow.children('.ays-chart-chart-source-data-input-box').eq(index).find('.ays-text-input').focus();
243 } else {
244 var nextRow;
245 if (parent.next('.ays-chart-chart-source-data-edit-block').length === 0) {
246 this.addChartDataRow();
247 }
248 nextRow = parent.next('.ays-chart-chart-source-data-edit-block');
249
250 nextRow.children('.ays-chart-chart-source-data-input-box').eq(index).find('.ays-text-input').focus();
251 }
252
253 return false;
254 }
255 }
256 }
257
258 // Change tabs (tabulation)
259 ChartBuilderChartsJs.prototype.changeTabs = function(e){
260 if(! $(this).hasClass('no-js')){
261 var elemenetID = $(this).attr('href');
262 var active_tab = $(this).attr('data-tab');
263 $(document).find('.nav-tab-wrapper a.nav-tab').each(function () {
264 if ($(this).hasClass('nav-tab-active')) {
265 $(this).removeClass('nav-tab-active');
266 }
267 });
268 $(this).addClass('nav-tab-active');
269 $(document).find('.ays-tab-content').each(function () {
270 $(this).removeClass('ays-tab-content-active');
271 });
272 $(document).find("[name='ays_chart_tab']").val(active_tab);
273 $('.ays-tab-content' + elemenetID).addClass('ays-tab-content-active');
274 e.preventDefault();
275 }
276 }
277
278 ChartBuilderChartsJs.prototype.changeCurrentUrl = function(key){
279 var linkModified = location.href.split('?')[1].split('&');
280 for(var i = 0; i < linkModified.length; i++){
281 if(linkModified[i].split("=")[0] == key){
282 linkModified.splice(i, 1);
283 }
284 }
285 linkModified = linkModified.join('&');
286 window.history.replaceState({}, document.title, '?'+linkModified);
287 }
288
289 ChartBuilderChartsJs.prototype.toggleDDmenu = function(e){
290 var ddmenu = $(this).next();
291 var state = ddmenu.attr('data-expanded');
292 switch (state) {
293 case 'true':
294 $(this).find('.ays_fa').css({
295 transform: 'rotate(0deg)'
296 });
297 ddmenu.attr('data-expanded', 'false');
298 break;
299 case 'false':
300 $(this).find('.ays_fa').css({
301 transform: 'rotate(90deg)'
302 });
303 ddmenu.attr('data-expanded', 'true');
304 break;
305 }
306 }
307
308 ChartBuilderChartsJs.prototype.submitOnce = function(el) {
309 setTimeout(function() {
310 $(document).find('.ays-chart-loader-banner').attr('disabled', true);
311 }, 50);
312
313 setTimeout(function() {
314 $(document).find('.ays-chart-loader-banner').attr('disabled', false);
315 }, 5000);
316 }
317
318 // Load charts by given type main function
319 ChartBuilderChartsJs.prototype.initLibraries = function (){
320 var _this = this;
321
322 _this.$el.find('#ays-chart-option-create-author').select2({
323 placeholder: aysChartBuilderAdmin.selectUser,
324 minimumInputLength: 1,
325 allowClear: true,
326 language: {
327 searching: function() {
328 return aysChartBuilderAdmin.searching;
329 },
330 inputTooShort: function () {
331 return aysChartBuilderAdmin.pleaseEnterMore;
332 }
333 },
334 ajax: {
335 url: ajaxurl,
336 dataType: 'json',
337 data: function (response) {
338 var checkedUsers = _this.$el.find('#ays-chart-option-create-author').val();
339 return {
340 action: _this.ajaxAction,
341 function: window.aysChartBuilderChartSettings.ajax['actions']['author_user_search'],
342 security: window.aysChartBuilderChartSettings.ajax['nonces']['author_user_search'],
343 params: {
344 search: response.term,
345 val: checkedUsers
346 }
347 };
348 }
349 }
350 });
351
352 _this.$el.find('.' + _this.htmlClassPrefix + 'chart-source-data-content').sortable({
353 items: "> div." + _this.htmlClassPrefix + "chart-source-data-edit-block:not(:first)",
354 handle: "." + _this.htmlClassPrefix + "chart-source-data-move-row",
355 update: function (event, ui) {
356 _this.$el.find('div.' + _this.htmlClassPrefix + 'chart-source-data-edit-block:not(:first)').each(function (index) {
357 $(this).attr('data-source-id', index + 1);
358 $(this).find("input.ays-text-input").attr('name', 'ays_chart_source_data[' + (index + 1) + '][]')
359 });
360 }
361 });
362 }
363
364 // Load charts by given type main function
365 ChartBuilderChartsJs.prototype.loadChartBySource = function(isChangedType = false){
366 var _this = this;
367
368 if( ! _this.chartType ){
369 _this.chartType = _this.chartSourceData.chartType;
370 }
371
372 if(typeof _this.chartType !== undefined && _this.chartType){
373 switch (_this.chartType) {
374 case 'pie_chart':
375 _this.pieChartView(isChangedType);
376 break;
377 case 'bar_chart':
378 _this.barChartView(isChangedType);
379 break;
380 case 'line_chart':
381 _this.lineChartView(isChangedType);
382 break;
383 default:
384 _this.pieChartView(isChangedType);
385 break;
386 }
387 }
388 }
389
390 // Load chart by pie chart
391 ChartBuilderChartsJs.prototype.pieChartView = function(isChangedType){
392 var _this = this;
393 var getChartSource = _this.chartSourceData.source;
394
395 var dataTypes = _this.chartConvertData( getChartSource );
396
397 var settings = _this.chartSourceData.settings;
398 var nSettings = _this.configOptionsForCharts(settings);
399
400 var ctx = document.getElementById(_this.htmlClassPrefix + '-canvas');
401
402 _this.chartObject = new Chart(ctx, {
403 type: 'pie',
404 data: {
405 labels: dataTypes?.labels,
406 datasets: dataTypes?.dataSets,
407 },
408 options: {
409 radius: nSettings.outerRadius,
410 spacing: nSettings.sliceSpacing,
411 circumference: nSettings.circumference,
412 rotation: nSettings.startAngle,
413 }
414 });
415
416 _this.resizeChart();
417 }
418
419 // Load chart by bar chart
420 ChartBuilderChartsJs.prototype.barChartView = function(isChangedType){
421 var _this = this;
422 var getChartSource = _this.chartSourceData.source;
423
424 var dataTypes = _this.chartConvertData( getChartSource );
425
426 var settings = _this.chartSourceData.settings;
427 var nSettings = _this.configOptionsForCharts(settings);
428
429 var ctx = document.getElementById(_this.htmlClassPrefix + '-canvas');
430
431 _this.chartObject = new Chart(ctx, {
432 type: 'bar',
433 data: {
434 labels: dataTypes?.labels,
435 datasets: dataTypes?.dataSets,
436 },
437 options: {
438 // indexAxis: nSettings.indexAxis,
439 }
440 });
441
442 _this.resizeChart();
443 }
444
445 // Load chart by line chart
446 ChartBuilderChartsJs.prototype.lineChartView = function(isChangedType){
447 var _this = this;
448 var getChartSource = _this.chartSourceData.source;
449
450 var dataTypes = _this.chartConvertData( getChartSource );
451
452 var settings = _this.chartSourceData.settings;
453 var nSettings = _this.configOptionsForCharts(settings);
454
455 var ctx = document.getElementById(_this.htmlClassPrefix + '-canvas');
456
457 _this.chartObject = new Chart(ctx, {
458 type: 'line',
459 data: {
460 labels: dataTypes?.labels,
461 datasets: dataTypes?.dataSets,
462 },
463 options: {
464 // indexAxis: nSettings.indexAxis,
465 }
466 });
467
468 _this.resizeChart();
469 }
470
471 /*
472 Configure all settings for all chart types
473 Getting settings for each chart type in respective function
474 */
475 ChartBuilderChartsJs.prototype.configOptionsForCharts = function (settings) {
476 var newSettings = {};
477
478 // newSettings.indexAxis = (settings['index_axis'] == 'checked') ? 'y' : 'x';
479 newSettings.outerRadius = settings['outer_radius'];
480 newSettings.sliceSpacing = settings['slice_spacing'];
481 newSettings.circumference = settings['circumference'];
482 newSettings.startAngle = settings['start_angle'];
483
484 return newSettings;
485 }
486
487 // Detect window resize moment to draw charts responsively
488 ChartBuilderChartsJs.prototype.resizeChart = function(){
489 var _this = this;
490
491 //create trigger to resizeEnd event
492 $(window).resize(function() {
493 if(this.resizeTO) clearTimeout(this.resizeTO);
494 this.resizeTO = setTimeout(function() {
495 $(this).trigger('resizeEnd');
496 }, 100);
497 });
498
499 //redraw graph when window resize is completed
500 $(window).on('resizeEnd', function() {
501 _this.chartObject.resize();
502 });
503
504 }
505
506 ChartBuilderChartsJs.prototype.chartConvertData = function( data ){
507 var _this = this;
508 var dataTypes = [];
509
510 var dataTypes = Array.from({ length: data[0].length }, () => []);
511 for (var i = 0; i < data.length; i++) {
512 for (var j = 0; j < data[i].length; j++) {
513 if (i === 0) {
514 dataTypes[j].push(data[i][j]);
515 } else {
516 dataTypes[j].push(i === 0 || j === 0 ? data[i][j] : +data[i][j]);
517 }
518 }
519 }
520
521 var labels = dataTypes.shift();
522 labels.shift();
523 var dataSets = dataTypes.map(item => ({
524 label: item[0],
525 data: item.slice(1).map(Number),
526 }));
527
528 return {labels, dataSets};
529 }
530
531 // Update chart data and display immediately
532 ChartBuilderChartsJs.prototype.updateChartData = function(){
533 var _this = this;
534 var newData = _this.chartConvertData( _this.chartData );
535
536 _this.chartObject.data = {
537 ..._this.chartObject.data,
538 labels: newData?.labels,
539 datasets: newData?.dataSets,
540 },
541 _this.chartObject.update();
542 }
543
544 ChartBuilderChartsJs.prototype.addChartDataRow = function (element){
545 var _this = this;
546 var content = '';
547
548 var addedTermsandConds = this.$el.find("."+this.htmlClassPrefix+"chart-source-data-edit-block");
549 var addedTermsandCondsId = this.$el.find("."+this.htmlClassPrefix+"chart-source-data-edit-block:last-child");
550 var dataId = addedTermsandConds.length >= 1 ? addedTermsandCondsId.data("sourceId") + 1 : 1;
551 var colCount = addedTermsandConds.first().children().length - 1;
552
553 var termsCondsMessageAttrName = this.newTermsCondsMessageAttrName( this.htmlNamePrefix + 'chart_source_data' , dataId );
554
555 content += '<div class = "'+this.htmlClassPrefix+'chart-source-data-edit-block" data-source-id="' + dataId + '" >';
556 content += '<div class="'+this.htmlClassPrefix+'chart-source-data-move-block '+this.htmlClassPrefix+'chart-source-data-move-row">';
557 content += '<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512"><path d="M278.6 9.4c-12.5-12.5-32.8-12.5-45.3 0l-64 64c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l9.4-9.4V224H109.3l9.4-9.4c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-64 64c-12.5 12.5-12.5 32.8 0 45.3l64 64c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3l-9.4-9.4H224V402.7l-9.4-9.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l64 64c12.5 12.5 32.8 12.5 45.3 0l64-64c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-9.4 9.4V288H402.7l-9.4 9.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0l64-64c12.5-12.5 12.5-32.8 0-45.3l-64-64c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3l9.4 9.4H288V109.3l9.4 9.4c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3l-64-64z" style="fill: #b8b8b8;" /></svg>';
558 content += '</div>';
559 content += '<div class="'+this.htmlClassPrefix+'icons-box '+this.htmlClassPrefix+'icons-remove-box">';
560 content += '<svg class="'+this.htmlClassPrefix+'chart-source-data-remove-block '+this.htmlClassPrefix+'chart-source-data-remove-row" data-trigger="hover" data-bs-toggle="tooltip" title="Delete row" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path d="M310.6 150.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L160 210.7 54.6 105.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L114.7 256 9.4 361.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L160 301.3 265.4 406.6c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L205.3 256 310.6 150.6z" style="fill: #b8b8b8;" /></svg>';
561 content += '</div>';
562 for (var i = 0; i < colCount; i++) {
563 if (i == 0) {
564 content += '<div class="'+this.htmlClassPrefix+'chart-source-data-input-box '+this.htmlClassPrefix+'chart-source-data-name-input-box" data-cell-id="'+i+'">';
565 content += '<input type="text" class="ays-text-input form-control" name="' + termsCondsMessageAttrName + '">';
566 content += '</div>';
567 } else {
568 content += '<div class="'+this.htmlClassPrefix+'chart-source-data-input-box '+this.htmlClassPrefix+'chart-source-data-input-number" data-cell-id="'+i+'">';
569 content += '<input type="number" class="ays-text-input form-control" name="' + termsCondsMessageAttrName + '" step="any">';
570 content += '</div>';
571 }
572 }
573 content += '</div>';
574
575 this.$el.find('.'+this.htmlClassPrefix+'chart-source-data-content').append(content);
576 $('[data-bs-toggle="tooltip"]').tooltip();
577 }
578
579 ChartBuilderChartsJs.prototype.addChartDataColumn = function (e){
580 var _this = this;
581
582 var rows = _this.$el.find("."+_this.htmlClassPrefix+"chart-source-data-content").children();
583 var lastColId = +$(rows[0]).find('.'+_this.htmlClassPrefix+'chart-source-data-input-box:last-child').attr('data-cell-id');
584 rows.each(function(key, row){
585 var dataIDEach = row.getAttribute('data-source-id');
586 var content = '';
587
588 if (key === 0) {
589 content += '<div class="'+_this.htmlClassPrefix+'chart-source-data-input-box ' +_this.htmlClassPrefix+'chart-source-title-box" data-cell-id="'+(lastColId+1)+'">';
590 content += '<svg class="'+_this.htmlClassPrefix+'chart-source-data-remove-block '+_this.htmlClassPrefix+'chart-source-data-remove-col" data-trigger="hover" data-bs-toggle="tooltip" title="Delete column" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" width="10px">';
591 content += '<path d="M310.6 150.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L160 210.7 54.6 105.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L114.7 256 9.4 361.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L160 301.3 265.4 406.6c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L205.3 256 310.6 150.6z" style="fill: #b8b8b8;" />';
592 content += '</svg>';
593
594 content += '<div class="' + _this.htmlClassPrefix + 'chart-source-data-titles-box-item">';
595 content += '<input type="text" class="ays-text-input form-control ' + _this.htmlClassPrefix+'chart-source-title-input" name="' + _this.newTermsCondsMessageAttrName( _this.htmlNamePrefix + 'chart_source_data' , dataIDEach ) + '">';
596 content += '<svg class="' + _this.htmlClassPrefix + 'chart-source-data-sort" data-sort-order="asc" xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 320 512">';
597 content += '<path d="M137.4 41.4c12.5-12.5 32.8-12.5 45.3 0l128 128c9.2 9.2 11.9 22.9 6.9 34.9s-16.6 19.8-29.6 19.8H32c-12.9 0-24.6-7.8-29.6-19.8s-2.2-25.7 6.9-34.9l128-128zm0 429.3l-128-128c-9.2-9.2-11.9-22.9-6.9-34.9s16.6-19.8 29.6-19.8H288c12.9 0 24.6 7.8 29.6 19.8s2.2 25.7-6.9 34.9l-128 128c-12.5 12.5-32.8 12.5-45.3 0z" style="fill: #b8b8b8;" />';
598 content += '</svg>';
599 content += '</div>'
600 content += '</div>';
601 } else {
602 content += '<div class="'+_this.htmlClassPrefix+'chart-source-data-input-box ' +_this.htmlClassPrefix+'chart-source-data-input-number" data-cell-id="'+(lastColId+1)+'">';
603 content += '<input type="number" class="ays-text-input form-control" name="' + _this.newTermsCondsMessageAttrName( _this.htmlNamePrefix + 'chart_source_data' , dataIDEach ) + '" step="any">';
604 content += '</div>';
605 }
606
607 $(row).append(content);
608 $('[data-bs-toggle="tooltip"]').tooltip();
609 });
610 }
611
612 ChartBuilderChartsJs.prototype.deleteChartDataRow = function (element){
613 var _this = this;
614
615 var rows = _this.$el.find("."+_this.htmlClassPrefix+"chart-source-data-content").children();
616 if ((rows.length - 1) >= 2) {
617 var confirm = window.confirm(aysChartBuilderAdmin.confirmRowDelete);
618 if (confirm) {
619 var thisMainParent = element.parent().parent();
620 thisMainParent.remove();
621 }
622 } else {
623 alert(aysChartBuilderAdmin.minRowNotice);
624 }
625
626 element.blur();
627 element.tooltip('hide');
628 }
629
630 ChartBuilderChartsJs.prototype.deleteChartDataColumn = function (element){
631 var _this = this;
632
633 var rows = _this.$el.find("."+_this.htmlClassPrefix+"chart-source-data-content").children();
634 if (rows.eq(1).children('.ays-chart-chart-source-data-input-number').length >= 2) {
635 var confirm = window.confirm(aysChartBuilderAdmin.confirmColDelete);
636 if (confirm) {
637 var parent = element.parents('.ays-chart-chart-source-title-box');
638 var index = _this.returnIndexOfEl(parent);
639 rows.each(function(key, row){
640 var dataRow = $(row).find('.ays-chart-chart-source-data-input-box:not("ays-chart-chart-source-data-name-input-box")');
641 $(dataRow).each(function(ind, cell){
642 if (ind == index) {
643 $(cell).remove();
644 }
645 });
646 });
647 }
648 } else {
649 alert(aysChartBuilderAdmin.minColNotice);
650 }
651
652 element.blur();
653 element.tooltip('hide');
654 }
655
656 ChartBuilderChartsJs.prototype.sortDataByColumn = function (el) {
657 var _this = this;
658
659 var sortingOrder = el.attr('data-sort-order');
660 var colFirst = el.parents('.'+_this.htmlClassPrefix+'chart-source-data-input-box');
661 var colIndex = colFirst.attr('data-cell-id');
662 var colList = colFirst.parents('.'+_this.htmlClassPrefix+'chart-source-data-content').find('.'+_this.htmlClassPrefix+'chart-source-data-input-box[data-cell-id="'+colIndex+'"] input.ays-text-input');
663
664 var sorted = {};
665 colList.each(function(key, input){
666 if (key !== 0) {
667 var rowIndex = $(input).parents('.'+_this.htmlClassPrefix+'chart-source-data-edit-block').attr('data-source-id');
668 sorted[rowIndex] = +input.value
669 }
670 });
671
672 if (sortingOrder === 'asc') {
673 sorted = _this.sortDataAsc(sorted);
674 el.attr('data-sort-order', 'desc');
675 } else if (sortingOrder === 'desc') {
676 sorted = _this.sortDataDesc(sorted);
677 el.attr('data-sort-order', 'asc');
678 } else {
679 sorted = _this.sortDataAsc(sorted);
680 el.attr('data-sort-order', 'desc');
681 }
682
683 var container = _this.$el.find('.'+_this.htmlClassPrefix+'chart-source-data-content');
684 var newContainer = [];
685 sorted.forEach((index, newIndex) => {
686 var row = container.find("."+_this.htmlClassPrefix+"chart-source-data-edit-block[data-source-id='"+index+"']");
687 var inputs = row.find('input.ays-text-input');
688 var inputName = _this.newTermsCondsMessageAttrName(_this.htmlNamePrefix + 'chart_source_data', newIndex + 1);
689
690 $(inputs).each(function (inputIndex, input) {
691 input.setAttribute('name', inputName);
692 });
693
694 newContainer.push(row);
695 });
696 sorted.forEach((index, newIndex) => {
697 var row = container.find("."+_this.htmlClassPrefix+"chart-source-data-edit-block[data-source-id='"+index+"']");
698 newContainer.push(row);
699 });
700
701 container.innerHTML = '';
702 newContainer.forEach(row => {
703 container.append(row);
704 });
705
706 var rows = container.find("."+_this.htmlClassPrefix+"chart-source-data-edit-block");
707 for (var i = 0; i < rows.length; i++) {
708 $(rows[i]).attr('data-source-id', i);
709 }
710 }
711
712 ChartBuilderChartsJs.prototype.sortDataAsc = function (data) {
713 return Object.keys(data).sort(function (a, b) {return data[a] - data[b]});
714 }
715
716 ChartBuilderChartsJs.prototype.sortDataDesc = function (data) {
717 return Object.keys(data).sort(function (a, b) {return data[b] - data[a]});
718 }
719
720 ChartBuilderChartsJs.prototype.returnIndexOfEl = function (element){
721 var i = 0;
722 var elements = element.parent().find('.ays-chart-chart-source-title-box');
723 elements.each(function(key, cell){
724 if ($(cell).is(element)) {
725 i = key;
726 }
727 });
728 return i;
729 }
730
731 ChartBuilderChartsJs.prototype.configureOptions = function() {
732 var _this = this;
733
734 _this.$el.find('.ays-chart-chart-source-title-box').eq(0).find('.ays-chart-chart-source-data-remove-block').css('visibility', 'hidden');
735
736 var removeCols = _this.$el.find('.ays-chart-chart-source-title-box').find('.ays-chart-chart-source-data-remove-block');
737 if (_this.chartType === 'pie_chart') {
738 removeCols.css('visibility', 'hidden');
739 }
740
741 var options = _this.$el.find('.cb-changable-opt');
742 var typeOptions = _this.$el.find('.cb-'+_this.chartType+'-opt');
743 options.addClass('display_none');
744 typeOptions.removeClass('display_none');
745
746 var manualTabs = _this.$el.find('.cb-changable-manual:not(.cb-'+_this.chartType+'-manual)');
747 var typeManualTab = _this.$el.find('.cb-'+_this.chartType+'-manual');
748 manualTabs.remove();
749 typeManualTab.removeClass('display_none');
750
751 var tabs = _this.$el.find('.cb-changable-tab:not(.cb-'+_this.chartType+'-tab)').parents('fieldset.ays-accordion-options-container');
752 var currentTabs = _this.$el.find('.cb-'+_this.chartType+'-tab').parents('fieldset.ays-accordion-options-container');
753 tabs.addClass('display_none');
754 currentTabs.removeClass('display_none');
755 }
756
757 ChartBuilderChartsJs.prototype.newTermsCondsMessageAttrName = function (termCondName, termCondId){
758 var _this = this;
759 return termCondName + '['+ termCondId +'][]';
760 }
761
762 ChartBuilderChartsJs.prototype.setAccordionEvents = function(e){
763 var _this = this;
764
765 _this.$el.on('click', '.ays-accordion-options-header', function(e){
766 _this.openCloseAccordion(e, _this);
767 });
768
769 _this.$el.on('click', '.ays-slices-accordion-options-header', function(e){
770 _this.openCloseAccordion(e, _this, '-slices');
771 });
772
773 _this.$el.on('click', '.ays-series-accordion-options-header', function(e){
774 _this.openCloseAccordion(e, _this, '-series');
775 });
776
777 _this.$el.on('click', '.ays-rows-accordion-options-header', function(e){
778 _this.openCloseAccordion(e, _this, '-rows');
779 });
780 }
781
782 ChartBuilderChartsJs.prototype.openCloseAccordion = function(e, _this, contType = ""){
783 var container = $(e.target).parents('.ays' + contType + '-accordion-options-container');
784 var parent = (contType != "") ? container.parents('.ays-chart' + contType + '-settings') : _this.$el.find('#' + container.parents('.ays-tab-content').attr('id') + ' .ays-accordions-container').eq(0);
785 var index = (contType != "") ? container.index() : -1;
786
787 if( container.attr('data-collapsed') === 'true' ){
788 _this.closeAllAccordions( parent, contType, index );
789 setTimeout(() => {
790 container.find('.ays' + contType + '-accordion-options-content').slideDown();
791 container.attr('data-collapsed', 'false');
792 container.find('.ays' + contType + '-accordion-options-header .ays' + contType + '-accordion-arrow').find('path').css('fill', '#008cff');
793 container.find('.ays' + contType + '-accordion-options-header .ays' + contType + '-accordion-arrow').removeClass('ays' + contType + '-accordion-arrow-right').addClass('ays' + contType + '-accordion-arrow-down');
794 }, 150);
795 }else{
796 setTimeout(() => {
797 container.find('.ays' + contType + '-accordion-options-content').slideUp();
798 container.attr('data-collapsed', 'true');
799 container.find('.ays' + contType + '-accordion-options-header .ays' + contType + '-accordion-arrow').find('path').css('fill', '#c4c4c4');
800 container.find('.ays' + contType + '-accordion-options-header .ays' + contType + '-accordion-arrow').removeClass('ays' + contType + '-accordion-arrow-down').addClass('ays' + contType + '-accordion-arrow-right');
801 }, 150);
802 }
803 }
804
805 ChartBuilderChartsJs.prototype.closeAllAccordions = function( container, contType, index ){
806 var _this = this;
807
808 container.find('.ays' + contType + '-accordion-options-container').each(function (i){
809 var $this = $(this);
810 if (i != index) {
811 setTimeout(() => {
812 $this.find('.ays' + contType + '-accordion-options-content').slideUp();
813 $this.attr('data-collapsed', 'true');
814 $this.find('.ays' + contType + '-accordion-options-header .ays' + contType + '-accordion-arrow').find('path').css('fill', '#c4c4c4');
815 $this.find('.ays' + contType + '-accordion-options-header .ays' + contType + '-accordion-arrow').removeClass('ays' + contType + '-accordion-arrow-down').addClass('ays' + contType + '-accordion-arrow-right');
816 }, 150);
817 }
818 });
819 }
820
821 ChartBuilderChartsJs.prototype.quickSaveHotKeys = function() {
822 $(document).on('keydown', function(e){
823 var saveButton = $(document).find('input#ays-button-apply');
824 if ( saveButton.length > 0 ) {
825 if (!(e.which == 83 && e.ctrlKey) && !(e.which == 19)){
826 return true;
827 }
828 saveButton.trigger("click");
829 e.preventDefault();
830 return false;
831 }
832 });
833 }
834
835 // Manual data preview button
836 ChartBuilderChartsJs.prototype.showOnChart = function () { // TODO
837 var _this = this;
838
839 var lastId = $(document).find(".ays-chart-chart-source-data-edit-block:last-child").attr('data-source-id');
840 var chartData = [];
841
842 var rowTitles = $(document).find('.ays-chart-chart-source-data-name-input-box');
843 rowTitles.each(function(key, el) {
844 var value = $(el).find('input').val();
845 if ( value == '' ) {
846 $(el).find('input').val('Option');
847 }
848 });
849
850 var form = $(document).find("#ays-charts-form-chart-js");
851 var data = form.serializeFormJSON();
852
853 for (var i = 0; i <= lastId; i++) {
854 if (data['ays_chart_source_data['+i+'][]'] !== undefined) {
855 var dataRow = data['ays_chart_source_data['+i+'][]'];
856 var filteredRow = [];
857 for (var key = 0; key < dataRow.length; key++) {
858 var value = dataRow[key];
859 if (value != '') {
860 filteredRow.push(value);
861 } else {
862 if (i == 0) {
863 filteredRow.push('Title'+key);
864 } else {
865 filteredRow.push('0');
866 }
867 }
868 }
869 if (filteredRow.length != 0) {
870 chartData.push(filteredRow);
871 }
872 }
873 }
874
875 _this.chartData = chartData;
876 _this.updateChartData();
877 }
878
879 ChartBuilderChartsJs.prototype.htmlDecode = function (input) {
880 if (!input) return input;
881
882 var e = document.createElement('div');
883 e.innerHTML = input;
884 return e.childNodes[0].nodeValue;
885 }
886
887 $.fn.ChartBuilderChartsJsMain = function(options) {
888 return this.each(function() {
889 if (!$.data(this, 'ChartBuilderChartsJsMain')) {
890 $.data(this, 'ChartBuilderChartsJsMain', new ChartBuilderChartsJs(this, options));
891 } else {
892 try {
893 $(this).data('ChartBuilderChartsJsMain').init();
894 } catch (err) {
895 console.error('ChartBuilderChartsJsMain has not initiated properly');
896 }
897 }
898 });
899 };
900 $(document).find('#ays-charts-form-chart-js').ChartBuilderChartsJsMain();
901
902 })(jQuery);
903
904 (function ($) {
905
906 $.fn.serializeFormJSON = function () {
907 let o = {},
908 a = this.serializeArray();
909 $.each(a, function () {
910 if (o[this.name]) {
911 if (!o[this.name].push) {
912 o[this.name] = [o[this.name]];
913 }
914 o[this.name].push(this.value || '');
915 } else {
916 o[this.name] = this.value || '';
917 }
918 });
919 return o;
920 };
921
922 $.fn.lock = function () {
923 $(this).each(function () {
924 var $this = $(this);
925 var position = $this.css('position');
926
927 if (!position) {
928 position = 'static';
929 }
930
931 switch (position) {
932 case 'absolute':
933 case 'relative':
934 break;
935 default:
936 $this.css('position', 'relative');
937 break;
938 }
939 $this.data('position', position);
940
941 var width = $this.width(),
942 height = $this.height();
943
944 var locker = $('<div class="locker"></div>');
945 locker.width(width).height(height);
946
947 var loader = $('<div class="locker-loader"></div>');
948 loader.width(width).height(height);
949
950 locker.append(loader);
951 $this.append(locker);
952 $(window).resize(function () {
953 $this.find('.locker,.locker-loader').width($this.width()).height($this.height());
954 });
955 });
956
957 return $(this);
958 };
959
960 $.fn.unlock = function () {
961 $(this).each(function () {
962 $(this).find('.locker').remove();
963 $(this).css('position', $(this).data('position'));
964 });
965
966 return $(this);
967 };
968 })(jQuery);