| @@ -1,26 +1,24 @@ | ||
| 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-chart-shortcode').length > 0) { | |
| 9 | + var clipboard1 = new ClipboardJS('a.visualizer-chart-shortcode'); // jshint ignore:line | |
| 10 | + clipboard1.on('success', function(e) { | |
| 12 | 11 | window.alert(v.i10n['copied']); |
| 13 | - | |
| 14 | 12 | }); |
| 15 | 13 | } |
| 16 | 14 | |
| 17 | - if($('a.visualizer-action[data-visualizer-type=copy]').length > 0 && vizClipboard2 === null) { | |
| 15 | + if($('a.visualizer-action[data-visualizer-type=copy]').length > 0) { | |
| 18 | 16 | $('a.visualizer-action[data-visualizer-type=copy]').on('click', function(e) { |
| 19 | 17 | e.preventDefault(); |
| 20 | 18 | }); |
| 21 | - vizClipboard2 = new ClipboardJS('a.visualizer-action[data-visualizer-type=copy]'); // jshint ignore:line | |
| 22 | - vizClipboard2.on('success', function(e) { | |
| 19 | + var clipboard = new ClipboardJS('a.visualizer-action[data-visualizer-type=copy]'); // jshint ignore:line | |
| 20 | + clipboard.on('success', function(e) { | |
| 23 | 21 | window.alert(v.i10n['copied']); |
| 24 | 22 | }); |
| 25 | 23 | } |
| 26 | 24 | $('a.visualizer-action[data-visualizer-type!=copy]').off('click').on('click', function(e) { |
| @@ -102,8 +100,25 @@ | ||
| 102 | 100 | var event = new CustomEvent('visualizer:render:currentchart:update', {detail: {visualizer: visualizer}}); |
| 103 | 101 | document.body.dispatchEvent(event); |
| 104 | 102 | }; |
| 105 | 103 | |
| 104 | + // facade loads N times in the library and front end (where N = the number of different chart libraries supported) | |
| 105 | + // so all charts are also loaded N times | |
| 106 | + // this will ensure that no matter how many times facade is loaded, it initializes all charts only once. | |
| 107 | + // fixed as part of the issue to add annotations. | |
| 108 | + if(localStorage.getItem( 'viz-facade-loaded' ) === '1'){ | |
| 109 | + // prevent library from hanging. | |
| 110 | + setTimeout( function(){ | |
| 111 | + localStorage.removeItem( 'viz-facade-loaded' ); | |
| 112 | + }, 2000); | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + localStorage.setItem( 'viz-facade-loaded', '1'); | |
| 116 | + // remove the flag so that repeated loading of the library does not cause problems. | |
| 117 | + setTimeout( function(){ | |
| 118 | + localStorage.removeItem( 'viz-facade-loaded' ); | |
| 119 | + }, 2000); | |
| 120 | + | |
| 106 | 121 | initChartDisplay(); |
| 107 | 122 | initActionsButtons(visualizer); |
| 108 | 123 | registerDefaultActions(); |
| 109 | 124 | }); |
| @@ -110,9 +125,9 @@ | ||
| 110 | 125 | |
| 111 | 126 | function initChartDisplay() { |
| 112 | 127 | if(visualizer.is_front == true){ // jshint ignore:line |
| 113 | 128 | displayChartsOnFrontEnd(); |
| 114 | - } else { | |
| 129 | + }else{ | |
| 115 | 130 | showChart(); |
| 116 | 131 | } |
| 117 | 132 | } |
| 118 | 133 | |
| @@ -120,9 +135,9 @@ | ||
| 120 | 135 | * If an `id` is provided, that particular chart will load. |
| 121 | 136 | */ |
| 122 | 137 | function showChart(id) { |
| 123 | 138 | // clone the visualizer object so that the original object is not affected. |
| 124 | - var viz = Object.assign(visualizer, window.visualizer || {}); | |
| 139 | + var viz = Object.assign({}, visualizer); | |
| 125 | 140 | if(id){ |
| 126 | 141 | viz.id = id; |
| 127 | 142 | } |
| 128 | 143 | $('body').trigger('visualizer:render:chart:start', viz); |
| @@ -128,48 +143,12 @@ | ||
| 128 | 143 | $('body').trigger('visualizer:render:chart:start', viz); |
| 129 | 144 | } |
| 130 | 145 | |
| 131 | 146 | 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); | |
| 147 | + // display all charts that are NOT to be lazy-loaded. | |
| 148 | + $('div.visualizer-front:not(.visualizer-lazy)').each(function(index, element){ | |
| 149 | + var id = $(element).attr('id'); | |
| 150 | + showChart(id); | |
| 172 | 151 | }); |
| 173 | 152 | |
| 174 | 153 | // interate through all charts that are to be lazy-loaded and observe each one. |
| 175 | 154 | $('div.visualizer-front.visualizer-lazy').each(function(index, element){ |