summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/util/undostack.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-08-13 16:12:02 -0400
committerJules Laplace <jules@okfoc.us>2014-08-13 16:21:26 -0400
commit90cb5b343f3d56372f9b43faf215ed80dd879fe1 (patch)
treedfac1c586b713a70c9bd5ffce01f93a46a256e75 /public/assets/javascripts/rectangles/util/undostack.js
parent14228e82f4836b1bffa3389aa9e8d12959aa3810 (diff)
undo for scenery stuff
Diffstat (limited to 'public/assets/javascripts/rectangles/util/undostack.js')
-rw-r--r--public/assets/javascripts/rectangles/util/undostack.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/public/assets/javascripts/rectangles/util/undostack.js b/public/assets/javascripts/rectangles/util/undostack.js
new file mode 100644
index 0000000..b93c79e
--- /dev/null
+++ b/public/assets/javascripts/rectangles/util/undostack.js
@@ -0,0 +1,52 @@
+(function(){
+
+ var UndoStack = function(){
+ this.debug = true
+ this.stack = []
+ this.types = {}
+ this.pointer = -1
+ }
+ UndoStack.prototype.push = function(action){
+ this.pointer++
+ this.stack[this.pointer] = action
+ this.purge()
+ }
+ UndoStack.prototype.purge = function(){
+ if (this.stack.length-1 == this.pointer) return
+ this.stack.length = this.pointer+1
+ }
+ UndoStack.prototype.undo = function(){
+ if (this.pointer == -1) return false
+ var action = this.stack[this.pointer]
+ this.debug && console.log("undo", action.type)
+ this.types[ action.type ].undo(action.undo)
+ this.pointer--
+ return this.pointer > -1
+ }
+ UndoStack.prototype.redo = function(){
+ if (this.pointer == this.stack.length-1) return false
+ this.pointer++
+ var action = this.stack[this.pointer]
+ this.debug && console.log("redo", action.type)
+ this.types[ action.type ].redo(action.redo)
+ return this.pointer < this.stack.length-1
+ }
+ UndoStack.prototype.register = function(actionType){
+ if (actionType.length) {
+ actionType.forEach(this.registerOne.bind(this))
+ }
+ else {
+ this.registerOne(actionType)
+ }
+ }
+ UndoStack.prototype.registerOne = function(actionType){
+ this.types[ actionType.type ] = actionType
+ }
+ if ('window' in this) {
+ window.UndoStack = new UndoStack
+ }
+ else {
+ module.exports = new UndoStack
+ }
+
+})()