salix/client/core/src/components/step-control/step-control.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

import ngModule from '../../module';
import './style.scss';
export default class StepControl {
constructor($state) {
this.$state = $state;
}
set currentState(state) {
let isAllowed = true;
if (this.onStepChange)
isAllowed = this.onStepChange({state});
if (isAllowed)
this.$state.go(state);
}
get totalSteps() {
return this.steps.length;
}
get currentState() {
return this.$state.current.name;
}
get currentStepIndex() {
for (let i = 0; i < this.steps.length; i++) {
if (this.steps[i].state == this.$state.current.name)
return i;
}
}
onPreviousClick() {
let state = this.steps[this.currentStepIndex - 1].state;
this.currentState = state;
}
onNextClick() {
let state = this.steps[this.currentStepIndex + 1].state;
this.currentState = state;
}
canMovePrevious() {
return this.steps[0].state != this.currentState;
}
canFinalize() {
let lastStep = this.steps[this.totalSteps - 1];
return lastStep.state == this.currentState;
}
canMoveNext() {
let lastStep = this.steps[this.totalSteps - 1];
return lastStep.state != this.currentState;
}
}
StepControl.$inject = ['$state'];
ngModule.component('vnStepControl', {
template: require('./step-control.html'),
controller: StepControl,
bindings: {
steps: '<',
onStepChange: '&?',
onStepEnd: '&?'
}
});