feat(weather): improve weather locations list
This commit is contained in:
@@ -3,6 +3,7 @@ import "./components";
|
||||
import "./language";
|
||||
import "./main.scss";
|
||||
import "./theme";
|
||||
import "./weather/weather";
|
||||
|
||||
document.addEventListener("DOMContentLoaded", (event) => {
|
||||
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
|
||||
|
||||
2
static/src/lib/bootstrap.scss
vendored
2
static/src/lib/bootstrap.scss
vendored
@@ -30,7 +30,7 @@
|
||||
//@import "bootstrap/scss/accordion";
|
||||
//@import "bootstrap/scss/breadcrumb";
|
||||
//@import "bootstrap/scss/pagination";
|
||||
//@import "bootstrap/scss/badge";
|
||||
@import "bootstrap/scss/badge";
|
||||
//@import "bootstrap/scss/alert";
|
||||
//@import "bootstrap/scss/progress";
|
||||
@import "bootstrap/scss/list-group";
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
@import "./lib/weather-icons/weather-icons";
|
||||
|
||||
@import "./widget.scss";
|
||||
@import "./weather.scss";
|
||||
@import "./weather/weather.scss";
|
||||
|
||||
.table.table-compact {
|
||||
td {
|
||||
|
||||
116
static/src/weather/weather.ts
Normal file
116
static/src/weather/weather.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
export interface Location {
|
||||
id: string;
|
||||
name: string;
|
||||
provider: string;
|
||||
country_code: string;
|
||||
country?: string;
|
||||
district?: string;
|
||||
subdistrict?: string;
|
||||
}
|
||||
|
||||
export class WeatherLocationStorage {
|
||||
private storageKey: string = "weather:locations";
|
||||
|
||||
getAll(): Location[] {
|
||||
const locations = JSON.parse(window.localStorage.getItem(this.storageKey) || "{}");
|
||||
return Object.values(locations);
|
||||
}
|
||||
|
||||
add(location: Location) {
|
||||
const locations = JSON.parse(window.localStorage.getItem(this.storageKey) || "{}");
|
||||
locations[location.id] = location;
|
||||
window.localStorage.setItem(this.storageKey, JSON.stringify(locations));
|
||||
}
|
||||
|
||||
remove(id: string) {
|
||||
const locations = JSON.parse(window.localStorage.getItem(this.storageKey) || "{}");
|
||||
delete locations[id];
|
||||
window.localStorage.setItem(this.storageKey, JSON.stringify(locations));
|
||||
}
|
||||
}
|
||||
|
||||
export class WeatherLocationManager {
|
||||
loadLocations(selector: string) {
|
||||
const locations = window.weatherLocationStorage.getAll();
|
||||
const container = document.querySelector(selector);
|
||||
if (container) {
|
||||
container.innerHTML = "";
|
||||
for (const location of locations) {
|
||||
const element = new WeatherLocationElement();
|
||||
element.setAttribute("location", JSON.stringify(location));
|
||||
element.setAttribute("removable", "");
|
||||
container.appendChild(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class WeatherLocationElement extends HTMLElement {
|
||||
static observedAttributes = ["location", "removable"];
|
||||
|
||||
location: Location | null = null;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.handleClick = this.handleClick.bind(this);
|
||||
this.handleRemoveClick = this.handleRemoveClick.bind(this);
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.location = JSON.parse(this.getAttribute("location") || "{}");
|
||||
this.innerHTML = `
|
||||
<a href="${this.location?.provider}/${this.location?.id}"
|
||||
class="list-group-item list-group-item-action px-4 d-flex justify-content-between align-items-start">
|
||||
<span>
|
||||
<span class="fi fi-${this.location?.country_code} me-1"></span>
|
||||
<span class="text-primary">${this.location?.name}</span>
|
||||
<span class="small ms-1 text-secondary">
|
||||
${this.location?.country}, ${this.location?.district}, ${this.location?.subdistrict}
|
||||
</span>
|
||||
</span>
|
||||
<span>
|
||||
<span class="badge text-bg-secondary">${this.location?.provider}</span>
|
||||
<span class="text-danger ms-2" data-remove>
|
||||
✕
|
||||
</span>
|
||||
</span>
|
||||
</a>`;
|
||||
this.addEventListener("click", this.handleClick);
|
||||
if (this.hasAttribute("removable")) {
|
||||
this.querySelector<HTMLElement>("[data-remove]")?.addEventListener("click", this.handleRemoveClick);
|
||||
} else {
|
||||
this.querySelector("[data-remove]")?.remove();
|
||||
}
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
this.removeEventListener("click", this.handleClick);
|
||||
}
|
||||
|
||||
handleClick(event: MouseEvent) {
|
||||
if (this.location) {
|
||||
window.weatherLocationStorage.add(this.location);
|
||||
}
|
||||
}
|
||||
|
||||
handleRemoveClick(event: MouseEvent) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (this.location) {
|
||||
window.weatherLocationStorage.remove(this.location.id);
|
||||
}
|
||||
this.remove();
|
||||
}
|
||||
}
|
||||
|
||||
customElements.define("weather-location", WeatherLocationElement);
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
weatherLocationStorage: WeatherLocationStorage;
|
||||
weatherLocationManager: WeatherLocationManager;
|
||||
}
|
||||
}
|
||||
|
||||
window.weatherLocationStorage = new WeatherLocationStorage();
|
||||
window.weatherLocationManager = new WeatherLocationManager();
|
||||
Reference in New Issue
Block a user