|
15 | 15 | <?php if (is_array($this->getChild('diagrams')->getTabsIds())) : ?> |
16 | 16 | <script type="text/javascript"> |
17 | 17 | //<![CDATA[ |
18 | | -function changeDiagramsPeriod(periodObj) { |
19 | | - periodParam = periodObj.value ? 'period/' + periodObj.value + '/' : ''; |
20 | | -<?php foreach ($this->getChild('diagrams')->getTabsIds() as $tabId): ?> |
21 | | - ajaxBlockParam = 'block/tab_<?= $tabId ?>/'; |
22 | | - ajaxBlockUrl = '<?= $this->getUrl('*/*/ajaxBlock', ['_current' => true, 'block' => '', 'period' => '']) ?>' + ajaxBlockParam + periodParam; |
23 | | - const formData_<?= $tabId ?> = new FormData(); |
24 | | - formData_<?= $tabId ?>.append('isAjax', 'true'); |
25 | | - formData_<?= $tabId ?>.append('form_key', FORM_KEY); |
26 | | - |
27 | | - mahoFetch(ajaxBlockUrl, { |
28 | | - method: 'POST', |
29 | | - body: formData_<?= $tabId ?> |
30 | | - }) |
31 | | - .then(response => { |
32 | | - tabContentElementId = '<?= $this->getChild('diagrams')->getId() ?>_<?= $tabId ?>_content'; |
33 | | - try { |
34 | | - let responseData = response; |
35 | | - if (typeof response === 'string') { |
36 | | - try { |
37 | | - responseData = JSON.parse(response); |
38 | | - if (responseData.error) { |
39 | | - alert(responseData.message); |
40 | | - } |
41 | | - if(responseData.ajaxExpired && responseData.ajaxRedirect) { |
42 | | - setLocation(responseData.ajaxRedirect); |
43 | | - } |
44 | | - } catch (e) { |
45 | | - document.getElementById(tabContentElementId).innerHTML = response; |
46 | | - } |
47 | | - } else { |
48 | | - document.getElementById(tabContentElementId).innerHTML = response; |
49 | | - } |
| 18 | +function loadChartJs() { |
| 19 | + return new Promise((resolve, reject) => { |
| 20 | + if (window.Chart) { |
| 21 | + resolve(); |
| 22 | + return; |
50 | 23 | } |
51 | | - catch (e) { |
52 | | - document.getElementById(tabContentElementId).innerHTML = response; |
53 | | - } |
54 | | - }) |
55 | | - .catch(error => { |
56 | | - console.error('Ajax block error:', error); |
| 24 | + const script = document.createElement('script'); |
| 25 | + script.src = 'https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.5.0/chart.umd.min.js'; |
| 26 | + script.integrity = 'sha512-Y51n9mtKTVBh3Jbx5pZSJNDDMyY+yGe77DGtBPzRlgsf/YLCh13kSZ3JmfHGzYFCmOndraf0sQgfM654b7dJ3w=='; |
| 27 | + script.crossOrigin = 'anonymous'; |
| 28 | + script.async = true; |
| 29 | + script.onload = resolve; |
| 30 | + script.onerror = () => reject(new Error('Failed to load Chart.js')); |
| 31 | + document.head.appendChild(script); |
57 | 32 | }); |
58 | | -<?php endforeach ?> |
59 | | - ajaxBlockUrl = '<?= $this->getUrl('*/*/ajaxBlock', ['_current' => true, 'block' => 'totals', 'period' => '']) ?>' + periodParam; |
60 | | - const formData2 = new FormData(); |
61 | | - formData2.append('isAjax', 'true'); |
62 | | - formData2.append('form_key', FORM_KEY); |
| 33 | +} |
63 | 34 |
|
64 | | - mahoFetch(ajaxBlockUrl, { |
65 | | - method: 'POST', |
66 | | - body: formData2 |
67 | | - }) |
68 | | - .then(response => { |
69 | | - tabContentElementId = 'dashboard_diagram_totals'; |
| 35 | +function initDashboardCharts(container) { |
| 36 | + container.querySelectorAll('.dashboard-chart').forEach(el => { |
| 37 | + const canvas = el.querySelector('canvas'); |
| 38 | + if (!canvas) return; |
| 39 | + |
| 40 | + let labels, data; |
70 | 41 | try { |
71 | | - let responseData = response; |
72 | | - if (typeof response === 'string') { |
73 | | - try { |
74 | | - responseData = JSON.parse(response); |
75 | | - if (responseData.error) { |
76 | | - alert(responseData.message); |
77 | | - } |
78 | | - if(responseData.ajaxExpired && responseData.ajaxRedirect) { |
79 | | - setLocation(responseData.ajaxRedirect); |
80 | | - } |
81 | | - } catch (e) { |
82 | | - document.getElementById(tabContentElementId).outerHTML = response; |
| 42 | + labels = JSON.parse(el.dataset.chartLabels); |
| 43 | + data = JSON.parse(el.dataset.chartData); |
| 44 | + } catch (e) { |
| 45 | + console.error('Dashboard chart: invalid data', e); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + if (canvas._chartInstance) canvas._chartInstance.destroy(); |
| 50 | + canvas._chartInstance = new Chart(canvas, { |
| 51 | + type: 'line', |
| 52 | + data: { |
| 53 | + labels: labels, |
| 54 | + datasets: [{ |
| 55 | + data: data, |
| 56 | + borderColor: '#adb41a', |
| 57 | + tension: 0.2 |
| 58 | + }] |
| 59 | + }, |
| 60 | + options: { |
| 61 | + plugins: { |
| 62 | + legend: { display: false } |
83 | 63 | } |
84 | | - } else { |
85 | | - document.getElementById(tabContentElementId).outerHTML = response; |
86 | 64 | } |
87 | | - } |
88 | | - catch (e) { |
89 | | - document.getElementById(tabContentElementId).outerHTML = response; |
90 | | - } |
91 | | - }) |
92 | | - .catch(error => { |
93 | | - console.error('Ajax block totals error:', error); |
| 65 | + }); |
94 | 66 | }); |
95 | 67 | } |
96 | 68 |
|
| 69 | +function changeDiagramsPeriod(periodObj) { |
| 70 | + const periodParam = periodObj.value ? 'period/' + periodObj.value + '/' : ''; |
| 71 | + |
| 72 | +<?php foreach ($this->getChild('diagrams')->getTabsIds() as $tabId): ?> |
| 73 | + mahoFetch('<?= $this->getUrl('*/*/ajaxBlock', ['_current' => true, 'block' => '', 'period' => '']) ?>' + 'block/tab_<?= $tabId ?>/' + periodParam, { |
| 74 | + method: 'POST' |
| 75 | + }).then(response => { |
| 76 | + const el = document.getElementById('<?= $this->getChild('diagrams')->getId() ?>_<?= $tabId ?>_content'); |
| 77 | + el.innerHTML = response; |
| 78 | + loadChartJs().then(() => initDashboardCharts(el)); |
| 79 | + }).catch(e => console.error('Dashboard diagram error:', e)); |
| 80 | +<?php endforeach ?> |
| 81 | + |
| 82 | + mahoFetch('<?= $this->getUrl('*/*/ajaxBlock', ['_current' => true, 'block' => 'totals', 'period' => '']) ?>' + periodParam, { |
| 83 | + method: 'POST' |
| 84 | + }).then(response => { |
| 85 | + document.getElementById('dashboard_diagram_totals').outerHTML = response; |
| 86 | + }).catch(e => console.error('Dashboard totals error:', e)); |
| 87 | +} |
| 88 | + |
97 | 89 | function toggleCal(id) { |
98 | 90 | const calDiv = document.getElementById('dashboard_'+id+'_cal_div'); |
99 | 91 | const rangeDiv = document.getElementById('dashboard_'+id+'_range_div'); |
100 | 92 | calDiv.style.display = calDiv.style.display === 'none' ? 'block' : 'none'; |
101 | 93 | rangeDiv.style.display = rangeDiv.style.display === 'none' ? 'block' : 'none'; |
102 | 94 | } |
| 95 | + |
| 96 | +mahoOnReady(() => loadChartJs().then(() => initDashboardCharts(document))); |
103 | 97 | //]]> |
104 | 98 | </script> |
105 | 99 | <?php endif ?> |
|
0 commit comments