Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active September 17, 2019 08:23
Show Gist options
  • Save kou1okada/512d364202e8a3b096205ce464c1230b to your computer and use it in GitHub Desktop.
Save kou1okada/512d364202e8a3b096205ce464c1230b to your computer and use it in GitHub Desktop.
Followbox with Pure JS
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Followbox</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
<script src='main.js'></script>
</head>
<body>
<h1>Followbox</h1>
<div class="followbox">
<div class="header">
<h2>Who to follow</h2><a href="#" class="refresh">Refresh</a>
</div>
<ul class="suggestions">
<li class="suggestion">
<img />
<a href="#" target="_blank" class="username">blank</a>
<a href="#" class="close">x</a>
</li>
<li class="suggestion">
<img />
<a href="#" target="_blank" class="username">blank</a>
<a href="#" class="close">x</a>
</li>
<li class="suggestion">
<img />
<a href="#" target="_blank" class="username">blank</a>
<a href="#" class="close">x</a>
</li>
</ul>
</div>
</body>
</html>
.followbox .header * {
display: inline-block;
padding: 0;
margin: 4px;
}
.followbox .header {
background: #ECECEC;
margin: 0;
}
.followbox .refresh {
margin-left: 1ex;
}
.followbox .suggestions {
border: 2px solid #ECECEC;
margin: 0;
}
.followbox ul {
list-style: none;
padding: 0;
margin: 0;
}
.followbox img {
width: 40px;
height: 40px;
border-radius: 20px;
}
.followbox .username, .followbox .close {
display: inline-block;
position: relative;
bottom: 15px;
left: 5px;
}
class Followbox {
static rewrite(e, user) {
e.style.visibility = user ? "visible" : "hidden";
user = user || {};
Object.assign(e.querySelector(".username"), {href: user.html_url, textContent: user.login});
e.querySelector("img").src = user.avatar_url;
}
static refresh(e) {
if (e) Followbox.rewrite(e); else return;
fetchJSON(`https://api.github.com/users?since=${randomInt(0,500)}`)
.then(randomPickup)
.then(user=>Followbox.rewrite(e, user));
}
static onRefresh(e) {
Followbox.refresh(querySelectorToRoot(e.srcElement||e, ".suggestion"));
}
static onRefreshGroup(e) {
querySelectorMap(querySelectorToRoot(e.srcElement||e, ".followbox"), ".suggestion", Followbox.refresh);
}
static attach(e) {
querySelectorMap(e, ".close" , e=>e.addEventListener("click", Followbox.onRefresh));
querySelectorMap(e, ".refresh", e=>e.addEventListener("click", Followbox.onRefreshGroup));
querySelectorMap(e, ".refresh", e=>e.click());
}
}
window.addEventListener("load", function() {
querySelectorMap(document, ".followbox", Followbox.attach);
});
function fetchJSON(url) {
return fetch(url).then(response=>response.json());
}
function querySelectorMap(e, selector, func) {
return e ? [].map.call(e.querySelectorAll(selector), func) : [];
}
function randomInt(n1, n2) {
return parseInt(Math.random() * (n2 - n1) + n1);
}
function randomPickup(e) {
return e[randomInt(0, e.length)];
}
function querySelectorToRoot(e, selector) {
return !e || !e.matches
? null : e.matches(selector)
? e : arguments.callee(e.parentNode, selector);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment