PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.5
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.5
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
visualizer / js / render-facade.js

render-facade.js in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.4.5, at js/render-facade.js

245 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 Clipboard('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 Clipboard('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 success: function(data) {
35 if (data && data.data) {
36 switch(type){
37 case 'csv':
38 var blob = new Blob([data.data.csv], {type: mime });
39 if(window.navigator.msSaveOrOpenBlob){
40 window.navigator.msSaveOrOpenBlob(blob, data.data.name);
41 } else {
42 var url = window.URL.createObjectURL(blob);
43 var $a = $("<a>");
44 $a.attr("href", url);
45 $("body").append($a);
46 $a.attr("download", data.data.name);
47 $a[0].click();
48 setTimeout(function () {
49 window.URL.revokeObjectURL(url);
50 $a.remove();
51 }, 100);
52 }
53 break;
54 case 'xls':
55 if(window.navigator.msSaveOrOpenBlob){
56 blob = new Blob([s2ab(atob(data.data.raw))], {type: '' });
57 window.navigator.msSaveOrOpenBlob(blob, data.data.name);
58 } else {
59 var $a = $("<a>"); // jshint ignore:line
60 $a.attr("href", data.data.csv);
61 $("body").append($a);
62 $a.attr("download", data.data.name);
63 $a[0].click();
64 $a.remove();
65 }
66 break;
67 case 'print':
68 case 'image':
69 $('body').trigger('visualizer:action:specificchart', {action: type, id: container, data: data.data.csv, dataObj: data.data});
70 break;
71 default:
72 if(window.visualizer_perform_action) {
73 window.visualizer_perform_action(type, chart, data.data);
74 }
75 break;
76 }
77 }
78 lock.unlock();
79 }
80 });
81 });
82 }
83
84 function s2ab(s) {
85 var buf = new ArrayBuffer(s.length);
86 var view = new Uint8Array(buf);
87 for (var i=0; i !== s.length; ++i) {
88 view[i] = s.charCodeAt(i) & 0xFF;
89 }
90 return buf;
91 }
92
93 $(document).ready(function(){
94
95 // for updating the currently displayed chart (preview mode)
96 window.vizUpdateChartPreview = function(){
97 var event = new CustomEvent('visualizer:render:currentchart:update', {detail: {visualizer: visualizer}});
98 document.body.dispatchEvent(event);
99 };
100
101 // facade loads N times in the library and front end (where N = the number of different chart libraries supported)
102 // so all charts are also loaded N times
103 // this will ensure that no matter how many times facade is loaded, it initializes all charts only once.
104 // fixed as part of the issue to add annotations.
105 if(localStorage.getItem( 'viz-facade-loaded' ) === '1'){
106 // prevent library from hanging.
107 setTimeout( function(){
108 localStorage.removeItem( 'viz-facade-loaded' );
109 }, 2000);
110 return;
111 }
112 localStorage.setItem( 'viz-facade-loaded', '1');
113 // remove the flag so that repeated loading of the library does not cause problems.
114 setTimeout( function(){
115 localStorage.removeItem( 'viz-facade-loaded' );
116 }, 2000);
117
118 initChartDisplay();
119 initActionsButtons(visualizer);
120 registerDefaultActions();
121 });
122
123 function initChartDisplay() {
124 if(visualizer.is_front == true){ // jshint ignore:line
125 displayChartsOnFrontEnd();
126 }else{
127 showChart();
128 }
129 }
130
131 /**
132 * If an `id` is provided, that particular chart will load.
133 */
134 function showChart(id) {
135 // clone the visualizer object so that the original object is not affected.
136 var viz = Object.assign({}, visualizer);
137 if(id){
138 viz.id = id;
139 }
140 $('body').trigger('visualizer:render:chart:start', viz);
141 }
142
143 function displayChartsOnFrontEnd() {
144 // display all charts that are NOT to be lazy-loaded.
145 $('div.visualizer-front:not(.visualizer-lazy)').each(function(index, element){
146 var id = $(element).attr('id');
147 showChart(id);
148 });
149
150 // interate through all charts that are to be lazy-loaded and observe each one.
151 $('div.visualizer-front.visualizer-lazy').each(function(index, element){
152 var id = $(element).attr('id');
153 var limit = $(element).attr('data-lazy-limit');
154 if(!limit){
155 limit = 300;
156 }
157 var target = document.querySelector('#' + id);
158
159 // configure the intersection observer instance
160 var intersectionObserverOptions = {
161 root: null,
162 rootMargin: limit + 'px',
163 threshold: 0
164 };
165
166 var observer = new IntersectionObserver(function(entries) {
167 entries.forEach(function(entry) {
168 if (entry.isIntersecting) {
169 observer.unobserve(target);
170 showChart($(entry.target).attr('id'));
171 }
172 });
173 }, intersectionObserverOptions);
174
175 // start observing.
176 observer.observe(target);
177 });
178 }
179
180 function registerDefaultActions(){
181 $('body').off('visualizer:action:specificchart:defaultprint').on('visualizer:action:specificchart:defaultprint', function(event, v){
182 var iframe = $('<iframe>').attr("name", "print-visualization").attr("id", "print-visualization").css("position", "absolute");
183 iframe.appendTo($('body'));
184 var iframe_doc = iframe.get(0).contentWindow || iframe.get(0).contentDocument.document || iframe.get(0).contentDocument;
185 iframe_doc.document.open();
186 iframe_doc.document.write(v.data);
187 iframe_doc.document.close();
188 setTimeout(function(){
189 window.frames['print-visualization'].focus();
190 window.frames['print-visualization'].print();
191 iframe.remove();
192 }, 500);
193 });
194 }
195 })(jQuery, visualizer);
196
197 (function ($) {
198 $.fn.lock = function () {
199 $(this).each(function () {
200 var $this = $(this);
201 var position = $this.css('position');
202
203 if (!position) {
204 position = 'static';
205 }
206
207 switch (position) {
208 case 'absolute':
209 case 'relative':
210 break;
211 default:
212 $this.css('position', 'relative');
213 break;
214 }
215 $this.data('position', position);
216
217 var width = $this.width(),
218 height = $this.height();
219
220 var locker = $('<div class="locker"></div>');
221 locker.width(width).height(height);
222
223 var loader = $('<div class="locker-loader"></div>');
224 loader.width(width).height(height);
225
226 locker.append(loader);
227 $this.append(locker);
228 $(window).resize(function () {
229 $this.find('.locker,.locker-loader').width($this.width()).height($this.height());
230 });
231 });
232
233 return $(this);
234 };
235
236 $.fn.unlock = function () {
237 $(this).each(function () {
238 $(this).find('.locker').remove();
239 $(this).css('position', $(this).data('position'));
240 });
241
242 return $(this);
243 };
244 })(jQuery);
245