shaarli-qrcode.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Show the QR-Code of a permalink (when the QR-Code icon is clicked).
  2. function showQrCode(caller,loading)
  3. {
  4. // Dynamic javascript lib loading: We only load qr.js if the QR code icon is clicked:
  5. if (typeof(qr) == 'undefined') // Load qr.js only if not present.
  6. {
  7. if (!loading) // If javascript lib is still loading, do not append script to body.
  8. {
  9. var element = document.createElement("script");
  10. element.src = "plugins/qrcode/qr-1.1.3.min.js";
  11. document.body.appendChild(element);
  12. }
  13. setTimeout(function() { showQrCode(caller,true);}, 200); // Retry in 200 milliseconds.
  14. return false;
  15. }
  16. // Remove previous qrcode if present.
  17. removeQrcode();
  18. // Build the div which contains the QR-Code:
  19. var element = document.createElement('div');
  20. element.id = 'permalinkQrcode';
  21. // Make QR-Code div commit sepuku when clicked:
  22. if ( element.attachEvent ){
  23. element.attachEvent('onclick', 'this.parentNode.removeChild(this);' );
  24. } else {
  25. // Damn IE
  26. element.setAttribute('onclick', 'this.parentNode.removeChild(this);' );
  27. }
  28. // Build the QR-Code:
  29. var image = qr.image({size: 8,value: caller.dataset.permalink});
  30. if (image)
  31. {
  32. element.appendChild(image);
  33. element.innerHTML += "<br>Click to close";
  34. caller.parentNode.appendChild(element);
  35. // Show the QRCode
  36. qrcodeImage = document.getElementById('permalinkQrcode');
  37. // Workaround to deal with newly created element lag for transition.
  38. window.getComputedStyle(qrcodeImage).opacity;
  39. qrcodeImage.className = 'show';
  40. }
  41. else
  42. {
  43. element.innerHTML = "Your browser does not seem to be HTML5 compatible.";
  44. }
  45. return false;
  46. }
  47. // Remove any displayed QR-Code
  48. function removeQrcode()
  49. {
  50. var elem = document.getElementById('permalinkQrcode');
  51. if (elem) {
  52. elem.parentNode.removeChild(elem);
  53. }
  54. return false;
  55. }