-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
167 lines (154 loc) · 4.88 KB
/
script.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"use strict";
let dashbutton_state;
let underscorebutton_state;
var input;
var wordlist = [];
let withoutdigits;
var output;
const inputText = document.querySelector("#titletextbox");
const GenerateButton = document.querySelector("#generate-button");
const DashButton = document.querySelector(".dashbutton");
const UnderscoreButton = document.querySelector(".underscorebutton");
const ClearButton = document.querySelector("#clear");
const ResetButton = document.querySelector("#reset");
const OutputZone = document.querySelector("#outputzone");
const OutputTextArea = document.querySelector("#output-slug");
const DigitButton = document.querySelector("#withoutdigit");
const copybutton = document.querySelector("#copy-button");
// function to prepare for the initial stage
const init = function () {
input = "";
inputText.value = "";
wordlist = [];
output = "";
OutputZone.style.display = "none";
// OutputTextArea.style.display = "none";
// copybutton.style.display = "none";
OutputTextArea.textContent = "";
dashbutton_state = false;
underscorebutton_state = false;
withoutdigits = false;
DashButton.classList.remove("active");
UnderscoreButton.classList.remove("active");
};
// function to remove numeric values from the array
const removedigits = function () {
for (const w of wordlist) {
if (!isNaN(w)) {
const index = wordlist.indexOf(w);
wordlist.splice(index, 1);
}
}
console.log(wordlist);
};
// function to format the string with dash
const dashformatting = function () {
if (withoutdigits) {
removedigits();
}
return wordlist.join("-");
};
// function to format the string with underscore
const underscoreformatting = function () {
if (withoutdigits) {
removedigits();
}
return wordlist.join("_");
};
// function to show the output string
const showOutput = function () {
if (dashbutton_state) {
output = dashformatting();
}
if (underscorebutton_state) {
output = underscoreformatting();
}
OutputZone.style.display = "block";
OutputTextArea.textContent = output;
OutputTextArea.style.display = "inline-block";
};
init();
// function to highlight change in button
const changeButton = function (Button, buttonstate) {
if (buttonstate) {
Button.classList.remove("btn-primary");
Button.classList.add("btn-secondary");
} else {
Button.classList.remove("btn-secondary");
Button.classList.add("btn-primary");
}
};
// dash button handler
DashButton.addEventListener("click", function () {
dashbutton_state = true;
underscorebutton_state = false;
// console.log(DashButton.classList);
DashButton.classList.add("active");
changeButton(DashButton, dashbutton_state);
if (UnderscoreButton.classList.contains("active")) {
UnderscoreButton.classList.remove("active");
}
changeButton(UnderscoreButton, underscorebutton_state);
});
// underscore button handler
UnderscoreButton.addEventListener("click", function () {
dashbutton_state = false;
underscorebutton_state = true;
UnderscoreButton.classList.add("active");
changeButton(UnderscoreButton, underscorebutton_state);
if (DashButton.classList.contains("active")) {
DashButton.classList.remove("active");
}
changeButton(DashButton, dashbutton_state);
});
// button handler to check whether the user wants digit or not in the output string, the radio button
DigitButton.addEventListener("click", function () {
if (DigitButton.checked) {
withoutdigits = true;
} else withoutdigits = false;
});
// button handler to generate slug
GenerateButton.addEventListener("click", function () {
input = inputText.value;
if (input === "") {
window.alert("You have not entered anything yet in the input field.");
} else {
wordlist = input.split(" ");
if (dashbutton_state || underscorebutton_state) {
showOutput();
} else {
window.alert(
"You haven't selected an option. Please select 'Seperate with Dash' or 'Separate with Underscore.'"
);
}
}
});
// button handler to clear text
ClearButton.addEventListener("click", function () {
inputText.value = "";
OutputTextArea.textContent = "";
dashbutton_state = false;
underscorebutton_state = false;
changeButton(DashButton, dashbutton_state);
changeButton(UnderscoreButton, underscorebutton_state);
});
// button handler to reset everything
ResetButton.addEventListener("click", function () {
init();
inputText.value = "";
OutputTextArea.textContent = "";
DigitButton.checked = false;
changeButton(DashButton, dashbutton_state);
changeButton(UnderscoreButton, underscorebutton_state);
});
// button handler to copy the output slug
copybutton.addEventListener("click", function () {
let copytext = OutputTextArea.value;
// console.log(copytext);
if (copytext === "") {
window.alert("There is nothing in the output slug.");
} else {
navigator.clipboard.writeText(copytext);
window.alert(`The text has been copied. ${copytext}`);
}
});