Skip to content

Commit

Permalink
Expose whether each step has a next/previous step
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlafroscia committed Apr 1, 2018
1 parent 21e6519 commit bed1b48
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 21 deletions.
14 changes: 7 additions & 7 deletions addon/-private/state-machine/-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ export default EmberObject.extend({
set(this, 'firstStep', name);
}

// Set the last step, if it hasn't been yet
if (!get(this, 'lastStep')) {
set(this, 'lastStep', name);
// Set the last step to transition to the new one, event if the last step
// is this one
const lastStep = get(this, 'lastStep');
if (lastStep) {
set(this, `stepTransitions.${lastStep}`, name);
}

// Add to the transition map based on the super-class
Expand All @@ -79,17 +81,15 @@ export default EmberObject.extend({
assert('must be implemented by parent class');
},

pickNext() {
pickNext(currentStep = get(this, 'currentStep')) {
const transitions = get(this, 'stepTransitions');
const currentStep = get(this, 'currentStep');

return transitions[currentStep];
},

pickPrevious() {
pickPrevious(currentStep = get(this, 'currentStep')) {
let previous;
const transitions = get(this, 'stepTransitions');
const currentStep = get(this, 'currentStep');

for (const k in transitions) {
if (transitions[k] === currentStep) {
Expand Down
5 changes: 0 additions & 5 deletions addon/-private/state-machine/circular.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ import { set, get } from '@ember/object';
*/
export default BaseStateMachine.extend({
_addStepToTransitions(name) {
// Set the last step to transition to the new one, event if the last step
// is this one
const lastStep = get(this, 'lastStep');
set(this, `stepTransitions.${lastStep}`, name);

// Set the new step to transition to the first one
const firstStep = get(this, 'firstStep');
set(this, `stepTransitions.${name}`, firstStep);
Expand Down
8 changes: 1 addition & 7 deletions addon/-private/state-machine/linear.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BaseStateMachine from './-base';
import { set, get } from '@ember/object';
import { set } from '@ember/object';

/**
* Keeps track of the order of the steps in the step manager, as well as
Expand All @@ -12,12 +12,6 @@ import { set, get } from '@ember/object';
*/
export default BaseStateMachine.extend({
_addStepToTransitions(name) {
// Set the last step to transition to the new one, event if the last step
// is this one
const lastStep = get(this, 'lastStep');
set(this, `stepTransitions.${lastStep}`, name);

// Set the new step to transition to the first one
set(this, `stepTransitions.${name}`, null);
}
});
5 changes: 4 additions & 1 deletion addon/components/step-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,12 @@ export default Component.extend({
*/
'register-step-component'(stepComponent) {
const name = get(stepComponent, 'name');
const transitions = get(this, 'transitions');

stepComponent.set('transitions', transitions);

schedule('actions', () => {
get(this, 'transitions').addStep(name);
transitions.addStep(name);
});
},

Expand Down
21 changes: 20 additions & 1 deletion addon/components/step-manager/step.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import Component from '@ember/component';
import { computed, get, observer } from '@ember/object';
import { isPresent } from '@ember/utils';
import { assert } from '@ember/debug';
import hbs from 'htmlbars-inline-precompile';

export default Component.extend({
tagName: '',
layout: hbs`
{{#if isActive}}
{{yield}}
{{yield (hash
hasNext=hasNext
hasPrevious=hasPrevious
)
}}
{{else if (hasBlock 'inverse')}}
{{yield to='inverse'}}
{{/if}}
Expand Down Expand Up @@ -35,12 +40,26 @@ export default Component.extend({
*/
name: null,

transitions: null,

/**
* Whether this state is currently the active one
* @property {boolean} isActive
* @private
*/
isActive: computed('currentStep', 'name', function() {
return get(this, 'currentStep') === get(this, 'name');
}),

hasNext: computed('transitions.length', function() {
const name = get(this, 'name');

return isPresent(get(this, 'transitions').pickNext(name));
}),

hasPrevious: computed('transitions.length', function() {
const name = get(this, 'name');

return isPresent(get(this, 'transitions').pickPrevious(name));
})
});
54 changes: 54 additions & 0 deletions tests/integration/step-manager/step-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,58 @@ module('step-manger/step', function(hooks) {
assert.dom(hook('step')).doesNotExist();
});
});

module('yielding whether the step has a next step', function() {
test('when it has a next step', async function(assert) {
await render(hbs`
{{#step-manager as |w|}}
{{#w.step as |step|}}
<p>{{step.hasNext}}</p>
{{/w.step}}
{{w.step}}
{{/step-manager}}
`);

assert.dom('p').hasText('true');
});

test('when it does not have a next step', async function(assert) {
await render(hbs`
{{#step-manager as |w|}}
{{#w.step as |step|}}
<p>{{step.hasNext}}</p>
{{/w.step}}
{{/step-manager}}
`);

assert.dom('p').hasText('false');
});
});

module('yielding whether the step has a previous step', function() {
test('when it has a previous step', async function(assert) {
await render(hbs`
{{#step-manager currentStep='bar' as |w|}}
{{w.step name='foo'}}
{{#w.step name='bar' as |step|}}
<p>{{step.hasPrevious}}</p>
{{/w.step}}
{{/step-manager}}
`);

assert.dom('p').hasText('true');
});

test('when it does not have a previous step', async function(assert) {
await render(hbs`
{{#step-manager as |w|}}
{{#w.step as |step|}}
<p>{{step.hasPrevious}}</p>
{{/w.step}}
{{/step-manager}}
`);

assert.dom('p').hasText('false');
});
});
});
16 changes: 16 additions & 0 deletions tests/unit/-private/state-machine/linear-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ module('-private/state-machine/linear', function() {

assert.equal(m.pickNext(), null);
});

test('can get the next step from a specific step', function(assert) {
const m = new StateMachine();
m.addStep('foo');
m.addStep('bar');

assert.equal(m.pickNext('bar'), null);
});
});

module('#pickPrevious', function() {
Expand All @@ -60,6 +68,14 @@ module('-private/state-machine/linear', function() {

assert.equal(m.pickPrevious(), null);
});

test('can get the previous step from a specific step', function(assert) {
const m = new StateMachine();
m.addStep('foo');
m.addStep('bar');

assert.equal(m.pickPrevious('bar'), 'foo');
});
});

module('#activate', function(hooks) {
Expand Down

0 comments on commit bed1b48

Please sign in to comment.