PluginProbe
Route ‑ Shipping Protection / 2.4.13
Route ‑ Shipping Protection v2.4.13
2.4.19 2.4.18 2.4.17 2.4.16 2.4.15 2.4.14 2.4.13 2.4.12 2.4.11 2.4.10 2.4.9 2.4.8 2.4.7 2.4.6 2.4.5 2.4.4 2.4.3 2.4.2 2.2.4 2.2.5 2.2.6 2.2.8 2.2.9 2.3.0 2.3.1 All 153 releases
routeapp / admin / js / routeapp-admin.js

routeapp-admin.js in Route ‑ Shipping Protection 2.4.13, at admin/js/routeapp-admin.js

220 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function( $ ) {
2 'use strict';
3
4 /**
5 * All of the code for your admin-facing JavaScript source
6 * should reside in this file.
7 *
8 * Note: It has been assumed you will write jQuery code here, so the
9 * $ function reference has been prepared for usage within the scope
10 * of this function.
11 *
12 * This enables you to define handlers, for when the DOM is ready:
13 *
14 * $(function() {
15 *
16 * });
17 *
18 * When the window is loaded:
19 *
20 * $( window ).load(function() {
21 *
22 * });
23 *
24 * ...and/or other possibilities.
25 *
26 * Ideally, it is not considered best practise to attach more than a
27 * single DOM-ready or window-load handler for a particular page.
28 * Although scripts in the WordPress core, Plugins and Themes may be
29 * practising this, we should strive to set a better example in our own work.
30 */
31
32 function setRouteCookie(name, value, expire)
33 {
34 let date = new Date();
35 date.setTime(date.getTime() + (expire * 24 * 360000));
36 document.cookie = name + "=" + value + "; expires=" + date.toGMTString()+"; path=/";
37 }
38
39 function getRouteCookie(cname)
40 {
41 let name = cname + "=";
42 let allCookieArray = document.cookie.split(';');
43 for(let i=0; i<allCookieArray.length; i++)
44 {
45 let temp = allCookieArray[i].trim();
46 if (temp.indexOf(name)==0)
47 return temp.substring(name.length,temp.length);
48 }
49 return "";
50 }
51
52 function isSaveOrderItemsRequest(settings) {
53 if (!settings || !settings.data) {
54 return false;
55 }
56
57 if (typeof settings.data === 'string') {
58 try {
59 return new URLSearchParams(settings.data).get('action') === 'woocommerce_save_order_items';
60 } catch (e) {
61 return settings.data.indexOf('action=woocommerce_save_order_items') !== -1;
62 }
63 }
64
65 if (typeof settings.data === 'object') {
66 return settings.data.action === 'woocommerce_save_order_items';
67 }
68
69 return false;
70 }
71
72 function refreshRouteFeeButton() {
73 if (typeof woocommerce_admin_meta_boxes === 'undefined') {
74 return;
75 }
76
77 var data = {
78 action: 'routeapp_refresh_admin_fee_button',
79 security: woocommerce_admin_meta_boxes.order_item_nonce,
80 order_id: woocommerce_admin_meta_boxes.post_id
81 };
82
83 $.post(
84 woocommerce_admin_meta_boxes.ajax_url,
85 data,
86 function( response ) {
87 if (!response.success) {
88 return;
89 }
90
91 $('#add-route-fee, #remove-route-fee').remove();
92
93 if (response.data && response.data.html) {
94 $('.wc-order-bulk-actions .add-items .calculate-action').first().before(response.data.html);
95 }
96 }
97 ).fail(function(xhr) {
98 console.error('Failed to refresh Route fee button.', xhr);
99 });
100 }
101
102 $(document).ready(function () {
103 $('.routeapp-redirect-countdown').each(function () {
104 var $wrap = $(this);
105 var mode = $wrap.data('countdown-mode') || 'once';
106 var alwaysRun = mode === 'always';
107
108 if (!alwaysRun && getRouteCookie('route_installed')) {
109 $wrap.closest('.notice-success').hide();
110 return;
111 }
112
113 if (!alwaysRun) {
114 setRouteCookie('route_installed', 1, 360);
115 }
116
117 var $num = $wrap.find('.routeapp-redirect-countdown__num');
118 var seconds = parseInt($wrap.data('seconds'), 10);
119 if (isNaN(seconds) || seconds < 1) {
120 seconds = 5;
121 }
122 $num.text(seconds);
123
124 var intervalId = setInterval(function () {
125 seconds--;
126 if (seconds > 0) {
127 $num.text(seconds);
128 } else {
129 clearInterval(intervalId);
130 $num.text(0);
131 var dest = $wrap.attr('data-redirect-src');
132 if (dest) {
133 window.location.href = dest;
134 }
135 }
136 }, 1000);
137 });
138
139 $(document).on('click', '#add-route-fee', function (e) {
140 e.preventDefault();
141
142 var data = {
143 action: 'routeapp_add_admin_fee',
144 security: woocommerce_admin_meta_boxes.order_item_nonce,
145 order_id: woocommerce_admin_meta_boxes.post_id
146 };
147
148 $.post(
149 woocommerce_admin_meta_boxes.ajax_url,
150 data,
151 function( response )
152 {
153 console.log(response);
154 // Errors
155 if( !response.success )
156 {
157 // No data came back, maybe a security error
158 if( !response.data )
159 console.log( 'AJAX ERROR: no response' );
160 // Error from PHP
161 else
162 console.log( 'Response Error: ' + response.data.error );
163 }
164 // Success
165 else {
166 $('.calculate-action').click();
167 console.log( 'Response Success: ' + response.data );
168 }
169 }
170 );
171
172 });
173
174 $(document).on('click', '#remove-route-fee', function (e) {
175 e.preventDefault();
176
177 var data = {
178 action: 'routeapp_remove_admin_fee',
179 security: woocommerce_admin_meta_boxes.order_item_nonce,
180 order_id: woocommerce_admin_meta_boxes.post_id
181 };
182
183 $.post(
184 woocommerce_admin_meta_boxes.ajax_url,
185 data,
186 function( response )
187 {
188 console.log(response);
189 // Errors
190 if( !response.success )
191 {
192 // No data came back, maybe a security error
193 if( !response.data )
194 console.log( 'AJAX ERROR: no response' );
195 // Error from PHP
196 else
197 console.log( 'Response Error: ' + response.data.error );
198 }
199 // Success
200 else {
201 $('.calculate-action').click();
202 console.log( 'Response Success: ' + response.data );
203 }
204 }
205 );
206
207 });
208
209 $(document.body).on('order-totals-recalculate-success', refreshRouteFeeButton);
210
211 $(document).ajaxSuccess(function(event, xhr, settings) {
212 if (isSaveOrderItemsRequest(settings)) {
213 refreshRouteFeeButton();
214 }
215 });
216
217 })
218
219 })( jQuery );
220