import util from "./util" import { Message, Author } from "./message" import { MessageThread } from "./thread" var _ = util._ var $ = util.$ export class AdapterElement extends HTMLElement { static observedAttributes = [ "data-latest", "data-view", "data-viewing" ] private _latest: string = "" ; private _view: string = ""; private _name: string = "" private _viewing: string = ""; // TODO: use visibility of the thread to organize into DMs and public threads private _threads: MessageThread[] = []; private _orphans: Message[] = []; constructor() { super(); } connectedCallback() { const name = this.getAttribute("data-name"); this._name = name ?? ""; this._view = ""; this.buildThreads(); this.setAttribute("data-view", "index"); } attributeChangedCallback() { // set the viewing subject if it's changed const viewing = this.getAttribute("data-viewing"); if (this._viewing != viewing && viewing != null) { this._viewing = viewing; // if the viewing subject changed (not to nothing), unset the view // this will force it to refresh if (this._viewing) { this._view = ""; } } // initialize the view if it's changed const view = this.getAttribute("data-view"); if (this._view != view ?? "index") { console.log("view changed! let's go") this._view = view ?? "index"; console.log(this._view); switch (this._view) { case "index": this.setIdxView(); this.populateIdxView(); break; case "thread": this.setThreadView(); this.populateThreadView(); break; case "profile": this.setProfileView(); this.populateProfileView(); break; } } // if latest changed, check if it's a message const latest = this.getAttribute("latest"); if (latest ?? "" != this._latest) { this._latest = latest ?? ""; console.log("about to update the index view"); let datastore = _("datastore"); console.log(datastore); const latestMsg = datastore.messages.get(this._latest); if (latestMsg) { const rootId = this.placeMsg(this._latest); // if rootId is null, this is an orphan and we don't need to actually do any updates yet if (rootId) { switch (this._view) { case "index": this.updateIdxView(this._latest, rootId); break; case "thread": // if the the message is part of this thread, update it case "profile": // if the message is from this user, show it in their profile break; } } } else { const latestAuthor = _("datastore")[this._name].profileCache.get(this._latest); switch (this._view) { case "index": case "thread": case "profile": break; } } // so, try to insert it into the threads // then, switch on view // if index, iterate through the topics and find the one to indicate new activity, // if thread, if any relatives are in this thread, insert message appropriately // if profile, if latest is this profile, update it } } setIdxView() { this.innerHTML = "