Last active
March 16, 2016 00:26
-
-
Save Rplus/61ef71396980c5550bab to your computer and use it in GitHub Desktop.
codepen-collection.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
html(lang="en") | |
head | |
meta(charset="UTF-8") | |
title Codepen collection lookup | |
meta(name="viewport", content="width=device-width") | |
link(rel="stylesheet", href="style.css") | |
link(rel="stylesheet", href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css") | |
body | |
h1.intro Codepen collection lookup | |
form.form | |
label(for="js-cid") collection ID: | |
input#js-cid(type="text", value="Hwfiu") | |
input(type="submit") | |
.box | |
script#pen-tpl(type="x-tmpl-mustache") | |
.Pen | |
a.Pen__thumbnail(href="{{ link }}" target="_blank" ref="noopener") | |
img(src="{{ images.small }}") | |
img.shadow(src="{{ images.small }}") | |
.Pen__info | |
a.title(href="{{ link }}" target="_blank" ref="noopener") {{ title }} | |
a.Author(href="http://codepen.io/{{ user.username }}" target="_blank" ref="noopener") | |
img.Author__avatar(src="{{ user.avatar }}") | |
.Author__name {{ user.nicename }} | |
.Pen__stats | |
.Pen__stats__view | |
.fa.fa-eye | |
| {{ views }} | |
.Pen__stats__heart | |
.fa.fa-heart | |
| {{ loves }} | |
.Pen__stats__comment | |
.fa.fa-comment | |
| {{ comments }} | |
blockquote.info | |
| codepen API source: #[a(href="http://cpv2api.com/") CodePen v2 API] | |
script(src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.2.1/mustache.min.js") | |
script(src="script.js", defer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* global Mustache, location, fetch, console */ | |
let Form = {}; | |
Form.form = document.querySelector('.form'); | |
Form.input = document.getElementById('js-cid'); | |
if (location.search) { | |
let cid = location.search.match(/cid=([^&]+)/); | |
if (cid && cid[1]) { | |
Form.input.value = cid[1]; | |
} | |
} | |
Form.form.addEventListener('submit', (e) => { | |
e.preventDefault(); | |
let newId = Form.input.value.trim(); | |
if (Pen.collectionId !== Form.input.value.trim() || Pen.pens.length === 1) { | |
Pen.init(); | |
Pen.getInfo(); | |
} | |
}); | |
let Pen = {}; | |
Pen.wrap = document.querySelector('.box'); | |
Pen.tpl = document.getElementById('pen-tpl').innerHTML; | |
Pen.init = () => { | |
Pen.wrap.innerHTML = ''; | |
Pen.pens = [0]; | |
Pen.page = 1; | |
Pen.collectionId = Form.input.value.trim(); | |
}; | |
Pen.getInfo = () => { | |
let url = `http://cpv2api.com/collection/${Pen.collectionId}/?page=${Pen.page}`; | |
console.log(url); | |
fetch(url) | |
.then((_res) => _res.json()) | |
.then((res) => { | |
if (!res.success) { return; } | |
Pen.pens.push(res.data); | |
Pen.render(Pen.page); | |
if (res.data.length === 6) { | |
Pen.page += 1; | |
Pen.getInfo(); | |
} | |
}) | |
.catch((error) => { | |
console.log('There has been a problem with your fetch operation: ' + error.message); | |
}); | |
}; | |
Pen.render = (order) => { | |
let _html = `<hr><a class="pagenation" href="http://codepen.io/collection/${Pen.collectionId}/${order}">page: #${order}</a>`; | |
console.log(`now is: ${order}`); | |
Pen.pens[order].forEach((penData) => { | |
_html += Mustache.render(Pen.tpl, penData); | |
}); | |
Pen.wrap.insertAdjacentHTML('beforeend', _html); | |
}; | |
Pen.init(); | |
Pen.getInfo(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.Pen { | |
display: flex; | |
align-items: flex-start; | |
padding: .5em 1em; | |
a { | |
color: #ccc; | |
text-decoration: none; | |
&:hover { | |
text-decoration: underline; | |
} | |
} | |
} | |
.Pen__thumbnail { | |
position: relative; | |
width: 128px; | |
max-width: 30vw; | |
margin-right: .5em; | |
&::before { | |
content: ""; | |
display: block; | |
padding-bottom: 100% * 75 / 128; | |
} | |
img { | |
position: absolute; | |
top: 0; | |
right: 0; | |
width: 100%; | |
height: 100%; | |
} | |
.shadow { | |
width: auto; | |
height: auto; | |
opacity: 0; | |
pointer-events: none; | |
z-index: 1; | |
transition: opacity .2s; | |
} | |
&:hover .shadow { | |
opacity: 1; | |
} | |
} | |
.Pen__info { | |
flex: 1; | |
} | |
.Author { | |
display: flex; | |
text-align-last: center; | |
margin-bottom: .25em; | |
opacity: .75; | |
} | |
.Author__avatar { | |
width: 20px; | |
height: 20px; | |
margin-right: .25em; | |
} | |
.title { | |
display: block; | |
margin-bottom: .25em; | |
font-weight: bold; | |
} | |
.Pen__stats { | |
display: flex; | |
flex-wrap: wrap; | |
opacity: .5; | |
font-family: sans-serif; | |
font-size: .75em; | |
.fa { | |
padding: 0 .5em 0 1.5em; | |
opacity: .5; | |
} | |
} | |
.pagenation { | |
display: inline-block; | |
margin-bottom: .5em; | |
text-align: center; | |
color: #fff; | |
font-size: 2em; | |
} | |
.form { | |
margin-bottom: 2em; | |
text-align: center; | |
} | |
.box { | |
flex: 1; | |
width: 100%; | |
max-width: 40rem; | |
margin: 0 auto; | |
} | |
/// reset | |
*, | |
*::before, | |
*::after { | |
box-sizing: border-box; | |
margin: 0; | |
} | |
html { | |
height: 100%; | |
} | |
body { | |
display: flex; | |
flex-direction: column; | |
min-height: 100%; | |
margin: 0; | |
background: #333; | |
color: #fff; | |
} | |
a { | |
color: inherit; | |
} | |
.intro { | |
width: 90%; | |
max-width: 30rem; | |
padding-bottom: 1rem; | |
margin: 0 auto 1em; | |
padding-top: .5em; | |
font-size: calc(1rem + 2vmin); | |
text-transform: capitalize; | |
border-bottom: 1px dashed rgba(#000, .3); | |
text-align: center; | |
small { | |
display: block; | |
opacity: .5; | |
font-style: italic; | |
text-transform: none; | |
} | |
} | |
.info { | |
margin: 0; | |
padding: 1em; | |
font-size: .9em; | |
font-style: italic; | |
font-family: serif; | |
text-align: right; | |
opacity: .5; | |
} | |
.sr-only { | |
position: absolute; | |
width: 1px; | |
height: 1px; | |
padding: 0; | |
margin: -1px; | |
overflow: hidden; | |
clip: rect(0, 0, 0, 0); | |
border: 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment