Skip to content

Instantly share code, notes, and snippets.

@Ahrengot
Last active December 21, 2015 06:49
Show Gist options
  • Save Ahrengot/6266976 to your computer and use it in GitHub Desktop.
Save Ahrengot/6266976 to your computer and use it in GitHub Desktop.
define ["underscore", "view/elements/DrawerElement"], (_, DrawerElementView) ->
class ConfigurationLoader
constructor: ->
_.bindAll @
@load()
getConfigId: ->
regex = ///
(.+?) # Match anything before config/
config/ # Match "config/"
([0-9]+) # Match any number from 0 to infinity and save it as $2
/? # optionally match a trailing slash
///
location.hash.replace( regex, "$2" )
getJsonPath: ->
GLOBAL.BASE_URL + GLOBAL.JSON_TEMPLATES_DIR + "/template#{ @getConfigId() }.json"
load: ->
$.getJSON( @getJsonPath() ).then @parseConfiguration
parseConfiguration: (data) ->
@buildContainer container for container in data.nodes
return
getContainerView: (name) ->
model = app.config.elements.findWhere { name:name }
if model then return new DrawerElementView model:model
else return false
getElementPosition: (el) ->
unless @artboardOffset? then @artboardOffset = app.artboard.$el.offset().left
return { x: el.position_x + @artboardOffset, y: el.position_y }
buildContainer: (container) ->
view = @getContainerView container.element_name
position = @getElementPosition container
$(document).trigger "ahrengot/spawn_element", [view, position]
return ConfigurationLoader
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment