]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - plugins/qrcode/shaarli-qrcode.js
0a8de21de7f7a6c8b3b258e97041ae7e41a5cc60
[github/shaarli/Shaarli.git] / plugins / qrcode / shaarli-qrcode.js
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
17 // Remove previous qrcode if present.
18 removeQrcode();
19
20 // Build the div which contains the QR-Code:
21 var element = document.createElement('div');
22 element.id="permalinkQrcode";
23
24 // Make QR-Code div commit sepuku when clicked:
25 if ( element.attachEvent ){
26 element.attachEvent('onclick', 'this.parentNode.removeChild(this);' );
27
28 } else {
29 // Damn IE
30 element.setAttribute('onclick', 'this.parentNode.removeChild(this);' );
31 }
32
33 // Build the QR-Code:
34 var image = qr.image({size: 8,value: caller.dataset.permalink});
35 if (image)
36 {
37 element.appendChild(image);
38 element.innerHTML += "<br>Click to close";
39 caller.parentNode.appendChild(element);
40 }
41 else
42 {
43 element.innerHTML = "Your browser does not seem to be HTML5 compatible.";
44 }
45 return false;
46 }
47
48 // Remove any displayed QR-Code
49 function removeQrcode()
50 {
51 var elem = document.getElementById("permalinkQrcode");
52 if (elem) {
53 elem.parentNode.removeChild(elem);
54 }
55 return false;
56 }