| 1 |
/* global console */ |
| 2 |
/* global visualizer */ |
| 3 |
/* global jQuery */ |
| 4 |
|
| 5 |
(function($, visualizer){ |
| 6 |
|
| 7 |
function initActionsButtons(v) { |
| 8 |
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 |
| 10 |
clipboard.on('success', function(e) { |
| 11 |
window.alert(v.i10n['copied']); |
| 12 |
}); |
| 13 |
} |
| 14 |
$('a.visualizer-action[data-visualizer-type!=copy]').off('click').on('click', function(e) { |
| 15 |
var type = $(this).attr( 'data-visualizer-type' ); |
| 16 |
var chart = $(this).attr( 'data-visualizer-chart-id' ); |
| 17 |
var container = $(this).attr( 'data-visualizer-container-id' ); |
| 18 |
var lock = $('.visualizer-front.visualizer-front-' + chart); |
| 19 |
lock.lock(); |
| 20 |
e.preventDefault(); |
| 21 |
$.ajax({ |
| 22 |
url : v.rest_url.replace('#id#', chart).replace('#type#', type), |
| 23 |
success: function(data) { |
| 24 |
if (data && data.data) { |
| 25 |
switch(type){ |
| 26 |
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); |
| 38 |
break; |
| 39 |
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(); |
| 46 |
break; |
| 47 |
case 'print': |
| 48 |
$('body').trigger('visualizer:action:specificchart', {action: 'print', id: container, data: data.data.csv}); |
| 49 |
break; |
| 50 |
default: |
| 51 |
if(window.visualizer_perform_action) { |
| 52 |
window.visualizer_perform_action(type, chart, data.data); |
| 53 |
} |
| 54 |
break; |
| 55 |
} |
| 56 |
} |
| 57 |
lock.unlock(); |
| 58 |
} |
| 59 |
}); |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
$(document).ready(function(){ |
| 64 |
$('body').trigger('visualizer:render:chart:start', visualizer); |
| 65 |
initActionsButtons(visualizer); |
| 66 |
registerDefaultActions(); |
| 67 |
}); |
| 68 |
|
| 69 |
function registerDefaultActions(){ |
| 70 |
$('body').off('visualizer:action:specificchart:defaultprint').on('visualizer:action:specificchart:defaultprint', function(event, v){ |
| 71 |
var iframe = $('<iframe>').attr("name", "print-visualization").attr("id", "print-visualization").css("position", "absolute"); |
| 72 |
iframe.appendTo($('body')); |
| 73 |
var iframe_doc = iframe.get(0).contentWindow || iframe.get(0).contentDocument.document || iframe.get(0).contentDocument; |
| 74 |
iframe_doc.document.open(); |
| 75 |
iframe_doc.document.write(v.data); |
| 76 |
iframe_doc.document.close(); |
| 77 |
setTimeout(function(){ |
| 78 |
window.frames['print-visualization'].focus(); |
| 79 |
window.frames['print-visualization'].print(); |
| 80 |
iframe.remove(); |
| 81 |
}, 500); |
| 82 |
}); |
| 83 |
} |
| 84 |
})(jQuery, visualizer); |
| 85 |
|
| 86 |
(function ($) { |
| 87 |
$.fn.lock = function () { |
| 88 |
$(this).each(function () { |
| 89 |
var $this = $(this); |
| 90 |
var position = $this.css('position'); |
| 91 |
|
| 92 |
if (!position) { |
| 93 |
position = 'static'; |
| 94 |
} |
| 95 |
|
| 96 |
switch (position) { |
| 97 |
case 'absolute': |
| 98 |
case 'relative': |
| 99 |
break; |
| 100 |
default: |
| 101 |
$this.css('position', 'relative'); |
| 102 |
break; |
| 103 |
} |
| 104 |
$this.data('position', position); |
| 105 |
|
| 106 |
var width = $this.width(), |
| 107 |
height = $this.height(); |
| 108 |
|
| 109 |
var locker = $('<div class="locker"></div>'); |
| 110 |
locker.width(width).height(height); |
| 111 |
|
| 112 |
var loader = $('<div class="locker-loader"></div>'); |
| 113 |
loader.width(width).height(height); |
| 114 |
|
| 115 |
locker.append(loader); |
| 116 |
$this.append(locker); |
| 117 |
$(window).resize(function () { |
| 118 |
$this.find('.locker,.locker-loader').width($this.width()).height($this.height()); |
| 119 |
}); |
| 120 |
}); |
| 121 |
|
| 122 |
return $(this); |
| 123 |
}; |
| 124 |
|
| 125 |
$.fn.unlock = function () { |
| 126 |
$(this).each(function () { |
| 127 |
$(this).find('.locker').remove(); |
| 128 |
$(this).css('position', $(this).data('position')); |
| 129 |
}); |
| 130 |
|
| 131 |
return $(this); |
| 132 |
}; |
| 133 |
})(jQuery); |