2017-04-05 21:14:08 +00:00
|
|
|
import { Component, Output, Input, EventEmitter } from 'ng-metadata/core';
|
2017-02-21 23:59:26 +00:00
|
|
|
import { LinearWorkflowSectionComponent } from './linear-workflow-section.component';
|
2017-02-20 02:35:46 +00:00
|
|
|
|
|
|
|
|
2017-02-21 23:59:26 +00:00
|
|
|
/**
|
|
|
|
* A component that which displays a linear workflow of sections, each completed in order before the next
|
|
|
|
* step is made visible.
|
|
|
|
*/
|
2017-02-20 02:35:46 +00:00
|
|
|
@Component({
|
2017-04-05 21:14:08 +00:00
|
|
|
selector: 'linear-workflow',
|
2017-02-21 23:59:26 +00:00
|
|
|
templateUrl: '/static/js/directives/ui/linear-workflow/linear-workflow.component.html',
|
2017-04-05 21:14:08 +00:00
|
|
|
legacy: {
|
|
|
|
transclude: true
|
|
|
|
}
|
2017-02-20 02:35:46 +00:00
|
|
|
})
|
2017-04-05 21:14:08 +00:00
|
|
|
export class LinearWorkflowComponent {
|
2017-02-20 02:35:46 +00:00
|
|
|
|
2017-02-21 23:59:26 +00:00
|
|
|
@Input('@') public doneTitle: string;
|
2017-04-05 21:14:08 +00:00
|
|
|
@Output() public onWorkflowComplete: EventEmitter<any> = new EventEmitter();
|
2017-02-21 23:59:26 +00:00
|
|
|
private sections: SectionInfo[] = [];
|
|
|
|
private currentSection: SectionInfo;
|
|
|
|
|
2017-02-22 00:21:54 +00:00
|
|
|
public addSection(component: LinearWorkflowSectionComponent): void {
|
2017-02-21 23:59:26 +00:00
|
|
|
this.sections.push({
|
|
|
|
index: this.sections.length,
|
2017-02-22 00:21:54 +00:00
|
|
|
component: component,
|
2017-02-21 23:59:26 +00:00
|
|
|
});
|
2017-02-22 00:21:54 +00:00
|
|
|
|
2017-05-22 20:59:12 +00:00
|
|
|
if (this.sections.length > 0 && !this.currentSection) {
|
|
|
|
this.setNextSection(0);
|
2017-02-22 00:21:54 +00:00
|
|
|
}
|
2017-02-21 23:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public onNextSection(): void {
|
2017-02-22 23:44:20 +00:00
|
|
|
if (this.currentSection.component.sectionValid && this.currentSection.index + 1 >= this.sections.length) {
|
2017-04-05 21:14:08 +00:00
|
|
|
this.onWorkflowComplete.emit({});
|
2017-02-21 23:59:26 +00:00
|
|
|
}
|
2017-02-22 23:44:20 +00:00
|
|
|
else if (this.currentSection.component.sectionValid && this.currentSection.index + 1 < this.sections.length) {
|
|
|
|
this.currentSection.component.isCurrentSection = false;
|
2017-05-22 20:59:12 +00:00
|
|
|
this.setNextSection(this.currentSection.index + 1);
|
2017-02-22 23:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public onSectionInvalid(sectionId: string): void {
|
|
|
|
var invalidSection = this.sections.filter(section => section.component.sectionId == sectionId)[0];
|
2017-02-23 00:33:34 +00:00
|
|
|
if (invalidSection.index <= this.currentSection.index) {
|
|
|
|
invalidSection.component.isCurrentSection = true;
|
|
|
|
this.currentSection = invalidSection;
|
2017-05-22 20:59:12 +00:00
|
|
|
|
2017-02-23 00:33:34 +00:00
|
|
|
this.sections.forEach((section) => {
|
|
|
|
if (section.index > invalidSection.index) {
|
|
|
|
section.component.sectionVisible = false;
|
|
|
|
section.component.isCurrentSection = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-02-21 23:59:26 +00:00
|
|
|
}
|
2017-05-22 20:59:12 +00:00
|
|
|
|
|
|
|
private setNextSection(startingIndex: number = 0): void {
|
|
|
|
// Find the next section that is not set to be skipped
|
|
|
|
this.currentSection = this.sections.slice(startingIndex)
|
|
|
|
.filter(section => !section.component.skipSection)[0];
|
|
|
|
|
|
|
|
if (this.currentSection) {
|
|
|
|
this.currentSection.component.sectionVisible = true;
|
|
|
|
this.currentSection.component.isCurrentSection = true;
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 23:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A type representing a section of the linear workflow.
|
|
|
|
*/
|
|
|
|
export type SectionInfo = {
|
|
|
|
index: number;
|
2017-02-22 00:21:54 +00:00
|
|
|
component: LinearWorkflowSectionComponent;
|
2017-06-20 06:17:42 +00:00
|
|
|
};
|