Changeset - 5f31e02fbf70
[Not reviewed]
default
0 1 0
Drew Perttula - 6 years ago 2019-06-01 02:26:05
drewp@bigasterisk.com
live page stays on the same effect when you reload, now that it writes effectChoice to the url
Ignore-this: f1143abd07d4775f97868abaf32d359c
1 file changed with 18 insertions and 0 deletions:
0 comments (0 inline, 0 general)
light9/web/live/live.coffee
Show inline comments
 
@@ -339,12 +339,15 @@ class GraphToControls
 
    return value? and value != 0 and value != '#000000'
 

	
 
  emptyEffect: ->
 
    @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)
 
    effectSetting = @activeSettings.effectSettingLookup(device, deviceAttr)
 

	
 
    # sometimes this misses an existing setting, which leads to a mess
 
@@ -409,12 +412,13 @@ coffeeElementSetup(class Light9LiveContr
 
    'onEffectChoice(effectChoice)'
 
    ]
 

	
 
  constructor: ->
 
    super()
 
    @graphToControls = null
 
    @okToWriteUrl = false
 

	
 
  ready: ->
 
    super.ready()
 
    @currentSettings = {}
 

	
 
  onGraph: ->
 
@@ -422,27 +426,41 @@ coffeeElementSetup(class Light9LiveContr
 
    @graph.runHandler(@update.bind(@), 'Light9LiveControls update')
 

	
 
    # need graph to be loaded, so we don't make double settings? not sure.
 
    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
 
      
 
  onEffectChoice: ->
 
    U = (x) => @graph.Uri(x)
 
    if not @effectChoice?
 
      # unlink
 
      @graphToControls.setEffect(null) if @graphToControls?
 
    else
 
      log('load', @effectChoice)
 
      @graphToControls.setEffect(@graph.Uri(@effectChoice)) if @graphToControls?
 
    @writeToUrl(@effectChoice)
 
 
 
  clearAll: ->
 
    # clears the effect!
 
    @graphToControls.emptyEffect()
 
    
 
  update: ->
0 comments (0 inline, 0 general)