Skip to content

Commit

Permalink
split out constituent files
Browse files Browse the repository at this point in the history
  • Loading branch information
collin committed Apr 15, 2013
1 parent 863d22c commit 346a043
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 286 deletions.
17 changes: 17 additions & 0 deletions app/assets/javascripts/alpha_simprini/stacker/app.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Stacker.App
constructor: (@root, options={}) ->
options.history ||= window.history
options.storage ||= window.sessionStorage

@networkController = new Stacker.NetworkController
@historyStack = Stacker.alloc(Stacker.Cards)
@stackView = new Stacker.StackView
model:@historyStack,
el:options.container,
header:options.header
@historyController = new Stacker.HistoryController(
@historyStack, options.history, options.storage
)
@navigationController = new Stacker.NavigationController(
@root, @networkController, @historyStack, @historyController
)
25 changes: 25 additions & 0 deletions app/assets/javascripts/alpha_simprini/stacker/card.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Stacker.Card extends Backbone.Model

constructor: ->
super
@get('id') or @set(id: _.uniqueId('id-') )

_validate: -> true

@fromJSON: (data, repository) ->
console.log "Stacker.Card#fromJSON", data
stackId = data.stackId
data.stackId = undefined
stack = data.stack = repository.get(stackId) || Stacker.alloc(Stacker.Cards, [], id:stackId)
card = Stacker.alloc Stacker.Card, data
Stacker.updateCardFromHtml(card, data.html)
stack.add card
card

toJSON: ->
data = _.pick @attributes, 'html', 'link', 'id'
data.stackId = @get('stack').id
data

toString: ->
"<Stacker.Card #{@cid} #{@get 'title'}>"
13 changes: 13 additions & 0 deletions app/assets/javascripts/alpha_simprini/stacker/cards.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Stacker.Cards extends Backbone.Collection
model: Stacker.Card

constructor: (models=[], options={}) ->
super
@cid = _.uniqueId('c')
@id ||= options.id || _.uniqueId('id-')

@fromJSON: (data) ->
cards = Stacker.alloc(this)
for item in data
cards.add Stacker.fromJSON(Stacker.Card, item)
cards
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class Stacker.HistoryController
@START: {start:true}
START: @START
constructor: (@stack, @history, @storage) ->
console.log "Starting State", @history.state

@forwardStack = Stacker.alloc(Stacker.Cards)

if @_madeState(@history.state)
@loadStash()

@stack.on 'add', => @clearForwardStack()
@stack.on 'add', (item) => @pushState(item)
@stack.on 'jump', (count) => history.go(count)

@stack.on 'add remove change', @stash

$(window).on 'popstate', (event) => @popstate(event.originalEvent)

if @_madeState(@history.state)
@popstate @history
# else
# @history.replaceState @START, null, location.href

currentCard: -> @stack.last()

currentStack: -> @currentCard()?.get('stack')

loadStash: ->
return unless raw = @storage.getItem "Stacker-stash"
data = JSON.parse(raw)

@stack.set Stacker.Cards.fromJSON(data.stack).models, silent:true
@forwardStack.set Stacker.Cards.fromJSON(data.forwardStack).models, silent:true

stash: =>
@storage.setItem "Stacker-stash", JSON.stringify(
stack: @stack.toJSON(),
forwardStack: @forwardStack.toJSON()
)

popstate: ({state}) ->
console.log "POPSTATE", state
return false unless state?.id? or state?.start is true

if state.id and item = @stack.get(state)
list = @stack.slice @stack.indexOf(item) + 1, @stack.length
@stack.remove(item, silent:true) for item in list
@forwardStack.add(item, at:0) for item in list.reverse()

else if state.id and item = @forwardStack.get(state)
list = @forwardStack.slice 0, @forwardStack.indexOf(item) + 1
@forwardStack.remove(item) for item in list
@stack.add(item, silent:true) for item in list

else if state.start is true
for item in @stack.slice().reverse()
@forwardStack.add(item, at:0)
@stack.set([], silent: true)

@stack.trigger('change')

pushState: (item) ->
return unless item
state = id:item.get('id'), namespace:"Stacker.HistoryController"
@history.pushState(state, null, item.get('link'))

clearForwardStack: ->
@forwardStack.set [], silent:true

_madeState: (state) ->
state?.namespace is "Stacker.HistoryController"
Loading

0 comments on commit 346a043

Please sign in to comment.