| @@ -1,16 +1,15 @@ | ||
| 1 | 1 | /* global console */ |
| 2 | 2 | /* global visualizer */ |
| 3 | 3 | /* global jQuery */ |
| 4 | -var vizClipboard1=null; | |
| 4 | + | |
| 5 | 5 | (function($, visualizer){ |
| 6 | - | |
| 6 | + | |
| 7 | 7 | function initActionsButtons(v) { |
| 8 | - if($('a.visualizer-chart-shortcode').length > 0 && vizClipboard1 === null) { | |
| 9 | - vizClipboard1 = new ClipboardJS('a.visualizer-chart-shortcode'); // jshint ignore:line | |
| 10 | - 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) { | |
| 11 | 11 | window.alert(v.i10n['copied']); |
| 12 | - | |
| 13 | 12 | }); |
| 14 | 13 | } |
| 15 | 14 | |
| 16 | 15 | if($('a.visualizer-action[data-visualizer-type=copy]').length > 0) { |
| @@ -101,8 +100,25 @@ | ||
| 101 | 100 | var event = new CustomEvent('visualizer:render:currentchart:update', {detail: {visualizer: visualizer}}); |
| 102 | 101 | document.body.dispatchEvent(event); |
| 103 | 102 | }; |
| 104 | 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 | + | |
| 105 | 121 | initChartDisplay(); |
| 106 | 122 | initActionsButtons(visualizer); |
| 107 | 123 | registerDefaultActions(); |
| 108 | 124 | }); |
| @@ -109,9 +125,9 @@ | ||
| 109 | 125 | |
| 110 | 126 | function initChartDisplay() { |
| 111 | 127 | if(visualizer.is_front == true){ // jshint ignore:line |
| 112 | 128 | displayChartsOnFrontEnd(); |
| 113 | - } else { | |
| 129 | + }else{ | |
| 114 | 130 | showChart(); |
| 115 | 131 | } |
| 116 | 132 | } |
| 117 | 133 | |
| @@ -119,9 +135,9 @@ | ||
| 119 | 135 | * If an `id` is provided, that particular chart will load. |
| 120 | 136 | */ |
| 121 | 137 | function showChart(id) { |
| 122 | 138 | // clone the visualizer object so that the original object is not affected. |
| 123 | - var viz = Object.assign(visualizer, window.visualizer || {}); | |
| 139 | + var viz = Object.assign({}, visualizer); | |
| 124 | 140 | if(id){ |
| 125 | 141 | viz.id = id; |
| 126 | 142 | } |
| 127 | 143 | $('body').trigger('visualizer:render:chart:start', viz); |
| @@ -127,22 +143,12 @@ | ||
| 127 | 143 | $('body').trigger('visualizer:render:chart:start', viz); |
| 128 | 144 | } |
| 129 | 145 | |
| 130 | 146 | function displayChartsOnFrontEnd() { |
| 131 | - | |
| 132 | - $('div.visualizer-front:not(.viz-facade-loaded):not(.visualizer-lazy):not(.visualizer-cw-error):empty').each(function(index, element){ | |
| 133 | - | |
| 134 | - // Do not render charts that are intentionally hidden. | |
| 135 | - var style = window.getComputedStyle(element); | |
| 136 | - if (style.display === 'none' || style.visibility === 'hidden') { | |
| 137 | - return; | |
| 138 | - } | |
| 139 | - | |
| 140 | - var id = $(element).addClass('viz-facade-loaded').attr('id'); | |
| 141 | - setTimeout(function(){ | |
| 142 | - // Add a short delay between each chart to avoid overloading the browser event loop. | |
| 143 | - showChart(id); | |
| 144 | - }, ( 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); | |
| 145 | 151 | }); |
| 146 | 152 | |
| 147 | 153 | // interate through all charts that are to be lazy-loaded and observe each one. |
| 148 | 154 | $('div.visualizer-front.visualizer-lazy').each(function(index, element){ |