-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
346 additions
and
286 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
app/assets/javascripts/alpha_simprini/stacker/app.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
app/assets/javascripts/alpha_simprini/stacker/card.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
app/assets/javascripts/alpha_simprini/stacker/cards.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
72 changes: 72 additions & 0 deletions
72
app/assets/javascripts/alpha_simprini/stacker/history_controller.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Oops, something went wrong.