-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
193 lines (160 loc) · 6.34 KB
/
script.js
File metadata and controls
193 lines (160 loc) · 6.34 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const easyMDE = new EasyMDE({minWidth : "500px", placeholder: "(Optional) Detailed description of the commit."});
window.onload = () => {
// locate doms
const input_type = document.getElementById("commit_type");
const input_title = document.getElementById("commit_title");
const advanced_tab = document.getElementById("advanced");
const advanced_enabled = document.getElementById("advanced_enabled");
const advanced_breaking = document.getElementById("advanced_breaking");
const advanced_issue = document.getElementById("advanced_issue");
const advanced_scope = document.getElementById("advanced_scope");
const button_generate = document.getElementById("generate");
const button_copy = document.getElementById("copy");
const button_clear = document.getElementById("clear");
const clearables = document.getElementsByClassName("clearable");
const modal = document.getElementById("result_modal");
const command_display = document.getElementById("command_display");
const modal_title = document.getElementById("modal_title");
const spinner = document.getElementById("spinner");
//global param
let full_command = "";
/**
* @name validationCheck
* @function
* @description function that checks if required inputs are filled.
* This generator's required arguments are : commit type and title.
* every time this function is called (by 'onchange' event)
* it checks if input_type and input_title is changed and properly filled.
* It then enables the generate button only when both input is ready.
*/
var validationCheck = ()=>{
if(input_type.value!="unchosen" && input_title.value.length>0) {
if(button_generate.classList.contains("disabled")){
button_generate.classList.remove("disabled");
button_generate.classList.remove("btn-secondary");
button_generate.classList.add("btn-primary");
}
}
else {
if(!button_generate.classList.contains("disabled")){
button_generate.classList.add("disabled");
button_generate.classList.remove("btn-primary");
button_generate.classList.add("btn-secondary");
}
}
};
/**
* @name controlClearButton
* @function
* @description This function controls clear button.
* The clear button is enabled only when any of the inputs on control is filled.
*/
var controlClearButton = () =>{
const type_empty = (input_type.value=="unchosen");
const title_empty = (input_title.value=="");
const desc_empty = (easyMDE.value()=="");
const breaking_empty = !(advanced_breaking.checked);
const issue_empty = (advanced_issue.value=="");
const scope_empty = (advanced_scope.value=="");
const all_empty = type_empty&&title_empty&&desc_empty&&breaking_empty&&issue_empty&&scope_empty;
if(!all_empty) {
if(button_clear.classList.contains("disabled")){
button_clear.classList.remove("disabled");
button_clear.classList.remove("btn-outline-danger");
button_clear.classList.add("btn-danger");
}
} else {
if(!button_clear.classList.contains("disabled")) {
button_clear.classList.add("disabled");
button_clear.classList.add("btn-outline-danger");
button_clear.classList.remove("btn-danger");
}
}
};
/**
* @name generateCommand
* @function
* @description function for generating the command and showing
* it on the modal.
*/
var generateCommand = () =>{
const type = input_type.value;
const title = input_title.value;
const desciption = easyMDE.value();
const is_advanced_mode = advanced_enabled.checked;
let breaking = "";
let scope = "";
let issue = "";
if(is_advanced_mode) {
if(advanced_breaking.checked) breaking = "!";
if(advanced_scope.value.length>0) scope = `(${advanced_scope.value})`;
if(advanced_issue.value.length>0) issue = `(#${advanced_issue.value})`;
}
let command_desc = "";
if (desciption.length>0) command_desc=" -m \""+desciption+"\"";
const command_title = type+scope+breaking+ " : "+title+issue;
full_command = "git commit -m \""+command_title+"\""+command_desc;
spinner.classList.add("visually-hidden");
command_display.value=full_command;
modal_title.textContent="Command is generated!";
command_display.classList.remove("visually-hidden");
};
/**
* @name copyCommand
* @function
* @description function for copying the generated command to clipboard.
*/
var copyCommand = () =>{
navigator.clipboard.writeText(full_command).then(()=>{
const popover = bootstrap.Popover.getOrCreateInstance("#copy");
popover.show();
setTimeout(()=>{popover.hide();},2000);
});
}
/**
* @name clearInputs
* @function
* @description function for clearing all the inputs into initial state.
*/
var clearInputs = () => {
if(confirm("Do you really want to clear all the inputs?")==true) {
input_type.value="unchosen";
input_title.value="";
easyMDE.value("");
advanced_enabled.checked = false;
advanced_breaking.checked = false;
advanced_issue.value = "";
advanced_scope.value = "";
button_clear.classList.add("disabled");
button_clear.classList.add("btn-outline-danger");
button_clear.classList.remove("btn-danger");
advanced_tab.classList.remove("show");
validationCheck();
}
}
/**
* @name reinitailizeModal
* @function
* @description turns back the modal into default state.
*/
var reinitailizeModal = ()=>{
modal_title.textContent="Generating...";
command_display.value="If you see this, there was an error in finishing generation.";
command_display.classList.add("visually-hidden");
spinner.classList.remove("visually-hidden");
}
// adding event listeners to input doms.
input_type.addEventListener("change",validationCheck);
input_title.addEventListener("input",validationCheck);
easyMDE.codemirror.on("change", controlClearButton);
//event listeners for clear button enablization.
for(var i=0; i<clearables.length;i++) {
clearables[i].addEventListener("input",controlClearButton);
}
// linking buttons to their corresponding executing functions.
button_generate.addEventListener("click",generateCommand);
button_copy.addEventListener("click",copyCommand);
button_clear.addEventListener("click",clearInputs);
// modal functionality stabilization.
modal.addEventListener("hidden.bs.modal",reinitailizeModal);
}