Skip to content

Commit 22e2178

Browse files
committed
Render community metrics as simple extensible list
1 parent 7956bd0 commit 22e2178

1 file changed

Lines changed: 60 additions & 53 deletions

File tree

js/community-metrics.js

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,57 @@
2828
});
2929
}
3030

31-
function createItem(label, value) {
32-
return "<li><strong>" + label + ":</strong> " + value + "</li>";
31+
function createMetricItem(label, value) {
32+
var item = document.createElement("li");
33+
item.innerHTML = "<strong>" + label + ":</strong> " + value;
34+
return item;
3335
}
3436

35-
function createCardColumn(title, description, items, linkUrl, linkLabel) {
36-
var column = document.createElement("div");
37-
column.className = "col-md-12";
37+
function createSection(title, description, metrics, linkUrl, linkLabel) {
38+
var section = document.createElement("section");
39+
section.style.marginBottom = "14px";
40+
41+
var heading = document.createElement("h4");
42+
heading.textContent = title;
43+
heading.style.marginBottom = "4px";
44+
section.appendChild(heading);
45+
46+
if (description) {
47+
var blurb = document.createElement("p");
48+
blurb.className = "text-muted";
49+
blurb.style.marginBottom = "6px";
50+
blurb.style.fontSize = "0.95em";
51+
blurb.textContent = description;
52+
section.appendChild(blurb);
53+
}
54+
55+
var list = document.createElement("ul");
56+
list.className = "list-unstyled";
57+
list.style.marginBottom = "6px";
3858

39-
var card = document.createElement("div");
40-
card.className = "panel panel-primary panel-precice";
41-
card.style.marginBottom = "14px";
59+
for (var i = 0; i < metrics.length; i += 1) {
60+
var metric = metrics[i];
61+
list.appendChild(createMetricItem(metric[0], metric[1]));
62+
}
63+
section.appendChild(list);
4264

43-
var list = "<ul class=\"list-unstyled no-margin\">" + items.join("") + "</ul>";
44-
var descriptionHtml = description ? "<p class=\"text-muted\" style=\"font-size: 0.9em; margin-bottom: 8px;\">" + description + "</p>" : "";
45-
var linkHtml = "";
4665
if (linkUrl && linkLabel) {
47-
linkHtml =
48-
"<p class=\"no-margin\" style=\"margin-top: 10px;\"><a href=\"" +
49-
linkUrl +
50-
"\" target=\"_blank\" rel=\"noopener noreferrer\" class=\"no-external-marker\">" +
51-
linkLabel +
52-
" &nbsp;<i class=\"fas fa-chevron-right\"></i></a></p>";
66+
var paragraph = document.createElement("p");
67+
paragraph.className = "no-margin";
68+
var link = document.createElement("a");
69+
link.href = linkUrl;
70+
link.target = "_blank";
71+
link.rel = "noopener noreferrer";
72+
link.className = "no-external-marker";
73+
link.innerHTML = linkLabel + " &nbsp;<i class=\"fas fa-chevron-right\"></i>";
74+
paragraph.appendChild(link);
75+
section.appendChild(paragraph);
5376
}
5477

55-
card.innerHTML =
56-
"<div class=\"panel-heading-precice text-left\"><strong>" +
57-
title +
58-
"</strong></div>" +
59-
"<div class=\"panel-body\" style=\"font-size: 0.92em; line-height: 1.35;\">" +
60-
descriptionHtml +
61-
list +
62-
linkHtml +
63-
"</div>";
64-
65-
column.appendChild(card);
66-
return column;
78+
return section;
6779
}
6880

69-
function createRepositoryCard(repo) {
81+
function createRepositorySection(repo) {
7082
var latestRelease = repo.latest_release;
7183
var latestReleaseValue = "n/a";
7284
if (latestRelease && latestRelease.url) {
@@ -82,33 +94,33 @@
8294

8395
var releaseDownloads = latestRelease ? formatNumber(latestRelease.downloads_count) : "n/a";
8496

85-
return createCardColumn(
97+
return createSection(
8698
repo.label,
8799
REPO_BLURBS[repo.id] || "",
88100
[
89-
createItem("Stars", formatNumber(repo.stars)),
90-
createItem("Contributors", formatNumber(repo.contributors)),
91-
createItem("Open issues", formatNumber(repo.open_issues)),
92-
createItem("Latest commit", formatDate(repo.latest_commit_at)),
93-
createItem("Latest release", latestReleaseValue),
94-
createItem("Release downloads", releaseDownloads),
101+
["Stars", formatNumber(repo.stars)],
102+
["Contributors", formatNumber(repo.contributors)],
103+
["Open issues", formatNumber(repo.open_issues)],
104+
["Latest commit", formatDate(repo.latest_commit_at)],
105+
["Latest release", latestReleaseValue],
106+
["Release downloads", releaseDownloads],
95107
],
96108
repo.url,
97109
"Open repository"
98110
);
99111
}
100112

101-
function createDiscourseCard(discourse) {
102-
return createCardColumn(
113+
function createDiscourseSection(discourse) {
114+
return createSection(
103115
"Discourse forum",
104116
"Community activity snapshot.",
105117
[
106-
createItem("Users", formatNumber(discourse.users_count)),
107-
createItem("Topics", formatNumber(discourse.topics_count)),
108-
createItem("Posts", formatNumber(discourse.posts_count)),
109-
createItem("Active users (30d)", formatNumber(discourse.active_users_30_days)),
110-
createItem("Topics (30d)", formatNumber(discourse.topics_30_days)),
111-
createItem("Posts (30d)", formatNumber(discourse.posts_30_days)),
118+
["Users", formatNumber(discourse.users_count)],
119+
["Topics", formatNumber(discourse.topics_count)],
120+
["Posts", formatNumber(discourse.posts_count)],
121+
["Active users (30d)", formatNumber(discourse.active_users_30_days)],
122+
["Topics (30d)", formatNumber(discourse.topics_30_days)],
123+
["Posts (30d)", formatNumber(discourse.posts_30_days)],
112124
],
113125
discourse.url,
114126
"Open forum"
@@ -143,16 +155,11 @@
143155
throw new Error("Missing metrics data");
144156
}
145157

146-
var row = document.createElement("div");
147-
row.className = "row";
148-
158+
container.innerHTML = "";
149159
for (var i = 0; i < repositories.length; i += 1) {
150-
row.appendChild(createRepositoryCard(repositories[i]));
160+
container.appendChild(createRepositorySection(repositories[i]));
151161
}
152-
row.appendChild(createDiscourseCard(discourse));
153-
154-
container.innerHTML = "";
155-
container.appendChild(row);
162+
container.appendChild(createDiscourseSection(discourse));
156163

157164
if (status) {
158165
status.textContent =

0 commit comments

Comments
 (0)