Add 'frontend/' from commit '8bbd8aa8c11221880a8517effd756892185e4bfd'

git-subtree-dir: frontend
git-subtree-mainline: 3c177009d1
git-subtree-split: 8bbd8aa8c1
This commit is contained in:
Daniel Cortes
2020-11-25 01:48:01 -03:00
4 changed files with 816 additions and 0 deletions

57
frontend/index.html Normal file
View File

@@ -0,0 +1,57 @@
<html>
<head>
<meta charset=utf-8>
<meta name=viewport content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="normalize.css">
<link rel="stylesheet" href="styles.css">
<title>Home</title>
</head>
<body>
<div class="container">
<div id="photos-box" class="box">
<div class="photos" id="photos-div"></div>
<div class="photos-controls">
<button id="photos-prev">Anterior</button>
<span id="photos-page">1</span>
<button id="photos-next">Siguiente</button>
</div>
</div>
<div id="search-box" class="box">
<input id="search-input" name="search" placeholder="Buscar en DuckDuckGo"/>
</div>
<div id="weather-box" class="box">
<div class="weather-description">
<span class="weather-degrees">99º</span>
<span class="weather-where">Nether</span>
</div>
<img class="weather-icon" src="http://openweathermap.org/img/wn/04n@4x.png"/>
</div>
<div id="quote-box" class="box">
<p id="quote"></p>
<button id="new-quote">Otra Frase</button>
</div>
<div id="currency-box" class="box">
<div class="currency">
<input class="input" id="currency-money" type="number" value="1"/>
<div class="select-group">
<select class="select" id="currency-from"></select>
<span class="arrow">&rarr;</span>
<select class="select" id="currency-to"/></select>
</div>
<input class="result" id="currency-result" readonly="readonly">
<button class="button" id="update-currency">Actualizar</button>
</div>
</div>
</div>
<script defer src="script.js"></script>
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
</body>
</html>

299
frontend/normalize.css vendored Normal file
View File

@@ -0,0 +1,299 @@
/*! modern-normalize v1.0.0 | MIT License | https://github.com/sindresorhus/modern-normalize */
/*
Document
========
*/
/**
Use a better box model (opinionated).
*/
*,
*::before,
*::after {
box-sizing: border-box;
}
/**
Use a more readable tab size (opinionated).
*/
:root {
-moz-tab-size: 4;
tab-size: 4;
}
/**
1. Correct the line height in all browsers.
2. Prevent adjustments of font size after orientation changes in iOS.
*/
html {
line-height: 1.15; /* 1 */
-webkit-text-size-adjust: 100%; /* 2 */
}
/*
Sections
========
*/
/**
Remove the margin in all browsers.
*/
body {
margin: 0;
}
/**
Improve consistency of default fonts in all browsers. (https://github.com/sindresorhus/modern-normalize/issues/3)
*/
body {
font-family:
system-ui,
-apple-system, /* Firefox supports this but not yet `system-ui` */
'Segoe UI',
Roboto,
Helvetica,
Arial,
sans-serif,
'Apple Color Emoji',
'Segoe UI Emoji';
}
/*
Grouping content
================
*/
/**
1. Add the correct height in Firefox.
2. Correct the inheritance of border color in Firefox. (https://bugzilla.mozilla.org/show_bug.cgi?id=190655)
*/
hr {
height: 0; /* 1 */
color: inherit; /* 2 */
}
/*
Text-level semantics
====================
*/
/**
Add the correct text decoration in Chrome, Edge, and Safari.
*/
abbr[title] {
text-decoration: underline dotted;
}
/**
Add the correct font weight in Edge and Safari.
*/
b,
strong {
font-weight: bolder;
}
/**
1. Improve consistency of default fonts in all browsers. (https://github.com/sindresorhus/modern-normalize/issues/3)
2. Correct the odd 'em' font sizing in all browsers.
*/
code,
kbd,
samp,
pre {
font-family:
ui-monospace,
SFMono-Regular,
Consolas,
'Liberation Mono',
Menlo,
monospace; /* 1 */
font-size: 1em; /* 2 */
}
/**
Add the correct font size in all browsers.
*/
small {
font-size: 80%;
}
/**
Prevent 'sub' and 'sup' elements from affecting the line height in all browsers.
*/
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
/*
Tabular data
============
*/
/**
1. Remove text indentation from table contents in Chrome and Safari. (https://bugs.chromium.org/p/chromium/issues/detail?id=999088, https://bugs.webkit.org/show_bug.cgi?id=201297)
2. Correct table border color inheritance in all Chrome and Safari. (https://bugs.chromium.org/p/chromium/issues/detail?id=935729, https://bugs.webkit.org/show_bug.cgi?id=195016)
*/
table {
text-indent: 0; /* 1 */
border-color: inherit; /* 2 */
}
/*
Forms
=====
*/
/**
1. Change the font styles in all browsers.
2. Remove the margin in Firefox and Safari.
*/
button,
input,
optgroup,
select,
textarea {
font-family: inherit; /* 1 */
font-size: 100%; /* 1 */
line-height: 1.15; /* 1 */
margin: 0; /* 2 */
}
/**
Remove the inheritance of text transform in Edge and Firefox.
1. Remove the inheritance of text transform in Firefox.
*/
button,
select { /* 1 */
text-transform: none;
}
/**
Correct the inability to style clickable types in iOS and Safari.
*/
button,
[type='button'],
[type='reset'],
[type='submit'] {
-webkit-appearance: button;
}
/**
Remove the inner border and padding in Firefox.
*/
::-moz-focus-inner {
border-style: none;
padding: 0;
}
/**
Restore the focus styles unset by the previous rule.
*/
:-moz-focusring {
outline: 1px dotted ButtonText;
}
/**
Remove the additional ':invalid' styles in Firefox.
See: https://github.com/mozilla/gecko-dev/blob/2f9eacd9d3d995c937b4251a5557d95d494c9be1/layout/style/res/forms.css#L728-L737
*/
:-moz-ui-invalid {
box-shadow: none;
}
/**
Remove the padding so developers are not caught out when they zero out 'fieldset' elements in all browsers.
*/
legend {
padding: 0;
}
/**
Add the correct vertical alignment in Chrome and Firefox.
*/
progress {
vertical-align: baseline;
}
/**
Correct the cursor style of increment and decrement buttons in Safari.
*/
::-webkit-inner-spin-button,
::-webkit-outer-spin-button {
height: auto;
}
/**
1. Correct the odd appearance in Chrome and Safari.
2. Correct the outline style in Safari.
*/
[type='search'] {
-webkit-appearance: textfield; /* 1 */
outline-offset: -2px; /* 2 */
}
/**
Remove the inner padding in Chrome and Safari on macOS.
*/
::-webkit-search-decoration {
-webkit-appearance: none;
}
/**
1. Correct the inability to style clickable types in iOS and Safari.
2. Change font properties to 'inherit' in Safari.
*/
::-webkit-file-upload-button {
-webkit-appearance: button; /* 1 */
font: inherit; /* 2 */
}
/*
Interactive
===========
*/
/*
Add the correct display in Chrome and Safari.
*/
summary {
display: list-item;
}

255
frontend/script.js Normal file
View File

@@ -0,0 +1,255 @@
const currency = new function() {
this.query_currency = async (base) => {
// Llama a la api para obtener los datos de conversion de divisa
// guardando los datos en localstorage
//
// En caso que se vuelva a llamar esta funcion pidiendo la misma base
// se va a evitar llamar a la api y se obtendran los datos desde localstorage
const old = JSON.parse(localStorage.getItem('currency'))
if (old != null && old.base === base) return old
const data = await fetch(`https://home.danielcortes.xyz/currency?base=${base}`)
const response = await data.json()
localStorage.setItem('currency', JSON.stringify(response))
return response
}
this.convert = async (money, from, to) => {
// Hace la transformacion de una divisa a otra
// Adicionalmente lo formatea segun el locale del navegador
const data = await this.query_currency(from)
return (data.rates[to] * money).toLocaleString(undefined, {miminumFractionDigits: 2})
}
this.fill_currencies = async () => {
// Rellena los datos de los input select con las monedas
// disponibles en la api
//
// Por default deja seleccionados para from el USD y para
// to el CLP
const data = await this.query_currency('USD')
const from_select = document.getElementById('currency-from')
const to_select = document.getElementById('currency-to')
Object.keys(data.symbols).forEach(key => {
const option = document.createElement('option');
option.value = key
option.innerHTML = `${key}`
const from_option = option.cloneNode(true)
const to_option = option.cloneNode(true)
if(key === 'USD') {
from_option.selected = 'selected'
}
if(key === 'CLP') {
to_option.selected = 'selected'
}
from_select.appendChild(from_option)
to_select.appendChild(to_option)
})
}
this.show_currency = async () => {
// Muestra los datos del cambio de divisa con los datos
// que ingreso el usuario en los campos correspondientes
//
// eso de la validacion no existe aqui :3, es para nenas
const money = document.getElementById('currency-money').value
const from_select = document.getElementById('currency-from')
const to_select = document.getElementById('currency-to')
const from = from_select.options[from_select.selectedIndex].value
const to = to_select.options[to_select.selectedIndex].value
const result = await this.convert(money, from, to)
document.getElementById('currency-result').value = `${result}`
}
this.on_load = async () => {
document.getElementById('update-currency').addEventListener('click', this.show_currency)
this.fill_currencies().then(() => this.show_currency())
}
}
const photos = new function() {
this.msnry = null;
this.imgs = [];
this.current_photos = null;
this.current_page = 1;
this.query_photos = async (page) => {
// Llama a la api para obtener una lista de imagenes
// guardando los datos en localstorage para cache
//
// En caso que se vuelva a llamar esta funcion pidiendo la misma pagina
// se va a evitar llamar a la api y se obtendran los datos desde la variable
// que la tiene almacenada o localstorage
if (this.current_photos !== null && this.current_photos.page === page)
return this.currrent_photos
const old = JSON.parse(localStorage.getItem('photos'))
if (old != null && old.page=== page) return old
const data = await fetch(`https://home.danielcortes.xyz/photos?page=${page}`)
const response = await data.json()
this.current_photos = response
localStorage.setItem('page', JSON.stringify(response))
return this.current_photos;
}
this.set_as_background = async (url) => {
document.querySelector('.container').style.backgroundImage= `url(${url})`
localStorage.setItem('background', url)
}
this.clear_photos = async () => {
this.imgs.forEach(img => {
this.msnry.remove(img)
})
this.msnry.layout()
this.imgs = []
}
this.wait_for_load = () => {
return Promise.all(Array.from(document.images)
.filter(img => !img.complete)
.map(img => new Promise(resolve => {
img.onload = img.onerror = resolve;
})))
}
this.fill_photos = async () => {
const photos = await this.query_photos(this.current_page)
const photos_div = document.getElementById('photos-div')
const photos_page = document.getElementById('photos-page')
const fragment = document.createDocumentFragment()
photos.results.forEach((photo) => {
const img = document.createElement('img')
img.src = photo.urls.small
img.alt = photo.alt
img.addEventListener('click', () => this.set_as_background(photo.urls.full))
fragment.appendChild(img)
this.imgs.push(img)
})
photos_page.innerHTML = this.current_page;
photos_div.appendChild(fragment)
await this.wait_for_load()
this.msnry.appended(this.imgs)
this.msnry.layout()
}
this.next_page = async () => {
this.current_page++
await this.clear_photos()
await this.fill_photos()
}
this.previous_page = async () => {
if(this.current_page - 1 >= 1) {
this.current_page--
await this.clear_photos()
await this.fill_photos()
}
}
this.on_load = async () => {
const previous_background = localStorage.getItem('background')
if(previous_background !== null) this.set_as_background(previous_background)
this.msnry = new Masonry( '.photos', {
itemSelector: 'img',
gutter: 10,
columnWidth: 120
});
await this.fill_photos()
document.getElementById('photos-next').addEventListener('click', this.next_page);
document.getElementById('photos-prev').addEventListener('click', this.previous_page);
}
}
const search = new function () {
this.search_bar = document.getElementById('search-input')
this.submit_search = (query) => {
const encoded = encodeURIComponent(query)
window.location.href = `https://www.duckduckgo.com/?q=${encoded}`
}
this.on_load = () => {
this.search_bar.addEventListener('keydown', (e) => {
if(e.key === 'Enter' && this.search_bar.value.length !== 0)
this.submit_search(this.search_bar.value)
})
}
}
const weather = new function () {
this.query_weather = async (city) => {
const data = await fetch(`https://home.danielcortes.xyz/weather?city=${city}`)
return await data.json()
}
this.show_weather = async () => {
const degrees_span = document.getElementsByClassName('weather-degrees')[0]
const where_span = document.getElementsByClassName('weather-where')[0]
const icon_img = document.getElementsByClassName('weather-icon')[0]
const weather = await this.query_weather('temuco')
degrees_span.innerHTML = `${weather.temp}º`;
where_span.innerHTML = weather.name;
icon_img.src = weather.icon;
}
this.on_load = () => {
this.show_weather()
}
}
const quotes = new function () {
this.query_quotes = async () => {
const data = await fetch(`https://home.danielcortes.xyz/quotes`)
return await data.json()
}
this.show_quote = async () => {
const quote_p = document.getElementById('quote')
const quote = await this.query_quotes()
quote_p.innerHTML = quote.quote
}
this.on_load = async () => {
this.show_quote()
document.getElementById('new-quote').addEventListener('click', this.show_quote)
}
}
window.addEventListener('load', () => {
currency.on_load()
photos.on_load()
search.on_load()
weather.on_load()
quotes.on_load()
});

205
frontend/styles.css Normal file
View File

@@ -0,0 +1,205 @@
/*
* BASE
*/
* {
font-family: sans-serif;
}
.container {
background-image: linear-gradient(to top, #4481eb 0%, #04befe 100%);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
display: grid;
height: 100%;
width: 100%;
padding: 20px;
overflow: auto;
grid-template-columns: 300px 1fr 600px 1fr 300px;
grid-template-rows: repeat(6, 1fr);
gap: 20px;
}
#photos-box {
grid-row: 1 / 7;
grid-column: 1;
justify-content: space-between;
}
#search-box {
grid-row: 2;
grid-column: 3;
}
#weather-box {
grid-row: 1 / 3;
grid-column: 5;
}
#quote-box {
grid-row: 3 / 5;
grid-column: 5;
}
#currency-box {
grid-row: 5 / 7;
grid-column: 5;
}
.box {
display: flex;
flex-direction: column;
justify-content: center;
background-color: white;
padding: 1em;
border-radius: 4px;
box-shadow: rgba(255, 255, 255, 0.1) 0px 1px 1px 0px inset, rgba(50, 50, 93, 0.25) 0px 50px 100px -20px, rgba(0, 0, 0, 0.3) 0px 30px 60px -30px;
}
/*
* Currency
*/
.box .currency {
display: flex;
flex-direction: column;
}
.currency > * {
text-align: center;
}
.currency .arrow {
display: inline-flex;
align-items: center;
font-size: 2em;
}
.currency .select-group {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.currency .title {
font-size: 1.6em;
margin-bottom: .3em;
margin-top: 0;
}
.currency input {
width: 100%;
padding: .6em 0;
margin-bottom: .5em;
margin-top: .5em;
}
.currency .select {
padding: .6em 0;
width: 6em;
}
.currency .result {
font-weight: 600;
}
.currency .button {
padding: .6em 0;
font-size: 1.1em;
}
/*
* Photos
*/
.box .photos {
overflow-y: scroll;
overflow-x: hidden;
height: 100%;
}
.photos img {
width: 120px;
margin-bottom: 5px;
border-radius: 4px;
box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px, rgba(0, 0, 0, 0.08) 0px 0px 0px 1px;
}
#photos-page {
display: inline-flex;
align-items: center;
}
.box .photos-controls {
display: flex;
flex-direction: row;
justify-content: space-between;
margin-top: 10px;
}
button {
padding: .6em 0;
}
/*
* Search
*/
#search-box {
height: 5em;
}
#search-input{
font-size: 1.5em;
font-weight: 600;
border: none;
}
/* Weather */
#weather-box {
}
.weather-description {
display: flex;
flex-direction: column;
justify-content: center;
padding: 1em;
}
.weather-description .weather-degrees {
font-size: 2em;
font-weight: 600;
text-align: center;
}
.weather-description .weather-where{
font-size: 1.3em;
text-align: center;
}
.weather-icon {
object-fit: contain;
height: 130px;
}
/* Quote */
#quote-box {
justify-content: space-between;
}
#quote {
display: flex;
height: 100%;
align-items: center;
overflow: auto;
}