aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/qr_code.js
blob: 4d50272212443e5e93d377855838d5d9c0902787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { $gettext } from "./gettext";
import Dialog from "./dialog.js";

const QRCode = {
    defaultOptions: {
        title: false,
    },
    show(text, options = {}) {
        options = Object.assign({}, QRCode.defaultOptions, options);

        // Prepare content
        const content = $('<div class="qr-code-display"/>');
        $('<img/>').attr('src', STUDIP.ASSETS_URL + 'images/logos/logoklein.png').appendTo(content);
        if (options.title) {
            $('<h1>').text(options.title).appendTo(content);
        }

        const code = $('<div class="code"/>').appendTo(content);
        const url = $('<div class="url"/>').appendTo(content);
        $('<a/>', {
            href: text,
            target: '_blank'
        }).text(text).appendTo(url);

        if (options.description) {
            $('<div class="description"/>').text(options.description).appendTo(content);
        }

        // Actually generate code
        code.qrcode({
            text: text,
            width: 1280,
            height: 1280,
            correctLevel: 3
        });

        // Prepare dialog
        let buttons = {
            fullscreen: {
                text: $gettext('Vollbild'),
                'class': 'fullscreen',
                click () {
                    if (content[0].requestFullscreen) {
                        content[0].requestFullscreen();
                    } else if (content[0].msRequestFullscreen) {
                        content[0].msRequestFullscreen();
                    } else if (content[0].mozRequestFullScreen) {
                        content[0].mozRequestFullScreen();
                    } else if (content[0].webkitRequestFullscreen) {
                        content[0].webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
                    }
                }
            },
            cancel: {
                text: $gettext('Schließen'),
                'class': 'cancel',
                click () {
                    Dialog.close();
                }
            },
        };

        if (options.print) {
            buttons = Object.assign({
                print: {
                    text: $gettext('Drucken'),
                    'class': 'print',
                    click () {
                        var openWindow = window.open("", '_blank');
                        openWindow.document.write(`<body style="text-align: center;">${content.html()}</body>`);
                        // Copy qrcode canvas itself (as it is not included in .html()
                        openWindow.document.querySelector('canvas').getContext('2d').drawImage(
                            $('canvas', code)[0],
                            0,
                            0
                        );
                        openWindow.document.close();
                        openWindow.focus();
                        openWindow.print();
                        openWindow.close();
                    }
                },
            }, buttons);
        }

        Dialog.show(content, {
            title: options.title ?? $gettext('QR-Code'),
            size: 'big',
            buttons
        });
    }
};

export default QRCode;