refactor(static): add base component types
This commit is contained in:
@@ -23,7 +23,6 @@
|
|||||||
<div class="app col-lg-8 mx-auto p-3 py-md-5">
|
<div class="app col-lg-8 mx-auto p-3 py-md-5">
|
||||||
{% if not is_widget %}
|
{% if not is_widget %}
|
||||||
<header class="app-header pb-3 mb-5 border-bottom">
|
<header class="app-header pb-3 mb-5 border-bottom">
|
||||||
<div>{{request.query_params.widget}}</div>
|
|
||||||
<div class="link-list">
|
<div class="link-list">
|
||||||
<app-link href="/"
|
<app-link href="/"
|
||||||
icon="gear">API Gallery</app-link>
|
icon="gear">API Gallery</app-link>
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ async def get_schedule_index(request: AppRequest, provider: str | None = None, q
|
|||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
request=request,
|
request=request,
|
||||||
name="index.html",
|
name="index.html",
|
||||||
context={"channels": channels, "groups": GROUP_MAP.keys()},
|
context={"channels": [item.model_dump() for item in channels], "groups": GROUP_MAP.keys()},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -35,23 +35,26 @@
|
|||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
{% if channels %}
|
{% if channels %}
|
||||||
<ul id="channels"
|
<div class="mb-4">
|
||||||
class="list-group mb-5">
|
<schedule-channel-list value="{{channels | tojson | forceescape}}"
|
||||||
{% for channel in channels %}
|
onclick="scheduleChannelManager.store(element)"
|
||||||
<schedule-channel channel="{{channel.model_dump() | tojson | forceescape}}"
|
onadd="scheduleChannelManager.add(element)"></schedule-channel-list>
|
||||||
addeable></schedule-channel>
|
<hr>
|
||||||
{% endfor %}
|
</div>
|
||||||
</ul>
|
|
||||||
<hr>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<ul id="storedChannels"
|
<div class="mb-4">
|
||||||
class="list-group mb-4">
|
<h3>{{_("Stored channels")}}</h3>
|
||||||
</ul>
|
<schedule-channel-list id="storedChannels"
|
||||||
<ul class="list-group">
|
onremove="scheduleChannelManager.remove(element)"></schedule-channel-list>
|
||||||
{% for group in groups %}
|
</div>
|
||||||
<schedule-channel-group group="{{group}}"></schedule-channel-group>
|
<div class="mb-4">
|
||||||
{% endfor %}
|
<h3>{{_("Channel groups")}}</h3>
|
||||||
</ul>
|
<ul class="list-group">
|
||||||
|
{% for group in groups %}
|
||||||
|
<schedule-channel-group value="{{group | tojson | forceescape}}"></schedule-channel-group>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
<script>
|
<script>
|
||||||
(function () {
|
(function () {
|
||||||
const params = new URLSearchParams(window.location.search);
|
const params = new URLSearchParams(window.location.search);
|
||||||
@@ -59,9 +62,8 @@
|
|||||||
if (searchQuery) {
|
if (searchQuery) {
|
||||||
document.querySelector('#query').value = searchQuery;
|
document.querySelector('#query').value = searchQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", (event) => {
|
document.addEventListener("DOMContentLoaded", (event) => {
|
||||||
scheduleChannelManager.loadChannels('#storedChannels');
|
scheduleChannelManager.load();
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ async def get_weather_index(request: AppRequest, provider: str | None = None, qu
|
|||||||
return templates.TemplateResponse(
|
return templates.TemplateResponse(
|
||||||
request=request,
|
request=request,
|
||||||
name="index.html",
|
name="index.html",
|
||||||
context={"locations": locations},
|
context={"locations": [item.model_dump() for item in locations]},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -35,17 +35,18 @@
|
|||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
{% if locations %}
|
{% if locations %}
|
||||||
<ul id="locations"
|
<div class="mb-4">
|
||||||
class="list-group mb-5">
|
<weather-location-list value="{{locations | tojson | forceescape}}"
|
||||||
{% for location in locations %}
|
onclick="weatherLocationManager.store(element)"
|
||||||
<weather-location location="{{location.model_dump() | tojson | forceescape}}"></weather-location>
|
onadd="weatherLocationManager.add(element)"></weather-location-list>
|
||||||
{% endfor %}
|
<hr>
|
||||||
</ul>
|
</div>
|
||||||
<hr>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<ul id="storedLocations"
|
<div class="mb-4">
|
||||||
class="list-group mb-5">
|
<h3>{{_("Stored locations")}}</h3>
|
||||||
</ul>
|
<weather-location-list id="storedLocations"
|
||||||
|
onremove="weatherLocationManager.remove(element)"></weather-location-list>
|
||||||
|
</div>
|
||||||
<script>
|
<script>
|
||||||
(function () {
|
(function () {
|
||||||
const params = new URLSearchParams(window.location.search);
|
const params = new URLSearchParams(window.location.search);
|
||||||
@@ -55,7 +56,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", (event) => {
|
document.addEventListener("DOMContentLoaded", (event) => {
|
||||||
weatherLocationManager.loadLocations('#storedLocations');
|
weatherLocationManager.load();
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -69,9 +69,18 @@ msgstr "Давление, мм рт. ст."
|
|||||||
msgid "Humidity, %"
|
msgid "Humidity, %"
|
||||||
msgstr "Влажность, %"
|
msgstr "Влажность, %"
|
||||||
|
|
||||||
|
msgid "Stored locations"
|
||||||
|
msgstr "Сохранённые локации"
|
||||||
|
|
||||||
# tv
|
# tv
|
||||||
msgid "TV program"
|
msgid "TV program"
|
||||||
msgstr "Телепрограмма"
|
msgstr "Телепрограмма"
|
||||||
|
|
||||||
msgid "Live broadcasts"
|
msgid "Live broadcasts"
|
||||||
msgstr "Прямые трансляции"
|
msgstr "Прямые трансляции"
|
||||||
|
|
||||||
|
msgid "Stored channels"
|
||||||
|
msgstr "Сохранённые каналы"
|
||||||
|
|
||||||
|
msgid "Channel groups"
|
||||||
|
msgstr "Группы каналов"
|
||||||
@@ -0,0 +1,196 @@
|
|||||||
|
export interface Type<T> extends Function {
|
||||||
|
new (...args: any[]): T;
|
||||||
|
}
|
||||||
|
|
||||||
|
export abstract class ValueElement<T, O> extends HTMLElement {
|
||||||
|
static observedAttributes = ["value"];
|
||||||
|
|
||||||
|
private _value: T | null = null;
|
||||||
|
|
||||||
|
get value(): T | null {
|
||||||
|
return this._value;
|
||||||
|
}
|
||||||
|
|
||||||
|
set value(value: T | null) {
|
||||||
|
if (value) {
|
||||||
|
this.setAttribute("value", JSON.stringify(value));
|
||||||
|
} else if (this.hasAttribute("value")) {
|
||||||
|
this.removeAttribute("value");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
options: O | null;
|
||||||
|
|
||||||
|
constructor(value: T | null = null, options: O | null = null) {
|
||||||
|
super();
|
||||||
|
if (value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
this.options = options;
|
||||||
|
this.onInit();
|
||||||
|
}
|
||||||
|
|
||||||
|
onInit() {}
|
||||||
|
|
||||||
|
beforeRefresh() {}
|
||||||
|
|
||||||
|
onRefresh() {}
|
||||||
|
|
||||||
|
abstract buildHTML(): string;
|
||||||
|
|
||||||
|
refresh() {
|
||||||
|
const rawValue = this.getAttribute("value");
|
||||||
|
if (rawValue) {
|
||||||
|
this._value = JSON.parse(rawValue);
|
||||||
|
} else {
|
||||||
|
this._value = null;
|
||||||
|
}
|
||||||
|
if (this.className) {
|
||||||
|
this.classList = this.className;
|
||||||
|
}
|
||||||
|
this.beforeRefresh();
|
||||||
|
this.innerHTML = this.buildHTML();
|
||||||
|
this.onRefresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
connectedCallback() {
|
||||||
|
this.refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
attributeChangedCallback(name: string, oldValue: any, newValue: any) {
|
||||||
|
setTimeout(() => this.refresh());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ListOptions<T> {
|
||||||
|
onClick?: (element: ListItemElement<T>) => void;
|
||||||
|
onAdd?: (element: ListItemElement<T>) => void;
|
||||||
|
onRemove?: (element: ListItemElement<T>) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export abstract class ListItemElement<T> extends ValueElement<T, ListOptions<T>> {
|
||||||
|
abstract getHref(value: T): string;
|
||||||
|
|
||||||
|
abstract getContent(value: T): string;
|
||||||
|
|
||||||
|
className = "list-group-item list-group-item-action";
|
||||||
|
|
||||||
|
onInit(): void {
|
||||||
|
this.handleClick = this.handleClick.bind(this);
|
||||||
|
this.handleRemoveClick = this.handleRemoveClick.bind(this);
|
||||||
|
this.handleAddClick = this.handleAddClick.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
buildHTML(): string {
|
||||||
|
if (!this.value) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
const href = this.getHref(this.value);
|
||||||
|
const content = this.getContent(this.value);
|
||||||
|
return `
|
||||||
|
<a href="${href}"
|
||||||
|
class="d-flex justify-content-between align-items-start text-decoration-none">
|
||||||
|
<span>
|
||||||
|
<span class="text-success me-2 ${this.options?.onAdd ? "" : "d-none"}" style="color: green;" data-add>
|
||||||
|
+
|
||||||
|
</span>
|
||||||
|
${content}
|
||||||
|
</span>
|
||||||
|
<span>
|
||||||
|
<span class="text-danger ms-2 ${this.options?.onRemove ? "" : "d-none"}" data-remove>
|
||||||
|
✕
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
</a>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
onRefresh(): void {
|
||||||
|
this.addEventListener("click", this.handleClick);
|
||||||
|
if (this.options?.onAdd) {
|
||||||
|
this.querySelector<HTMLElement>("[data-add]")?.addEventListener("click", this.handleAddClick);
|
||||||
|
} else {
|
||||||
|
this.querySelector("[data-add]")?.remove();
|
||||||
|
}
|
||||||
|
if (this.options?.onRemove) {
|
||||||
|
this.querySelector<HTMLElement>("[data-remove]")?.addEventListener("click", this.handleRemoveClick);
|
||||||
|
} else {
|
||||||
|
this.querySelector("[data-remove]")?.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleClick(event: MouseEvent) {
|
||||||
|
if (this.value && this.options?.onClick) {
|
||||||
|
this.options.onClick(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleAddClick(event: MouseEvent) {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
if (this.value && this.options?.onAdd) {
|
||||||
|
this.options.onAdd(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleRemoveClick(event: MouseEvent) {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
if (this.value && this.options?.onRemove) {
|
||||||
|
this.options.onRemove(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnectedCallback() {
|
||||||
|
this.removeEventListener("click", this.handleClick);
|
||||||
|
this.querySelector<HTMLElement>("[data-add]")?.removeEventListener("click", this.handleAddClick);
|
||||||
|
this.querySelector<HTMLElement>("[data-remove]")?.removeEventListener("click", this.handleRemoveClick);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export abstract class ListElement<T, C extends ListItemElement<T> = ListItemElement<T>> extends ValueElement<
|
||||||
|
T[],
|
||||||
|
ListOptions<T>
|
||||||
|
> {
|
||||||
|
static observedAttributes = ["value", "onadd", "onremove", "onclick"];
|
||||||
|
|
||||||
|
abstract childType: Type<C>;
|
||||||
|
|
||||||
|
className = "list-group";
|
||||||
|
|
||||||
|
buildHTML(): string {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeRefresh() {
|
||||||
|
if (this.hasAttribute("onclick")) {
|
||||||
|
const onclick = new Function("element", this.getAttribute("onclick")!) as ListOptions<T>["onClick"];
|
||||||
|
if (!this.options) {
|
||||||
|
this.options = {};
|
||||||
|
}
|
||||||
|
this.options.onClick = onclick;
|
||||||
|
}
|
||||||
|
if (this.hasAttribute("onadd")) {
|
||||||
|
const onadd = new Function("element", this.getAttribute("onadd")!) as ListOptions<T>["onAdd"];
|
||||||
|
if (!this.options) {
|
||||||
|
this.options = {};
|
||||||
|
}
|
||||||
|
this.options.onAdd = onadd;
|
||||||
|
}
|
||||||
|
if (this.hasAttribute("onremove")) {
|
||||||
|
const onremove = new Function("element", this.getAttribute("onremove")!) as ListOptions<T>["onRemove"];
|
||||||
|
if (!this.options) {
|
||||||
|
this.options = {};
|
||||||
|
}
|
||||||
|
this.options.onRemove = onremove;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onRefresh() {
|
||||||
|
if (this.value) {
|
||||||
|
for (const item of this.value) {
|
||||||
|
this.appendChild(new this.childType(item, this.options));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
import { ListElement, ListItemElement } from "./component";
|
||||||
|
import { DataStorage, StorageValue } from "./storage";
|
||||||
|
|
||||||
|
export class DataManager<T extends StorageValue> {
|
||||||
|
private storage: DataStorage<T>;
|
||||||
|
|
||||||
|
private get dataList(): ListElement<T> {
|
||||||
|
return document.querySelector(this.dataListSelector)!;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
listComponentType: typeof ListElement<T>, //prevent removing as unusing
|
||||||
|
storageKey: string,
|
||||||
|
private dataListSelector: string,
|
||||||
|
) {
|
||||||
|
this.storage = new DataStorage(storageKey);
|
||||||
|
this.load = this.load.bind(this);
|
||||||
|
this.store = this.store.bind(this);
|
||||||
|
this.add = this.add.bind(this);
|
||||||
|
this.remove = this.remove.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
load() {
|
||||||
|
this.dataList.value = this.storage.getAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
store(element: ListItemElement<T>) {
|
||||||
|
if (element.value) {
|
||||||
|
this.storage.add(element.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
add(element: ListItemElement<T>) {
|
||||||
|
const value = element.value;
|
||||||
|
value;
|
||||||
|
if (value && this.storage.add(value)) {
|
||||||
|
const clone: ListItemElement<T> = element.cloneNode(true) as ListItemElement<T>;
|
||||||
|
clone.options = { onRemove: this.remove };
|
||||||
|
this.dataList.appendChild(clone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
remove(element: ListItemElement<T>) {
|
||||||
|
const value = element.value;
|
||||||
|
if (value) {
|
||||||
|
this.storage.remove(value.id);
|
||||||
|
element.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
export class DataStorage<T extends { id: string }> {
|
export interface StorageValue {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DataStorage<T extends StorageValue> {
|
||||||
constructor(private storageKey: string) {}
|
constructor(private storageKey: string) {}
|
||||||
|
|
||||||
private readValues(): T[] {
|
private readValues(): T[] {
|
||||||
|
|||||||
@@ -1,110 +1,52 @@
|
|||||||
|
import { ListElement, ListItemElement, ValueElement } from "../base/component";
|
||||||
import { Channel } from "./model";
|
import { Channel } from "./model";
|
||||||
|
|
||||||
export class ScheduleChannelElement extends HTMLElement {
|
export class ScheduleChannelGroup extends ValueElement<string | Channel[], {}> {
|
||||||
static observedAttributes = ["channel", "removable"];
|
className: string = "list-group-item list-group-item-action";
|
||||||
|
|
||||||
channel: Channel | null = null;
|
buildHTML(): string {
|
||||||
|
const groupName = Array.isArray(this.value) ? "Open All" : this.value;
|
||||||
constructor() {
|
const groupId = Array.isArray(this.value)
|
||||||
super();
|
? this.value.map((item) => `${item.provider}:${item.id}`).join(",")
|
||||||
this.handleClick = this.handleClick.bind(this);
|
: this.value || "";
|
||||||
this.handleRemoveClick = this.handleRemoveClick.bind(this);
|
return `
|
||||||
this.handleAddClick = this.handleAddClick.bind(this);
|
<a href="group/${encodeURIComponent(groupId)}"
|
||||||
}
|
|
||||||
|
|
||||||
connectedCallback() {
|
|
||||||
this.channel = JSON.parse(this.getAttribute("channel") || "{}");
|
|
||||||
this.innerHTML = `
|
|
||||||
<a href="${this.channel?.provider}/${this.channel?.id}"
|
|
||||||
class="d-flex justify-content-between align-items-start text-decoration-none">
|
class="d-flex justify-content-between align-items-start text-decoration-none">
|
||||||
<span>
|
<span>
|
||||||
<span class="text-success me-2" style="color: green;" data-add>
|
<span class="text-primary" style="text-transform: capitalize">${groupName}</span>
|
||||||
+
|
|
||||||
</span>
|
|
||||||
<span class="badge me-1 text-bg-secondary">${this.channel?.provider}</span>
|
|
||||||
<span class="text-primary">${this.channel?.name}</span>
|
|
||||||
</span>
|
|
||||||
<span>
|
|
||||||
<span class="text-danger ms-2" data-remove>
|
|
||||||
✕
|
|
||||||
</span>
|
|
||||||
</span>
|
</span>
|
||||||
</a>`;
|
</a>`;
|
||||||
this.classList = "list-group-item list-group-item-action";
|
}
|
||||||
this.addEventListener("click", this.handleClick);
|
}
|
||||||
if (this.hasAttribute("removable")) {
|
|
||||||
this.querySelector<HTMLElement>("[data-remove]")?.addEventListener("click", this.handleRemoveClick);
|
customElements.define("schedule-channel-group", ScheduleChannelGroup);
|
||||||
|
|
||||||
|
export class ScheduleChannelItem extends ListItemElement<Channel> {
|
||||||
|
getHref(value: Channel): string {
|
||||||
|
return `${value.provider}/${value.id}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
getContent(value: Channel): string {
|
||||||
|
return `
|
||||||
|
<span class="badge me-1 text-bg-secondary">${value.provider}</span>
|
||||||
|
<span class="text-primary">${value.name}</span>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ScheduleChannelList extends ListElement<Channel, ScheduleChannelItem> {
|
||||||
|
childType = ScheduleChannelItem;
|
||||||
|
|
||||||
|
public onRefresh(): void {
|
||||||
|
super.onRefresh();
|
||||||
|
if (this.value?.length) {
|
||||||
|
const groupElement = new ScheduleChannelGroup(this.value);
|
||||||
|
this.prepend(groupElement);
|
||||||
} else {
|
} else {
|
||||||
this.querySelector("[data-remove]")?.remove();
|
this.prepend("Empty");
|
||||||
}
|
|
||||||
if (this.hasAttribute("addeable")) {
|
|
||||||
this.querySelector<HTMLElement>("[data-add]")?.addEventListener("click", this.handleAddClick);
|
|
||||||
} else {
|
|
||||||
this.querySelector("[data-add]")?.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
disconnectedCallback() {
|
|
||||||
this.removeEventListener("click", this.handleClick);
|
|
||||||
this.querySelector<HTMLElement>("[data-remove]")?.removeEventListener("click", this.handleRemoveClick);
|
|
||||||
}
|
|
||||||
|
|
||||||
handleClick(event: MouseEvent) {
|
|
||||||
if (this.channel) {
|
|
||||||
window.scheduleChannelStorage.add(this.channel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
handleRemoveClick(event: MouseEvent) {
|
|
||||||
event.preventDefault();
|
|
||||||
event.stopPropagation();
|
|
||||||
if (this.channel) {
|
|
||||||
window.scheduleChannelStorage.remove(this.channel.id);
|
|
||||||
}
|
|
||||||
this.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
handleAddClick(event: MouseEvent) {
|
|
||||||
event.preventDefault();
|
|
||||||
event.stopPropagation();
|
|
||||||
if (this.channel) {
|
|
||||||
if (window.scheduleChannelStorage.add(this.channel)) {
|
|
||||||
const storedChannels = document.querySelector("#storedChannels");
|
|
||||||
if (storedChannels) {
|
|
||||||
const clone: Element = this.cloneNode(true) as Element;
|
|
||||||
clone.removeAttribute("addeable");
|
|
||||||
clone.setAttribute("removable", "");
|
|
||||||
storedChannels.appendChild(clone);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class ScheduleChannelGroupElement extends HTMLElement {
|
customElements.define("schedule-channel-item", ScheduleChannelItem);
|
||||||
static observedAttributes = ["group", "channels"];
|
customElements.define("schedule-channel-list", ScheduleChannelList);
|
||||||
|
|
||||||
group: string | null = null;
|
|
||||||
channels: Channel[] | null = null;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
connectedCallback() {
|
|
||||||
this.channels = JSON.parse(this.getAttribute("channels") || "[]");
|
|
||||||
this.group = this.getAttribute("group");
|
|
||||||
const groupId = this.channels?.map((item) => `${item.provider}:${item.id}`).join(",") || this.group || "n/a";
|
|
||||||
this.innerHTML = `
|
|
||||||
<a href="group/${encodeURIComponent(groupId)}"
|
|
||||||
class="d-flex justify-content-between align-items-start text-decoration-none">
|
|
||||||
<span>
|
|
||||||
<span class="text-primary" style="text-transform: capitalize">${this.group || "Open group"}</span>
|
|
||||||
</span>
|
|
||||||
</a>`;
|
|
||||||
this.classList = "list-group-item list-group-item-action";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
customElements.define("schedule-channel", ScheduleChannelElement);
|
|
||||||
customElements.define("schedule-channel-group", ScheduleChannelGroupElement);
|
|
||||||
|
|||||||
@@ -1,34 +1,11 @@
|
|||||||
import { DataStorage } from "../base/storage";
|
import { DataManager } from "../base/manager";
|
||||||
import { ScheduleChannelElement, ScheduleChannelGroupElement } from "./component";
|
import { ScheduleChannelList } from "./component";
|
||||||
import { Channel } from "./model";
|
import { Channel } from "./model";
|
||||||
|
|
||||||
export class ScheduleChannelManager {
|
|
||||||
loadChannels(selector: string) {
|
|
||||||
const channels = window.scheduleChannelStorage.getAll();
|
|
||||||
const container = document.querySelector(selector);
|
|
||||||
if (container) {
|
|
||||||
container.innerHTML = "";
|
|
||||||
if (channels.length > 0) {
|
|
||||||
const groupElement = new ScheduleChannelGroupElement();
|
|
||||||
groupElement.setAttribute("channels", JSON.stringify(channels));
|
|
||||||
container.append(groupElement);
|
|
||||||
}
|
|
||||||
for (const channel of channels) {
|
|
||||||
const element = new ScheduleChannelElement();
|
|
||||||
element.setAttribute("channel", JSON.stringify(channel));
|
|
||||||
element.setAttribute("removable", "");
|
|
||||||
container.appendChild(element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
scheduleChannelStorage: DataStorage<Channel>;
|
scheduleChannelManager: DataManager<Channel>;
|
||||||
scheduleChannelManager: ScheduleChannelManager;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.scheduleChannelStorage = new DataStorage("schedule:channels");
|
window.scheduleChannelManager = new DataManager(ScheduleChannelList, "schedule:channels", "#storedChannels");
|
||||||
window.scheduleChannelManager = new ScheduleChannelManager();
|
|
||||||
|
|||||||
@@ -1,63 +1,33 @@
|
|||||||
|
import { ListElement, ListItemElement } from "../base/component";
|
||||||
import { Location } from "./model";
|
import { Location } from "./model";
|
||||||
|
|
||||||
export class WeatherLocationElement extends HTMLElement {
|
export class WheatherLocationItem extends ListItemElement<Location> {
|
||||||
static observedAttributes = ["location", "removable"];
|
getHref(value: Location): string {
|
||||||
|
return `${value.provider}/${value.id}`;
|
||||||
location: Location | null = null;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
this.handleClick = this.handleClick.bind(this);
|
|
||||||
this.handleRemoveClick = this.handleRemoveClick.bind(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
getContent(value: Location): string {
|
||||||
this.location = JSON.parse(this.getAttribute("location") || "{}");
|
return `
|
||||||
this.innerHTML = `
|
<span class="badge me-1 text-bg-secondary">${value.provider}</span>
|
||||||
<a href="${this.location?.provider}/${this.location?.id}"
|
<span class="fi fi-${value.country_code} me-1"></span>
|
||||||
class="d-flex justify-content-between align-items-start text-decoration-none">
|
<span class="text-primary">${value.name}</span>
|
||||||
<span>
|
<span class="small ms-1 text-secondary">
|
||||||
<span class="badge me-1 text-bg-secondary">${this.location?.provider}</span>
|
${value.country}, ${value.district}, ${value.subdistrict}
|
||||||
<span class="fi fi-${this.location?.country_code} me-1"></span>
|
</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="text-danger ms-2" data-remove>
|
|
||||||
✕
|
|
||||||
</span>
|
|
||||||
</span>
|
|
||||||
</a>`;
|
|
||||||
this.classList = "list-group-item list-group-item-action";
|
|
||||||
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);
|
|
||||||
this.querySelector<HTMLElement>("[data-remove]")?.removeEventListener("click", this.handleRemoveClick);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
export class WeatherLocationList extends ListElement<Location, WheatherLocationItem> {
|
||||||
|
childType = WheatherLocationItem;
|
||||||
|
|
||||||
|
public onRefresh(): void {
|
||||||
|
super.onRefresh();
|
||||||
|
if (!this.value?.length) {
|
||||||
|
this.prepend("Empty");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
customElements.define("weather-location-item", WheatherLocationItem);
|
||||||
|
customElements.define("weather-location-list", WeatherLocationList);
|
||||||
|
|||||||
@@ -1,29 +1,11 @@
|
|||||||
import { DataStorage } from "../base/storage";
|
import { DataManager } from "../base/manager";
|
||||||
import { WeatherLocationElement } from "./component";
|
import { WeatherLocationList } from "./component";
|
||||||
import { Location } from "./model";
|
import { Location } from "./model";
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
weatherLocationStorage: DataStorage<Location>;
|
weatherLocationManager: DataManager<Location>;
|
||||||
weatherLocationManager: WeatherLocationManager;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.weatherLocationStorage = new DataStorage("weather:locations");
|
window.weatherLocationManager = new DataManager(WeatherLocationList, "weather:locations", "#storedLocations");
|
||||||
window.weatherLocationManager = new WeatherLocationManager();
|
|
||||||
|
|||||||
Reference in New Issue
Block a user