changeset 2290:e9239995eb57

fix effect new/clear commands
author drewp@bigasterisk.com
date Mon, 29 May 2023 23:08:15 -0700
parents f2c6b39c155c
children 1281b4677862
files light9/live/Effect.ts light9/live/Light9DeviceSettings.ts
diffstat 2 files changed, 40 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/light9/live/Effect.ts	Mon May 29 22:49:40 2023 -0700
+++ b/light9/live/Effect.ts	Mon May 29 23:08:15 2023 -0700
@@ -28,6 +28,32 @@
   }
 }
 
+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 @@
     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 @@
     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");
--- a/light9/live/Light9DeviceSettings.ts	Mon May 29 22:49:40 2023 -0700
+++ b/light9/live/Light9DeviceSettings.ts	Mon May 29 23:08:15 2023 -0700
@@ -6,7 +6,7 @@
 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 @@
 
   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 @@
   }
 
   newEffect() {
-    // this.effectChoice = this.graphToControls.newEffect();
+    this.setCurrentEffect(newEffect(this.graph));
   }
 
   clearAll() {
-    // clears the effect!
-    return; //this.graphToControls.emptyEffect();
+    this.currentEffect?.clearAllSettings()
   }
 }