hedera-web/js/htk/assistant/index.js

162 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-06-06 08:53:59 +00:00
require('./style.scss');
2022-06-06 12:49:18 +00:00
var Component = require('vn/component');
2022-06-06 08:53:59 +00:00
var Step = require('../step');
var Toast = require('../toast');
/**
* A simple assistant with steps.
*/
module.exports = new Class({
2022-06-06 12:49:18 +00:00
Extends: Component,
Tag: 'htk-assistant',
Properties: {
/**
* The current step index.
*/
step: {
type: Number,
2022-11-16 01:46:44 +00:00
set(x) {
this.setStep(x);
},
2022-11-16 01:46:44 +00:00
get() {
return this._stepIndex;
2015-07-03 05:49:45 +00:00
}
},
/**
* The current step name.
*/
stepName: {
type: String,
2022-11-16 01:46:44 +00:00
set(x) {
this.setStepByName(x);
},
2022-11-16 01:46:44 +00:00
get() {
return this._stepName;
2015-07-03 05:49:45 +00:00
}
},
/**
* The steps index.
*/
stepsIndex: {
type: Array,
2022-11-16 01:46:44 +00:00
set(x) {
this._stepsIndex = x;
this.setStep(this._stepIndex);
2022-05-30 01:30:33 +00:00
this.emit('steps-change');
},
2022-11-16 01:46:44 +00:00
get() {
return this._stepsIndex;
2015-07-03 05:49:45 +00:00
}
},
/**
* The total number of steps in the assistant.
*/
stepCount: {
type: Number,
2022-11-16 01:46:44 +00:00
get() {
if (this._stepsIndex)
return this._stepsIndex.length;
return 0;
2015-07-03 05:49:45 +00:00
}
},
/**
* Function to call on every step change.
*/
stepFunc: {
type: Function,
2018-05-17 08:52:27 +00:00
value: null
},
/**
* Function to call on every step change.
*/
endFunc: {
type: Function,
value: null
2015-07-03 05:49:45 +00:00
},
},
_stepNode: null,
_stepIndex: 0,
_stepName: null,
steps: {},
2022-11-16 01:46:44 +00:00
initialize(props) {
2022-06-06 16:02:17 +00:00
Component.prototype.initialize.call(this, props);
2022-06-06 17:13:57 +00:00
this.createRoot('div');
},
2022-11-16 01:46:44 +00:00
appendChild(step) {
if (!(step instanceof Step))
throw new Error('Invalid child for assistant');
2015-07-03 05:49:45 +00:00
this.node.appendChild(step.node);
this.steps[step.name] = step;
},
2015-07-03 05:49:45 +00:00
2022-11-16 01:46:44 +00:00
setStepByName(stepName) {
if (this._stepsIndex)
this.setStep(this._stepsIndex.indexOf(stepName));
},
2022-11-16 01:46:44 +00:00
setStep(stepIndex) {
if ((this.currentStep && stepIndex == this._stepIndex)
|| !this._stepsIndex
|| !(stepIndex >= -1 && stepIndex < this._stepsIndex.length))
2015-07-03 05:49:45 +00:00
return;
if (stepIndex > this._stepIndex) {
var validateIni = Math.max(this._stepIndex, 0);
2015-07-03 05:49:45 +00:00
try {
for (var i = validateIni; i < stepIndex; i++) {
var validateName = this._stepsIndex[i];
var validateStep = this.steps[validateName];
validateStep.validate();
}
} catch(e) {
Toast.showError(e.message);
if (i == this._stepIndex) return;
stepIndex = i;
2015-07-03 05:49:45 +00:00
}
}
var stepName = this._stepsIndex[stepIndex];
var step = this.steps[stepName];
var currentStep = this.currentStep;
if (currentStep)
currentStep.hide();
2015-07-03 05:49:45 +00:00
this._stepIndex = stepIndex;
this._stepName = stepName;
this.currentStep = step;
step.show();
2022-05-30 01:30:33 +00:00
this.emit('step-change', stepIndex);
2018-05-17 08:52:27 +00:00
if (this.stepFunc)
this.stepFunc(step);
},
/**
* Moves the assistant to the previous step.
*/
2022-11-16 01:46:44 +00:00
movePrevious() {
this.setStep(this._stepIndex - 1);
},
2015-07-03 05:49:45 +00:00
/**
* Moves the assistant to the next step.
*/
2022-11-16 01:46:44 +00:00
moveNext() {
this.setStep(this._stepIndex + 1);
2018-05-17 08:52:27 +00:00
},
/**
* Ends the assistant. If defined, the stepFunc will be called.
*/
2022-11-16 01:46:44 +00:00
end() {
2018-05-17 08:52:27 +00:00
if (this.endFunc)
this.endFunc();
2015-07-03 05:49:45 +00:00
}
});