| @@ -5,8 +5,11 @@ | ||
| 5 | 5 | (function($, visualizer){ |
| 6 | 6 | |
| 7 | 7 | function initActionsButtons(v) { |
| 8 | 8 | if($('a.visualizer-action[data-visualizer-type=copy]').length > 0) { |
| 9 | + $('a.visualizer-action[data-visualizer-type=copy]').on('click', function(e) { | |
| 10 | + e.preventDefault(); | |
| 11 | + }); | |
| 9 | 12 | var clipboard = new Clipboard('a.visualizer-action[data-visualizer-type=copy]'); // jshint ignore:line |
| 10 | 13 | clipboard.on('success', function(e) { |
| 11 | 14 | window.alert(v.i10n['copied']); |
| 12 | 15 | }); |
| @@ -15,8 +18,9 @@ | ||
| 15 | 18 | var type = $(this).attr( 'data-visualizer-type' ); |
| 16 | 19 | var chart = $(this).attr( 'data-visualizer-chart-id' ); |
| 17 | 20 | var container = $(this).attr( 'data-visualizer-container-id' ); |
| 18 | 21 | var lock = $('.visualizer-front.visualizer-front-' + chart); |
| 22 | + var mime = $(this).attr( 'data-visualizer-mime' ); | |
| 19 | 23 | lock.lock(); |
| 20 | 24 | e.preventDefault(); |
| 21 | 25 | $.ajax({ |
| 22 | 26 | url : v.rest_url.replace('#id#', chart).replace('#type#', type), |
| @@ -23,30 +27,40 @@ | ||
| 23 | 27 | success: function(data) { |
| 24 | 28 | if (data && data.data) { |
| 25 | 29 | switch(type){ |
| 26 | 30 | case 'csv': |
| 27 | - var a = document.createElement("a"); | |
| 28 | - document.body.appendChild(a); | |
| 29 | - a.style = "display: none"; | |
| 30 | - var blob = new Blob([data.data.csv], {type: $(this).attr( 'data-visualizer-mime' ) }), | |
| 31 | - url = window.URL.createObjectURL(blob); | |
| 32 | - a.href = url; | |
| 33 | - a.download = data.data.name; | |
| 34 | - a.click(); | |
| 35 | - setTimeout(function () { | |
| 36 | - window.URL.revokeObjectURL(url); | |
| 37 | - }, 100); | |
| 31 | + var blob = new Blob([data.data.csv], {type: mime }); | |
| 32 | + if(window.navigator.msSaveOrOpenBlob){ | |
| 33 | + window.navigator.msSaveOrOpenBlob(blob, data.data.name); | |
| 34 | + } else { | |
| 35 | + var url = window.URL.createObjectURL(blob); | |
| 36 | + var $a = $("<a>"); | |
| 37 | + $a.attr("href", url); | |
| 38 | + $("body").append($a); | |
| 39 | + $a.attr("download", data.data.name); | |
| 40 | + $a[0].click(); | |
| 41 | + setTimeout(function () { | |
| 42 | + window.URL.revokeObjectURL(url); | |
| 43 | + $a.remove(); | |
| 44 | + }, 100); | |
| 45 | + } | |
| 38 | 46 | break; |
| 39 | 47 | case 'xls': |
| 40 | - var $a = $("<a>"); | |
| 41 | - $a.attr("href",data.data.csv); | |
| 42 | - $("body").append($a); | |
| 43 | - $a.attr("download",data.data.name); | |
| 44 | - $a[0].click(); | |
| 45 | - $a.remove(); | |
| 48 | + if(window.navigator.msSaveOrOpenBlob){ | |
| 49 | + blob = new Blob([s2ab(atob(data.data.raw))], {type: '' }); | |
| 50 | + window.navigator.msSaveOrOpenBlob(blob, data.data.name); | |
| 51 | + } else { | |
| 52 | + var $a = $("<a>"); // jshint ignore:line | |
| 53 | + $a.attr("href", data.data.csv); | |
| 54 | + $("body").append($a); | |
| 55 | + $a.attr("download", data.data.name); | |
| 56 | + $a[0].click(); | |
| 57 | + $a.remove(); | |
| 58 | + } | |
| 46 | 59 | break; |
| 47 | 60 | case 'print': |
| 48 | - $('body').trigger('visualizer:action:specificchart', {action: 'print', id: container, data: data.data.csv}); | |
| 61 | + case 'image': | |
| 62 | + $('body').trigger('visualizer:action:specificchart', {action: type, id: container, data: data.data.csv, dataObj: data.data}); | |
| 49 | 63 | break; |
| 50 | 64 | default: |
| 51 | 65 | if(window.visualizer_perform_action) { |
| 52 | 66 | window.visualizer_perform_action(type, chart, data.data); |
| @@ -59,9 +73,42 @@ | ||
| 59 | 73 | }); |
| 60 | 74 | }); |
| 61 | 75 | } |
| 62 | 76 | |
| 77 | + function s2ab(s) { | |
| 78 | + var buf = new ArrayBuffer(s.length); | |
| 79 | + var view = new Uint8Array(buf); | |
| 80 | + for (var i=0; i !== s.length; ++i) { | |
| 81 | + view[i] = s.charCodeAt(i) & 0xFF; | |
| 82 | + } | |
| 83 | + return buf; | |
| 84 | + } | |
| 85 | + | |
| 63 | 86 | $(document).ready(function(){ |
| 87 | + | |
| 88 | + // for updating the currently displayed chart (preview mode) | |
| 89 | + window.vizUpdateChartPreview = function(){ | |
| 90 | + var event = new CustomEvent('visualizer:render:currentchart:update', {detail: {visualizer: visualizer}}); | |
| 91 | + document.body.dispatchEvent(event); | |
| 92 | + }; | |
| 93 | + | |
| 94 | + // facade loads N times in the library and front end (where N = the number of different chart libraries supported) | |
| 95 | + // so all charts are also loaded N times | |
| 96 | + // this will ensure that no matter how many times facade is loaded, it initializes all charts only once. | |
| 97 | + // fixed as part of the issue to add annotations. | |
| 98 | + if(localStorage.getItem( 'viz-facade-loaded' ) === '1'){ | |
| 99 | + // prevent library from hanging. | |
| 100 | + setTimeout( function(){ | |
| 101 | + localStorage.removeItem( 'viz-facade-loaded' ); | |
| 102 | + }, 2000); | |
| 103 | + return; | |
| 104 | + } | |
| 105 | + localStorage.setItem( 'viz-facade-loaded', '1'); | |
| 106 | + // remove the flag so that repeated loading of the library does not cause problems. | |
| 107 | + setTimeout( function(){ | |
| 108 | + localStorage.removeItem( 'viz-facade-loaded' ); | |
| 109 | + }, 2000); | |
| 110 | + | |
| 64 | 111 | $('body').trigger('visualizer:render:chart:start', visualizer); |
| 65 | 112 | initActionsButtons(visualizer); |
| 66 | 113 | registerDefaultActions(); |
| 67 | 114 | }); |
| @@ -129,5 +176,5 @@ | ||
| 129 | 176 | }); |
| 130 | 177 | |
| 131 | 178 | return $(this); |
| 132 | 179 | }; |
| 133 | -})(jQuery); | |
| 180 | +})(jQuery); | |