| 1 |
/* global console */ |
| 2 |
/* global visualizer */ |
| 3 |
/* global jQuery */ |
| 4 |
|
| 5 |
(function($, visualizer){ |
| 6 |
|
| 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 |
|
| 15 |
if($('a.visualizer-action[data-visualizer-type=copy]').length > 0) { |
| 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 |
| 20 |
clipboard.on('success', function(e) { |
| 21 |
window.alert(v.i10n['copied']); |
| 22 |
}); |
| 23 |
} |
| 24 |
$('a.visualizer-action[data-visualizer-type!=copy]').off('click').on('click', function(e) { |
| 25 |
var type = $(this).attr( 'data-visualizer-type' ); |
| 26 |
var chart = $(this).attr( 'data-visualizer-chart-id' ); |
| 27 |
var container = $(this).attr( 'data-visualizer-container-id' ); |
| 28 |
var lock = $('.visualizer-front.visualizer-front-' + chart); |
| 29 |
var mime = $(this).attr( 'data-visualizer-mime' ); |
| 30 |
lock.lock(); |
| 31 |
e.preventDefault(); |
| 32 |
$.ajax({ |
| 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 |
}, |
| 37 |
success: function(data) { |
| 38 |
if (data && data.data) { |
| 39 |
switch(type){ |
| 40 |
case 'csv': |
| 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 |
} |
| 56 |
break; |
| 57 |
case 'xls': |
| 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 |
} |
| 69 |
break; |
| 70 |
case 'print': |
| 71 |
case 'image': |
| 72 |
$('body').trigger('visualizer:action:specificchart', {action: type, id: container, data: data.data.csv, dataObj: data.data}); |
| 73 |
break; |
| 74 |
default: |
| 75 |
if(window.visualizer_perform_action) { |
| 76 |
window.visualizer_perform_action(type, chart, data.data); |
| 77 |
} |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
lock.unlock(); |
| 82 |
} |
| 83 |
}); |
| 84 |
}); |
| 85 |
} |
| 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 |
|
| 96 |
$(document).ready(function(){ |
| 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 |
initChartDisplay(); |
| 105 |
initActionsButtons(visualizer); |
| 106 |
registerDefaultActions(); |
| 107 |
}); |
| 108 |
|
| 109 |
function initChartDisplay() { |
| 110 |
if(visualizer.is_front == true){ // jshint ignore:line |
| 111 |
displayChartsOnFrontEnd(); |
| 112 |
}else{ |
| 113 |
showChart(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* If an `id` is provided, that particular chart will load. |
| 119 |
*/ |
| 120 |
function showChart(id) { |
| 121 |
// clone the visualizer object so that the original object is not affected. |
| 122 |
var viz = Object.assign({}, visualizer); |
| 123 |
if(id){ |
| 124 |
viz.id = id; |
| 125 |
} |
| 126 |
$('body').trigger('visualizer:render:chart:start', viz); |
| 127 |
} |
| 128 |
|
| 129 |
function displayChartsOnFrontEnd() { |
| 130 |
// display all charts that are NOT to be lazy-loaded. |
| 131 |
$('div.visualizer-front:not(.visualizer-lazy):not(.viz-facade-loaded)').each(function(index, element){ |
| 132 |
var id = $(element).addClass('viz-facade-loaded').attr('id'); |
| 133 |
showChart(id); |
| 134 |
}); |
| 135 |
|
| 136 |
// interate through all charts that are to be lazy-loaded and observe each one. |
| 137 |
$('div.visualizer-front.visualizer-lazy').each(function(index, element){ |
| 138 |
var id = $(element).attr('id'); |
| 139 |
var limit = $(element).attr('data-lazy-limit'); |
| 140 |
if(!limit){ |
| 141 |
limit = 300; |
| 142 |
} |
| 143 |
var target = document.querySelector('#' + id); |
| 144 |
|
| 145 |
// configure the intersection observer instance |
| 146 |
var intersectionObserverOptions = { |
| 147 |
root: null, |
| 148 |
rootMargin: limit + 'px', |
| 149 |
threshold: 0 |
| 150 |
}; |
| 151 |
|
| 152 |
var observer = new IntersectionObserver(function(entries) { |
| 153 |
entries.forEach(function(entry) { |
| 154 |
if (entry.isIntersecting) { |
| 155 |
observer.unobserve(target); |
| 156 |
showChart($(entry.target).attr('id')); |
| 157 |
} |
| 158 |
}); |
| 159 |
}, intersectionObserverOptions); |
| 160 |
|
| 161 |
// start observing. |
| 162 |
observer.observe(target); |
| 163 |
}); |
| 164 |
} |
| 165 |
|
| 166 |
function registerDefaultActions(){ |
| 167 |
$('body').off('visualizer:action:specificchart:defaultprint').on('visualizer:action:specificchart:defaultprint', function(event, v){ |
| 168 |
var iframe = $('<iframe>').attr("name", "print-visualization").attr("id", "print-visualization").css("position", "absolute"); |
| 169 |
iframe.appendTo($('body')); |
| 170 |
var iframe_doc = iframe.get(0).contentWindow || iframe.get(0).contentDocument.document || iframe.get(0).contentDocument; |
| 171 |
iframe_doc.document.open(); |
| 172 |
iframe_doc.document.write(v.data); |
| 173 |
iframe_doc.document.close(); |
| 174 |
setTimeout(function(){ |
| 175 |
window.frames['print-visualization'].focus(); |
| 176 |
window.frames['print-visualization'].print(); |
| 177 |
iframe.remove(); |
| 178 |
}, 500); |
| 179 |
}); |
| 180 |
} |
| 181 |
})(jQuery, visualizer); |
| 182 |
|
| 183 |
(function ($) { |
| 184 |
$.fn.lock = function () { |
| 185 |
$(this).each(function () { |
| 186 |
var $this = $(this); |
| 187 |
var position = $this.css('position'); |
| 188 |
|
| 189 |
if (!position) { |
| 190 |
position = 'static'; |
| 191 |
} |
| 192 |
|
| 193 |
switch (position) { |
| 194 |
case 'absolute': |
| 195 |
case 'relative': |
| 196 |
break; |
| 197 |
default: |
| 198 |
$this.css('position', 'relative'); |
| 199 |
break; |
| 200 |
} |
| 201 |
$this.data('position', position); |
| 202 |
|
| 203 |
var width = $this.width(), |
| 204 |
height = $this.height(); |
| 205 |
|
| 206 |
var locker = $('<div class="locker"></div>'); |
| 207 |
locker.width(width).height(height); |
| 208 |
|
| 209 |
var loader = $('<div class="locker-loader"></div>'); |
| 210 |
loader.width(width).height(height); |
| 211 |
|
| 212 |
locker.append(loader); |
| 213 |
$this.append(locker); |
| 214 |
$(window).resize(function () { |
| 215 |
$this.find('.locker,.locker-loader').width($this.width()).height($this.height()); |
| 216 |
}); |
| 217 |
}); |
| 218 |
|
| 219 |
return $(this); |
| 220 |
}; |
| 221 |
|
| 222 |
$.fn.unlock = function () { |
| 223 |
$(this).each(function () { |
| 224 |
$(this).find('.locker').remove(); |
| 225 |
$(this).css('position', $(this).data('position')); |
| 226 |
}); |
| 227 |
|
| 228 |
return $(this); |
| 229 |
}; |
| 230 |
})(jQuery); |
| 231 |
|