Changeset - e27a45d2b90a
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 20 months ago 2023-05-18 02:08:19
drewp@bigasterisk.com
suppress some ts warnings on already-broken code
1 file changed with 22 insertions and 18 deletions:
0 comments (0 inline, 0 general)
light9/web/ResourceDisplay.ts
Show inline comments
 
import debug from "debug";
 
import { css, html, LitElement, PropertyValues } from "lit";
 
import { customElement, property, state } from "lit/decorators.js";
 
import { NamedNode } from "n3";
 
// import { GraphChangedEvent } from "../../web/RdfdbSyncedGraph";
 
// import { runHandler } from "./GraphAwarePage";
 
import { Patch, patchContainsPreds, patchSizeSummary } from "./patch";
 
import { getTopGraph } from "./RdfdbSyncedGraph";
 
import { SyncedGraph } from "./SyncedGraph";
 
debug.enable("*");
 
const log = debug("device-el");
 
const RDFS_LABEL = new NamedNode("http://www.w3.org/2000/01/rdf-schema#label");
 

	
 
@customElement("resource-display")
 
export class ResourceDisplay extends LitElement {
 
  graph!: SyncedGraph;
 
  private renameTo?: string
 
  static styles = [
 
    css`
 
      :host {
 
        display: inline-block;
 
      }
 

	
 
      a.resource {
 
        color: inherit;
 
        text-decoration: none;
 
      }
 

	
 
      .resource {
 
        border: 1px solid #545454;
 
        border-radius: 5px;
 
        padding: 1px;
 
        margin: 2px;
 
        background: rgb(49, 49, 49);
 
        display: inline-block;
 
        text-shadow: 1px 1px 2px black;
 
      }
 
      .resource.minor {
 
        background: none;
 
        border: none;
 
      }
 
      .resource a {
 
        color: rgb(150, 150, 255);
 
        padding: 1px;
 
        display: inline-block;
 
      }
 
      .resource.minor a {
 
        text-decoration: none;
 
        color: rgb(155, 155, 193);
 
        padding: 0;
 
      }
 
    `,
 
  ];
 

	
 
  render() {
 
    return html` <span class="${this.resClasses()}">
 
      <a href="${this.href()}" id="uri"> <!-- type icon goes here -->${this.label}</a>
 
    </span>`;
 
    // <template is="dom-if" if="{{rename}}">
 
    //   <button on-click="onRename">Rename</button>
 

	
 
    //   <paper-dialog id="renameDialog" modal on-iron-overlay-closed="onRenameClosed">
 
    //     <p>
 
    //       New label:
 
    //       <input id="renameTo" autofocus type="text" value="{{renameTo::input}}" on-keydown="onRenameKey" />
 
@@ -98,73 +99,76 @@ export class ResourceDisplay extends Lit
 
    if (!this.uri) {
 
      return "javascript:;";
 
    }
 
    return typeof this.uri === "string" ? this.uri : this.uri.value;
 
  }
 

	
 
  updated(changedProperties: PropertyValues) {
 
    if (changedProperties.has("uri")) {
 
      if (!this.graph) {
 
        return; /*too soon*/
 
      }
 
      this.runUriHandler();
 
    }
 
  }
 

	
 
  resClasses() {
 
    return this.minor ? "resource minor" : "resource";
 
  }
 

	
 
  runUriHandler() {
 

	
 
    this.graph.runHandler(this.onUri.bind(this), `rdisplay ${this.href()}` /*needs uniqueness?*/);
 
  }
 

	
 
  onUri(patch?: Patch) {
 
    if (patch && !patchContainsPreds(patch, [this.graph.Uri("rdfs:label")])) {
 
      return;
 
    }
 

	
 
    if (!this.uri) {
 
      this.label = "<no uri>";
 
      return;
 
    }
 

	
 
    const uri = this.realUri();
 
    this.graph.runHandler(this.setLabel.bind(this), `label ${uri.value}`);
 
  }
 

	
 
  setLabel(patch?: Patch) {
 
    if (patch && !patchContainsPreds(patch, [RDFS_LABEL])) {
 
      return;
 
    }
 
    const uri = this.realUri();
 
    this.label = this.graph.labelOrTail(uri);
 
  }
 

	
 
  onRename() {
 
    this.renameTo = this.label;
 
    this.shadowRoot.querySelector("#renameDialog").open();
 
    this.shadowRoot.querySelector("#renameTo").setSelectionRange(0, -1);
 
    throw new Error("fixme");
 
    // this.shadowRoot.querySelector("#renameDialog").open();
 
    // this.shadowRoot.querySelector("#renameTo")!.setSelectionRange(0, -1);
 
  }
 

	
 
  onRenameKey(ev) {
 
    if (ev.key == "Enter") {
 
      this.shadowRoot.querySelector("[dialog-confirm]").click();
 
    }
 
    if (ev.key == "Escape") {
 
      this.shadowRoot.querySelector("[dialog-dismiss]").click();
 
    }
 
  onRenameKey(ev: KeyboardEvent) {
 
    throw new Error("fixme");
 
    // if (ev.key == "Enter") {
 
    //   this.shadowRoot.querySelector("[dialog-confirm]").click();
 
    // }
 
    // if (ev.key == "Escape") {
 
    //   this.shadowRoot.querySelector("[dialog-dismiss]").click();
 
    // }
 
  }
 

	
 
  onRenameClosed() {
 
    var dialog = this.shadowRoot.querySelector("#renameDialog");
 
    if (dialog.closingReason.confirmed) {
 
      var label = this.graph.Uri("rdfs:label");
 
      var ctxs = this.graph.contextsWithPattern(this.uri, label, null);
 
      if (ctxs.length != 1) {
 
        throw new Error(`${ctxs.length} label stmts for ${this.uri.label}`);
 
      }
 
      this.graph.patchObject(typeof this.uri === "string" ? this.graph.Uri(this.uri) : this.uri, label, this.graph.Literal(this.renameTo), ctxs[0]);
 
    }
 
    throw new Error("fixme");
 
    // var dialog = this.shadowRoot.querySelector("#renameDialog");
 
    // if (dialog.closingReason.confirmed) {
 
    //   var label = this.graph.Uri("rdfs:label");
 
    //   var ctxs = this.graph.contextsWithPattern(this.uri, label, null);
 
    //   if (ctxs.length != 1) {
 
    //     throw new Error(`${ctxs.length} label stmts for ${this.uri.label}`);
 
    //   }
 
    //   this.graph.patchObject(typeof this.uri === "string" ? this.graph.Uri(this.uri) : this.uri, label, this.graph.Literal(this.renameTo), ctxs[0]);
 
    // }
 
  }
 
}
0 comments (0 inline, 0 general)