| 1 |
/* |
| 2 |
|
| 3 |
This file is part of the plugin Pay with Vipps and MobilePay for WooCommerce |
| 4 |
Copyright (c) 2019 WP-Hosting AS |
| 5 |
|
| 6 |
MIT License |
| 7 |
|
| 8 |
Copyright (c) 2019 WP-Hosting AS |
| 9 |
|
| 10 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 |
of this software and associated documentation files (the "Software"), to deal |
| 12 |
in the Software without restriction, including without limitation the rights |
| 13 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 |
copies of the Software, and to permit persons to whom the Software is |
| 15 |
furnished to do so, subject to the following conditions: |
| 16 |
|
| 17 |
The above copyright notice and this permission notice shall be included in all |
| 18 |
copies or substantial portions of the Software. |
| 19 |
|
| 20 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 |
SOFTWARE. |
| 27 |
*/ |
| 28 |
|
| 29 |
// This file contains various javascript code for Vipps-specific functionality in the backend. |
| 30 |
// |
| 31 |
(function () { |
| 32 |
|
| 33 |
if (pagenow == 'woocommerce_page_wc-settings') { |
| 34 |
jQuery(document).ready(function () { |
| 35 |
jQuery('input.vippspw').focus( function () { jQuery(this).attr('type','text') });; |
| 36 |
jQuery('input.vippspw').focusout( function () { jQuery(this).attr('type','password'); }); |
| 37 |
}); |
| 38 |
|
| 39 |
// Image upload stuff |
| 40 |
jQuery('body').on( 'click', '.woo-vipps-image-upload', function(e){ |
| 41 |
e.preventDefault(); |
| 42 |
var button = jQuery(this), |
| 43 |
custom_uploader = wp.media({ |
| 44 |
library : { |
| 45 |
type : 'image' |
| 46 |
}, |
| 47 |
button: { |
| 48 |
}, |
| 49 |
multiple: false |
| 50 |
}).on('select', function() { |
| 51 |
var attachment = custom_uploader.state().get('selection').first().toJSON(); |
| 52 |
|
| 53 |
url = false; |
| 54 |
if (attachment['url']) { |
| 55 |
url = attachment.url; |
| 56 |
} |
| 57 |
if (attachment['sizes'] && attachment['sizes']['thumbnail']) { |
| 58 |
url = attachment.sizes.thumbnail.url; |
| 59 |
} |
| 60 |
if (!url) return; |
| 61 |
|
| 62 |
button.find('img').attr('src', url); |
| 63 |
button.find('img').show(); |
| 64 |
button.find('span').hide(); |
| 65 |
button.next().next().val(attachment.id); |
| 66 |
button.next().show(); |
| 67 |
}).open(); |
| 68 |
}); |
| 69 |
jQuery('body').on('click', '.woo-vipps-image-remove', function(e){ |
| 70 |
e.preventDefault(); |
| 71 |
var button = jQuery(this); |
| 72 |
button.next().val(''); |
| 73 |
button.prev().find('img').attr('src', ''); |
| 74 |
button.prev().find('img').hide(); |
| 75 |
button.prev().find('span').show(); |
| 76 |
button.hide(); |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
/* Make the Vipps Checkout shipping options only show if the method in question is activated */ |
| 81 |
if (pagenow == 'woocommerce_page_wc-settings') { |
| 82 |
if (jQuery('#vipps-settings-page').length > 0) { |
| 83 |
function vipps_cs_showhide_shipping (e) { |
| 84 |
jQuery('input.vcs_main').each(function () { |
| 85 |
let showit = jQuery(this).data('vcs-show'); |
| 86 |
if (jQuery(this).is(':checked')) { |
| 87 |
jQuery(showit).closest('tr').show(); |
| 88 |
} else { |
| 89 |
jQuery(showit).closest('tr').hide(); |
| 90 |
} |
| 91 |
}) |
| 92 |
|
| 93 |
} |
| 94 |
jQuery('input.vcs_main').change(vipps_cs_showhide_shipping); |
| 95 |
vipps_cs_showhide_shipping(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/* Tab-ify the settings page */ |
| 100 |
if (pagenow == 'woocommerce_page_wc-settings') { |
| 101 |
|
| 102 |
if (jQuery('#vipps-settings-page').length > 0) { |
| 103 |
let toptitle = jQuery('h3.wc-settings-sub-title').parent().find('h2').first(); |
| 104 |
let tabholder = jQuery('<div id="vippstabholder" class="vippstabholder"></div>').insertAfter(toptitle); |
| 105 |
|
| 106 |
/* Pick out all the h3 subheadings, wrap their following elements in a div, and move them to the tabholder above */ |
| 107 |
jQuery('h3.wc-settings-sub-title.tab').each(function () { |
| 108 |
let curid = jQuery(this).attr('id'); |
| 109 |
let optionstab = jQuery('<div id="' + curid + '-table' + '" class="vippsoptions vippstabs ' + curid + '"></div>').insertAfter(jQuery(this)); |
| 110 |
jQuery(this).nextUntil('h3.wc-settings-sub-title.tab,p.submit').each(function () { |
| 111 |
optionstab.append(jQuery(this)); |
| 112 |
}); |
| 113 |
jQuery(this).attr('tabindex', -1); |
| 114 |
jQuery(this).attr('aria-selected', false); |
| 115 |
jQuery(this).attr('title', jQuery(this).text()); |
| 116 |
|
| 117 |
tabholder.append(jQuery(this)); |
| 118 |
}); |
| 119 |
/* Add a final empty h3 for the a e s t h e t i c s */ |
| 120 |
tabholder.append(jQuery('<h3 id="lasttab" class="wc-settings-sub-title tab last"></h3>')); |
| 121 |
|
| 122 |
/* Navigate tabs using left + right key */ |
| 123 |
jQuery(document).keydown(function (e) { |
| 124 |
if (e.target.parentElement.id == "vippstabholder") { |
| 125 |
if (e.key == "ArrowRight") { |
| 126 |
e.preventDefault(); |
| 127 |
let next = jQuery(e.target).next('h3.wc-settings-sub-title'); |
| 128 |
if (next.length == 0 || next.attr('id') == 'lasttab') { |
| 129 |
jQuery('#vippstabholder h3').first().click(); |
| 130 |
} else { |
| 131 |
next.click(); |
| 132 |
} |
| 133 |
} else if (e.key == "ArrowLeft") { |
| 134 |
e.preventDefault(); |
| 135 |
let current = jQuery(e.target); |
| 136 |
console.log("id " + e.target.id); |
| 137 |
if (e.target.id == 'woocommerce_vipps_main_options') { |
| 138 |
current = jQuery('#vippstabholder h3.wc-settings-sub-title#lasttab'); |
| 139 |
} |
| 140 |
let prev= current.prev('h3.wc-settings-sub-title'); |
| 141 |
if (prev.length>0) { |
| 142 |
prev.click(); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
}); |
| 147 |
|
| 148 |
|
| 149 |
/* Set the main options tab to active */ |
| 150 |
let thetab = 'woocommerce_vipps_main_options'; |
| 151 |
let tabselected = window.location.hash ? window.location.hash.match(/tab:(.+)/) : false; |
| 152 |
if (tabselected && tabselected.length>0) { |
| 153 |
thetab = tabselected[1]; |
| 154 |
} |
| 155 |
let idselector = '#' + thetab; |
| 156 |
let fieldselector = '.vippsoptions.vippstabs.' + thetab; |
| 157 |
jQuery(idselector + ',' + fieldselector).addClass('active'); |
| 158 |
jQuery(idselector).attr('aria-selected', true); |
| 159 |
jQuery(idselector).attr('tabindex', 0); |
| 160 |
jQuery(idselector).focus(); |
| 161 |
|
| 162 |
|
| 163 |
/* Make the tab headers active */ |
| 164 |
jQuery('div#vippstabholder h3.wc-settings-sub-title').click(function (e) { |
| 165 |
e.preventDefault(); |
| 166 |
|
| 167 |
let curid = jQuery(this).attr('id'); |
| 168 |
if (curid == 'lasttab') return; |
| 169 |
|
| 170 |
if (curid == 'woocommerce_vipps_main_options') { |
| 171 |
history.pushState(null, null, ' ') |
| 172 |
} else { |
| 173 |
history.pushState({}, "", "#tab:"+curid); |
| 174 |
} |
| 175 |
|
| 176 |
jQuery('#vippstabholder .active').removeClass('active'); |
| 177 |
jQuery('.vippsoptions.vippstabs.active').removeClass('active'); |
| 178 |
|
| 179 |
jQuery(this).addClass('active'); |
| 180 |
jQuery('.vippsoptions.vippstabs.' + curid).addClass('active'); |
| 181 |
|
| 182 |
|
| 183 |
jQuery('#vippstabholder h3').attr('tabindex', -1); |
| 184 |
jQuery('#vippstabholder h3').attr('aria-selected', false); |
| 185 |
jQuery(this).attr('aria-selected', true); |
| 186 |
jQuery(this).attr('tabindex', 0); |
| 187 |
jQuery(this).focus(); |
| 188 |
|
| 189 |
|
| 190 |
}); |
| 191 |
// Integrate with history |
| 192 |
addEventListener('hashchange', function (e) { |
| 193 |
jQuery('#vippstabholder .active').removeClass('active'); |
| 194 |
jQuery('.vippsoptions.vippstabs.active').removeClass('active'); |
| 195 |
let thetab = 'woocommerce_vipps_main_options'; |
| 196 |
let tabselected = window.location.hash ? window.location.hash.match(/tab:(.+)/) : false; |
| 197 |
if (tabselected && tabselected.length>0) { |
| 198 |
thetab = tabselected[1]; |
| 199 |
} |
| 200 |
|
| 201 |
let idselector = '#' + thetab; |
| 202 |
let fieldselector = '.vippsoptions.vippstabs.' + thetab; |
| 203 |
|
| 204 |
jQuery(idselector + ',' + fieldselector).addClass('active'); |
| 205 |
jQuery(idselector).attr('aria-selected', true); |
| 206 |
jQuery(idselector).attr('tabindex', 0); |
| 207 |
jQuery(idselector).focus(); |
| 208 |
|
| 209 |
}); |
| 210 |
|
| 211 |
} |
| 212 |
} |
| 213 |
if (pagenow == 'product') { |
| 214 |
// Called on the product edit screen by a button. |
| 215 |
function vipps_create_shareable_link() { |
| 216 |
jQuery('#vipps-share-link').attr('disabled','disabled'); |
| 217 |
|
| 218 |
jQuery('#vipps-shareable-link-error').text(''); |
| 219 |
jQuery('.vipps-shareable-link-error').hide(); |
| 220 |
|
| 221 |
|
| 222 |
var prodid = jQuery('#vipps_sharelink_id').val(); |
| 223 |
var varid=0; |
| 224 |
var isvariant = false; |
| 225 |
if (!prodid) { |
| 226 |
jQuery('#vipps-share-link').removeAttr('disabled'); |
| 227 |
return false; |
| 228 |
} |
| 229 |
var varselector = jQuery('#vipps_sharelink_variant'); |
| 230 |
if (varselector.length > 0) isvariant = true; |
| 231 |
if (isvariant) { |
| 232 |
varid = varselector.val(); |
| 233 |
if (!varid) { |
| 234 |
jQuery('#vipps-share-link').removeAttr('disabled'); |
| 235 |
return false; |
| 236 |
} |
| 237 |
} |
| 238 |
var nonce = jQuery('#vipps_share_sec').val(); |
| 239 |
var data = { 'action': 'vipps_create_shareable_link', 'prodid':prodid,'varid':varid, 'vipps_share_sec':nonce }; |
| 240 |
|
| 241 |
jQuery.ajax(ajaxurl, { |
| 242 |
"method": "POST", |
| 243 |
"data":data, |
| 244 |
"cache":false, |
| 245 |
"dataType": "json", |
| 246 |
"timeout":0, |
| 247 |
"headers": {"Accept-Language": `${VippsConfig['vippslocale']}, *`}, |
| 248 |
"error": function (xhr, statustext, error) { |
| 249 |
jQuery('#vipps-share-link').removeAttr('disabled'); |
| 250 |
console.log("Error creating shareable link" + statustext + " " + error); |
| 251 |
jQuery('#vipps-shareable-link-error').text(' : ' +error); |
| 252 |
jQuery('.vipps-shareable-link-error').show(); |
| 253 |
}, |
| 254 |
"success": function (result, statustext, xhr) { |
| 255 |
jQuery('#vipps-share-link').removeAttr('disabled'); |
| 256 |
if (result["ok"]) { |
| 257 |
console.log("Shareable link created "); |
| 258 |
jQuery('#woo_vipps_shareables').show(); |
| 259 |
var newrow = jQuery('<tr>'); |
| 260 |
if (result['variant']) newrow.append('<td>'+result['variant']+'</td>'); |
| 261 |
var link = jQuery('#woo_vipps_shareable_link_template').clone(); |
| 262 |
link.removeAttr('id'); |
| 263 |
link.find('a').text(result['url']); |
| 264 |
link.find('input').attr('value',result['key']); |
| 265 |
newrow.append('<td>'+link.html()+'</td>'); |
| 266 |
var actions = jQuery('#woo_vipps_shareable_command_template').clone(); |
| 267 |
actions.removeAttr('id'); |
| 268 |
newrow.append('<td align=center>' + actions.html() + '<td>'); |
| 269 |
jQuery('#woo_vipps_shareables tbody').append(newrow); |
| 270 |
add_shareable_commands(newrow); |
| 271 |
} else { |
| 272 |
console.log("Error creating shareable link " + result['msg']); |
| 273 |
jQuery('#vipps-shareable-link-error').text(' : ' +result['msg']); |
| 274 |
jQuery('.vipps-shareable-link-error').show(); |
| 275 |
} |
| 276 |
}, |
| 277 |
}); |
| 278 |
|
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
function add_shareable_commands(element) { |
| 283 |
if (!element) element = jQuery('#woo_vipps_shareables'); |
| 284 |
element.find('.shareable').click(function () { |
| 285 |
jQuery('<input type=hidden/>').appendTo('body').val(jQuery(this).text()).select(); |
| 286 |
document.execCommand('copy'); |
| 287 |
}); |
| 288 |
element.find('.copyaction').click(function () { |
| 289 |
var link = jQuery(this).closest('tr').find('.shareable'); |
| 290 |
jQuery('<input type=hidden/>').appendTo('body').val(link.text()).select(); |
| 291 |
document.execCommand('copy'); |
| 292 |
}); |
| 293 |
element.find('.deleteaction').click(function () { |
| 294 |
var link = jQuery(this).closest('tr').find('.shareable'); |
| 295 |
link.toggleClass('deleted'); |
| 296 |
if (link.hasClass('deleted')) { |
| 297 |
link.siblings('.deletemarker').attr('name','woo_vipps_shareable_delenda[]'); |
| 298 |
} else { |
| 299 |
link.siblings('.deletemarker').removeAttr('name'); |
| 300 |
} |
| 301 |
|
| 302 |
if (jQuery('a.shareable.deleted').length>0) { |
| 303 |
jQuery('#vipps-shareable-link-delete-message').show(); |
| 304 |
} else { |
| 305 |
jQuery('#vipps-shareable-link-delete-message').hide(); |
| 306 |
} |
| 307 |
|
| 308 |
}); |
| 309 |
} |
| 310 |
|
| 311 |
jQuery(document).ready(function () { |
| 312 |
// Require a variant to have been selected in order for the shareable-link thing to work |
| 313 |
jQuery('#vipps_sharelink_variant').change(function () { |
| 314 |
if (jQuery(this).val()) { |
| 315 |
jQuery('#vipps-share-link').removeAttr('disabled'); |
| 316 |
} else { |
| 317 |
jQuery('#vipps-share-link').attr('disabled','disabled'); |
| 318 |
} |
| 319 |
}); |
| 320 |
jQuery('#vipps-share-link').click(vipps_create_shareable_link); |
| 321 |
add_shareable_commands(); |
| 322 |
}); |
| 323 |
} |
| 324 |
|
| 325 |
if (pagenow == 'vipps_qr_code') { |
| 326 |
function select_url_bit (which) { |
| 327 |
let all = jQuery('body.post-type-vipps_qr_code.wp-admin .url-section .url-options .url-option'); |
| 328 |
let thisone = jQuery('body.post-type-vipps_qr_code.wp-admin .url-section .url-options .url-option.' + which); |
| 329 |
all.removeClass('active'); |
| 330 |
thisone.addClass('active'); |
| 331 |
all.find('input,select').prop('required', false); |
| 332 |
thisone.find('input,select').prop('required', true); |
| 333 |
} |
| 334 |
jQuery(document).ready(function () { |
| 335 |
jQuery('.url-section .link-selector .vipps_urltype').on('change', function () { |
| 336 |
select_url_bit(jQuery(this).val()); |
| 337 |
}); |
| 338 |
select_url_bit(jQuery('.url-section .link-selector .vipps_urltype:checked').val()); |
| 339 |
}); |
| 340 |
} |
| 341 |
|
| 342 |
if (pagenow == 'shop_order' || pagenow == 'woocommerce_page_wc-orders') { |
| 343 |
console.log("Shop order page!"); |
| 344 |
|
| 345 |
jQuery(document).ready(function () { |
| 346 |
jQuery('button.vipps-action').click(function (e) { |
| 347 |
console.log("Clickety"); |
| 348 |
e.preventDefault(); |
| 349 |
|
| 350 |
let button = jQuery(this); |
| 351 |
if (button.hasClass('disabled')) return; |
| 352 |
button.attr('disabled', 'disabled'); |
| 353 |
button.addClass('disabled'); |
| 354 |
|
| 355 |
let nonce = VippsConfig['vippssecnonce']; |
| 356 |
|
| 357 |
|
| 358 |
let orderid = jQuery(this).data('orderid'); |
| 359 |
let action = jQuery(this).data('action'); |
| 360 |
|
| 361 |
let data = { 'action': 'woo_vipps_order_action', 'do': action, 'orderid':orderid, 'vipps_sec':nonce }; |
| 362 |
|
| 363 |
console.log("Ajaxurl " + ajaxurl + " and data %j", data); |
| 364 |
|
| 365 |
jQuery.ajax(ajaxurl, { |
| 366 |
"method": "POST", |
| 367 |
"data":data, |
| 368 |
"cache":false, |
| 369 |
"dataType": "json", |
| 370 |
"timeout":0, |
| 371 |
"headers": {"Accept-Language": `${VippsConfig['vippslocale']}, *`}, |
| 372 |
"error": function (xhr, statustext, error) { |
| 373 |
button.removeAttr('disabled'); |
| 374 |
button.removeClass('disabled'); |
| 375 |
console.log("Error performing Vipps action " + statustext + " " + error); |
| 376 |
alert("Error performing Vipps action " + statustext + " " + error); |
| 377 |
}, |
| 378 |
"success": function (result, statustext, xhr) { |
| 379 |
window.location.reload(); |
| 380 |
} |
| 381 |
}); |
| 382 |
}); |
| 383 |
}); |
| 384 |
} |
| 385 |
|
| 386 |
})(); |
| 387 |
|
| 388 |
function VippsGetPaymentDetails(orderid,nonce) { |
| 389 |
var source = ajaxurl + '?vipps_paymentdetails_sec='+nonce+'&action=vipps_payment_details&orderid='+orderid; |
| 390 |
var y = window.outerHeight / 4 + window.screenY - (320/2); |
| 391 |
var x = window.outerWidth / 2 + window.screenX - (320/2); |
| 392 |
var qrwin = window.open('','_blank','height=320,width=640,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes,copyhistory=no,top='+y+',left='+x); |
| 393 |
qrwin.document.write('<p style="position:fixed;top:50%;left:50%"><i>loading...</i></p>'); |
| 394 |
qrwin.location.href = source; |
| 395 |
qrwin.title ="Payment details"; |
| 396 |
} |
| 397 |
|
| 398 |
// Handle permanent notice dismissal |
| 399 |
jQuery(document).ready(function () { |
| 400 |
jQuery('.notice-vipps .notice-dismiss').click(function () { |
| 401 |
let nonce = VippsConfig['vippssecnonce']; |
| 402 |
let key = jQuery(this).closest('.notice').data('key'); |
| 403 |
if (! key) return; |
| 404 |
let data = { 'action': 'vipps_dismiss_notice', 'vipps_sec':nonce, 'key': key, headers: {"Accept-Language": `${VippsConfig['vippslocale']}, *`} }; |
| 405 |
jQuery.ajax(ajaxurl, { "method": "POST", "data":data, "cache":false, "dataType": "json", "timeout":0 }); |
| 406 |
}); |
| 407 |
}); |
| 408 |
|
| 409 |
|
| 410 |
jQuery(document).ready(function () { |
| 411 |
var unsynch = jQuery('#vipps_unsynchronized_qr_codes'); |
| 412 |
if (unsynch.length > 0) { |
| 413 |
jQuery('#vipps_unsynchronized_qr_codes').dialog({ |
| 414 |
dialogClass: 'wp-dialog', |
| 415 |
autoOpen: false, |
| 416 |
draggable: true, |
| 417 |
width: 'auto', |
| 418 |
modal: true, |
| 419 |
resizable: true, |
| 420 |
closeOnEscape: true, |
| 421 |
|
| 422 |
position: { |
| 423 |
my: "top+10%", |
| 424 |
at: "top", |
| 425 |
of: window, |
| 426 |
collision: 'fit' |
| 427 |
}, |
| 428 |
|
| 429 |
open: function () { |
| 430 |
// close dialog by clicking the overlay behind it |
| 431 |
jQuery('.ui-widget-overlay').bind('click', function(){ |
| 432 |
jQuery('#vipps_unsynchronized_qr_codes').dialog('close'); |
| 433 |
}) |
| 434 |
}, |
| 435 |
create: function () { |
| 436 |
// style fix for WordPress admin |
| 437 |
jQuery('.ui-dialog-titlebar-close').addClass('ui-button'); |
| 438 |
}, |
| 439 |
}); |
| 440 |
// bind a button or a link to open the dialog |
| 441 |
jQuery('a.open_unsynchronized_qr_codes').click(function(e) { |
| 442 |
e.preventDefault(); |
| 443 |
jQuery('#vipps_unsynchronized_qr_codes').dialog('open'); |
| 444 |
}); |
| 445 |
} |
| 446 |
}); |
| 447 |
|
| 448 |
|
| 449 |
console.log("Vipps admin scripts loaded - v1.12.3"); |
| 450 |
|