diff --git a/light9/live/Effect.ts b/light9/live/Effect.ts --- a/light9/live/Effect.ts +++ b/light9/live/Effect.ts @@ -28,6 +28,32 @@ function valuePred(graph: SyncedGraph, a } } +function effContext(graph: SyncedGraph, uri: NamedNode): NamedNode { + return graph.Uri(uri.value.replace("light9.bigasterisk.com/effect", `light9.bigasterisk.com/show/${shortShow}/effect`)); +} + +export function newEffect(graph: SyncedGraph): NamedNode { + // wrong- this should be our editor's scratch effect, promoted to a + // real one when you name it. + const uri = graph.nextNumberedResource(graph.Uri("http://light9.bigasterisk.com/effect/effect")); + + const effect = new Effect(graph, uri); + const U = graph.U(); + const ctx = effContext(graph, uri); + const quad = (s: Quad_Subject, p: Quad_Predicate, o: Quad_Object) => graph.Quad(s, p, o, ctx); + + const addQuads = [ + quad(uri, U("rdf:type"), U(":Effect")), + quad(uri, U("rdfs:label"), graph.Literal(uri.value.replace(/.*\//, ""))), + quad(uri, U(":publishAttr"), U(":strength")), + ]; + const patch = new Patch([], addQuads); + log("init new effect", patch); + graph.applyAndSendPatch(patch); + + return effect.uri; +} + // effect settings data; r/w sync with the graph export class Effect { private settings: Array<{ device: NamedNode; deviceAttr: NamedNode; setting: NamedNode; value: ControlValue }> = []; @@ -37,25 +63,10 @@ export class Effect { public graph: SyncedGraph, public uri: NamedNode // called if the graph changes our values and not when the caller uses edit() ) { - this.ctxForEffect = this.graph.Uri(this.uri.value.replace("light9.bigasterisk.com/effect", `light9.bigasterisk.com/show/${shortShow}/effect`)); + this.ctxForEffect = effContext(this.graph, this.uri); graph.runHandler(this.rebuildSettingsFromGraph.bind(this), `effect sync ${uri.value}`); } - addNewEffectToGraph() { - const U = this.graph.U(); - const quad = (s: Quad_Subject, p: Quad_Predicate, o: Quad_Object) => this.graph.Quad(s, p, o, this.ctxForEffect); - - const addQuads = [ - quad(this.uri, U("rdf:type"), U(":Effect")), - quad(this.uri, U("rdfs:label"), this.graph.Literal(this.uri.value.replace(/.*\//, ""))), - quad(this.uri, U(":publishAttr"), U(":strength")), - ]; - const patch = new Patch([], addQuads); - log("init new effect", patch); - this.settings = []; - this.graph.applyAndSendPatch(patch); - } - rebuildSettingsFromGraph(patch?: Patch) { const U = this.graph.U(); if (patch && !patch.containsAnyPreds([U(":setting"), U(":device"), U(":deviceAttr")])) { @@ -186,6 +197,12 @@ export class Effect { return new Patch(toDel, []); } +clearAllSettings() { + for (let s of this.settings) { + this.graph.applyAndSendPatch(this.removeEffectSetting(s.setting)) + } +} + private nodeForValue(value: ControlValue): NamedNode | Literal { if (value === null) { throw new Error("no value"); diff --git a/light9/live/Light9DeviceSettings.ts b/light9/live/Light9DeviceSettings.ts --- a/light9/live/Light9DeviceSettings.ts +++ b/light9/live/Light9DeviceSettings.ts @@ -6,7 +6,7 @@ import { sortBy, uniq } from "underscore import { Patch } from "../web/patch"; import { getTopGraph } from "../web/RdfdbSyncedGraph"; import { SyncedGraph } from "../web/SyncedGraph"; -import { Effect } from "./Effect"; +import { Effect, newEffect } from "./Effect"; export { EditChoice } from "../web/EditChoice"; export { Light9DeviceControl as Light9LiveDeviceControl } from "./Light9DeviceControl"; const log = debug("settings"); @@ -80,8 +80,12 @@ export class Light9DeviceSettings extend onEffectChoice2(ev: CustomEvent) { const uri = ev.detail.newValue as NamedNode; + this.setCurrentEffect(uri); + } + setCurrentEffect(uri: NamedNode) { if (uri === null) { this.currentEffect = null; + // todo: wipe the UI settings } else { this.currentEffect = new Effect(this.graph, uri); } @@ -140,11 +144,10 @@ export class Light9DeviceSettings extend } newEffect() { - // this.effectChoice = this.graphToControls.newEffect(); + this.setCurrentEffect(newEffect(this.graph)); } clearAll() { - // clears the effect! - return; //this.graphToControls.emptyEffect(); + this.currentEffect?.clearAllSettings() } }