-
-
Notifications
You must be signed in to change notification settings - Fork 686
/
script.js
264 lines (229 loc) · 7.48 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const elements = {
toggleBtn: document.querySelector('.toggle-btn'),
bgColor: document.querySelector('.bg-color'),
loader: document.querySelector('.loader'),
placeholder: document.querySelector('.chart_placeholder'),
copyContainer: document.querySelector('.copy_container'),
textArea: document.querySelector('.text'),
copyText: document.querySelector('.copy_text'),
submitButton: document.getElementById('submit-button'),
username: document.getElementById('username')
};
const inputElements = {
bgInput: document.getElementById('bgColor'),
lineInput: document.getElementById('line'),
ltgInput: document.getElementById('color'),
pointInput: document.getElementById('point')
};
const valueToCopy = {
username: '',
bgColor: '#ffcfe9',
color: '#9e4c98',
line: '#9e4c98',
point: '#403d3d'
};
/*-----------------------------------------
Fetch user's contribution data
and generate graph link
-------------------------------------------*/
//For displaying loading animation
const setLoader = () => {
elements.loader.classList.add('active');
};
const removeLoader = () => {
elements.loader.classList.remove('active');
};
const removePlaceholder = () => {
elements.placeholder.classList.remove('active');
elements.copyContainer.style.marginTop = '2rem';
};
// Generate chart link with user data
const generateLink = () => {
let link = `[![Ashutosh's github activity graph](https://github-readme-activity-graph.vercel.app/graph?username=${
valueToCopy.username
}&bg_color=${valueToCopy.bgColor.slice(1)}&color=${valueToCopy.color.slice(
1
)}&line=${valueToCopy.line.slice(1)}&point=${valueToCopy.point.slice(
1
)}&area=true&hide_border=true)](https://github.com/ashutosh00710/github-readme-activity-graph)`;
elements.textArea.value = link;
elements.copyText.childNodes[3].style.backgroundColor = 'rgb(87, 132, 245)';
return link;
};
// Fetch user data and create graph
const getGraph = (username) => {
// Graph Configuration
const options = {
height: 380,
axisY: {
title: 'Contributions',
onlyInteger: true,
offset: 70,
labelOffset: {
y: 4.5
}
},
axisX: {
title: 'Days',
offset: 50,
labelOffset: {
x: -4.5
}
},
chartPadding: {
top: 80,
right: 50,
bottom: 20,
left: 20
},
showArea: true,
fullWidth: true,
plugins: [
Chartist.plugins.ctAxisTitle({
axisX: {
axisTitle: 'Days',
axisClass: 'ct-axis-title',
offset: {
x: 0,
y: 50
},
textAnchor: 'middle'
},
axisY: {
axisTitle: 'Contributions',
axisClass: 'ct-axis-title',
offset: {
x: 0,
y: 50
},
textAnchor: 'middle',
flipTitle: true
}
})
]
};
axios({
url: `https://github-readme-activity-graph.vercel.app/data?username=${username}`,
method: 'GET'
})
.then((contributionData) => {
const days = contributionData.data.contributions;
const data = {
labels: days.map((day) => day.date),
series: [{ value: days.map((day) => day.contributionCount) }]
};
new Chartist.Line('.ct-chart', data, options); //Create chart
removeLoader();
removePlaceholder();
generateLink();
})
.catch((err) => {
console.error(err);
removeLoader();
alert('Sorry! something went wrong.');
});
};
// Handle submit button
const onSubmit = (event) => {
event.preventDefault();
//get username
const username = elements.username.value;
if (username.length > 0) {
valueToCopy.username = username;
setLoader();
getGraph(username);
} else {
alert('Enter your Username');
return;
}
};
/*-----------------------------------------
Dark Theme
-------------------------------------------*/
let darkMode = localStorage.getItem('darkMode');
const enableDarkMode = () => {
elements.bgColor.classList.add('active');
localStorage.setItem('darkMode', 'enabled');
};
const disableDarkMode = () => {
elements.bgColor.classList.remove('active');
localStorage.setItem('darkMode', '');
};
if (darkMode === 'enabled') {
enableDarkMode();
}
const handleDarkMode = () => {
darkMode = localStorage.getItem('darkMode');
if (darkMode !== 'enabled') {
enableDarkMode();
} else {
disableDarkMode();
}
};
//Dynamic changes
// Background Color
const changeBgColor = () => {
valueToCopy.bgColor = inputElements.bgInput.value;
document.querySelector('.rect').style.backgroundColor = inputElements.bgInput.value;
generateLink();
};
// Line Color
const changeLineColor = () => {
valueToCopy.line = inputElements.lineInput.value;
document.querySelector('.ct-line').style.stroke = inputElements.lineInput.value;
generateLink();
};
//Lables, Title and Grid color
const changeLabelsAndGridColor = () => {
const ltgValue = inputElements.ltgInput.value;
valueToCopy.color = ltgValue;
const allLables = document.querySelectorAll('.ct-label');
const axisTitles = document.querySelectorAll('.ct-axis-title');
const grids = document.querySelectorAll('.ct-grid');
allLables.forEach((el) => {
el.style.fill = ltgValue;
el.style.color = ltgValue;
});
axisTitles.forEach((el) => {
el.style.fill = ltgValue;
});
grids.forEach((el) => {
el.style.stroke = ltgValue;
});
generateLink();
};
// Point Color
const changePointColor = () => {
valueToCopy.point = inputElements.pointInput.value;
const allPointLables = document.querySelectorAll('.ct-point');
const len = allPointLables.length;
for (let idx = 0; idx < len; idx++) {
allPointLables[idx].style.stroke = inputElements.pointInput.value;
}
generateLink();
};
// Copy to clipboard
const copyLink = () => {
if (elements.textArea.value !== '') {
let copyBtn = elements.copyText.childNodes[3];
if (copyBtn.style.backgroundColor === 'rgb(87, 132, 245)') {
const copiedText = elements.textArea.value;
navigator.clipboard.writeText(copiedText);
const input = elements.copyText.querySelector('input.text');
input.select();
copyBtn.style.backgroundColor = '#2bbe60';
elements.copyText.classList.add('active');
setTimeout(() => {
elements.copyText.classList.remove('active');
}, 2500);
}
}
};
//Event Handlers
elements.toggleBtn.addEventListener('click', handleDarkMode);
elements.submitButton.addEventListener('click', onSubmit);
elements.copyText.addEventListener('click', copyLink);
inputElements.bgInput.addEventListener('input', changeBgColor);
inputElements.lineInput.addEventListener('input', changeLineColor);
inputElements.ltgInput.addEventListener('input', changeLabelsAndGridColor);
inputElements.pointInput.addEventListener('input', changePointColor);