]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - plugins/qrcode/shaarli-qrcode.js
Merge pull request #1512 from shaarli/dependabot/npm_and_yarn/elliptic-6.5.3
[github/shaarli/Shaarli.git] / plugins / qrcode / shaarli-qrcode.js
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
29 // Show the QR-Code of a permalink (when the QR-Code icon is clicked).
30 function showQrCode(caller,loading)
31 {
32 // Dynamic javascript lib loading: We only load qr.js if the QR code icon is clicked:
33 if (typeof(qr) == 'undefined') // Load qr.js only if not present.
34 {
35 if (!loading) // If javascript lib is still loading, do not append script to body.
36 {
37 var basePath = document.querySelector('input[name="js_base_path"]').value;
38 var element = document.createElement("script");
39 element.src = basePath + "/plugins/qrcode/qr-1.1.3.min.js";
40 document.body.appendChild(element);
41 }
42 setTimeout(function() { showQrCode(caller,true);}, 200); // Retry in 200 milliseconds.
43 return false;
44 }
45
46 // Remove previous qrcode if present.
47 removeQrcode();
48
49 // Build the div which contains the QR-Code:
50 var element = document.createElement('div');
51 element.id = 'permalinkQrcode';
52
53 // Make QR-Code div commit sepuku when clicked:
54 if ( element.attachEvent ){
55 element.attachEvent('onclick', 'this.parentNode.removeChild(this);' );
56
57 } else {
58 // Damn IE
59 element.setAttribute('onclick', 'this.parentNode.removeChild(this);' );
60 }
61
62 // Build the QR-Code:
63 var image = qr.image({size: 8,value: caller.dataset.permalink});
64 if (image)
65 {
66 element.appendChild(image);
67 element.innerHTML += "<br>Click to close";
68 caller.parentNode.appendChild(element);
69
70 // Show the QRCode
71 qrcodeImage = document.getElementById('permalinkQrcode');
72 // Workaround to deal with newly created element lag for transition.
73 window.getComputedStyle(qrcodeImage).opacity;
74 qrcodeImage.className = 'show';
75 }
76 else
77 {
78 element.innerHTML = "Your browser does not seem to be HTML5 compatible.";
79 }
80 return false;
81 }
82
83 // Remove any displayed QR-Code
84 function removeQrcode()
85 {
86 var elem = document.getElementById('permalinkQrcode');
87 if (elem) {
88 elem.parentNode.removeChild(elem);
89 }
90 return false;
91 }