-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcontent.js
More file actions
1009 lines (818 loc) · 28 KB
/
Copy pathcontent.js
File metadata and controls
1009 lines (818 loc) · 28 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const NOT_FOUND = -1;
class ToolTipIcon {
constructor(toolTipElement, toolTipClass, toolTipText, gitHubElement) {
this.toolTipElement = toolTipElement;
this.toolTipClass = toolTipClass;
this.toolTipText = toolTipText;
this.gitHubElement = gitHubElement;
}
createIcon() {
const toolTipContainer = document.createElement('div');
toolTipContainer.className = this.toolTipClass;
const circleIcon = document.createElement('span');
circleIcon.className = 'helpIconCircle';
circleIcon.innerHTML = '?';
const toolTip = document.createElement('span');
toolTip.className = 'helpIconText';
toolTip.innerHTML = this.toolTipText;
toolTipContainer.appendChild(circleIcon);
toolTipContainer.appendChild(toolTip);
this.toolTipElement = toolTipContainer;
}
}
/**
* Function name: checkURL
* Checks the windows current URL for keywords to determine which tooltips
* to display
*/
function checkURL() {
// check if the user wants to edit a file that they are not an owner of
if (checkIsEditingForkedFile()) {
addForkToolTips();
}
// if the user is editing a markdown file
else if (
window.location.href.indexOf('.md') !== NOT_FOUND &&
window.location.href.indexOf('edit') !== NOT_FOUND
) {
addReadMeToolTips();
}
// if the user is reviewing a pull request
else if (window.location.href.indexOf('compare') !== NOT_FOUND) {
addProposeChangesToolTips();
} else if (
window.location.href.indexOf('pull') !== NOT_FOUND &&
window.location.href.indexOf('quick_pull') === NOT_FOUND
) {
addReviewPullRequestTips();
}
// if the user is opening a pull request
else if (document.getElementsByClassName('h-card').length !== 0) {
createProfileCard();
}
// if the user is creating a new issue
else if (
window.location.href.indexOf('issues') !== NOT_FOUND &&
window.location.href.indexOf('new') !== NOT_FOUND
) {
addReportIssueTips();
} else if (
window.location.href.indexOf('issues') !== NOT_FOUND &&
window.location.href.indexOf('new') === NOT_FOUND
) {
addReviewIssueTips();
} else {
// do nothing
}
}
checkURL();
/**
* Function name: checkIsEditingForkedFile
* Checks if the user is viewing a file that they do not own
*/
function checkIsEditingForkedFile() {
try {
// check if there is a pencil icon with this aria label
return (
document.getElementsByClassName('tooltipped')[2].getAttribute('aria-label') ===
'Edit the file in your fork of this project'
);
} catch (error) {
return false;
}
}
/**
* Function name: addProgressBar
* Adds progressBar above forms in GitHub pages to let user how far they are
* in editing files
* @param currentStep current step in process
* @param totalSteps the amount of steps in process to determine overall progress
* @param rootElement className of GitHub HTML element that the progress bar
* will be added to
*/
function addProgressBar(currentStep, totalSteps, rootElement, stepsList) {
// create the progress bar element
const progressBarContainer = document.createElement('div');
progressBarContainer.className = 'container';
const progressBar = document.createElement('div');
progressBar.className = 'progressbar';
const itemList = document.createElement('ul');
let index = 1;
for (index = 1; index <= stepsList.length; index += 1) {
const listItem = document.createElement('li');
listItem.innerHTML = stepsList[index - 1];
if (currentStep === totalSteps) {
listItem.className = 'completed';
} else if (index === currentStep) {
listItem.className = 'partial';
}
// if the user has already completed a step
else if (index < currentStep) {
listItem.className = 'partial completed';
}
itemList.appendChild(listItem);
}
progressBar.appendChild(itemList);
progressBarContainer.appendChild(progressBar);
$(progressBarContainer).insertBefore(rootElement);
if (isProcessCompleted()) {
createSuccessRibbon();
}
}
/**
* Function name: isComplete
* Checks if issue/ pull request was succesfully created and is open in the repo
*/
function isProcessCompleted() {
let status = '';
try {
status = document.getElementsByClassName('State')[0].getAttribute('title');
} catch (error) {
return false;
}
return status === 'Status: Open';
}
/**
* Function name: createSuccessRibbon
* Creates ribbon above progress bar to inform the user that the process is successful
*/
function createSuccessRibbon() {
let processType = '';
if (window.location.href.indexOf('issues') != NOT_FOUND) {
processType = 'issue';
} else {
processType = 'pull request';
}
const successRibbonContainer = document.createElement('div');
successRibbonContainer.className = 'successRibbon';
const ribbonMessage = document.createTextNode(
`The ${processType} was created successfully and will be reviewed shortly`
);
successRibbonContainer.appendChild(ribbonMessage);
$(successRibbonContainer).insertBefore('.container');
}
/**
* Function name: addReadMeToolTips
* Adds tooltips to webpage when editing markdown files
* First step in editing markdown files
*/
function addReadMeToolTips() {
const steps = ['Edit File', 'Confirm Pull Request', 'Pull Request Opened'];
// progress bar above editor
addProgressBar(1, 3, '.js-blob-form', steps);
// icon to right of file name input
const fileNameChangeText =
'This is the file name, changing it will create a new file with the new name';
const breadCrumbDiv = '.d-md-inline-block';
const fileNameChangeIcon = new ToolTipIcon('H4', 'helpIcon', fileNameChangeText, breadCrumbDiv);
fileNameChangeIcon.createIcon();
$(fileNameChangeIcon.toolTipElement).insertAfter(fileNameChangeIcon.gitHubElement);
// banner above commit message input
const commitTitleText =
'This is the title. Give a brief description of the change. Be short and objective.';
const inputTitleLabel = document.createElement('h3');
inputTitleLabel.innerHTML = 'Insert a title here';
inputTitleLabel.style.display = 'inline-block';
inputTitleLabel.style.marginRight = '20px';
$(inputTitleLabel).insertBefore('#commit-summary-input');
const commitMessageIcon = new ToolTipIcon(
'H4',
'helpIcon',
commitTitleText,
'#commit-summary-input'
);
commitMessageIcon.createIcon();
$(commitMessageIcon.toolTipElement).insertAfter('#commit-summary-input');
const descriptionText =
'Add a more detailed description if needed. Here you can present your arguments and reasoning that lead to change.';
const inputDescriptionLabel = document.createElement('h3');
inputDescriptionLabel.innerHTML = 'Insert a <br> description here';
inputDescriptionLabel.style.display = 'inline-block';
inputDescriptionLabel.style.marginRight = '22px';
$(inputDescriptionLabel).insertBefore('#commit-description-textarea');
const extendedDescIcon = new ToolTipIcon(
'H4',
'helpIcon',
descriptionText,
'#commit-description-textarea'
);
extendedDescIcon.createIcon();
$(extendedDescIcon.toolTipElement).insertAfter(extendedDescIcon.gitHubElement);
const commitChangesDirectlyText =
'By clicking the Commit Changes button the changes will automatically be pushed to the repo';
const submitChangesIcon = new ToolTipIcon(
'H4',
'helpIcon',
commitChangesDirectlyText,
'#submit-file'
);
submitChangesIcon.createIcon();
submitChangesIcon.toolTipElement.style.marginRight = '20px';
$(submitChangesIcon.toolTipElement).insertBefore(submitChangesIcon.gitHubElement);
}
// On pull request step 1, toggle icon text to help inform user
let onDirectPull = true;
let iconText = '';
const pullChangesText =
'By clicking the Propose changes button you will start the submission process. You will have the chance to check your changes before finalizing it.';
$('input[name="commit-choice"]').click(() => {
document.getElementsByClassName('helpIcon')[3].remove();
if (onDirectPull) {
iconText = pullChangesText;
onDirectPull = false;
} else {
iconText =
'By clicking the Commit Changes button the changes will be directly pushed to the repo';
onDirectPull = true;
}
const submitChangesIcon = new ToolTipIcon('H4', 'helpIcon', iconText, '#submit-file');
submitChangesIcon.createIcon();
submitChangesIcon.toolTipElement.style.marginRight = '20px';
$(submitChangesIcon.toolTipElement).insertBefore(submitChangesIcon.gitHubElement);
});
/**
* Function name: addProposeChangesToolTips
* Adds tooltips to webpage when confirming a change to file
* Second step in editing markdown files
*/
function addProposeChangesToolTips() {
const steps = ['Edit File', 'Create Pull Request', 'Pull Request Opened'];
addProgressBar(2, 3, '.repository-content', steps);
$('#pull_request_body').attr(
'placeholder',
'You can add a more detailed description here if needed.'
);
try {
var branchName = document.getElementsByClassName('branch-name')[0].innerText;
} catch {
var isComparingBranch = true;
}
let newHeaderText = `Finish the pull request submission below to allow others to accept the changes. These changes can be viewed later under the branch name: ' +
${branchName}`;
if (isComparingBranch) {
newHeaderText =
'Finish the pull request submission below to allow others to accept the changes';
$('.gh-header-title').text('Create Pull Request');
}
$('.gh-header-meta').text(newHeaderText);
let pullRequestTitle = document.getElementsByClassName('gh-header-title')[1];
pullRequestTitle.innerHTML = 'Create pull request';
const branchContainerText =
'This represents the origin and destination of your changes if you are not sure, leave it how it is, this is common for small changes.';
const topRibbon = document.getElementsByClassName('js-range-editor')[0];
topRibbon.style.width = '93%';
topRibbon.style.display = 'inline-block';
// ribbon above current current branch and new pull request branch
const currentBranchIcon = new ToolTipIcon(
'H4',
'helpIcon',
branchContainerText,
'.js-range-editor'
);
currentBranchIcon.createIcon();
$(currentBranchIcon.toolTipElement).insertAfter(currentBranchIcon.gitHubElement);
// move button row to left side of editor
const buttonRow = document.getElementsByClassName('d-flex flex-justify-end m-2')[0];
buttonRow.classList.remove('flex-justify-end');
buttonRow.classList.add('flex-justify-start');
const confirmPullRequestText =
'By clicking this button you will create the pull request to allow others to view your changes and accept them into the repository.';
const submitButtonClass = '.js-pull-request-button';
// icon next to create pull request button
const createPullRequestBtn = new ToolTipIcon(
'H4',
'helpIcon',
confirmPullRequestText,
submitButtonClass
);
createPullRequestBtn.createIcon();
$(createPullRequestBtn.toolTipElement).insertAfter(createPullRequestBtn.gitHubElement);
const summaryText =
'This shows the amount of commits in the pull request, the amount of files you changed in the pull request, how many comments were on the commits for the pull request and the ammount of people who worked together on this pull request.';
const summaryClass = '.overall-summary';
// override the container width and display to add icon
const numbersSummaryContainer = document.getElementsByClassName('overall-summary')[0];
numbersSummaryContainer.style.width = '93%';
numbersSummaryContainer.style.display = 'inline-block';
// icon above summary of changes and commits
const requestSummaryIcon = new ToolTipIcon('H4', 'helpIcon', summaryText, summaryClass);
requestSummaryIcon.createIcon();
requestSummaryIcon.toolTipElement.style = 'float:right;';
$(requestSummaryIcon.toolTipElement).insertAfter(requestSummaryIcon.gitHubElement);
const comparisonClass = '.details-collapse';
const changesText =
'This shows the changes between the orginal file and your version. Green(+) represents lines added. Red(-) represents removed lines';
// icon above container for changes in current pull request
const comparisonIcon = new ToolTipIcon('H4', 'helpIcon', changesText, comparisonClass);
comparisonIcon.createIcon();
const commitSummaryContainer = document.getElementsByClassName('details-collapse')[0];
commitSummaryContainer.style.width = '93%';
commitSummaryContainer.style.display = 'inline-block';
$(comparisonIcon.toolTipElement).insertAfter(comparisonIcon.gitHubElement);
}
/**
* Function name: addReviewPullRequestTips
* Adds tooltips to webpage when reviewing pull requests
* Third step in editing markdown files
*/
function addReviewPullRequestTips() {
const steps = ['Edit File', 'Confirm Pull Request', 'Pull Request Opened'];
addProgressBar(3, 3, '.gh-header-show', steps);
const titleTest = 'new'; // document.getElementsByClassName('js-issue-title')[0];
const branchContainerText =
'This indicates that the pull request is open meaning someone will get to it soon.';
const pullRequestStatusIcon = new ToolTipIcon(
'H4',
'helpIcon',
branchContainerText,
'.js-clipboard-copy'
);
pullRequestStatusIcon.createIcon();
$(pullRequestStatusIcon.toolTipElement).insertAfter(pullRequestStatusIcon.gitHubElement);
const requestButtonsText =
'This will close the pull request meaning people cannot view this! Do not click close unless the request was solved.';
const requestButtonsClass = '.js-comment-and-button';
const closePullRequestIcon = new ToolTipIcon(
'H4',
'helpIcon',
requestButtonsText,
requestButtonsClass
);
closePullRequestIcon.createIcon();
$(closePullRequestIcon.toolTipElement).insertBefore(closePullRequestIcon.gitHubElement);
closePullRequestIcon.toolTipElement.style.marginRight = '20px';
/*
var submitButtons = document.getElementsByClassName('d-flex flex-justify-end')[0];
submitButtons.classList.remove('flex-justify-end');
submitButtons.classList.add('flex-justify-start');*/
$('.js-quick-submit-alternative').click((event) => {
if (!Confirm(`Are you sure that you want to close the pull request: ${titleTest}?`)) {
event.preventDefault();
}
});
}
/**
* Function name: addForkToolTips
* Edits tooltips when viewing a repository that you are not a contributor of
*/
function addForkToolTips() {
$('.tooltipped-nw:nth-child(2)').attr('aria-label', 'Edit Readme');
}
/**
* Function name: addIssueTips
* Adds tolltips to page when opening a new issue report
*/
function addReportIssueTips() {
const steps = ['Report Issue', 'confirm Issue Report', 'Issue Submitted'];
// progress bar above editor
addProgressBar(1, 3, '.new_issue', steps);
const submitButtonText = 'After clicking this, you will have a chance to update the issue report';
const submitButtonClass = '.flex-justify-end button:eq(0)';
const submitButtonIcon = new ToolTipIcon('H4', 'helpIcon', submitButtonText, submitButtonClass);
submitButtonIcon.createIcon();
$(submitButtonIcon.toolTipElement).insertAfter(submitButtonIcon.gitHubElement);
}
/**
* Function name: addIssueTips
* Adds tolltips to page when reviewing a new issue report
*/
function addReviewIssueTips() {
const issueTitle = document.getElementsByClassName('js-issue-title')[0].innerText;
const steps = ['Report Issue', 'confirm Issue Report', 'Issue Submitted'];
addProgressBar(3, 3, '.repository-content', steps);
const buttonRow = document.getElementsByClassName('d-flex flex-justify-end')[0];
buttonRow.classList.remove('flex-justify-end');
buttonRow.classList.add('flex-justify-start');
const closeIssueIconText =
'This will close the issue request meaning people cannot view this! Do not click close unless the request was solved. ';
const submitButtonClass = '.flex-justify-end button:eq(0)';
const submitButtonIcon = new ToolTipIcon('H4', 'helpIcon', closeIssueIconText, submitButtonClass);
submitButtonIcon.createIcon();
submitButtonIcon.toolTipElement.style.marginRight = '20px';
$(submitButtonIcon.toolTipElement).insertBefore(submitButtonIcon.gitHubElement);
$('.js-quick-submit-alternative').click(function (event) {
if (!confirm(`Are you sure that you want to close the issue: ${issueTitle}?`)) {
event.preventDefault();
}
});
}
/**
* function getCommits
* @param string username - GitHub username for API
* Uses GitHub API to view commit totals for user
*/
async function getCommits(repositories, username) {
const oAuthToken = '';
let repoObject = {};
let ctx = document.getElementById('repositories');
let skillGraphContainer = document.getElementById('skillGraph');
const barColors = [];
const headers = {
Authorization: 'Token ' + oAuthToken,
};
for (const repo of repositories) {
const commitUrl = `https://api.github.com/repos/${username}/${repo}/commits?page=1&per_page=25`;
const commitResponse = await fetch(commitUrl, {
method: 'GET',
headers: headers,
});
let commitResult = await commitResponse.json();
repoObject[repo] = commitResult.length;
barColors.push(getRandomColor());
}
const labels = Object.keys(repoObject);
const data = Object.values(repoObject);
labels.sort((a, b) => {
return repoObject[b] - repoObject[a];
});
data.sort((a, b) => {
return b - a;
});
myBarChart = new Chart(skillGraphContainer, {
type: 'bar',
data: {
labels: labels,
datasets: [
{
label: 'Commits',
backgroundColor: barColors,
data: data,
},
],
},
options: {
responsive: false,
legend: { display: false },
title: {
display: true,
text: `Commits per repository for: ${username}`,
},
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
max: 30,
stepSize: 1,
},
},
],
xAxes: [
{
ticks: {
fontSize: 8,
callback: function (value) {
if (value.length > 4) {
return value.substr(0, 4) + '...'; //truncate
} else {
return value;
}
},
},
},
],
},
animation: {
duration: 1,
onProgress: function () {
var chartInstance = this.chart,
ctx = chartInstance.ctx;
ctx.font = Chart.helpers.fontString(
Chart.defaults.global.defaultFontSize,
Chart.defaults.global.defaultFontStyle,
Chart.defaults.global.defaultFontFamily
);
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
this.data.datasets.forEach(function (dataset, i) {
var meta = chartInstance.controller.getDatasetMeta(i);
meta.data.forEach(function (bar, index) {
if (dataset.data[index] > 0) {
var data = dataset.data[index];
ctx.fillText(data, bar._model.x, bar._model.y);
}
});
});
},
},
},
});
}
/**
* function getRepos
* @param string username - GitHub username for API
* Uses GitHub API to view programming languages for user
*/
async function getRepos(username) {
const oAuthToken = '';
const url = `https://api.github.com/users/${username}/repos`;
const response = await fetch(url, {
method: 'GET',
headers: {
Authorization: `Token ${oAuthToken}`,
},
});
const result = await response.json();
const languages = [];
const repositoryNames = [];
let labels = {};
let dataSet = {};
const barColors = [];
result.forEach((index) => {
if (index.language != null) {
languages.push(index.language);
repositoryNames.push(index.name);
barColors.push(getRandomColor());
}
});
getCommits(repositoryNames, username);
const repoGraphContainer = document.getElementById('myChart');
const repositoriesObject = {};
for (let index = 0; index < languages.length; index += 1) {
if (!repositoriesObject[languages[index]]) {
repositoriesObject[languages[index]] = 0;
}
repositoriesObject[languages[index]] += 1;
}
labels = Object.keys(repositoriesObject);
dataSet = Object.values(repositoriesObject);
// use b - a for desc order and a - b for asc order
labels.sort((a, b) => {
return repositoriesObject[b] - repositoriesObject[a];
});
dataSet.sort((a, b) => {
return b - a;
});
myBarChart = new Chart(repoGraphContainer, {
type: 'bar',
data: {
labels: labels,
datasets: [
{
label: 'Repositories',
backgroundColor: barColors,
data: dataSet,
},
],
},
options: {
responsive: false,
legend: { display: false },
title: {
display: true,
text: `Programming languge totals for ${username}'s repositories`,
},
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
stepSize: 1,
},
},
],
},
animation: {
duration: 1,
onProgress: function () {
var chartInstance = this.chart,
ctx = chartInstance.ctx;
ctx.font = Chart.helpers.fontString(
Chart.defaults.global.defaultFontSize,
Chart.defaults.global.defaultFontStyle,
Chart.defaults.global.defaultFontFamily
);
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
this.data.datasets.forEach(function (dataset, i) {
var meta = chartInstance.controller.getDatasetMeta(i);
meta.data.forEach(function (bar, index) {
if (dataset.data[index] > 0) {
var data = dataset.data[index];
ctx.fillText(data, bar._model.x, bar._model.y);
}
});
});
},
},
},
});
}
/**
* Function: createCardContainer
* Creates structure of profile overview
*/
function createCardContainer() {
const outerContainer = document.getElementsByClassName('graph-before-activity-overview')[0];
outerContainer.className += ' card-container';
const cardBack = document.createElement('div');
cardBack.className = 'back';
const repoGraph = document.createElement('canvas');
repoGraph.className = 'graph';
repoGraph.style.borderRight = '1px solid black';
repoGraph.style.borderBottom = '1px solid black';
repoGraph.style.float = 'left';
repoGraph.id = 'myChart';
const skillGraph = document.createElement('canvas');
skillGraph.className = 'graph';
skillGraph.style.borderBottom = '1px solid black';
skillGraph.id = 'skillGraph';
skillGraph.style.float = 'right';
const commitsGraph = document.createElement('canvas');
commitsGraph.className = 'graph';
commitsGraph.style.borderRight = '1px solid black';
commitsGraph.style.float = 'left';
commitsGraph.id = 'commitsGraph';
const languagesGraph = document.createElement('canvas');
languagesGraph.className = 'graph';
languagesGraph.id = 'languagesGraph';
languagesGraph.style.float = 'right';
cardBack.appendChild(repoGraph);
cardBack.appendChild(skillGraph);
cardBack.appendChild(commitsGraph);
cardBack.appendChild(languagesGraph);
outerContainer.appendChild(cardBack);
}
/**
* Function name: createProfileCard
* Creates a 2x2 grid behind contribution graph on profile page with graphs
*/
function createProfileCard() {
const profileCardIconText = 'Click this tooltip to show more info about the user';
const contributionGraphClass = '.js-calendar-graph';
const showGraphIcon = new ToolTipIcon(
'H4',
'helpIcon graph-tooltip',
profileCardIconText,
contributionGraphClass
);
const username = document.getElementsByClassName('vcard-username')[0].innerHTML;
createCardContainer();
getRepos(username);
getApis(username);
showGraphIcon.createIcon();
$(showGraphIcon.toolTipElement).insertBefore(showGraphIcon.gitHubElement);
$('.helpIcon').click(() => {
if ($('.helpIconCircle').text() === '?') {
$('.helpIconCircle').text('X');
$('.helpIconText').addClass('removeText');
} else {
$('.helpIconCircle').text('?');
$('.helpIconText').removeClass('removeText');
}
$('.back').toggleClass('hovered');
$('#js-contribution-activity').toggleClass('hidden');
$('#user-activity-overview').toggleClass('hidden');
});
}
function getApis(username) {
const url = chrome.runtime.getURL('result.json');
fetch(url)
.then((response) => response.json()) // assuming file contains json
.then((json) => createApiGraph(json, username));
}
const colors = [
'#3e95cd',
'#8e5ea2',
'#3cba9f',
'#e8c3b9',
'#c45850',
'#3e95cd',
'#8e5ea2',
'#3cba9f',
'#e8c3b9',
'#c45850',
'#3e95cd',
'#8e5ea2',
'#3cba9f',
'#e8c3b9',
'#c45850',
];
function manageProgressBar() {
$('.progressbar').toggleClass('hiddenDisplay');
}
function manageIcons() {
$('.helpIcon').toggleClass('hiddenDisplay');
/*
chrome.storage.local.get('iconStatus', function(status) {
let iconStatus = status.iconStatus;
if(iconStatus) {
document.getElementById('iconBtn').checked = true;
} else {
document.getElementById('iconBtn').checked = false;
}
});
*/
}
function manageRibbon() {
$('.successRibbon').toggleClass('hiddenDisplay');
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'progress_bar') {
manageProgressBar();
} else if (request.message === 'icon') {
manageIcons();
} else if (request.message === 'ribbon') {
manageRibbon();
}
});
/**
* Function name: createApiGraph
* @param {JSON} userData
* creates graph on user profile card about langauges and apis
*/
function createApiGraph(userData, username) {
const apiGraphContainer = document.getElementById('commitsGraph');
const apis = [];
const apiTotals = [];
const languages = [];
let total = 0;
userData.Repos.forEach((index) => {
index.API.apis.forEach((api) => {
if (total < 10) {
apis.push(api.name);
apiTotals.push(api.count);
total += 1;
}
});
index.API.langs.forEach((language) => {
languages.push(language);
});
});
myBarChart = new Chart(apiGraphContainer, {
type: 'bar',
data: {
labels: apis,
datasets: [
{
label: 'Total: ',
backgroundColor: colors,
data: apiTotals,
},
],
},
options: {
responsive: false,
legend: { display: false },
title: {
display: true,
text: `Api Totals for ${username}`,
},
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
stepSize: 1,
},
},
],
xAxes: [
{
ticks: {
fontSize: 8,
callback: function (value) {
if (value.length > 4) {
return value.substr(0, 4) + '...'; //truncate
} else {
return value;
}
},
},
},
],
},
animation: {
duration: 1,
onProgress: function () {
var chartInstance = this.chart,
ctx = chartInstance.ctx;
ctx.font = Chart.helpers.fontString(
Chart.defaults.global.defaultFontSize,
Chart.defaults.global.defaultFontStyle,
Chart.defaults.global.defaultFontFamily
);
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
this.data.datasets.forEach(function (dataset, i) {
var meta = chartInstance.controller.getDatasetMeta(i);
meta.data.forEach(function (bar, index) {
if (dataset.data[index] > 0) {
var data = dataset.data[index];
ctx.fillText(data, bar._model.x, bar._model.y);
}
});
});
},
},
},
});
}