| 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 |
if (!getRouteCookie('route_installed') && document.getElementById("route_counter") !== null) { |
| 54 |
|
| 55 |
setRouteCookie('route_installed', 1, 360); |
| 56 |
|
| 57 |
let intervalId = setInterval(function () { |
| 58 |
let seconds = document.getElementById("route_counter").innerText; |
| 59 |
if(seconds > 0) { |
| 60 |
document.getElementById("route_counter").innerText = seconds - 1; |
| 61 |
}else{ |
| 62 |
clearInterval(intervalId) |
| 63 |
window.location = $("#route_counter").data('redirect-src'); |
| 64 |
} |
| 65 |
},1000) |
| 66 |
}else{ |
| 67 |
$('#route_counter').parents('.notice-success').hide() |
| 68 |
} |
| 69 |
|
| 70 |
$(document).on('click', '#add-route-fee', function (e) { |
| 71 |
e.preventDefault(); |
| 72 |
|
| 73 |
var data = { |
| 74 |
action: 'routeapp_add_admin_fee', |
| 75 |
security: woocommerce_admin_meta_boxes.order_item_nonce, |
| 76 |
order_id: woocommerce_admin_meta_boxes.post_id |
| 77 |
}; |
| 78 |
|
| 79 |
$.post( |
| 80 |
woocommerce_admin_meta_boxes.ajax_url, |
| 81 |
data, |
| 82 |
function( response ) |
| 83 |
{ |
| 84 |
console.log(response); |
| 85 |
// Errors |
| 86 |
if( !response.success ) |
| 87 |
{ |
| 88 |
// No data came back, maybe a security error |
| 89 |
if( !response.data ) |
| 90 |
console.log( 'AJAX ERROR: no response' ); |
| 91 |
// Error from PHP |
| 92 |
else |
| 93 |
console.log( 'Response Error: ' + response.data.error ); |
| 94 |
} |
| 95 |
// Success |
| 96 |
else { |
| 97 |
$('.calculate-action').click(); |
| 98 |
console.log( 'Response Success: ' + response.data ); |
| 99 |
} |
| 100 |
} |
| 101 |
); |
| 102 |
|
| 103 |
}); |
| 104 |
|
| 105 |
$(document).on('click', '#remove-route-fee', function (e) { |
| 106 |
e.preventDefault(); |
| 107 |
|
| 108 |
var data = { |
| 109 |
action: 'routeapp_remove_admin_fee', |
| 110 |
security: woocommerce_admin_meta_boxes.order_item_nonce, |
| 111 |
order_id: woocommerce_admin_meta_boxes.post_id |
| 112 |
}; |
| 113 |
|
| 114 |
$.post( |
| 115 |
woocommerce_admin_meta_boxes.ajax_url, |
| 116 |
data, |
| 117 |
function( response ) |
| 118 |
{ |
| 119 |
console.log(response); |
| 120 |
// Errors |
| 121 |
if( !response.success ) |
| 122 |
{ |
| 123 |
// No data came back, maybe a security error |
| 124 |
if( !response.data ) |
| 125 |
console.log( 'AJAX ERROR: no response' ); |
| 126 |
// Error from PHP |
| 127 |
else |
| 128 |
console.log( 'Response Error: ' + response.data.error ); |
| 129 |
} |
| 130 |
// Success |
| 131 |
else { |
| 132 |
$('.calculate-action').click(); |
| 133 |
console.log( 'Response Success: ' + response.data ); |
| 134 |
} |
| 135 |
} |
| 136 |
); |
| 137 |
|
| 138 |
}); |
| 139 |
|
| 140 |
}) |
| 141 |
|
| 142 |
})( jQuery ); |
| 143 |
|