2018-04-18 07:47:05 +00:00
|
|
|
import ngModule from '../../module';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
export default class StepControl {
|
|
|
|
constructor($state) {
|
|
|
|
this.$state = $state;
|
|
|
|
}
|
|
|
|
|
2018-05-07 06:33:45 +00:00
|
|
|
set steps(steps) {
|
|
|
|
if (!steps) return;
|
|
|
|
|
|
|
|
this._steps = steps;
|
|
|
|
|
|
|
|
if (this.currentStepIndex > 0)
|
|
|
|
this.$state.go(this._steps[0].state);
|
|
|
|
}
|
|
|
|
|
2018-04-18 07:47:05 +00:00
|
|
|
set currentState(state) {
|
2018-05-16 06:13:39 +00:00
|
|
|
if (!this.onStepChange)
|
|
|
|
return this.$state.go(state);
|
2018-04-18 07:47:05 +00:00
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
let change = this.onStepChange({state});
|
2018-04-18 07:47:05 +00:00
|
|
|
|
2018-05-16 06:13:39 +00:00
|
|
|
if (typeof change === 'object' && change.then) {
|
|
|
|
return change.then(isAllowed => {
|
|
|
|
if (isAllowed)
|
|
|
|
this.$state.go(state);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change)
|
2018-04-18 07:47:05 +00:00
|
|
|
this.$state.go(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
get totalSteps() {
|
2018-05-07 06:33:45 +00:00
|
|
|
return this._steps.length;
|
2018-04-18 07:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get currentState() {
|
|
|
|
return this.$state.current.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentStepIndex() {
|
2018-05-07 06:33:45 +00:00
|
|
|
for (let i = 0; i < this._steps.length; i++) {
|
|
|
|
if (this._steps[i].state == this.$state.current.name)
|
2018-04-18 07:47:05 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onPreviousClick() {
|
2018-05-07 06:33:45 +00:00
|
|
|
let state = this._steps[this.currentStepIndex - 1].state;
|
2018-04-18 07:47:05 +00:00
|
|
|
|
|
|
|
this.currentState = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
onNextClick() {
|
2018-05-07 06:33:45 +00:00
|
|
|
let state = this._steps[this.currentStepIndex + 1].state;
|
2018-04-18 07:47:05 +00:00
|
|
|
|
|
|
|
this.currentState = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
canMovePrevious() {
|
2018-05-07 06:33:45 +00:00
|
|
|
return this.currentStepIndex > 0;
|
2018-04-18 07:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canFinalize() {
|
2018-05-07 06:33:45 +00:00
|
|
|
return this.currentStepIndex === this.totalSteps - 1;
|
2018-04-18 07:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canMoveNext() {
|
2018-05-07 06:33:45 +00:00
|
|
|
return this.currentStepIndex < this.totalSteps - 1;
|
2018-04-18 07:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StepControl.$inject = ['$state'];
|
|
|
|
|
|
|
|
ngModule.component('vnStepControl', {
|
|
|
|
template: require('./step-control.html'),
|
|
|
|
controller: StepControl,
|
|
|
|
bindings: {
|
|
|
|
steps: '<',
|
|
|
|
onStepChange: '&?',
|
|
|
|
onStepEnd: '&?'
|
|
|
|
}
|
|
|
|
});
|