Skip to content

Commit

Permalink
Ability to print scenarios from UI (#76)
Browse files Browse the repository at this point in the history
* added ngx-markdown as dependency

* added printable endpoint

* added printable component

* printable component and ngx-markdown init

* routing for printable page

* printable button
  • Loading branch information
ebauman authored Jun 9, 2021
1 parent b4d7ced commit e9c5860
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 5 deletions.
85 changes: 83 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"lodash-es": "^4.17.15",
"moment": "^2.24.0",
"ng2-dragula": "^2.1.1",
"ngx-markdown": "^10.1.1",
"ngx-markdown-editor": "^2.5.0",
"rxjs": "^6.5.4",
"tslib": "^2.0.0",
Expand Down
8 changes: 8 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ConfigurationComponent } from './configuration/configuration.component'
import { EnvironmentsComponent } from './configuration/environments/environments.component';
import { ContentComponent } from './content/content.component';
import { CourseComponent } from './course/course.component';
import { PrintableComponent } from './printable/printable.component';

const routes: Routes = [
{path: '', redirectTo: '/home', pathMatch: 'full'},
Expand Down Expand Up @@ -64,6 +65,13 @@ const routes: Routes = [
component: EnvironmentsComponent
}
]
},
{
path: 'scenario/:scenario/printable',
component: PrintableComponent,
canActivate: [
AuthGuard
]
}
];

Expand Down
12 changes: 9 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA, SecurityContext } from '@angular/core';
import '@clr/icons';
import '@clr/icons/shapes/all-shapes';
import { AppRoutingModule } from './app-routing.module';
Expand Down Expand Up @@ -37,6 +37,8 @@ import { DeleteConfirmationComponent } from './delete-confirmation/delete-confir
import { EventStatusFilterComponent } from './event/event-status-filter/event-status-filter.component';
import { EditUserComponent } from './user/edit-user/edit-user.component';
import { EditAccessCodesComponent } from './user/edit-access-codes/edit-access-codes.component';
import { PrintableComponent } from './printable/printable.component';
import { MarkdownModule } from 'ngx-markdown';

export function jwtOptionsFactory() {
return {
Expand Down Expand Up @@ -77,7 +79,8 @@ export function jwtOptionsFactory() {
DeleteConfirmationComponent,
EventStatusFilterComponent,
EditUserComponent,
EditAccessCodesComponent
EditAccessCodesComponent,
PrintableComponent
],
imports: [
BrowserModule,
Expand All @@ -97,7 +100,10 @@ export function jwtOptionsFactory() {
}
}),
BrowserAnimationsModule,
DragulaModule.forRoot()
DragulaModule.forRoot(),
MarkdownModule.forRoot({
sanitize: SecurityContext.NONE
})
],
providers: [
ScenarioService
Expand Down
4 changes: 4 additions & 0 deletions src/app/data/scenario.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,8 @@ export class ScenarioService {

return this.http.post(environment.server + "/a/scenario/new", params)
}

public printable(id: string) {
return this.http.get(environment.server + "/a/scenario/" + id + "/printable", {responseType: 'text'})
}
}
1 change: 1 addition & 0 deletions src/app/printable/printable.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div [innerHTML]="scenario | markdown"></div>
Empty file.
25 changes: 25 additions & 0 deletions src/app/printable/printable.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { PrintableComponent } from './printable.component';

describe('PrintableComponent', () => {
let component: PrintableComponent;
let fixture: ComponentFixture<PrintableComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ PrintableComponent ]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(PrintableComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
38 changes: 38 additions & 0 deletions src/app/printable/printable.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, ParamMap } from '@angular/router';
import { first, switchMap } from 'rxjs/operators';
import { ScenarioService } from '../data/scenario.service';

@Component({
selector: 'app-printable',
templateUrl: './printable.component.html',
styleUrls: ['./printable.component.scss']
})
export class PrintableComponent implements OnInit {

public scenario: string = "";

constructor(
public route: ActivatedRoute,
public scenarioService: ScenarioService
) {}

ngOnInit(): void {
this.route.paramMap
.pipe(
first(),
switchMap((p: ParamMap) => {
return this.scenarioService.printable(p.get("scenario"))
})
).subscribe(
(content: any) => {
this.scenario = content;
},
(error: HttpErrorResponse) => {
this.scenario = "There was an error rendering printable scenario content: " + error.message
}
)
}

}
3 changes: 3 additions & 0 deletions src/app/scenario/scenario.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ <h3>Scenarios</h3>
</clr-stack-block>
</clr-stack-block>
</clr-stack-view>
<a [routerLink]="['/scenario', selectedscenario.id, 'printable']" target="_blank">
<button class="btn btn-info-outline">Print</button>
</a>
<button class="btn btn-danger-outline">Delete</button>
<button class="btn btn-success" (click)="savescenario()"
[disabled]="!this.selectedscenario.steps || !this.selectedscenario.virtualmachines">Save</button>
Expand Down

0 comments on commit e9c5860

Please sign in to comment.