Skip to content

Commit

Permalink
Refactor the codebase (#213)
Browse files Browse the repository at this point in the history
* refactor the codebase.

* rename recent contributors utility file.
  • Loading branch information
Rishabh570 authored and jywarren committed Jun 3, 2019
1 parent 4da40fc commit 30dd907
Show file tree
Hide file tree
Showing 15 changed files with 1,031 additions and 898 deletions.
867 changes: 469 additions & 398 deletions dist/community-toolbox.js

Large diffs are not rendered by default.

96 changes: 96 additions & 0 deletions examples/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
document.addEventListener('DOMContentLoaded', function () {

/*Scroll to top when arrow up clicked BEGIN*/
$(window).scroll(function() {
var height = $(window).scrollTop();
if (height > 100) {
$('#back2Top').fadeIn();
} else {
$('#back2Top').fadeOut();
}
});

$("#back2Top").click(function(event) {
event.preventDefault();
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});
/*Scroll to top when arrow up clicked END*/

var toolbox;

(function() {

var org = urlHash().getUrlHashParameter('o') || 'publiclab';
var repo = urlHash().getUrlHashParameter('r') || 'plots2';
var ftoLabel = urlHash().getUrlHashParameter('f') || 'first-timers-only';
var candidateLabel = urlHash().getUrlHashParameter('c') || 'fto-candidate';
var recencyLabel = urlHash().getUrlHashParameter('l') || 'week';

toolbox = CommunityToolbox(org, repo);

let d = document.getElementById('toggle-contributors');
let recency_label = document.getElementById('recency-label');
d.addEventListener("click", (e) => {
e.preventDefault();
if (recencyLabel=="week") {
toolbox.showRecentContributors(org, repo, recencyLabel);
d.innerHTML = "Show monthly";
recency_label.innerHTML = "last week: ";
recencyLabel = "month";
}else if (recencyLabel=="month") {
toolbox.showRecentContributors(org, repo, recencyLabel);
d.innerHTML = "Show weekly";
recency_label.innerHTML = "last month: ";
recencyLabel = "week";
}
})

if (repo === 'all') {

toolbox.getIssuesForOrg(org, { qs: { labels: ftoLabel } })
.then(displayIssues('.first-timers-only'));

toolbox.getIssuesForOrg(org, { qs: { labels: candidateLabel } })
.then(displayIssues('.candidates'));

toolbox.initialize(org, repo).then((res)=> {
if(res) {
// compile and display all contributors for given org
toolbox.showAllContributors(org, repo);
// Makes the toggle contributors list button click
d.click();
}
})

} else {

toolbox.api.Issues
.getIssuesForRepo(org, repo, { qs: { labels: ftoLabel } })
.then(displayIssues('.first-timers-only'));

toolbox.api.Issues
.getIssuesForRepo(org, repo, { qs: { labels: candidateLabel } })
.then(displayIssues('.candidates'));

toolbox.initialize(org, repo).then((res)=> {
if(res) {
// compile and display all contributors for given repo
toolbox.showRepoContributors(org, repo);
// Makes the toggle contributors list button click
d.click();
}
})
}

function displayIssues(selector) {
return function displayIssues(issues) {
if (typeof issues === "string") issues = JSON.parse(issues).items;
issues.forEach(function(issue) {
toolbox.issuesUI.insertIssue(issue, selector);
});
}
}

})();
})
101 changes: 1 addition & 100 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<script src="node_modules/chart.js/dist/Chart.js"></script>
<script src="node_modules/urlhash/urlHash.js"></script>
<script src="dist/community-toolbox.js" charset="utf-8"></script>

<script src="examples/demo.js"></script>
</head>

<body>
Expand Down Expand Up @@ -169,105 +169,6 @@ <h2 class="text-center d-block">Connect with us</h2>
</div>
</footer>

<script type="text/javascript">
/*Scroll to top when arrow up clicked BEGIN*/
$(window).scroll(function() {
var height = $(window).scrollTop();
if (height > 100) {
$('#back2Top').fadeIn();
} else {
$('#back2Top').fadeOut();
}
});
$(document).ready(function() {
$("#back2Top").click(function(event) {
event.preventDefault();
$("html, body").animate({ scrollTop: 0 }, "slow");
return false;
});

});
/*Scroll to top when arrow up clicked END*/

var toolbox;

(function() {

var org = urlHash().getUrlHashParameter('o') || 'publiclab';
var repo = urlHash().getUrlHashParameter('r') || 'plots2';
var ftoLabel = urlHash().getUrlHashParameter('f') || 'first-timers-only';
var candidateLabel = urlHash().getUrlHashParameter('c') || 'fto-candidate';
var recencyLabel = urlHash().getUrlHashParameter('l') || 'week';

toolbox = CommunityToolbox(org, repo);

let d = document.getElementById('toggle-contributors');
let recency_label = document.getElementById('recency-label');
d.addEventListener("click", (e) => {
e.preventDefault();
if (recencyLabel=="week") {
toolbox.getRecentCommits(org, repo, recencyLabel);
d.innerHTML = "Show monthly";
recency_label.innerHTML = "last week: ";
recencyLabel = "month";
}else if (recencyLabel=="month") {
toolbox.getRecentCommits(org, repo, recencyLabel);
d.innerHTML = "Show weekly";
recency_label.innerHTML = "last month: ";
recencyLabel = "week";
}
})

if (repo === 'all') {

toolbox.getIssuesForOrg(org, { qs: { labels: ftoLabel } })
.then(displayIssues('.first-timers-only'));

toolbox.getIssuesForOrg(org, { qs: { labels: candidateLabel } })
.then(displayIssues('.candidates'));

// compile and display all contributors for given org/repo:
setTimeout(()=> {
toolbox.getAllContributors(org);
// Makes the toggle contributors list button click
d.click();
},500)


} else {

toolbox.api.Issues
.getIssuesForRepo(org, repo, { qs: { labels: ftoLabel } })
.then(displayIssues('.first-timers-only'));

toolbox.api.Issues
.getIssuesForRepo(org, repo, { qs: { labels: candidateLabel } })
.then(displayIssues('.candidates'));

// compile and display all contributors for given org/repo:
setTimeout(()=> {
toolbox.showRepoContributors(org, repo);
// Makes the toggle contributors list button click
d.click();
},500);

}

function displayIssues(selector) {
return function displayIssues(issues) {
if (typeof issues === "string") issues = JSON.parse(issues).items;
issues.forEach(function(issue) {
toolbox.ui.insertIssue(issue, selector);
});
}
}

})();




</script>
<a id="back2Top" title="Back to top" href="#">&#10148;</a>
</body>

Expand Down
26 changes: 26 additions & 0 deletions src/UI/contributorsUI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var insertContributorsExec = false;


function insertContributors(AllContributors){
// This removes the spinner icon as soon as contributors list is loaded
document.getElementById("spinner-icon").style.display = "none";

let totalContributors = 0;
var usernames = AllContributors.map(function getContributorUsername(c) {
return `@${c.login}`;
});
var avatars = AllContributors.map(function getContributorAvatarURL(c) {
return `<a href="#" title="${c.login}"><img width="100px" src="${c.avatar_url}"></a>`;
});
totalContributors += AllContributors.length;
if(insertContributorsExec) $('.contributors > .usernames').append(', ');
$('.contributors-head').html('Contributors ('+totalContributors+'+)');
$('.contributors > .usernames').append(usernames.join(', '));
$('.contributors > .avatars').append(avatars.join(''));
insertContributorsExec=true;
}


module.exports = {
insertContributors: insertContributors,
};
37 changes: 37 additions & 0 deletions src/UI/issuesUI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var moment = require('moment');



function generateIssueHtml(title, body, githubUrl, repo) {
var repoName = githubUrl.split('/')[4],
repoUrl = githubUrl.split('/').slice(0, 5).join('/'),
html = '<div class="panel panel-default">\
<div class="panel-heading">\
<h3 class="panel-title" style="float:right;font-size:12px;"><b><a href="' + repoUrl + '">' + repoName + '</a></b></h3>\
<h3 class="panel-title"><b><a href="' + githubUrl + '">' + title + '</a></b></h3>\
</div>\
<div class="panel-body">\
' + body + '\
</div>\
</div>';
return html;
}

function insertIssue(issue, el) {
var body = "";
body += "<div style='float:right;' class='labels'>"
issue.labels.forEach(function(label) {
body += "<a class='label label-default' href='" + label.url + "' style='background:#" + label.color + ";'>" + label.name + "</a> ";
});
body += "</div>";
body += "<b>#" + issue.number + "</b> opened " + moment(issue.updated_at).fromNow() + " ";
body += "by <a href='https://github.com/" + issue.user.login + "'>" + issue.user.login + "</a>";
body += " <i class='fa fa-comment-o'></i> " + issue.comments;
$(el).append(generateIssueHtml(issue.title, body, issue.html_url, issue));
}


module.exports = {
generateIssueHtml: generateIssueHtml,
insertIssue: insertIssue,
};
24 changes: 24 additions & 0 deletions src/UI/recentContributorsUI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var insertRecentContributorsExec = false;


function insertRecentContributors(AllContributors){
let recentContributors = 0;
let usernames = AllContributors.map((commit, i) => {
return `<a href="${commit.author.html_url}">@${commit.author.login}</a>`;
})
let avatars = AllContributors.map((commit, i) => {
return `<a href="${commit.author.html_url}" class="hvr-Glow"><img width="100px" src="${commit.author.avatar_url}"></a>`;
})
recentContributors += AllContributors.length;

if(insertRecentContributorsExec) $('.recent-contributors > .usernames').append(', ');
$('.recent-contributors-head').html('Recent Contributors ('+recentContributors+'+)');
$('.recent-contributors > .usernames').html(usernames.join(', '));
$('.recent-contributors > .avatars').html(avatars.join(''));
insertRecentContributorsExec=true;
}


module.exports = {
insertRecentContributors: insertRecentContributors,
};
76 changes: 0 additions & 76 deletions src/UI/ui.js

This file was deleted.

Loading

0 comments on commit 30dd907

Please sign in to comment.