shaarli-qrcode.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /** @licstart The following is the entire license notice for the
  2. * JavaScript code in this page.
  3. *
  4. * Copyright: (c) 2011-2015 Sébastien SAUVAGE <sebsauvage@sebsauvage.net>
  5. * (c) 2011-2017 The Shaarli Community, see AUTHORS
  6. *
  7. * This software is provided 'as-is', without any express or implied warranty.
  8. * In no event will the authors be held liable for any damages arising from
  9. * the use of this software.
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software. If you use this software
  17. * in a product, an acknowledgment in the product documentation would
  18. * be appreciated but is not required.
  19. *
  20. * 2. Altered source versions must be plainly marked as such, and must
  21. * not be misrepresented as being the original software.
  22. *
  23. * 3. This notice may not be removed or altered from any source distribution.
  24. *
  25. * @licend The above is the entire license notice
  26. * for the JavaScript code in this page.
  27. */
  28. // Show the QR-Code of a permalink (when the QR-Code icon is clicked).
  29. function showQrCode(caller,loading)
  30. {
  31. // Dynamic javascript lib loading: We only load qr.js if the QR code icon is clicked:
  32. if (typeof(qr) == 'undefined') // Load qr.js only if not present.
  33. {
  34. if (!loading) // If javascript lib is still loading, do not append script to body.
  35. {
  36. var element = document.createElement("script");
  37. element.src = "plugins/qrcode/qr-1.1.3.min.js";
  38. document.body.appendChild(element);
  39. }
  40. setTimeout(function() { showQrCode(caller,true);}, 200); // Retry in 200 milliseconds.
  41. return false;
  42. }
  43. // Remove previous qrcode if present.
  44. removeQrcode();
  45. // Build the div which contains the QR-Code:
  46. var element = document.createElement('div');
  47. element.id = 'permalinkQrcode';
  48. // Make QR-Code div commit sepuku when clicked:
  49. if ( element.attachEvent ){
  50. element.attachEvent('onclick', 'this.parentNode.removeChild(this);' );
  51. } else {
  52. // Damn IE
  53. element.setAttribute('onclick', 'this.parentNode.removeChild(this);' );
  54. }
  55. // Build the QR-Code:
  56. var image = qr.image({size: 8,value: caller.dataset.permalink});
  57. if (image)
  58. {
  59. element.appendChild(image);
  60. element.innerHTML += "<br>Click to close";
  61. caller.parentNode.appendChild(element);
  62. // Show the QRCode
  63. qrcodeImage = document.getElementById('permalinkQrcode');
  64. // Workaround to deal with newly created element lag for transition.
  65. window.getComputedStyle(qrcodeImage).opacity;
  66. qrcodeImage.className = 'show';
  67. }
  68. else
  69. {
  70. element.innerHTML = "Your browser does not seem to be HTML5 compatible.";
  71. }
  72. return false;
  73. }
  74. // Remove any displayed QR-Code
  75. function removeQrcode()
  76. {
  77. var elem = document.getElementById('permalinkQrcode');
  78. if (elem) {
  79. elem.parentNode.removeChild(elem);
  80. }
  81. return false;
  82. }