]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - plugins/qrcode/shaarli-qrcode.js
UtilsTest: PHP 5.5 compatibility
[github/shaarli/Shaarli.git] / plugins / qrcode / shaarli-qrcode.js
CommitLineData
b9b41d25
A
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
14c8efbe
A
29// Show the QR-Code of a permalink (when the QR-Code icon is clicked).
30function 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 element = document.createElement("script");
38 element.src = "plugins/qrcode/qr-1.1.3.min.js";
39 document.body.appendChild(element);
40 }
41 setTimeout(function() { showQrCode(caller,true);}, 200); // Retry in 200 milliseconds.
42 return false;
43 }
44
45 // Remove previous qrcode if present.
46 removeQrcode();
47
48 // Build the div which contains the QR-Code:
49 var element = document.createElement('div');
28a4fc54 50 element.id = 'permalinkQrcode';
14c8efbe
A
51
52 // Make QR-Code div commit sepuku when clicked:
53 if ( element.attachEvent ){
54 element.attachEvent('onclick', 'this.parentNode.removeChild(this);' );
55
56 } else {
57 // Damn IE
58 element.setAttribute('onclick', 'this.parentNode.removeChild(this);' );
59 }
60
61 // Build the QR-Code:
62 var image = qr.image({size: 8,value: caller.dataset.permalink});
63 if (image)
64 {
65 element.appendChild(image);
66 element.innerHTML += "<br>Click to close";
67 caller.parentNode.appendChild(element);
28a4fc54
A
68
69 // Show the QRCode
70 qrcodeImage = document.getElementById('permalinkQrcode');
71 // Workaround to deal with newly created element lag for transition.
72 window.getComputedStyle(qrcodeImage).opacity;
73 qrcodeImage.className = 'show';
14c8efbe
A
74 }
75 else
76 {
77 element.innerHTML = "Your browser does not seem to be HTML5 compatible.";
78 }
79 return false;
80}
81
82// Remove any displayed QR-Code
83function removeQrcode()
84{
28a4fc54 85 var elem = document.getElementById('permalinkQrcode');
14c8efbe
A
86 if (elem) {
87 elem.parentNode.removeChild(elem);
88 }
89 return false;
90}