Undo button functionality Issue #40

This commit is contained in:
Peter Cottle 2013-02-20 09:22:15 -08:00
parent e94710ca64
commit c5a3f866ab
9 changed files with 144 additions and 12 deletions

View file

@ -4498,6 +4498,7 @@ var Backbone = (!require('../util').isBrowser()) ? require('backbone') : window.
var util = require('../util'); var util = require('../util');
var Main = require('../app'); var Main = require('../app');
var Errors = require('../util/errors');
var Visualization = require('../visuals/visualization').Visualization; var Visualization = require('../visuals/visualization').Visualization;
var ParseWaterfall = require('../level/parseWaterfall').ParseWaterfall; var ParseWaterfall = require('../level/parseWaterfall').ParseWaterfall;
@ -4523,6 +4524,7 @@ var Sandbox = Backbone.View.extend({
this.initCommandCollection(options); this.initCommandCollection(options);
this.initParseWaterfall(options); this.initParseWaterfall(options);
this.initGitShim(options); this.initGitShim(options);
this.initUndoStack(options);
if (!options.wait) { if (!options.wait) {
this.takeControl(); this.takeControl();
@ -4541,6 +4543,10 @@ var Sandbox = Backbone.View.extend({
}); });
}, },
initUndoStack: function(options) {
this.undoStack = [];
},
initCommandCollection: function(options) { initCommandCollection: function(options) {
// don't add it to just any collection -- adding to the // don't add it to just any collection -- adding to the
// CommandUI collection will put in history // CommandUI collection will put in history
@ -4552,6 +4558,9 @@ var Sandbox = Backbone.View.extend({
}, },
initGitShim: function(options) { initGitShim: function(options) {
this.gitShim = new GitShim({
beforeCB: _.bind(this.beforeCommandCB, this)
});
}, },
takeControl: function() { takeControl: function() {
@ -4595,6 +4604,31 @@ var Sandbox = Backbone.View.extend({
} }
}, },
beforeCommandCB: function(command) {
this.pushUndo();
},
pushUndo: function() {
// go ahead and push the three onto the stack
this.undoStack.push(this.mainVis.gitEngine.printTree());
},
undo: function(command, deferred) {
var toRestore = this.undoStack.pop();
if (!toRestore) {
command.set('error', new Errors.GitError({
msg: 'The undo stack is empty!'
}));
deferred.resolve();
return;
}
this.mainVis.reset(toRestore);
setTimeout(function() {
command.finishWith(deferred);
}, this.mainVis.getAnimationTime());
},
commandSubmitted: function(value) { commandSubmitted: function(value) {
// allow other things to see this command (aka command history on terminal) // allow other things to see this command (aka command history on terminal)
Main.getEvents().trigger('commandSubmittedPassive', value); Main.getEvents().trigger('commandSubmittedPassive', value);
@ -4689,6 +4723,7 @@ var Sandbox = Backbone.View.extend({
// some exceptions to the rule // some exceptions to the rule
var commandMap = { var commandMap = {
'reset solved': this.resetSolved, 'reset solved': this.resetSolved,
'undo': this.undo,
'help general': this.helpDialog, 'help general': this.helpDialog,
'help': this.helpDialog, 'help': this.helpDialog,
'reset': this.reset, 'reset': this.reset,
@ -4842,6 +4877,7 @@ var Sandbox = Backbone.View.extend({
reset: function(command, deferred) { reset: function(command, deferred) {
this.mainVis.reset(); this.mainVis.reset();
this.initUndoStack();
setTimeout(function() { setTimeout(function() {
command.finishWith(deferred); command.finishWith(deferred);
@ -6917,6 +6953,7 @@ var Level = Sandbox.extend({
initGitShim: function(options) { initGitShim: function(options) {
// ok we definitely want a shim here // ok we definitely want a shim here
this.gitShim = new GitShim({ this.gitShim = new GitShim({
beforeCB: _.bind(this.beforeCommandCB, this),
afterCB: _.bind(this.afterCommandCB, this), afterCB: _.bind(this.afterCommandCB, this),
afterDeferHandler: _.bind(this.afterCommandDefer, this) afterDeferHandler: _.bind(this.afterCommandDefer, this)
}); });
@ -6943,6 +6980,11 @@ var Level = Sandbox.extend({
return myRegexMap; return myRegexMap;
}, },
undo: function() {
this.gitCommandsIssued.pop();
Level.__super__.undo.apply(this, arguments);
},
afterCommandCB: function(command) { afterCommandCB: function(command) {
var matched = false; var matched = false;
_.each(this.commandsThatCount, function(regex) { _.each(this.commandsThatCount, function(regex) {
@ -7345,10 +7387,11 @@ var Visualization = Backbone.View.extend({
this.treeString = treeString; this.treeString = treeString;
}, },
reset: function() { reset: function(tree) {
var treeString = tree || this.treeString;
this.setTreeOpacity(0); this.setTreeOpacity(0);
if (this.treeString) { if (this.treeString) {
this.gitEngine.loadTreeFromString(this.treeString); this.gitEngine.loadTreeFromString(treeString);
} else { } else {
this.gitEngine.defaultInit(); this.gitEngine.defaultInit();
} }
@ -13424,7 +13467,8 @@ var regexMap = {
'build level': /^build level($|\s)/, 'build level': /^build level($|\s)/,
'export tree': /^export tree$/, 'export tree': /^export tree$/,
'import tree': /^import tree$/, 'import tree': /^import tree$/,
'import level': /^import level$/ 'import level': /^import level$/,
'undo': /^undo($|\s)/
}; };
exports.instantCommands = instantCommands; exports.instantCommands = instantCommands;
@ -22016,6 +22060,7 @@ var Level = Sandbox.extend({
initGitShim: function(options) { initGitShim: function(options) {
// ok we definitely want a shim here // ok we definitely want a shim here
this.gitShim = new GitShim({ this.gitShim = new GitShim({
beforeCB: _.bind(this.beforeCommandCB, this),
afterCB: _.bind(this.afterCommandCB, this), afterCB: _.bind(this.afterCommandCB, this),
afterDeferHandler: _.bind(this.afterCommandDefer, this) afterDeferHandler: _.bind(this.afterCommandDefer, this)
}); });
@ -22042,6 +22087,11 @@ var Level = Sandbox.extend({
return myRegexMap; return myRegexMap;
}, },
undo: function() {
this.gitCommandsIssued.pop();
Level.__super__.undo.apply(this, arguments);
},
afterCommandCB: function(command) { afterCommandCB: function(command) {
var matched = false; var matched = false;
_.each(this.commandsThatCount, function(regex) { _.each(this.commandsThatCount, function(regex) {
@ -22379,6 +22429,7 @@ var Backbone = (!require('../util').isBrowser()) ? require('backbone') : window.
var util = require('../util'); var util = require('../util');
var Main = require('../app'); var Main = require('../app');
var Errors = require('../util/errors');
var Visualization = require('../visuals/visualization').Visualization; var Visualization = require('../visuals/visualization').Visualization;
var ParseWaterfall = require('../level/parseWaterfall').ParseWaterfall; var ParseWaterfall = require('../level/parseWaterfall').ParseWaterfall;
@ -22404,6 +22455,7 @@ var Sandbox = Backbone.View.extend({
this.initCommandCollection(options); this.initCommandCollection(options);
this.initParseWaterfall(options); this.initParseWaterfall(options);
this.initGitShim(options); this.initGitShim(options);
this.initUndoStack(options);
if (!options.wait) { if (!options.wait) {
this.takeControl(); this.takeControl();
@ -22422,6 +22474,10 @@ var Sandbox = Backbone.View.extend({
}); });
}, },
initUndoStack: function(options) {
this.undoStack = [];
},
initCommandCollection: function(options) { initCommandCollection: function(options) {
// don't add it to just any collection -- adding to the // don't add it to just any collection -- adding to the
// CommandUI collection will put in history // CommandUI collection will put in history
@ -22433,6 +22489,9 @@ var Sandbox = Backbone.View.extend({
}, },
initGitShim: function(options) { initGitShim: function(options) {
this.gitShim = new GitShim({
beforeCB: _.bind(this.beforeCommandCB, this)
});
}, },
takeControl: function() { takeControl: function() {
@ -22476,6 +22535,31 @@ var Sandbox = Backbone.View.extend({
} }
}, },
beforeCommandCB: function(command) {
this.pushUndo();
},
pushUndo: function() {
// go ahead and push the three onto the stack
this.undoStack.push(this.mainVis.gitEngine.printTree());
},
undo: function(command, deferred) {
var toRestore = this.undoStack.pop();
if (!toRestore) {
command.set('error', new Errors.GitError({
msg: 'The undo stack is empty!'
}));
deferred.resolve();
return;
}
this.mainVis.reset(toRestore);
setTimeout(function() {
command.finishWith(deferred);
}, this.mainVis.getAnimationTime());
},
commandSubmitted: function(value) { commandSubmitted: function(value) {
// allow other things to see this command (aka command history on terminal) // allow other things to see this command (aka command history on terminal)
Main.getEvents().trigger('commandSubmittedPassive', value); Main.getEvents().trigger('commandSubmittedPassive', value);
@ -22570,6 +22654,7 @@ var Sandbox = Backbone.View.extend({
// some exceptions to the rule // some exceptions to the rule
var commandMap = { var commandMap = {
'reset solved': this.resetSolved, 'reset solved': this.resetSolved,
'undo': this.undo,
'help general': this.helpDialog, 'help general': this.helpDialog,
'help': this.helpDialog, 'help': this.helpDialog,
'reset': this.reset, 'reset': this.reset,
@ -22723,6 +22808,7 @@ var Sandbox = Backbone.View.extend({
reset: function(command, deferred) { reset: function(command, deferred) {
this.mainVis.reset(); this.mainVis.reset();
this.initUndoStack();
setTimeout(function() { setTimeout(function() {
command.finishWith(deferred); command.finishWith(deferred);
@ -22806,7 +22892,8 @@ var regexMap = {
'build level': /^build level($|\s)/, 'build level': /^build level($|\s)/,
'export tree': /^export tree$/, 'export tree': /^export tree$/,
'import tree': /^import tree$/, 'import tree': /^import tree$/,
'import level': /^import level$/ 'import level': /^import level$/,
'undo': /^undo($|\s)/
}; };
exports.instantCommands = instantCommands; exports.instantCommands = instantCommands;
@ -28471,10 +28558,11 @@ var Visualization = Backbone.View.extend({
this.treeString = treeString; this.treeString = treeString;
}, },
reset: function() { reset: function(tree) {
var treeString = tree || this.treeString;
this.setTreeOpacity(0); this.setTreeOpacity(0);
if (this.treeString) { if (this.treeString) {
this.gitEngine.loadTreeFromString(this.treeString); this.gitEngine.loadTreeFromString(treeString);
} else { } else {
this.gitEngine.defaultInit(); this.gitEngine.defaultInit();
} }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
build/bundle.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -409,7 +409,7 @@
For a much easier time perusing the source, see the individual files at: For a much easier time perusing the source, see the individual files at:
https://github.com/pcottle/learnGitBranching https://github.com/pcottle/learnGitBranching
--> -->
<script src="build/bundle.min.20261aa4.js"></script> <script src="build/bundle.min.6040aa35.js"></script>
<!-- The advantage of github pages: super-easy, simple, slick static hostic. <!-- The advantage of github pages: super-easy, simple, slick static hostic.
The downside? No raw logs to parse for analytics, so I have to include The downside? No raw logs to parse for analytics, so I have to include

View file

@ -257,6 +257,7 @@ var Level = Sandbox.extend({
initGitShim: function(options) { initGitShim: function(options) {
// ok we definitely want a shim here // ok we definitely want a shim here
this.gitShim = new GitShim({ this.gitShim = new GitShim({
beforeCB: _.bind(this.beforeCommandCB, this),
afterCB: _.bind(this.afterCommandCB, this), afterCB: _.bind(this.afterCommandCB, this),
afterDeferHandler: _.bind(this.afterCommandDefer, this) afterDeferHandler: _.bind(this.afterCommandDefer, this)
}); });
@ -283,6 +284,11 @@ var Level = Sandbox.extend({
return myRegexMap; return myRegexMap;
}, },
undo: function() {
this.gitCommandsIssued.pop();
Level.__super__.undo.apply(this, arguments);
},
afterCommandCB: function(command) { afterCommandCB: function(command) {
var matched = false; var matched = false;
_.each(this.commandsThatCount, function(regex) { _.each(this.commandsThatCount, function(regex) {

View file

@ -5,6 +5,7 @@ var Backbone = (!require('../util').isBrowser()) ? require('backbone') : window.
var util = require('../util'); var util = require('../util');
var Main = require('../app'); var Main = require('../app');
var Errors = require('../util/errors');
var Visualization = require('../visuals/visualization').Visualization; var Visualization = require('../visuals/visualization').Visualization;
var ParseWaterfall = require('../level/parseWaterfall').ParseWaterfall; var ParseWaterfall = require('../level/parseWaterfall').ParseWaterfall;
@ -30,6 +31,7 @@ var Sandbox = Backbone.View.extend({
this.initCommandCollection(options); this.initCommandCollection(options);
this.initParseWaterfall(options); this.initParseWaterfall(options);
this.initGitShim(options); this.initGitShim(options);
this.initUndoStack(options);
if (!options.wait) { if (!options.wait) {
this.takeControl(); this.takeControl();
@ -48,6 +50,10 @@ var Sandbox = Backbone.View.extend({
}); });
}, },
initUndoStack: function(options) {
this.undoStack = [];
},
initCommandCollection: function(options) { initCommandCollection: function(options) {
// don't add it to just any collection -- adding to the // don't add it to just any collection -- adding to the
// CommandUI collection will put in history // CommandUI collection will put in history
@ -59,6 +65,9 @@ var Sandbox = Backbone.View.extend({
}, },
initGitShim: function(options) { initGitShim: function(options) {
this.gitShim = new GitShim({
beforeCB: _.bind(this.beforeCommandCB, this)
});
}, },
takeControl: function() { takeControl: function() {
@ -102,6 +111,31 @@ var Sandbox = Backbone.View.extend({
} }
}, },
beforeCommandCB: function(command) {
this.pushUndo();
},
pushUndo: function() {
// go ahead and push the three onto the stack
this.undoStack.push(this.mainVis.gitEngine.printTree());
},
undo: function(command, deferred) {
var toRestore = this.undoStack.pop();
if (!toRestore) {
command.set('error', new Errors.GitError({
msg: 'The undo stack is empty!'
}));
deferred.resolve();
return;
}
this.mainVis.reset(toRestore);
setTimeout(function() {
command.finishWith(deferred);
}, this.mainVis.getAnimationTime());
},
commandSubmitted: function(value) { commandSubmitted: function(value) {
// allow other things to see this command (aka command history on terminal) // allow other things to see this command (aka command history on terminal)
Main.getEvents().trigger('commandSubmittedPassive', value); Main.getEvents().trigger('commandSubmittedPassive', value);
@ -196,6 +230,7 @@ var Sandbox = Backbone.View.extend({
// some exceptions to the rule // some exceptions to the rule
var commandMap = { var commandMap = {
'reset solved': this.resetSolved, 'reset solved': this.resetSolved,
'undo': this.undo,
'help general': this.helpDialog, 'help general': this.helpDialog,
'help': this.helpDialog, 'help': this.helpDialog,
'reset': this.reset, 'reset': this.reset,
@ -349,6 +384,7 @@ var Sandbox = Backbone.View.extend({
reset: function(command, deferred) { reset: function(command, deferred) {
this.mainVis.reset(); this.mainVis.reset();
this.initUndoStack();
setTimeout(function() { setTimeout(function() {
command.finishWith(deferred); command.finishWith(deferred);

View file

@ -57,7 +57,8 @@ var regexMap = {
'build level': /^build level($|\s)/, 'build level': /^build level($|\s)/,
'export tree': /^export tree$/, 'export tree': /^export tree$/,
'import tree': /^import tree$/, 'import tree': /^import tree$/,
'import level': /^import level$/ 'import level': /^import level$/,
'undo': /^undo($|\s)/
}; };
exports.instantCommands = instantCommands; exports.instantCommands = instantCommands;

View file

@ -131,10 +131,11 @@ var Visualization = Backbone.View.extend({
this.treeString = treeString; this.treeString = treeString;
}, },
reset: function() { reset: function(tree) {
var treeString = tree || this.treeString;
this.setTreeOpacity(0); this.setTreeOpacity(0);
if (this.treeString) { if (this.treeString) {
this.gitEngine.loadTreeFromString(this.treeString); this.gitEngine.loadTreeFromString(treeString);
} else { } else {
this.gitEngine.defaultInit(); this.gitEngine.defaultInit();
} }