| @@ -4,58 +4,73 @@ | ||
| 4 | 4 | |
| 5 | 5 | (function($, visualizer){ |
| 6 | 6 | |
| 7 | 7 | function initActionsButtons(v) { |
| 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 | + window.alert(v.i10n['copied']); | |
| 12 | + }); | |
| 13 | + } | |
| 14 | + | |
| 8 | 15 | 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 | |
| 16 | + $('a.visualizer-action[data-visualizer-type=copy]').on('click', function(e) { | |
| 17 | + e.preventDefault(); | |
| 18 | + }); | |
| 19 | + var clipboard = new ClipboardJS('a.visualizer-action[data-visualizer-type=copy]'); // jshint ignore:line | |
| 10 | 20 | clipboard.on('success', function(e) { |
| 11 | 21 | window.alert(v.i10n['copied']); |
| 12 | 22 | }); |
| 13 | 23 | } |
| 14 | - $('a.visualizer-action[data-visualizer-type!=copy]').on('click', function(e) { | |
| 24 | + $('a.visualizer-action[data-visualizer-type!=copy]').off('click').on('click', function(e) { | |
| 15 | 25 | var type = $(this).attr( 'data-visualizer-type' ); |
| 16 | 26 | var chart = $(this).attr( 'data-visualizer-chart-id' ); |
| 27 | + var container = $(this).attr( 'data-visualizer-container-id' ); | |
| 17 | 28 | var lock = $('.visualizer-front.visualizer-front-' + chart); |
| 29 | + var mime = $(this).attr( 'data-visualizer-mime' ); | |
| 18 | 30 | lock.lock(); |
| 19 | 31 | e.preventDefault(); |
| 20 | 32 | $.ajax({ |
| 21 | 33 | url : v.rest_url.replace('#id#', chart).replace('#type#', type), |
| 34 | + beforeSend: function ( xhr ) { | |
| 35 | + xhr.setRequestHeader( 'X-WP-Nonce', v.wp_nonce ); | |
| 36 | + }, | |
| 22 | 37 | success: function(data) { |
| 23 | 38 | if (data && data.data) { |
| 24 | 39 | switch(type){ |
| 25 | 40 | case 'csv': |
| 26 | - var a = document.createElement("a"); | |
| 27 | - document.body.appendChild(a); | |
| 28 | - a.style = "display: none"; | |
| 29 | - var blob = new Blob([data.data.csv], {type: $(this).attr( 'data-visualizer-mime' ) }), | |
| 30 | - url = window.URL.createObjectURL(blob); | |
| 31 | - a.href = url; | |
| 32 | - a.download = data.data.name; | |
| 33 | - a.click(); | |
| 34 | - setTimeout(function () { | |
| 35 | - window.URL.revokeObjectURL(url); | |
| 36 | - }, 100); | |
| 41 | + var blob = new Blob([data.data.csv], {type: mime }); | |
| 42 | + if(window.navigator.msSaveOrOpenBlob){ | |
| 43 | + window.navigator.msSaveOrOpenBlob(blob, data.data.name); | |
| 44 | + } else { | |
| 45 | + var url = window.URL.createObjectURL(blob); | |
| 46 | + var $a = $("<a>"); | |
| 47 | + $a.attr("href", url); | |
| 48 | + $("body").append($a); | |
| 49 | + $a.attr("download", data.data.name); | |
| 50 | + $a[0].click(); | |
| 51 | + setTimeout(function () { | |
| 52 | + window.URL.revokeObjectURL(url); | |
| 53 | + $a.remove(); | |
| 54 | + }, 100); | |
| 55 | + } | |
| 37 | 56 | break; |
| 38 | 57 | case 'xls': |
| 39 | - var $a = $("<a>"); | |
| 40 | - $a.attr("href",data.data.csv); | |
| 41 | - $("body").append($a); | |
| 42 | - $a.attr("download",data.data.name); | |
| 43 | - $a[0].click(); | |
| 44 | - $a.remove(); | |
| 58 | + if(window.navigator.msSaveOrOpenBlob){ | |
| 59 | + blob = new Blob([s2ab(atob(data.data.raw))], {type: '' }); | |
| 60 | + window.navigator.msSaveOrOpenBlob(blob, data.data.name); | |
| 61 | + } else { | |
| 62 | + var $a = $("<a>"); // jshint ignore:line | |
| 63 | + $a.attr("href", data.data.csv); | |
| 64 | + $("body").append($a); | |
| 65 | + $a.attr("download", data.data.name); | |
| 66 | + $a[0].click(); | |
| 67 | + $a.remove(); | |
| 68 | + } | |
| 45 | 69 | break; |
| 46 | 70 | case 'print': |
| 47 | - var iframe = $('<iframe>').attr("name", "print-visualization").attr("id", "print-visualization").css("position", "absolute"); | |
| 48 | - iframe.appendTo($('body')); | |
| 49 | - var iframe_doc = iframe.get(0).contentWindow || iframe.get(0).contentDocument.document || iframe.get(0).contentDocument; | |
| 50 | - iframe_doc.document.open(); | |
| 51 | - iframe_doc.document.write(data.data.csv); | |
| 52 | - iframe_doc.document.close(); | |
| 53 | - setTimeout(function(){ | |
| 54 | - window.frames['print-visualization'].focus(); | |
| 55 | - window.frames['print-visualization'].print(); | |
| 56 | - iframe.remove(); | |
| 57 | - }, 500); | |
| 71 | + case 'image': | |
| 72 | + $('body').trigger('visualizer:action:specificchart', {action: type, id: container, data: data.data.csv, dataObj: data.data}); | |
| 58 | 73 | break; |
| 59 | 74 | default: |
| 60 | 75 | if(window.visualizer_perform_action) { |
| 61 | 76 | window.visualizer_perform_action(type, chart, data.data); |
| @@ -68,12 +83,119 @@ | ||
| 68 | 83 | }); |
| 69 | 84 | }); |
| 70 | 85 | } |
| 71 | 86 | |
| 87 | + function s2ab(s) { | |
| 88 | + var buf = new ArrayBuffer(s.length); | |
| 89 | + var view = new Uint8Array(buf); | |
| 90 | + for (var i=0; i !== s.length; ++i) { | |
| 91 | + view[i] = s.charCodeAt(i) & 0xFF; | |
| 92 | + } | |
| 93 | + return buf; | |
| 94 | + } | |
| 95 | + | |
| 72 | 96 | $(document).ready(function(){ |
| 73 | - $('body').trigger('visualizer:render:chart:start', visualizer); | |
| 97 | + | |
| 98 | + // for updating the currently displayed chart (preview mode) | |
| 99 | + window.vizUpdateChartPreview = function(){ | |
| 100 | + var event = new CustomEvent('visualizer:render:currentchart:update', {detail: {visualizer: visualizer}}); | |
| 101 | + document.body.dispatchEvent(event); | |
| 102 | + }; | |
| 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 | + | |
| 121 | + initChartDisplay(); | |
| 74 | 122 | initActionsButtons(visualizer); |
| 123 | + registerDefaultActions(); | |
| 75 | 124 | }); |
| 125 | + | |
| 126 | + function initChartDisplay() { | |
| 127 | + if(visualizer.is_front == true){ // jshint ignore:line | |
| 128 | + displayChartsOnFrontEnd(); | |
| 129 | + }else{ | |
| 130 | + showChart(); | |
| 131 | + } | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * If an `id` is provided, that particular chart will load. | |
| 136 | + */ | |
| 137 | + function showChart(id) { | |
| 138 | + // clone the visualizer object so that the original object is not affected. | |
| 139 | + var viz = Object.assign({}, visualizer); | |
| 140 | + if(id){ | |
| 141 | + viz.id = id; | |
| 142 | + } | |
| 143 | + $('body').trigger('visualizer:render:chart:start', viz); | |
| 144 | + } | |
| 145 | + | |
| 146 | + function displayChartsOnFrontEnd() { | |
| 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); | |
| 151 | + }); | |
| 152 | + | |
| 153 | + // interate through all charts that are to be lazy-loaded and observe each one. | |
| 154 | + $('div.visualizer-front.visualizer-lazy').each(function(index, element){ | |
| 155 | + var id = $(element).attr('id'); | |
| 156 | + var limit = $(element).attr('data-lazy-limit'); | |
| 157 | + if(!limit){ | |
| 158 | + limit = 300; | |
| 159 | + } | |
| 160 | + var target = document.querySelector('#' + id); | |
| 161 | + | |
| 162 | + // configure the intersection observer instance | |
| 163 | + var intersectionObserverOptions = { | |
| 164 | + root: null, | |
| 165 | + rootMargin: limit + 'px', | |
| 166 | + threshold: 0 | |
| 167 | + }; | |
| 168 | + | |
| 169 | + var observer = new IntersectionObserver(function(entries) { | |
| 170 | + entries.forEach(function(entry) { | |
| 171 | + if (entry.isIntersecting) { | |
| 172 | + observer.unobserve(target); | |
| 173 | + showChart($(entry.target).attr('id')); | |
| 174 | + } | |
| 175 | + }); | |
| 176 | + }, intersectionObserverOptions); | |
| 177 | + | |
| 178 | + // start observing. | |
| 179 | + observer.observe(target); | |
| 180 | + }); | |
| 181 | + } | |
| 182 | + | |
| 183 | + function registerDefaultActions(){ | |
| 184 | + $('body').off('visualizer:action:specificchart:defaultprint').on('visualizer:action:specificchart:defaultprint', function(event, v){ | |
| 185 | + var iframe = $('<iframe>').attr("name", "print-visualization").attr("id", "print-visualization").css("position", "absolute"); | |
| 186 | + iframe.appendTo($('body')); | |
| 187 | + var iframe_doc = iframe.get(0).contentWindow || iframe.get(0).contentDocument.document || iframe.get(0).contentDocument; | |
| 188 | + iframe_doc.document.open(); | |
| 189 | + iframe_doc.document.write(v.data); | |
| 190 | + iframe_doc.document.close(); | |
| 191 | + setTimeout(function(){ | |
| 192 | + window.frames['print-visualization'].focus(); | |
| 193 | + window.frames['print-visualization'].print(); | |
| 194 | + iframe.remove(); | |
| 195 | + }, 500); | |
| 196 | + }); | |
| 197 | + } | |
| 76 | 198 | })(jQuery, visualizer); |
| 77 | 199 | |
| 78 | 200 | (function ($) { |
| 79 | 201 | $.fn.lock = function () { |
| @@ -121,5 +243,5 @@ | ||
| 121 | 243 | }); |
| 122 | 244 | |
| 123 | 245 | return $(this); |
| 124 | 246 | }; |
| 125 | -})(jQuery); | |
| 247 | +})(jQuery); | |