changeset 1904:5f31e02fbf70

live page stays on the same effect when you reload, now that it writes effectChoice to the url Ignore-this: f1143abd07d4775f97868abaf32d359c
author Drew Perttula <drewp@bigasterisk.com>
date Sat, 01 Jun 2019 02:26:05 +0000
parents bc8989d84bb2
children e29315086f9f
files light9/web/live/live.coffee
diffstat 1 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/light9/web/live/live.coffee	Sat Jun 01 02:25:28 2019 +0000
+++ b/light9/web/live/live.coffee	Sat Jun 01 02:26:05 2019 +0000
@@ -342,6 +342,9 @@
     @activeSettings.forAll(@_removeEffectSetting.bind(@))
 
   controlChanged: (device, deviceAttr, value) ->
+    # todo: controls should be disabled if there's no effect and they won't do anything.
+    return if not @effect
+    
     # value is float or #color or (Uri or null)
     if (value == undefined or (typeof value == "number" and isNaN(value)) or (typeof value == "object" and value != null and not value.id))
       throw new Error("controlChanged sent bad value " + value)
@@ -412,6 +415,7 @@
   constructor: ->
     super()
     @graphToControls = null
+    @okToWriteUrl = false
 
   ready: ->
     super.ready()
@@ -425,9 +429,22 @@
     setTimeout(@setFromUrl.bind(@), 1)
 
   setFromUrl: ->
+    # not a continuous bidi link between url and effect; it only reads
+    # the url when the page loads.
     effect = new URL(window.location.href).searchParams.get('effect')
     if effect?
+      log('found url', effect)
       @effectChoice = effect
+    @okToWriteUrl = true
+
+  writeToUrl: (effectStr) ->
+    return unless @okToWriteUrl
+    u = new URL(window.location.href)
+    if u.searchParams.get('effect') == effectStr
+      return        
+    u.searchParams.set('effect', effectStr)
+    window.history.replaceState({}, "", u.href)
+    log('wrote new url', u.href)
 
   newEffect: ->
     @effectChoice = @graphToControls.newEffect().value
@@ -440,6 +457,7 @@
     else
       log('load', @effectChoice)
       @graphToControls.setEffect(@graph.Uri(@effectChoice)) if @graphToControls?
+    @writeToUrl(@effectChoice)
  
   clearAll: ->
     # clears the effect!