| @@ -1,29 +1,17 @@ | ||
| 1 | 1 | /* global console */ |
| 2 | 2 | /* global visualizer */ |
| 3 | 3 | /* global jQuery */ |
| 4 | -window['vizClipboard1'] = window['vizClipboard1'] || null; | |
| 5 | -window['vizClipboard2'] = window['vizClipboard2'] || null; | |
| 4 | + | |
| 6 | 5 | (function($, visualizer){ |
| 7 | - | |
| 6 | + | |
| 8 | 7 | function initActionsButtons(v) { |
| 9 | - if($('a.visualizer-chart-shortcode').length > 0 && vizClipboard1 === null) { | |
| 10 | - vizClipboard1 = new ClipboardJS('a.visualizer-chart-shortcode'); // jshint ignore:line | |
| 11 | - vizClipboard1.on('success', function(e) { | |
| 8 | + if($('a.visualizer-action[data-visualizer-type=copy]').length > 0) { | |
| 9 | + var clipboard = new Clipboard('a.visualizer-action[data-visualizer-type=copy]'); // jshint ignore:line | |
| 10 | + clipboard.on('success', function(e) { | |
| 12 | 11 | window.alert(v.i10n['copied']); |
| 13 | - | |
| 14 | 12 | }); |
| 15 | 13 | } |
| 16 | - | |
| 17 | - if($('a.visualizer-action[data-visualizer-type=copy]').length > 0 && vizClipboard2 === null) { | |
| 18 | - $('a.visualizer-action[data-visualizer-type=copy]').on('click', function(e) { | |
| 19 | - e.preventDefault(); | |
| 20 | - }); | |
| 21 | - vizClipboard2 = new ClipboardJS('a.visualizer-action[data-visualizer-type=copy]'); // jshint ignore:line | |
| 22 | - vizClipboard2.on('success', function(e) { | |
| 23 | - window.alert(v.i10n['copied']); | |
| 24 | - }); | |
| 25 | - } | |
| 26 | 14 | $('a.visualizer-action[data-visualizer-type!=copy]').off('click').on('click', function(e) { |
| 27 | 15 | var type = $(this).attr( 'data-visualizer-type' ); |
| 28 | 16 | var chart = $(this).attr( 'data-visualizer-chart-id' ); |
| 29 | 17 | var container = $(this).attr( 'data-visualizer-container-id' ); |
| @@ -28,15 +16,13 @@ | ||
| 28 | 16 | var chart = $(this).attr( 'data-visualizer-chart-id' ); |
| 29 | 17 | var container = $(this).attr( 'data-visualizer-container-id' ); |
| 30 | 18 | var lock = $('.visualizer-front.visualizer-front-' + chart); |
| 31 | 19 | var mime = $(this).attr( 'data-visualizer-mime' ); |
| 20 | + console.log(mime); | |
| 32 | 21 | lock.lock(); |
| 33 | 22 | e.preventDefault(); |
| 34 | 23 | $.ajax({ |
| 35 | 24 | url : v.rest_url.replace('#id#', chart).replace('#type#', type), |
| 36 | - beforeSend: function ( xhr ) { | |
| 37 | - xhr.setRequestHeader( 'X-WP-Nonce', v.wp_nonce ); | |
| 38 | - }, | |
| 39 | 25 | success: function(data) { |
| 40 | 26 | if (data && data.data) { |
| 41 | 27 | switch(type){ |
| 42 | 28 | case 'csv': |
| @@ -69,10 +55,9 @@ | ||
| 69 | 55 | $a.remove(); |
| 70 | 56 | } |
| 71 | 57 | break; |
| 72 | 58 | case 'print': |
| 73 | - case 'image': | |
| 74 | - $('body').trigger('visualizer:action:specificchart', {action: type, id: container, data: data.data.csv, dataObj: data.data}); | |
| 59 | + $('body').trigger('visualizer:action:specificchart', {action: 'print', id: container, data: data.data.csv}); | |
| 75 | 60 | break; |
| 76 | 61 | default: |
| 77 | 62 | if(window.visualizer_perform_action) { |
| 78 | 63 | window.visualizer_perform_action(type, chart, data.data); |
| @@ -95,113 +80,13 @@ | ||
| 95 | 80 | return buf; |
| 96 | 81 | } |
| 97 | 82 | |
| 98 | 83 | $(document).ready(function(){ |
| 99 | - | |
| 100 | - // for updating the currently displayed chart (preview mode) | |
| 101 | - window.vizUpdateChartPreview = function(){ | |
| 102 | - var event = new CustomEvent('visualizer:render:currentchart:update', {detail: {visualizer: visualizer}}); | |
| 103 | - document.body.dispatchEvent(event); | |
| 104 | - }; | |
| 105 | - | |
| 106 | - initChartDisplay(); | |
| 84 | + $('body').trigger('visualizer:render:chart:start', visualizer); | |
| 107 | 85 | initActionsButtons(visualizer); |
| 108 | 86 | registerDefaultActions(); |
| 109 | 87 | }); |
| 110 | 88 | |
| 111 | - function initChartDisplay() { | |
| 112 | - if(visualizer.is_front == true){ // jshint ignore:line | |
| 113 | - displayChartsOnFrontEnd(); | |
| 114 | - } else { | |
| 115 | - showChart(); | |
| 116 | - } | |
| 117 | - } | |
| 118 | - | |
| 119 | - /** | |
| 120 | - * If an `id` is provided, that particular chart will load. | |
| 121 | - */ | |
| 122 | - function showChart(id) { | |
| 123 | - // clone the visualizer object so that the original object is not affected. | |
| 124 | - var viz = Object.assign(visualizer, window.visualizer || {}); | |
| 125 | - if(id){ | |
| 126 | - viz.id = id; | |
| 127 | - } | |
| 128 | - $('body').trigger('visualizer:render:chart:start', viz); | |
| 129 | - } | |
| 130 | - | |
| 131 | - function displayChartsOnFrontEnd() { | |
| 132 | - function renderVisibleLazyCharts() { | |
| 133 | - $('div.visualizer-front:not(.viz-facade-loaded):not(.visualizer-lazy):not(.visualizer-cw-error):empty').each(function(index, element){ | |
| 134 | - // Do not render charts that are intentionally hidden. | |
| 135 | - const style = window.getComputedStyle(element); | |
| 136 | - if (style.display === 'none' || style.visibility === 'hidden') { | |
| 137 | - return; | |
| 138 | - } | |
| 139 | - | |
| 140 | - // Only render charts that are currently within the viewport. | |
| 141 | - const rect = element.getBoundingClientRect(); | |
| 142 | - const inViewport = rect.bottom >= 0 && rect.top <= (window.innerHeight || document.documentElement.clientHeight); | |
| 143 | - if (!inViewport) { | |
| 144 | - return; | |
| 145 | - } | |
| 146 | - | |
| 147 | - const id = $(element).addClass('viz-facade-loaded').attr('id'); | |
| 148 | - setTimeout(function(){ | |
| 149 | - // Add a short delay between each chart to avoid overloading the browser event loop. | |
| 150 | - showChart(id); | |
| 151 | - }, ( index + 1 ) * 100); | |
| 152 | - }); | |
| 153 | - } | |
| 154 | - | |
| 155 | - $(window).on('scroll', renderVisibleLazyCharts); | |
| 156 | - | |
| 157 | - // Run once on page load to render any lazy charts already in the viewport. | |
| 158 | - renderVisibleLazyCharts(); | |
| 159 | - | |
| 160 | - $('div.visualizer-front-container:not(.visualizer-lazy-render)').each(function(index, element){ | |
| 161 | - // Do not render charts that are intentionally hidden. | |
| 162 | - const style = window.getComputedStyle($(element).find('.visualizer-front')[0]); | |
| 163 | - if (style.display === 'none' || style.visibility === 'hidden') { | |
| 164 | - return; | |
| 165 | - } | |
| 166 | - | |
| 167 | - const id = $(element).find('.visualizer-front').addClass('viz-facade-loaded').attr('id'); | |
| 168 | - setTimeout(function(){ | |
| 169 | - // Add a short delay between each chart to avoid overloading the browser event loop. | |
| 170 | - showChart(id); | |
| 171 | - }, ( index + 1 ) * 100); | |
| 172 | - }); | |
| 173 | - | |
| 174 | - // interate through all charts that are to be lazy-loaded and observe each one. | |
| 175 | - $('div.visualizer-front.visualizer-lazy').each(function(index, element){ | |
| 176 | - var id = $(element).attr('id'); | |
| 177 | - var limit = $(element).attr('data-lazy-limit'); | |
| 178 | - if(!limit){ | |
| 179 | - limit = 300; | |
| 180 | - } | |
| 181 | - var target = document.querySelector('#' + id); | |
| 182 | - | |
| 183 | - // configure the intersection observer instance | |
| 184 | - var intersectionObserverOptions = { | |
| 185 | - root: null, | |
| 186 | - rootMargin: limit + 'px', | |
| 187 | - threshold: 0 | |
| 188 | - }; | |
| 189 | - | |
| 190 | - var observer = new IntersectionObserver(function(entries) { | |
| 191 | - entries.forEach(function(entry) { | |
| 192 | - if (entry.isIntersecting) { | |
| 193 | - observer.unobserve(target); | |
| 194 | - showChart($(entry.target).attr('id')); | |
| 195 | - } | |
| 196 | - }); | |
| 197 | - }, intersectionObserverOptions); | |
| 198 | - | |
| 199 | - // start observing. | |
| 200 | - observer.observe(target); | |
| 201 | - }); | |
| 202 | - } | |
| 203 | - | |
| 204 | 89 | function registerDefaultActions(){ |
| 205 | 90 | $('body').off('visualizer:action:specificchart:defaultprint').on('visualizer:action:specificchart:defaultprint', function(event, v){ |
| 206 | 91 | var iframe = $('<iframe>').attr("name", "print-visualization").attr("id", "print-visualization").css("position", "absolute"); |
| 207 | 92 | iframe.appendTo($('body')); |
| @@ -264,5 +149,5 @@ | ||
| 264 | 149 | }); |
| 265 | 150 | |
| 266 | 151 | return $(this); |
| 267 | 152 | }; |
| 268 | -})(jQuery); | |
| 153 | +})(jQuery); | |