summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulie Lala <jules@okfoc.us>2014-08-12 09:15:30 -0400
committerJulie Lala <jules@okfoc.us>2014-08-12 09:15:30 -0400
commit38125881369cb87c35eb6a7b7e24f7c0130a32bb (patch)
tree60353113645483e0b8537605ec9e32df488c14bd
parent9cd88d59e45b530e483490804503e6b47030fd4d (diff)
register undo types
-rw-r--r--public/assets/javascripts/rectangles/engine/scenery/undo.js60
-rw-r--r--public/assets/javascripts/rectangles/util/undo.js15
-rw-r--r--test/09-test-undo.js8
3 files changed, 75 insertions, 8 deletions
diff --git a/public/assets/javascripts/rectangles/engine/scenery/undo.js b/public/assets/javascripts/rectangles/engine/scenery/undo.js
new file mode 100644
index 0000000..fb221f5
--- /dev/null
+++ b/public/assets/javascripts/rectangles/engine/scenery/undo.js
@@ -0,0 +1,60 @@
+(function(){
+ UndoStack.register([
+ {
+ type: "create-scenery",
+ undo: function(state){
+ },
+ redo: function(state){
+ },
+ },
+ {
+ type: "update-scenery",
+ undo: function(state){
+ },
+ redo: function(state){
+ },
+ },
+ {
+ type: "destroy-scenery",
+ undo: function(state){
+ },
+ redo: function(state){
+ },
+ },
+
+ //
+
+ {
+ type: "create-rectangle",
+ undo: function(state){
+ },
+ redo: function(state){
+ },
+ },
+ {
+ type: "update-rectangle",
+ undo: function(state){
+ },
+ redo: function(state){
+ },
+ },
+ {
+ type: "destroy-rectangle",
+ undo: function(state){
+ },
+ redo: function(state){
+ },
+ },
+
+ //
+
+ {
+ type: "update-wallpaper",
+ undo: function(state){
+ },
+ redo: function(state){
+ },
+ },
+
+ ])
+})() \ No newline at end of file
diff --git a/public/assets/javascripts/rectangles/util/undo.js b/public/assets/javascripts/rectangles/util/undo.js
index 3700817..5d8593c 100644
--- a/public/assets/javascripts/rectangles/util/undo.js
+++ b/public/assets/javascripts/rectangles/util/undo.js
@@ -17,7 +17,7 @@
UndoStack.prototype.undo = function(){
if (this.pointer == -1) return false
var action = this.stack[this.pointer]
- this.types[ action.type ].undo(action)
+ this.types[ action.type ].undo(action.prev)
this.pointer--
return this.pointer > -1
}
@@ -25,13 +25,20 @@
if (this.pointer == this.stack.length-1) return false
this.pointer++
var action = this.stack[this.pointer]
- this.types[ action.type ].redo(action)
+ this.types[ action.type ].redo(action.next)
return this.pointer < this.stack.length-1
}
UndoStack.prototype.register = function(actionType){
- this.types[ actionType.type ] = 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
}
diff --git a/test/09-test-undo.js b/test/09-test-undo.js
index 84b5d09..17998a9 100644
--- a/test/09-test-undo.js
+++ b/test/09-test-undo.js
@@ -9,11 +9,11 @@ describe('undo', function(){
UndoStack.register({
type: "demo",
- undo: function(action){
- state = action.prev
+ undo: function(myState){
+ state = myState
},
- redo: function(action){
- state = action.next
+ redo: function(myState){
+ state = myState
},
})