-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Authoring): Create select component component
- Loading branch information
1 parent
0830204
commit 1b9b54b
Showing
9 changed files
with
173 additions
and
103 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/app/authoring-tool/select-component/select-component.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<mat-form-field> | ||
<mat-label i18n>Component</mat-label> | ||
<mat-select [(ngModel)]="componentId" (ngModelChange)="componentChangedEvent.emit($event)"> | ||
<mat-option | ||
*ngFor="let component of components; index as componentIndex" | ||
[value]="component.id" | ||
[disabled]="!componentToIsAllowed.get(component.id) || component.id === thisComponentId" | ||
> | ||
<span>{{ componentIndex + 1 }}. {{ component.type }}</span> | ||
<span *ngIf="component.id === thisComponentId" i18n>(This Component)</span> | ||
</mat-option> | ||
</mat-select> | ||
</mat-form-field> |
29 changes: 29 additions & 0 deletions
29
src/app/authoring-tool/select-component/select-component.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { SelectComponentComponent } from './select-component.component'; | ||
import { HttpClientTestingModule } from '@angular/common/http/testing'; | ||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { StudentTeacherCommonServicesModule } from '../../student-teacher-common-services.module'; | ||
import { SelectStepComponent } from '../select-step/select-step.component'; | ||
|
||
describe('SelectComponentComponent', () => { | ||
let component: SelectComponentComponent; | ||
let fixture: ComponentFixture<SelectComponentComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
BrowserAnimationsModule, | ||
HttpClientTestingModule, | ||
SelectStepComponent, | ||
StudentTeacherCommonServicesModule | ||
] | ||
}); | ||
fixture = TestBed.createComponent(SelectComponentComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
src/app/authoring-tool/select-component/select-component.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component, EventEmitter, Input, Output, SimpleChanges } from '@angular/core'; | ||
import { FormsModule } from '@angular/forms'; | ||
import { MatFormFieldModule } from '@angular/material/form-field'; | ||
import { MatSelectModule } from '@angular/material/select'; | ||
import { ProjectService } from '../../../assets/wise5/services/projectService'; | ||
import { ComponentContent } from '../../../assets/wise5/common/ComponentContent'; | ||
|
||
@Component({ | ||
selector: 'select-component', | ||
templateUrl: './select-component.component.html', | ||
standalone: true, | ||
imports: [CommonModule, FormsModule, MatFormFieldModule, MatSelectModule] | ||
}) | ||
export class SelectComponentComponent { | ||
@Input() allowedComponentTypes: string[] = []; | ||
@Output() componentChangedEvent: EventEmitter<string> = new EventEmitter<string>(); | ||
@Input() componentId: string; | ||
protected components: ComponentContent[] = []; | ||
protected componentToIsAllowed: Map<string, boolean> = new Map<string, boolean>(); | ||
@Input() nodeId: string; | ||
@Input() thisComponentId: string; | ||
|
||
constructor(private projectService: ProjectService) {} | ||
|
||
ngOnChanges(changes: SimpleChanges): void { | ||
if (changes.nodeId) { | ||
this.nodeId = changes.nodeId.currentValue; | ||
this.componentId = null; | ||
this.calculateComponents(this.nodeId); | ||
this.automaticallySetComponentIfPossible(this.nodeId); | ||
} | ||
} | ||
|
||
private calculateComponents(nodeId: string): void { | ||
this.components = this.projectService.getComponents(nodeId); | ||
for (const component of this.components) { | ||
this.componentToIsAllowed.set( | ||
component.id, | ||
this.allowedComponentTypes.includes(component.type) | ||
); | ||
} | ||
} | ||
|
||
private automaticallySetComponentIfPossible(nodeId: string): void { | ||
let numAllowedComponents = 0; | ||
let allowedComponent = null; | ||
for (const component of this.projectService.getComponents(nodeId)) { | ||
if ( | ||
this.allowedComponentTypes.includes(component.type) && | ||
component.id !== this.thisComponentId | ||
) { | ||
numAllowedComponents += 1; | ||
allowedComponent = component; | ||
} | ||
} | ||
if (numAllowedComponents === 1) { | ||
this.componentId = allowedComponent.id; | ||
this.componentChanged(); | ||
} | ||
} | ||
|
||
protected componentChanged(): void { | ||
this.componentChangedEvent.emit(this.componentId); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/app/authoring-tool/select-component/select-component.harness.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ComponentHarness } from '@angular/cdk/testing'; | ||
import { MatSelectHarness } from '@angular/material/select/testing'; | ||
|
||
export class SelectComponentHarness extends ComponentHarness { | ||
static hostSelector = 'select-component'; | ||
getSelect = this.locatorFor(MatSelectHarness); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 9 additions & 52 deletions
61
src/app/authoring-tool/select-step-and-component/select-step-and-component.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,33 @@ | ||
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; | ||
import { ProjectService } from '../../../assets/wise5/services/projectService'; | ||
import { ChangeDetectorRef, Component, EventEmitter, Input, Output } from '@angular/core'; | ||
import { ReferenceComponent } from '../../domain/referenceComponent'; | ||
import { MatFormFieldModule } from '@angular/material/form-field'; | ||
import { MatSelectModule } from '@angular/material/select'; | ||
import { FormsModule } from '@angular/forms'; | ||
import { CommonModule } from '@angular/common'; | ||
import { SelectStepComponent } from '../select-step/select-step.component'; | ||
import { SelectComponentComponent } from '../select-component/select-component.component'; | ||
|
||
@Component({ | ||
selector: 'select-step-and-component', | ||
templateUrl: './select-step-and-component.component.html', | ||
styleUrls: ['./select-step-and-component.component.scss'], | ||
standalone: true, | ||
imports: [CommonModule, FormsModule, MatFormFieldModule, MatSelectModule, SelectStepComponent] | ||
imports: [SelectComponentComponent, SelectStepComponent] | ||
}) | ||
export class SelectStepAndComponentComponent implements OnInit { | ||
export class SelectStepAndComponentComponent { | ||
@Input() allowedComponentTypes: string[] = []; | ||
@Output() componentChange: EventEmitter<ReferenceComponent> = new EventEmitter(); | ||
protected components: any[] = []; | ||
protected componentToIsAllowed: Map<string, boolean> = new Map<string, boolean>(); | ||
@Input() referenceComponent: ReferenceComponent; | ||
@Output() stepChange: EventEmitter<ReferenceComponent> = new EventEmitter(); | ||
@Input() thisComponentId: string; | ||
|
||
constructor(private projectService: ProjectService) {} | ||
|
||
ngOnInit(): void { | ||
if (this.referenceComponent.nodeId != null) { | ||
this.calculateComponents(this.referenceComponent.nodeId); | ||
if (this.referenceComponent.componentId == null) { | ||
this.automaticallySetComponentIfPossible(this.referenceComponent.nodeId); | ||
} | ||
} | ||
} | ||
|
||
private calculateComponents(nodeId: string): void { | ||
this.components = this.projectService.getComponents(nodeId); | ||
for (const component of this.components) { | ||
this.componentToIsAllowed.set( | ||
component.id, | ||
this.allowedComponentTypes.includes(component.type) | ||
); | ||
} | ||
} | ||
constructor(private changeDetector: ChangeDetectorRef) {} | ||
|
||
protected stepChanged(nodeId: string): void { | ||
this.referenceComponent.nodeId = nodeId; | ||
this.referenceComponent.componentId = null; | ||
this.automaticallySetComponentIfPossible(nodeId); | ||
this.calculateComponents(nodeId); | ||
this.changeDetector.detectChanges(); | ||
this.stepChange.emit(this.referenceComponent); | ||
} | ||
|
||
protected componentChanged(): void { | ||
protected componentChanged(componentId: string): void { | ||
this.referenceComponent.componentId = componentId; | ||
this.changeDetector.detectChanges(); | ||
this.componentChange.emit(this.referenceComponent); | ||
} | ||
|
||
private automaticallySetComponentIfPossible(nodeId: string): void { | ||
let numAllowedComponents = 0; | ||
let allowedComponent = null; | ||
for (const component of this.projectService.getComponents(nodeId)) { | ||
if ( | ||
this.allowedComponentTypes.includes(component.type) && | ||
component.id !== this.thisComponentId | ||
) { | ||
numAllowedComponents += 1; | ||
allowedComponent = component; | ||
} | ||
} | ||
if (numAllowedComponents === 1) { | ||
this.referenceComponent.componentId = allowedComponent.id; | ||
this.componentChanged(); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/app/authoring-tool/select-step-and-component/select-step-and-component.harness.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { ComponentHarness } from '@angular/cdk/testing'; | ||
import { SelectStepHarness } from '../select-step/select-step.harness'; | ||
import { SelectComponentHarness } from '../select-component/select-component.harness'; | ||
|
||
export class SelectStepAndComponentHarness extends ComponentHarness { | ||
static hostSelector = 'select-step-and-component'; | ||
getSelectComponent = this.locatorFor(SelectComponentHarness); | ||
getSelectStep = this.locatorFor(SelectStepHarness); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters