-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (124 loc) · 5.23 KB
/
app.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { startQrScanner } from './qr-scanner';
import { getQrCodeSVG } from './qr-generator';
import * as utils from './utils';
const tabs = {
"create": { btnId: "btn-create-qr", boxId: "box-create-qr" },
"scan": { btnId: "btn-scan-qr", boxId: "box-qr-scan-result" },
"settings": { btnId: "btn-settings", boxId: "box-settings" },
};
let activeTabId = null;
let currentSvgContent = "";
window.onload = function () {
loadAndApplyColorModeFromLocalStorage();
let inputCreateQr = document.getElementById("input-create-qr");
let txtScanResult = document.getElementById("txt-qr-scan-result");
let qrWrapper = document.getElementById("div-qr-wrapper");
const btnDownloadSvg = document.getElementById("btn-download-svg");
inputCreateQr.value = "";
qrWrapper.innerHTML = "";
document.getElementById(tabs["create"].btnId).addEventListener("click", function () {
showTab("create");
});
document.getElementById(tabs["scan"].btnId).addEventListener("click", function () {
txtScanResult.textContent = "";
showTab("scan");
document.getElementById(tabs["scan"].boxId).style.display = "none"; // hide box until qr is scanned
startQrScanner(function (result) {
showTab("scan");
txtScanResult.textContent = result;
});
});
document.getElementById(tabs["settings"].btnId).addEventListener("click", function () {
showTab("settings");
});
inputCreateQr.addEventListener("input", function () {
let text = inputCreateQr.value;
showQrCode(text, qrWrapper, btnDownloadSvg);
});
btnDownloadSvg.addEventListener("click", () => downloadSvg(qrWrapper));
document.getElementById("btn-qr-scan-result-copy").addEventListener("click", function () {
utils.copyTextToClipboard(txtScanResult.textContent);
});
document.getElementById("btn-light-mode").addEventListener("click", function () {
setColorMode("light-mode");
});
document.getElementById("btn-dark-mode").addEventListener("click", function () {
setColorMode("dark-mode");
});
const checkboxApplyColorModeToQrCode = document.getElementById("checkbox-apply-color-mode-to-qr-code");
checkboxApplyColorModeToQrCode.addEventListener("change", function () {
setColorModeApplicabilityForQrCode(checkboxApplyColorModeToQrCode.checked);
});
document.getElementById("btn-share").addEventListener("click", function () {
showTab("create");
let text = window.location.href;
if (text.endsWith("/index.html")) {
text = text.substring(0, text.length - "/index.html".length)
}
inputCreateQr.value = text;
showQrCode(text, qrWrapper, btnDownloadSvg);
});
inputCreateQr.value = "";
qrWrapper.innerHTML = "";
showTab("create");
};
function showQrCode (text, qrWrapper, downloadButton) {
if (text) {
let svg = getQrCodeSVG(text);
currentSvgContent = svg;
} else {
currentSvgContent = "";
}
qrWrapper.innerHTML = currentSvgContent;
downloadButton.classList[currentSvgContent ? "remove" : "add"]("hidden");
}
function downloadSvg (baseElement) {
const data = (new XMLSerializer()).serializeToString(baseElement.querySelector("svg"));
const svgBlob = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
const dataUrl = URL.createObjectURL(svgBlob);
const a = document.createElement('a');
a.setAttribute('download', 'qrcode.svg');
a.setAttribute('href', dataUrl);
a.setAttribute('target', '_blank');
a.click();
}
function showTab (tabId) {
hideActiveTab();
let tab = tabs[tabId];
document.getElementById(tab.btnId).classList.add("active");
document.getElementById(tab.boxId).style.display = "block";
activeTabId = tabId;
}
function hideActiveTab () {
let tab = tabs[activeTabId];
if (!tab) return;
document.getElementById(tab.btnId).classList.remove("active");
document.getElementById(tab.boxId).style.display = "none";
document.getElementById("btn-stop-qr-scanner").style.display = "none";
}
const LS_COLOR_MODE = "qr-pwa-color-mode";
const LS_APPLY_COLOR_MODE_TO_QR_CODE = "qr-pwa-color-mode-for-qr-code";
function loadAndApplyColorModeFromLocalStorage () {
let colorMode = localStorage.getItem(LS_COLOR_MODE) || "dark-mode";
let applyColorModeToQrCode = (localStorage.getItem(LS_APPLY_COLOR_MODE_TO_QR_CODE) === "true");
setColorMode(colorMode, false);
setColorModeApplicabilityForQrCode(applyColorModeToQrCode, false);
}
function setColorMode (colorMode, saveToLocalStorage=true) {
document.body.setAttribute("data-color-mode", colorMode);
if (saveToLocalStorage) {
localStorage.setItem(LS_COLOR_MODE, colorMode);
}
}
function setColorModeApplicabilityForQrCode (applyColorModeToQrCode, saveToLocalStorage=true) {
const qrWrapper = document.getElementById("div-qr-wrapper");
if (applyColorModeToQrCode) {
qrWrapper.classList.add("apply-color-mode");
} else {
qrWrapper.classList.remove("apply-color-mode");
}
document.getElementById("checkbox-apply-color-mode-to-qr-code").checked = applyColorModeToQrCode;
if (saveToLocalStorage) {
localStorage.setItem(LS_APPLY_COLOR_MODE_TO_QR_CODE, applyColorModeToQrCode);
}
}