Custom components
En homebar
Overskrift med Tittel, navn, meny og info
class HomeBar extends HTMLElement {
constructor() {
super();
const heading = this.getAttribute("heading") || "Demo";
const username = this.getAttribute("username") || "Ole";
const info = this.getAttribute("info") || "4 ledige";
const menu = this.getAttribute("menu") || "Home";
this._root = this.attachShadow({ mode: "open" });
this._root.innerHTML = `
`;
}
static get observedAttributes() {
return ["username","heading","menu","info"];
}
attributeChangedCallback(name, oldValue, newValue) {
this._root.querySelector("#"+name).innerHTML = newValue;
}
}
window.customElements.define("home-bar", HomeBar);
Bruksmåte
Lag en mappe med navnet komponenter i prosjektet ditt, opprett filen over.
Du inkluderer filen som et vanlig script i header på html-filen din.
Du kan nå lage instanser på det nye html-elementet slik:
<home-bar title="Netbutikk"></home-bar>
En digital klokke
// @ts-check
class DigitalTime extends HTMLElement {
constructor() {
super();
let now = new Date();
let h = now.getHours();
let m = now.getMinutes();
let s = now.getSeconds();
this._root = this.attachShadow({ mode: "open" });
this._root.innerHTML = `
`;
setInterval(()=>{
let now = new Date();
let h = String(now.getHours());
let m = String(now.getMinutes());
let s = String(now.getSeconds());
this.setAttribute("h",h);
this.setAttribute("m",m);
this.setAttribute("s",s);
}, 1000);
}
static get observedAttributes() {
return ["h","m","s"];
}
attributeChangedCallback(name, oldValue, newValue) {
this._root.querySelector("#"+name).innerHTML = newValue;
}
}
window.customElements.define("digi-time", DigitalTime);
En befolkningspyramide (defunkt)
caution
Den api-en som brukes her er nå død - finner ingen andre som leverer bef.data
// @ts-check
class Pyramide extends HTMLElement {
constructor() {
super();
const country = this.getAttribute("country") || "Norway";
const year = this.getAttribute("year") || "1950";
const caption = this.getAttribute("caption") || `${country} ${year}`;
const width = this.getAttribute("width") || "400";
const size = Number(this.getAttribute("size")) || 3;
const height = (size + 2) * 100;
this._root = this.attachShadow({ mode: "open" });
this.halfw = Number(width) / 2;
this.data = {}; // store fetched data
this.country = undefined;
this.year = undefined;
this._root.innerHTML = `
`;
// this.hentDataOgTegn(country, year);
}
hentDataOgTegn(country = "Norway", year = "1950") {
let url = `http://api.population.io:80/1.0/population/${year}/${country}`;
fetch(url)
.then(r => r.json())
.then(data => {
this.data = data;
this.behandle(data);
})
.catch(e => console.log("Dette virka ikke."));
}
behandle(data) {
let divPyr = this._root.querySelector("#pyramide");
divPyr.innerHTML = "";
// let max = Math.max(... data.map(e => e.males));
let max = data.reduce((s, e) => Math.max(s, e.males), 0);
for (let i = data.length - 1; i >= 0; i--) {
let aardata = data[i];
let f = (this.halfw * Number(aardata.females)) / max;
let m = (this.halfw * Number(aardata.males)) / max;
let divm = document.createElement("div");
let divf = document.createElement("div");
divm.className = "soyle males";
divf.className = "soyle females";
divf.style.width = f + "px";
divm.style.width = m + "px";
divPyr.appendChild(divf);
divPyr.appendChild(divm);
}
}
caption() {
this._root.querySelector("#caption").innerHTML = ``;
}
static get observedAttributes() {
return ["country", "year"];
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === "country") {
this.country = newValue;
this.hentDataOgTegn(this.country, this.year);
}
if (name === "year") {
this.year = newValue;
this.hentDataOgTegn(this.country, this.year);
}
this.caption();
}
}
window.customElements.define("pop-pyr", Pyramide);