Skip to content

Instantly share code, notes, and snippets.

@c-spencer
Created April 2, 2012 11:31
Show Gist options
  • Save c-spencer/2282819 to your computer and use it in GitHub Desktop.
Save c-spencer/2282819 to your computer and use it in GitHub Desktop.
window.undo_tracking_labels = {}
class Sysdea.Undoer
constructor: (@label) ->
if @label? and !undo_tracking_labels[@label]
undo_tracking_labels[@label] = 0
@actions = []
push: (action) ->
if @label and @actions.length == 0
undo_tracking_labels[@label]++
@actions.push action
child: (label) ->
undoer = new Sysdea.Undoer "#{@label} > #{label}"
@push undoer.run
undoer
record: (undo) ->
@push undo
run: =>
@actions.reverse()
action() for action in @actions
if @label and @actions.length > 0
undo_tracking_labels[@label]--
@actions = []
onoff: (target, args...) ->
target.on.apply(target, args)
@push -> target.off.apply(target, args)
addremove: (target, args...) ->
target.addEventListener.apply(target, args)
@push -> target.removeEventListener.apply(target, args)
recordRemove: (target) ->
@push -> target.remove()
target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment