-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.js
More file actions
59 lines (56 loc) · 2.04 KB
/
Copy pathapplication.js
File metadata and controls
59 lines (56 loc) · 2.04 KB
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
function application() {
const setError = (msg, courceErrorPos) => {
document.querySelector(`.error${courceErrorPos}`).innerText = msg;
};
const clearError = () => {
document.querySelector(".errorLeft").innerText = "";
document.querySelector(".errorRight").innerText = "";
};
const leftInput = document.querySelector(".input");
const rightInput = document.querySelector(".output");
const camelRadio = document.querySelector('input[type="radio"][value="1"]');
const transHandler = (e) => {
const isO2C = e.target.classList.contains("object2CssText");
const sourceInput = isO2C ? leftInput : rightInput;
const targetInput = isO2C ? rightInput : leftInput;
const courceErrorPos = sourceInput === leftInput ? "Left" : "Right";
if (!sourceInput.value) return setError("Please input", courceErrorPos);
try {
if (isO2C) {
eval(`var cssObj = ${sourceInput.value}`);
if (!isObject(cssObj))
return setError("The input is not a css object", courceErrorPos);
targetInput.value = CssObjectTransfer.object2CssText(cssObj);
} else {
targetInput.value = CssObjectTransfer.cssText2Object(
sourceInput.value,
camelRadio.checked
);
}
clearError();
} catch (error) {
setError(error.message, courceErrorPos);
}
};
document
.querySelectorAll(".transBtn")
.forEach((v) => v.addEventListener("click", transHandler));
document.querySelectorAll('input[type="radio"]').forEach((v) =>
v.addEventListener("change", () => {
if (!leftInput.value) return undefined;
eval(`var cssObj = ${leftInput.value}`);
if (!isObject(cssObj))
return setError("The input is not a css object", true);
leftInput.value = JSON.stringify(
CssObjectTransfer.formatObjectKeyValue(cssObj, v.value === "1"),
null,
4
);
})
);
document.querySelectorAll(".clear").forEach((v) =>
v.addEventListener("click", (e) => {
e.target.parentNode.querySelector("textarea").value = "";
})
);
}