| 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 |
$(document).ready(function () { |
| 53 |
$('.routeapp-redirect-countdown').each(function () { |
| 54 |
var $wrap = $(this); |
| 55 |
var mode = $wrap.data('countdown-mode') || 'once'; |
| 56 |
var alwaysRun = mode === 'always'; |
| 57 |
|
| 58 |
if (!alwaysRun && getRouteCookie('route_installed')) { |
| 59 |
$wrap.closest('.notice-success').hide(); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
if (!alwaysRun) { |
| 64 |
setRouteCookie('route_installed', 1, 360); |
| 65 |
} |
| 66 |
|
| 67 |
var $num = $wrap.find('.routeapp-redirect-countdown__num'); |
| 68 |
var seconds = parseInt($wrap.data('seconds'), 10); |
| 69 |
if (isNaN(seconds) || seconds < 1) { |
| 70 |
seconds = 5; |
| 71 |
} |
| 72 |
$num.text(seconds); |
| 73 |
|
| 74 |
var intervalId = setInterval(function () { |
| 75 |
seconds--; |
| 76 |
if (seconds > 0) { |
| 77 |
$num.text(seconds); |
| 78 |
} else { |
| 79 |
clearInterval(intervalId); |
| 80 |
$num.text(0); |
| 81 |
var dest = $wrap.attr('data-redirect-src'); |
| 82 |
if (dest) { |
| 83 |
window.location.href = dest; |
| 84 |
} |
| 85 |
} |
| 86 |
}, 1000); |
| 87 |
}); |
| 88 |
|
| 89 |
$(document).on('click', '#add-route-fee', function (e) { |
| 90 |
e.preventDefault(); |
| 91 |
|
| 92 |
var data = { |
| 93 |
action: 'routeapp_add_admin_fee', |
| 94 |
security: woocommerce_admin_meta_boxes.order_item_nonce, |
| 95 |
order_id: woocommerce_admin_meta_boxes.post_id |
| 96 |
}; |
| 97 |
|
| 98 |
$.post( |
| 99 |
woocommerce_admin_meta_boxes.ajax_url, |
| 100 |
data, |
| 101 |
function( response ) |
| 102 |
{ |
| 103 |
console.log(response); |
| 104 |
// Errors |
| 105 |
if( !response.success ) |
| 106 |
{ |
| 107 |
// No data came back, maybe a security error |
| 108 |
if( !response.data ) |
| 109 |
console.log( 'AJAX ERROR: no response' ); |
| 110 |
// Error from PHP |
| 111 |
else |
| 112 |
console.log( 'Response Error: ' + response.data.error ); |
| 113 |
} |
| 114 |
// Success |
| 115 |
else { |
| 116 |
$('.calculate-action').click(); |
| 117 |
console.log( 'Response Success: ' + response.data ); |
| 118 |
} |
| 119 |
} |
| 120 |
); |
| 121 |
|
| 122 |
}); |
| 123 |
|
| 124 |
$(document).on('click', '#remove-route-fee', function (e) { |
| 125 |
e.preventDefault(); |
| 126 |
|
| 127 |
var data = { |
| 128 |
action: 'routeapp_remove_admin_fee', |
| 129 |
security: woocommerce_admin_meta_boxes.order_item_nonce, |
| 130 |
order_id: woocommerce_admin_meta_boxes.post_id |
| 131 |
}; |
| 132 |
|
| 133 |
$.post( |
| 134 |
woocommerce_admin_meta_boxes.ajax_url, |
| 135 |
data, |
| 136 |
function( response ) |
| 137 |
{ |
| 138 |
console.log(response); |
| 139 |
// Errors |
| 140 |
if( !response.success ) |
| 141 |
{ |
| 142 |
// No data came back, maybe a security error |
| 143 |
if( !response.data ) |
| 144 |
console.log( 'AJAX ERROR: no response' ); |
| 145 |
// Error from PHP |
| 146 |
else |
| 147 |
console.log( 'Response Error: ' + response.data.error ); |
| 148 |
} |
| 149 |
// Success |
| 150 |
else { |
| 151 |
$('.calculate-action').click(); |
| 152 |
console.log( 'Response Success: ' + response.data ); |
| 153 |
} |
| 154 |
} |
| 155 |
); |
| 156 |
|
| 157 |
}); |
| 158 |
|
| 159 |
}) |
| 160 |
|
| 161 |
})( jQuery ); |
| 162 |
|