PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.11.15
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.11.15
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.11.15, at js/render-facade.js

256 lines 10.2 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 var vizClipboard1=null;
5 (function($, visualizer){
6
7 function initActionsButtons(v) {
8 if($('a.visualizer-chart-shortcode').length > 0 && vizClipboard1 === null) {
9 vizClipboard1 = new ClipboardJS('a.visualizer-chart-shortcode'); // jshint ignore:line
10 vizClipboard1.on('success', function(e) {
11 window.alert(v.i10n['copied']);
12
13 });
14 }
15
16 if($('a.visualizer-action[data-visualizer-type=copy]').length > 0) {
17 $('a.visualizer-action[data-visualizer-type=copy]').on('click', function(e) {
18 e.preventDefault();
19 });
20 var clipboard = new ClipboardJS('a.visualizer-action[data-visualizer-type=copy]'); // jshint ignore:line
21 clipboard.on('success', function(e) {
22 window.alert(v.i10n['copied']);
23 });
24 }
25 $('a.visualizer-action[data-visualizer-type!=copy]').off('click').on('click', function(e) {
26 var type = $(this).attr( 'data-visualizer-type' );
27 var chart = $(this).attr( 'data-visualizer-chart-id' );
28 var container = $(this).attr( 'data-visualizer-container-id' );
29 var lock = $('.visualizer-front.visualizer-front-' + chart);
30 var mime = $(this).attr( 'data-visualizer-mime' );
31 lock.lock();
32 e.preventDefault();
33 $.ajax({
34 url : v.rest_url.replace('#id#', chart).replace('#type#', type),
35 beforeSend: function ( xhr ) {
36 xhr.setRequestHeader( 'X-WP-Nonce', v.wp_nonce );
37 },
38 success: function(data) {
39 if (data && data.data) {
40 switch(type){
41 case 'csv':
42 var blob = new Blob([data.data.csv], {type: mime });
43 if(window.navigator.msSaveOrOpenBlob){
44 window.navigator.msSaveOrOpenBlob(blob, data.data.name);
45 } else {
46 var url = window.URL.createObjectURL(blob);
47 var $a = $("<a>");
48 $a.attr("href", url);
49 $("body").append($a);
50 $a.attr("download", data.data.name);
51 $a[0].click();
52 setTimeout(function () {
53 window.URL.revokeObjectURL(url);
54 $a.remove();
55 }, 100);
56 }
57 break;
58 case 'xls':
59 if(window.navigator.msSaveOrOpenBlob){
60 blob = new Blob([s2ab(atob(data.data.raw))], {type: '' });
61 window.navigator.msSaveOrOpenBlob(blob, data.data.name);
62 } else {
63 var $a = $("<a>"); // jshint ignore:line
64 $a.attr("href", data.data.csv);
65 $("body").append($a);
66 $a.attr("download", data.data.name);
67 $a[0].click();
68 $a.remove();
69 }
70 break;
71 case 'print':
72 case 'image':
73 $('body').trigger('visualizer:action:specificchart', {action: type, id: container, data: data.data.csv, dataObj: data.data});
74 break;
75 default:
76 if(window.visualizer_perform_action) {
77 window.visualizer_perform_action(type, chart, data.data);
78 }
79 break;
80 }
81 }
82 lock.unlock();
83 }
84 });
85 });
86 }
87
88 function s2ab(s) {
89 var buf = new ArrayBuffer(s.length);
90 var view = new Uint8Array(buf);
91 for (var i=0; i !== s.length; ++i) {
92 view[i] = s.charCodeAt(i) & 0xFF;
93 }
94 return buf;
95 }
96
97 $(document).ready(function(){
98
99 // for updating the currently displayed chart (preview mode)
100 window.vizUpdateChartPreview = function(){
101 var event = new CustomEvent('visualizer:render:currentchart:update', {detail: {visualizer: visualizer}});
102 document.body.dispatchEvent(event);
103 };
104
105 initChartDisplay();
106 initActionsButtons(visualizer);
107 registerDefaultActions();
108 });
109
110 function initChartDisplay() {
111 if(visualizer.is_front == true){ // jshint ignore:line
112 displayChartsOnFrontEnd();
113 } else {
114 showChart();
115 }
116 }
117
118 /**
119 * If an `id` is provided, that particular chart will load.
120 */
121 function showChart(id) {
122 // clone the visualizer object so that the original object is not affected.
123 var viz = Object.assign(visualizer, window.visualizer || {});
124 if(id){
125 viz.id = id;
126 }
127 $('body').trigger('visualizer:render:chart:start', viz);
128 }
129
130 function displayChartsOnFrontEnd() {
131 $(window).on('scroll', function() {
132 $('div.visualizer-front:not(.viz-facade-loaded):not(.visualizer-lazy):not(.visualizer-cw-error):empty').each(function(index, element){
133 // Do not render charts that are intentionally hidden.
134 const style = window.getComputedStyle(element);
135 if (style.display === 'none' || style.visibility === 'hidden') {
136 return;
137 }
138
139 const id = $(element).addClass('viz-facade-loaded').attr('id');
140 setTimeout(function(){
141 // Add a short delay between each chart to avoid overloading the browser event loop.
142 showChart(id);
143 }, ( index + 1 ) * 100);
144 });
145 });
146
147 $('div.visualizer-front-container:not(.visualizer-lazy-render)').each(function(index, element){
148 // Do not render charts that are intentionally hidden.
149 const style = window.getComputedStyle($(element).find('.visualizer-front')[0]);
150 if (style.display === 'none' || style.visibility === 'hidden') {
151 return;
152 }
153
154 const id = $(element).find('.visualizer-front').addClass('viz-facade-loaded').attr('id');
155 setTimeout(function(){
156 // Add a short delay between each chart to avoid overloading the browser event loop.
157 showChart(id);
158 }, ( index + 1 ) * 100);
159 });
160
161 // interate through all charts that are to be lazy-loaded and observe each one.
162 $('div.visualizer-front.visualizer-lazy').each(function(index, element){
163 var id = $(element).attr('id');
164 var limit = $(element).attr('data-lazy-limit');
165 if(!limit){
166 limit = 300;
167 }
168 var target = document.querySelector('#' + id);
169
170 // configure the intersection observer instance
171 var intersectionObserverOptions = {
172 root: null,
173 rootMargin: limit + 'px',
174 threshold: 0
175 };
176
177 var observer = new IntersectionObserver(function(entries) {
178 entries.forEach(function(entry) {
179 if (entry.isIntersecting) {
180 observer.unobserve(target);
181 showChart($(entry.target).attr('id'));
182 }
183 });
184 }, intersectionObserverOptions);
185
186 // start observing.
187 observer.observe(target);
188 });
189 }
190
191 function registerDefaultActions(){
192 $('body').off('visualizer:action:specificchart:defaultprint').on('visualizer:action:specificchart:defaultprint', function(event, v){
193 var iframe = $('<iframe>').attr("name", "print-visualization").attr("id", "print-visualization").css("position", "absolute");
194 iframe.appendTo($('body'));
195 var iframe_doc = iframe.get(0).contentWindow || iframe.get(0).contentDocument.document || iframe.get(0).contentDocument;
196 iframe_doc.document.open();
197 iframe_doc.document.write(v.data);
198 iframe_doc.document.close();
199 setTimeout(function(){
200 window.frames['print-visualization'].focus();
201 window.frames['print-visualization'].print();
202 iframe.remove();
203 }, 500);
204 });
205 }
206 })(jQuery, visualizer);
207
208 (function ($) {
209 $.fn.lock = function () {
210 $(this).each(function () {
211 var $this = $(this);
212 var position = $this.css('position');
213
214 if (!position) {
215 position = 'static';
216 }
217
218 switch (position) {
219 case 'absolute':
220 case 'relative':
221 break;
222 default:
223 $this.css('position', 'relative');
224 break;
225 }
226 $this.data('position', position);
227
228 var width = $this.width(),
229 height = $this.height();
230
231 var locker = $('<div class="locker"></div>');
232 locker.width(width).height(height);
233
234 var loader = $('<div class="locker-loader"></div>');
235 loader.width(width).height(height);
236
237 locker.append(loader);
238 $this.append(locker);
239 $(window).resize(function () {
240 $this.find('.locker,.locker-loader').width($this.width()).height($this.height());
241 });
242 });
243
244 return $(this);
245 };
246
247 $.fn.unlock = function () {
248 $(this).each(function () {
249 $(this).find('.locker').remove();
250 $(this).css('position', $(this).data('position'));
251 });
252
253 return $(this);
254 };
255 })(jQuery);
256