Skip to content

Commit

Permalink
Refactor: Elegant-ify function def
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBizzle committed Aug 2, 2024
1 parent edefa41 commit 6f9eb38
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions app/assets/javascripts/beak/widgets/widget-controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,30 @@ class WidgetController
# (Number, Boolean, Array[Any]) => Unit
removeWidgetById: (id, wasNew, extraNotificationArgs) ->

widgetType = null
widgetObj = @ractive.get('widgetObj')

for key, w of widgetObj
if w.id is id
widgetType = w.type
delete widgetObj[key]
break

if widgetType is null
throw new Error('Could not find widget to remove by id')
wobj = @ractive.get('widgetObj')

@ractive.update('widgetObj')
@ractive.fire('deselect-widgets')
entry = Object.entries(wobj).find(([_, v]) -> v.id is id)

if entry?

[index, widget] = entry

widgetType = widget.type
delete wobj[index]

if wasNew
@ractive.fire('new-widget-cancelled', id, widgetType)
@ractive.update('widgetObj')
@ractive.fire('deselect-widgets')

if wasNew
@ractive.fire('new-widget-cancelled', id, widgetType)

else
switch widgetType
when "chooser", "inputBox", "plot", "slider", "switch" then @ractive.fire('recompile-sync', 'system')
@ractive.fire('widget-deleted', id, widgetType, extraNotificationArgs...)

else
switch widgetType
when "chooser", "inputBox", "plot", "slider", "switch" then @ractive.fire('recompile-sync', 'system')
@ractive.fire('widget-deleted', id, widgetType, extraNotificationArgs...)
throw new Error("Failed to find any widget in #{JSON.stringify(wobj)} with ID '#{id}'.")

return

Expand Down

0 comments on commit 6f9eb38

Please sign in to comment.