PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.7.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.7.2
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 3.10.4 All 148 releases
visualizer / js / render-facade.js

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

232 lines 9.1 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 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, window.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.viz-facade-loaded:not(.visualizer-lazy):empty' ).removeClass( 'viz-facade-loaded' );
132 $('div.visualizer-front:not(.visualizer-lazy):not(.viz-facade-loaded)').each(function(index, element){
133 var id = $(element).addClass('viz-facade-loaded').attr('id');
134 showChart(id);
135 });
136
137 // interate through all charts that are to be lazy-loaded and observe each one.
138 $('div.visualizer-front.visualizer-lazy').each(function(index, element){
139 var id = $(element).attr('id');
140 var limit = $(element).attr('data-lazy-limit');
141 if(!limit){
142 limit = 300;
143 }
144 var target = document.querySelector('#' + id);
145
146 // configure the intersection observer instance
147 var intersectionObserverOptions = {
148 root: null,
149 rootMargin: limit + 'px',
150 threshold: 0
151 };
152
153 var observer = new IntersectionObserver(function(entries) {
154 entries.forEach(function(entry) {
155 if (entry.isIntersecting) {
156 observer.unobserve(target);
157 showChart($(entry.target).attr('id'));
158 }
159 });
160 }, intersectionObserverOptions);
161
162 // start observing.
163 observer.observe(target);
164 });
165 }
166
167 function registerDefaultActions(){
168 $('body').off('visualizer:action:specificchart:defaultprint').on('visualizer:action:specificchart:defaultprint', function(event, v){
169 var iframe = $('<iframe>').attr("name", "print-visualization").attr("id", "print-visualization").css("position", "absolute");
170 iframe.appendTo($('body'));
171 var iframe_doc = iframe.get(0).contentWindow || iframe.get(0).contentDocument.document || iframe.get(0).contentDocument;
172 iframe_doc.document.open();
173 iframe_doc.document.write(v.data);
174 iframe_doc.document.close();
175 setTimeout(function(){
176 window.frames['print-visualization'].focus();
177 window.frames['print-visualization'].print();
178 iframe.remove();
179 }, 500);
180 });
181 }
182 })(jQuery, visualizer);
183
184 (function ($) {
185 $.fn.lock = function () {
186 $(this).each(function () {
187 var $this = $(this);
188 var position = $this.css('position');
189
190 if (!position) {
191 position = 'static';
192 }
193
194 switch (position) {
195 case 'absolute':
196 case 'relative':
197 break;
198 default:
199 $this.css('position', 'relative');
200 break;
201 }
202 $this.data('position', position);
203
204 var width = $this.width(),
205 height = $this.height();
206
207 var locker = $('<div class="locker"></div>');
208 locker.width(width).height(height);
209
210 var loader = $('<div class="locker-loader"></div>');
211 loader.width(width).height(height);
212
213 locker.append(loader);
214 $this.append(locker);
215 $(window).resize(function () {
216 $this.find('.locker,.locker-loader').width($this.width()).height($this.height());
217 });
218 });
219
220 return $(this);
221 };
222
223 $.fn.unlock = function () {
224 $(this).each(function () {
225 $(this).find('.locker').remove();
226 $(this).css('position', $(this).data('position'));
227 });
228
229 return $(this);
230 };
231 })(jQuery);
232