diff --git a/lib/command/CommandStack.js b/lib/command/CommandStack.js index 6243be945..253d0f0bf 100644 --- a/lib/command/CommandStack.js +++ b/lib/command/CommandStack.js @@ -107,10 +107,14 @@ export default function CommandStack(eventBus, injector) { * Current active commandStack execution * * @type {Object} + * @property {Object[]} actions + * @property {Object[]} dirty + * @property { 'undo' | 'redo' | 'clear' | 'execute' | null } trigger the cause of the current excecution */ this._currentExecution = { actions: [], - dirty: [] + dirty: [], + trigger: null }; @@ -141,6 +145,8 @@ CommandStack.prototype.execute = function(command, context) { throw new Error('command required'); } + this._currentExecution.trigger = 'execute'; + var action = { command: command, context: context }; this._pushAction(action); @@ -201,7 +207,7 @@ CommandStack.prototype.clear = function(emit) { this._stackIdx = -1; if (emit !== false) { - this._fire('changed'); + this._fire('changed', { trigger: 'clear' }); } }; @@ -214,6 +220,8 @@ CommandStack.prototype.undo = function() { next; if (action) { + this._currentExecution.trigger = 'undo'; + this._pushAction(action); while (action) { @@ -240,6 +248,8 @@ CommandStack.prototype.redo = function() { next; if (action) { + this._currentExecution.trigger = 'redo'; + this._pushAction(action); while (action) { @@ -446,6 +456,7 @@ CommandStack.prototype._pushAction = function(action) { CommandStack.prototype._popAction = function() { var execution = this._currentExecution, + trigger = execution.trigger, actions = execution.actions, dirty = execution.dirty; @@ -456,7 +467,9 @@ CommandStack.prototype._popAction = function() { dirty.length = 0; - this._fire('changed'); + this._fire('changed', { trigger: trigger }); + + execution.trigger = null; } }; diff --git a/test/spec/command/CommandStackSpec.js b/test/spec/command/CommandStackSpec.js index 00654f972..e883daaf5 100755 --- a/test/spec/command/CommandStackSpec.js +++ b/test/spec/command/CommandStackSpec.js @@ -876,4 +876,87 @@ describe('command/CommandStack', function() { }); + + describe('trigger', function() { + + beforeEach(inject(function(commandStack) { + commandStack.registerHandler('complex-command', ComplexCommand); + commandStack.registerHandler('pre-command', PreCommand); + commandStack.registerHandler('post-command', PostCommand); + })); + + + it('should indicate ', inject(function(eventBus, commandStack) { + + // given + var changedSpy = sinon.spy(function(event) { + expect(event.trigger).to.eql('execute'); + }); + + // when + eventBus.on('commandStack.changed', changedSpy); + + commandStack.execute('complex-command', { element: { trace: [] } }); + + // then + expect(changedSpy).to.have.been.calledOnce; + })); + + + it('with trigger ', inject(function(eventBus, commandStack) { + + // given + var changedSpy = sinon.spy(function(event) { + expect(event.trigger).to.eql('undo'); + }); + + commandStack.execute('complex-command', { element: { trace: [] } }); + + // when + eventBus.on('commandStack.changed', changedSpy); + + commandStack.undo(); + + // then + expect(changedSpy).to.have.been.calledOnce; + })); + + + it('with trigger ', inject(function(eventBus, commandStack) { + + // given + var changedSpy = sinon.spy(function(event) { + expect(event.trigger).to.eql('redo'); + }); + + commandStack.execute('complex-command', { element: { trace: [] } }); + commandStack.undo(); + + // when + eventBus.on('commandStack.changed', changedSpy); + commandStack.redo(); + + // then + expect(changedSpy).to.have.been.calledOnce; + })); + + + it('with trigger ', inject(function(eventBus, commandStack) { + + // given + var changedSpy = sinon.spy(function(event) { + expect(event.trigger).to.eql('clear'); + }); + + // when + eventBus.on('commandStack.changed', changedSpy); + + commandStack.clear(); + + // then + expect(changedSpy).to.have.been.calledOnce; + })); + + }); + });