summaryrefslogtreecommitdiff
path: root/public/assets/javascripts/rectangles/util/undo.js
diff options
context:
space:
mode:
authorJulie Lala <jules@okfoc.us>2014-08-11 00:10:24 -0400
committerJulie Lala <jules@okfoc.us>2014-08-11 00:10:24 -0400
commit02bde51c24ae1c6e189d031b80226e6a9f7cbc59 (patch)
treea64f5a040787b0b513c20ab8582aea171184736e /public/assets/javascripts/rectangles/util/undo.js
parent04c2c21ad2ed872137b7a7e633d6bebcd7b04b61 (diff)
parent9cd88d59e45b530e483490804503e6b47030fd4d (diff)
merge master
Diffstat (limited to 'public/assets/javascripts/rectangles/util/undo.js')
-rw-r--r--public/assets/javascripts/rectangles/util/undo.js42
1 files changed, 42 insertions, 0 deletions
diff --git a/public/assets/javascripts/rectangles/util/undo.js b/public/assets/javascripts/rectangles/util/undo.js
new file mode 100644
index 0000000..3700817
--- /dev/null
+++ b/public/assets/javascripts/rectangles/util/undo.js
@@ -0,0 +1,42 @@
+(function(){
+
+ var UndoStack = function(){
+ 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.types[ action.type ].undo(action)
+ 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.types[ action.type ].redo(action)
+ return this.pointer < this.stack.length-1
+ }
+ UndoStack.prototype.register = function(actionType){
+ this.types[ actionType.type ] = actionType
+ }
+
+ if ('window' in this) {
+ window.UndoStack = new UndoStack
+ }
+ else {
+ module.exports = new UndoStack
+ }
+
+})()