-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandHelper.js
143 lines (120 loc) · 4.09 KB
/
commandHelper.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
140
141
142
143
// Get DOM elements
const buttonContainer = document.getElementById("buttonContainer");
const commandOutput = document.getElementById("commandOutput");
const clearButton = document.getElementById("clearButton");
const copyButton = document.getElementById("copyButton");
const saveButton = document.getElementById("saveButton");
const historyContainer = document.getElementById("historyContainer");
let displayCommand = ""
// Load data from JSON file
fetch("data.json")
.then(response => response.json())
.then(data => {
const buttonGroups = data.buttonGroups;
const numHistoryData = data.numHistoryData;
let selectedButtons = [];
// Generate button groups and options
buttonGroups.forEach(group => {
const groupDiv = generateButtonGroup(group);
buttonContainer.appendChild(groupDiv);
});
// Add event listeners to buttons
const buttonOptions = document.querySelectorAll(".buttonOption");
buttonOptions.forEach(button => {
button.addEventListener("click", () => {
button.classList.toggle("selected");
updateSelectedButtons();
updateCommandOutput();
});
});
// Add event listener to clear button
clearButton.addEventListener("click", () => {
clearSelection();
});
// Add event listener to copy button
copyButton.addEventListener("click", () => {
copyCommandOutput();
});
// Add event listener to save button
saveButton.addEventListener("click", () => {
saveCommand();
clearSelection();
});
// Generate initial command output
updateSelectedButtons();
updateCommandOutput();
// Generate command history
let commandHistory = [];
generateCommandHistory();
// Helper functions
function generateButtonGroup(group) {
const groupDiv = document.createElement("div");
groupDiv.classList.add("buttonGroup");
const label = document.createElement("label");
label.textContent = group.groupName;
groupDiv.appendChild(label);
group.options.forEach(option => {
const button = document.createElement("button");
button.classList.add("buttonOption");
button.textContent = option.name;
button.dataset.value = option.value;
groupDiv.appendChild(button);
});
return groupDiv;
}
function updateSelectedButtons() {
selectedButtons = Array.from(document.querySelectorAll(".buttonOption.selected"));
}
function generateCommand() {
let command = "";
selectedButtons.forEach(button => {
command += " " + button.dataset.value;
});
return command;
}
function updateCommandOutput() {
const command = generateCommand();
commandOutput.textContent = command;
}
function clearSelection() {
commandOutput.textContent = "";
selectedButtons.forEach(button => {
button.classList.remove("selected");
});
}
function copyCommandOutput() {
const command = generateCommand();
const tempInput = document.createElement("input");
tempInput.value = command;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand("copy");
document.body.removeChild(tempInput);
alert("Command copied to clipboard!");
}
function saveCommand() {
const command = generateCommand();
commandHistory.push(command);
if (commandHistory.length > numHistoryData) {
commandHistory.shift();
}
clearSelection();
generateCommandHistory();
}
function generateCommandHistory() {
historyContainer.innerHTML = "";
const historyTitle = document.createElement("div");
historyTitle.classList.add("historyTitle");
historyTitle.textContent = "Chat History";
historyContainer.appendChild(historyTitle);
commandHistory.forEach(command => {
const historyItem = document.createElement("div");
historyItem.classList.add("historyItem");
historyItem.textContent = command;
historyContainer.appendChild(historyItem);
});
}
})
.catch(error => {
console.error("Error loading data:", error);
});