Created
April 2, 2012 11:31
-
-
Save c-spencer/2282819 to your computer and use it in GitHub Desktop.
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
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