Compare commits

..

2 commits

Author SHA1 Message Date
2b54da80b1 Create a draft search app 2024-03-01 19:48:46 +01:00
187b450f85 Add files for assignment 01 2024-03-01 18:07:26 +01:00
12 changed files with 314 additions and 0 deletions

Binary file not shown.

View file

@ -0,0 +1,34 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Movie Search</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container my-5">
<h1>Movie Search</h1>
<form id="movieSearch" class="mb-3">
<div class="input-group mb-3">
<input class="form-control">
<button class="btn btn-primary" type="submit">Search</button>
</div>
</form>
<table id="output" class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Poster</th>
<th scope="col">Name</th>
<th scope="col">Rating</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.min.js" crossorigin="anonymous"></script>
<script src="./main.js"></script>
</body>
</html>

View file

@ -0,0 +1,116 @@
const maxRating = 10;
window.addEventListener(
"load",
(e) => {
let config;
/**
* @type {HTMLTableElement}
*/
let table = document.querySelector("table#output");
let form = document.getElementById("movieSearch");
let textInput = form.querySelector("input");
table.hidden = true;
form.addEventListener(
"submit",
async (e) => {
e.preventDefault();
table.hidden = true;
config ??= await queryTMDb("configuration");
while (table.tBodies.length > 0) {
table.removeChild(table.tBodies[0]);
}
let result = await queryTMDb(
"search/movie",
{
include_adult: true,
language: "en-US",
query: textInput.value
});
let tBody = table.createTBody();
tBody.classList.add("align-middle");
for (let i = 0; i < result.results.length; i++) {
let movie = result.results[i];
let row = tBody.insertRow();
let title = document.createElement("a");
let poster = document.createElement("img");
title.href = `${new URL(`${movie.id}`, "https://themoviedb.org/movie/")}`;
title.classList.add("link-body-emphasis");
title.target = "_blank";
title.innerText = movie.title;
poster.src = new URL(
`./${movie.poster_path}`,
new URL(
`${config.images.poster_sizes[0]}/`,
config.images.secure_base_url).toString()).toString();
row.insertCell().innerText = `#${i + 1}`;
row.insertCell().appendChild(poster);
row.insertCell().appendChild(title);
row.insertCell().appendChild(createRating(movie.vote_average));
row.insertCell().innerText = movie.overview;
}
table.hidden = false;
console.log(config);
console.log(result);
});
});
/**
* Queries an endpoint of the TMDb API.
*
* @param {string} endpoint
* @param {Record<string, string>} params
* The parameters to send to the API.
*/
async function queryTMDb(endpoint, params) {
let url = new URL(endpoint, "https://api.themoviedb.org/3/");
for (let param in params) {
url.searchParams.append(param, `${params[param]}`);
}
return (
await fetch(
`${url}`,
{
headers: {
Accept: "application/json"
}
})).json();
}
function createRating(rating) {
let progressBar = document.createElement("div");
let indicator = progressBar.appendChild(document.createElement("div"));
let rounded = Math.round(rating * 10) / 10;
progressBar.style.minWidth = "10rem";
progressBar.classList.add("progress");
for (
let attribute of
[
["role", "progressbar"],
["aria-label", "Rating"],
["aria-valuenow", `${rating}`],
["aria-valuemin", `${0}`],
["aria-valuemax", `${maxRating}`]
])
{
progressBar.setAttribute(attribute[0], attribute[1]);
}
indicator.classList.add("progress-bar", "text-bg-warning", "overflow-visible");
indicator.style.width = `${rating * 10}%`;
indicator.textContent = `${rounded}/${maxRating}`;
return progressBar;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 876 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

View file

@ -0,0 +1,37 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Navibar Demo</title>
<script src="navibar.js"></script>
<style>
body {
margin: 0;
padding: 0;
}
navigation-bar {
width: 100vw;
position: fixed;
bottom: 0;
/* ... */
}
navigation-bar a img {
opacity: 0.3;
/* ... */
}
</style>
</head>
<body>
</body>
</html>

View file

@ -0,0 +1,26 @@
class CustomNavigationBar extends HTMLElement {
constructor() {
super()
const shadowRoot = this.attachShadow({mode: 'open'})
shadowRoot.innerHTML = `
<style>
div.navibar {
align-items: center;
background-color: #ccc;
margin: 0;
width: 100%;
height: 100%;
}
::slotted(a) {
display: block;
text-align: center;
}
</style>
<div class="navibar">
<slot></slot>
</div>`
}
}
customElements.define('navigation-bar', CustomNavigationBar)

View file

@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Progress Bar Demo</title>
<script src="progress.js"></script>
<script src="demo.js"></script>
</head>
<body>
<p>
<custom-progress-bar />
</p>
<p>
<custom-progress-bar value="25" />
</p>
<p>
<custom-progress-bar value="0" />
</p>
</body>
</html>

View file

@ -0,0 +1,17 @@
/*
* Progress Bar Demo
*/
window.onload = () => {
document.querySelector('p:nth-child(1) custom-progress-bar').progress = 75;
document.querySelector('p:nth-child(2) custom-progress-bar');
var pb = document.querySelector('p:nth-child(3) custom-progress-bar');
setInterval(() => changePb(pb), 20);
}
const changePb = (pb) => {
if (Math.random()>0.85) {
pb.progress = (parseInt(pb.progress)+1) % 101;
}
}

View file

@ -0,0 +1,59 @@
class CustomProgressBar extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `
<style>
:host {
display: inline-block;
width: 5rem;
height: 1rem;
}
.progress {
display: inline-block;
position: relative;
border: solid 1px #000;
padding: 1px;
width: 100%;
height: 100%;
}
.progress > .bar {
background: #9cf;
height: 100%;
}
.progress > .label {
position: absolute;
top: 0;
left: 0;
width: 100%;
text-align: center;
font-size: 0.8rem;
line-height: 1.1rem;
}
</style>
<div class="progress" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">
<div class="bar" style="width: 0px;"></div>
<div class="label">0%</div>
</div>
`;
this._progressElement = shadowRoot.querySelector('.progress');
this._label = shadowRoot.querySelector('.label');
this._bar = shadowRoot.querySelector('.bar');
}
static get observedAttributes() { return ['value']; }
attributeChangedCallback(name, oldValue, newValue, namespaceURI) {
if (name === 'value') {
const newPercentage = newValue === null ? 0 : parseInt(newValue);
this._progressElement.setAttribute('aria-valuenow', newPercentage);
this._label.textContent = newPercentage + '%';
this._bar.style.width = newPercentage + '%';
}
}
get progress() { return this.getAttribute('value'); }
set progress(newValue) { this.setAttribute('value', newValue); }
};
customElements.define('custom-progress-bar', CustomProgressBar);