Files
@ f9edd9819b7d
Branch filter:
Location: light9/light9/live/Light9LiveControls.ts
f9edd9819b7d
5.7 KiB
video/MP2T
move live/ out of web; it's just a normal (web-only) tool now
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import debug from "debug";
import { css, html, LitElement, PropertyValues } from "lit";
import { customElement, property } from "lit/decorators.js";
import { NamedNode } from "n3";
import { sortBy, uniq } from "underscore";
import { Patch, patchContainsPreds } from "../web/patch";
import { getTopGraph } from "../web/RdfdbSyncedGraph";
import { SyncedGraph } from "../web/SyncedGraph";
import { GraphToControls } from "./GraphToControls";
export { EditChoice } from "../web/EditChoice";
export { Light9DeviceControl as Light9LiveDeviceControl } from "./Light9DeviceControl";
const log = debug("controls");
@customElement("light9-live-controls")
export class Light9LiveControls extends LitElement {
graph!: SyncedGraph;
static styles = [
css`
:host {
display: flex;
flex-direction: column;
}
#preview {
width: 100%;
}
#deviceControls {
flex-grow: 1;
position: relative;
width: 100%;
overflow-y: auto;
}
light9-live-device-control > div {
break-inside: avoid-column;
}
light9-live-device-control {
}
`,
];
render() {
return html`
<rdfdb-synced-graph></rdfdb-synced-graph>
<h1>device control</h1>
<div id="save">
<div>
<button @click=${this.newEffect}>New effect</button>
<edit-choice .uri=${this.effectChoice}></edit-choice>
<button @click=${this.clearAll}>clear settings in this effect</button>
</div>
</div>
<div id="deviceControls">
${this.devices.map(
(device: NamedNode) => html`
<light9-device-control .uri=${device} .effect=${this.effectChoice} .graphToControls=${this.graphToControls}></light9-device-control>
`
)}
</div>
`;
}
devices: Array<NamedNode> = [];
// uri of the effect being edited, or null. This is the
// master value; GraphToControls follows.
@property() effectChoice: NamedNode | null = null;
graphToControls!: GraphToControls;
okToWriteUrl: boolean = false;
constructor() {
super();
getTopGraph().then((g) => {
this.graph = g;
this.graph.runHandler(this.findDevices.bind(this), "findDevices");
this.graphToControls = new GraphToControls(this.graph);
// this.graph.runHandler(this.xupdate.bind(this), "Light9LiveControls update");
this.setEffectFromUrl();
});
}
updated(changedProperties: PropertyValues) {
if (changedProperties.has("effectChoice")) {
log(`effectChoice to ${this.effectChoice?.value}`);
this.onEffectChoice();
}
}
findDevices(patch?: Patch) {
const U = this.graph.U();
if (patch && !patchContainsPreds(patch, [U("rdf:type")])) {
return;
}
this.devices = [];
let classes = this.graph.subjects(U("rdf:type"), U(":DeviceClass"));
log(`found ${classes.length} device classes`);
uniq(sortBy(classes, "value"), true).forEach((dc) => {
sortBy(this.graph.subjects(U("rdf:type"), dc), "value").forEach((dev) => {
log(`found dev ${dev.value}`)
this.devices.push(dev as NamedNode);
});
});
this.requestUpdate();
}
setEffectFromUrl() {
// not a continuous bidi link between url and effect; it only reads
// the url when the page loads.
const effect = new URL(window.location.href).searchParams.get("effect");
if (effect != null) {
log(`found effect in url ${effect}`);
this.effectChoice = this.graph.Uri(effect);
}
this.okToWriteUrl = true;
}
writeToUrl(effect: NamedNode | null) {
const effectStr = effect ? this.graph.shorten(effect) : "";
if (!this.okToWriteUrl) {
return;
}
const u = new URL(window.location.href);
if ((u.searchParams.get("effect") || "") === effectStr) {
return;
}
u.searchParams.set("effect", effectStr); // this escapes : and / and i wish it didn't
window.history.replaceState({}, "", u.href);
log("wrote new url", u.href);
}
newEffect() {
this.effectChoice = this.graphToControls.newEffect();
}
onEffectChoice() {
const U = (x: any) => this.graph.Uri(x);
if (this.effectChoice == null) {
// unlink
log("onEffectChoice unlink");
if (this.graphToControls != null) {
this.graphToControls.setEffect(null);
}
} else {
if (this.graphToControls != null) {
this.graphToControls.setEffect(this.effectChoice);
} else {
throw new Error("graphToControls not set");
}
}
this.writeToUrl(this.effectChoice);
}
clearAll() {
// clears the effect!
return this.graphToControls.emptyEffect();
}
configureFromGraph() {
const U = (x: string) => this.graph.Uri(x);
const newDevs: NamedNode[] = [];
for (let dc of Array.from(this.graph.sortedUris(this.graph.subjects(U("rdf:type"), U(":DeviceClass"))))) {
for (let dev of Array.from(this.graph.sortedUris(this.graph.subjects(U("rdf:type"), dc)))) {
if (this.graph.contains(dev, U(":hideInLiveUi"), null)) {
continue;
}
if (newDevs.length == 0) newDevs.push(dev);
}
}
log("is this called?");
log(`controls update now has ${newDevs.length} devices`);
this.devices = newDevs;
this.requestUpdate();
return;
// Tried css columns- big slowdown from relayout as I'm scrolling.
// Tried isotope- seems to only scroll to the right.
// Tried columnize- fails in jquery maybe from weird elements.
// not sure how to get this run after the children are created
return setTimeout(
() =>
$("#deviceControls").isotope({
// fitColumns would be nice, but it doesn't scroll vertically
layoutMode: "masonry",
containerStyle: null,
}),
2000
);
}
}
|