Skip to content

Commit

Permalink
Resource in step view (connects #668) (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbert authored and lmmrssa committed May 3, 2018
1 parent 1d42484 commit 5606082
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/app/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ $font-family: Roboto, sans-serif;
$screen-md: 1000px;
$screen-sm: 780px;

// Maximum full height of view container (avoiding vertical overflow)
$view-container-height: calc(100vh - 352px);

/*
* Old Planet Material Colors
* Saved in case we want to revert
Expand Down
3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { NgxImgModule } from 'ngx-img';
import { environment } from '../environments/environment';
import { MatIconRegistry } from '@angular/material';
import { FeedbackService } from './feedback/feedback.service';
import { ResourcesService } from './resources/resources.service';

@NgModule({
imports: [
Expand All @@ -36,7 +37,7 @@ import { FeedbackService } from './feedback/feedback.service';
AppComponent, PageNotFoundComponent
],
providers: [
CouchService, AuthService, UserService, ValidatorService, PlanetMessageService, MatIconRegistry, FeedbackService
CouchService, AuthService, UserService, ValidatorService, PlanetMessageService, MatIconRegistry, FeedbackService, ResourcesService
],
bootstrap: [ AppComponent ]
})
Expand Down
4 changes: 3 additions & 1 deletion src/app/courses/courses.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CoursesViewComponent } from './view-courses/courses-view.component';
import { CoursesStepComponent } from './add-courses/courses-step.component';
import { CoursesStepViewComponent } from './step-view-courses/courses-step-view.component';
import { CoursesService } from './courses.service';
import { ResourcesViewerComponent } from '../resources/view-resources/resources-viewer.component';

@NgModule({
imports: [
Expand All @@ -30,7 +31,8 @@ import { CoursesService } from './courses.service';
CoursesRequestComponent,
CoursesViewComponent,
CoursesStepComponent,
CoursesStepViewComponent
CoursesStepViewComponent,
ResourcesViewerComponent
],
providers: [
CoursesService
Expand Down
5 changes: 3 additions & 2 deletions src/app/courses/courses.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export class CoursesService {

// Components call this to get details of one course.
// If the id already matches what is stored on the service, return that.
requestCourse(courseId: string, opts: any = {}) {
if (courseId === this.course._id) {
// Or will get new version if forceLatest set to true
requestCourse({ courseId, forceLatest = false }, opts: any = {}) {
if (!forceLatest && courseId === this.course._id) {
this.courseUpdated.next(this.course);
} else {
this.getCourse(courseId, opts);
Expand Down
21 changes: 18 additions & 3 deletions src/app/courses/step-view-courses/courses-step-view.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@

<div class="space-container">
<mat-toolbar class="primary-color font-size-1">
<span class="margin-lr-3"><h3>{{stepDetail.stepTitle}}</h3></span>
<span class="margin-lr-3"><h3><ng-container i18n>Step</ng-container> {{stepNum}}: {{stepDetail.stepTitle}}</h3></span>
<span class="toolbar-fill"></span>
<a
mat-raised-button
color="accent"
class="margin-lr-10"
*ngIf="stepDetail.attachment"
[href]="resourceUrl"
target="_blank"
i18n>
Open resource new tab
</a>
<span i18n>{{stepNum}}/{{maxStep}}</span>
<button mat-icon-button [disabled]="stepNum === 1" (click)="changeStep(-1)"><mat-icon>navigate_before</mat-icon></button>
<button mat-icon-button [disabled]="stepNum === maxStep" (click)="changeStep(1)"><mat-icon>navigate_next</mat-icon></button>
</mat-toolbar>
<div class="view-container">
{{stepDetail.description}}
<div class="view-container view-full-height">
<div class="description">{{stepDetail.description}}</div>
<planet-resources-viewer
*ngIf="stepDetail.attachment"
[resourceId]="stepDetail.attachment.doc._id"
(resourceUrl)="setResourceUrl($event)">
</planet-resources-viewer>
</div>
</div>
10 changes: 8 additions & 2 deletions src/app/courses/step-view-courses/courses-step-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { Subject } from 'rxjs/Subject';
import { takeUntil } from 'rxjs/operators';

@Component({
templateUrl: './courses-step-view.component.html'
templateUrl: './courses-step-view.component.html',
styleUrls: [ './courses-step-view.scss' ]
})

export class CoursesStepViewComponent implements OnInit, OnDestroy {
Expand All @@ -14,6 +15,7 @@ export class CoursesStepViewComponent implements OnInit, OnDestroy {
stepNum = 0;
stepDetail: any = { stepTitle: '', description: '' };
maxStep = 1;
resourceUrl = '';

constructor(
private router: Router,
Expand All @@ -29,7 +31,7 @@ export class CoursesStepViewComponent implements OnInit, OnDestroy {
});
this.route.paramMap.pipe(takeUntil(this.onDestroy$)).subscribe((params: ParamMap) => {
this.stepNum = +params.get('stepNum'); // Leading + forces string to number
this.coursesService.requestCourse(params.get('id'));
this.coursesService.requestCourse({ courseId: params.get('id') });
});
}

Expand All @@ -47,4 +49,8 @@ export class CoursesStepViewComponent implements OnInit, OnDestroy {
this.router.navigate([ '../../' ], { relativeTo: this.route });
}

setResourceUrl(resourceUrl: string) {
this.resourceUrl = resourceUrl;
}

}
19 changes: 19 additions & 0 deletions src/app/courses/step-view-courses/courses-step-view.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
:host {

.view-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(10px, 1fr));
grid-template-rows: 1fr;
grid-column-gap: 1rem;

> * {
overflow-y: auto;
}

}

planet-resources-viewer * {
max-width: 100%;
}

}
2 changes: 1 addition & 1 deletion src/app/courses/view-courses/courses-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class CoursesViewComponent implements OnInit, OnDestroy {
ngOnInit() {
this.coursesService.courseUpdated$.pipe(takeUntil(this.onDestroy$)).subscribe(course => this.courseDetail = course);
this.route.paramMap.pipe(takeUntil(this.onDestroy$)).subscribe(
(params: ParamMap) => this.coursesService.requestCourse(params.get('id')),
(params: ParamMap) => this.coursesService.requestCourse({ courseId: params.get('id'), forceLatest: true }),
error => console.log(error)
);
}
Expand Down
4 changes: 1 addition & 3 deletions src/app/resources/resources.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { MaterialModule } from '../shared/material.module';
import { HttpClientModule, HttpClientJsonpModule } from '@angular/common/http';
import { ResourcesRatingComponent } from './rating-resources/resources-rating.component';
import { PlanetStackedBarComponent } from '../shared/planet-stacked-bar.component';
import { ResourcesService } from './resources.service';
import { PlanetDialogsModule } from '../shared/dialogs/planet-dialogs.module';

@NgModule({
Expand All @@ -25,7 +24,6 @@ import { PlanetDialogsModule } from '../shared/dialogs/planet-dialogs.module';
HttpClientJsonpModule,
PlanetDialogsModule
],
declarations: [ ResourcesComponent, ResourcesViewComponent, ResourcesAddComponent, ResourcesRatingComponent, PlanetStackedBarComponent ],
providers: [ ResourcesService ]
declarations: [ ResourcesComponent, ResourcesViewComponent, ResourcesAddComponent, ResourcesRatingComponent, PlanetStackedBarComponent ]
})
export class ResourcesModule {}
15 changes: 15 additions & 0 deletions src/app/resources/view-resources/resources-viewer.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div [ngSwitch]="mediaType">
<img [src]="resourceSrc" *ngSwitchCase="'image'">
<video controls *ngSwitchCase="'video'">
<source [src]="resourceSrc" [type]="contentType" />
Browser not supported
</video>
<audio controls *ngSwitchCase="'audio'">
<source [src]="resourceSrc" [type]="contentType" />
Browser not supported
</audio>
<iframe [src]="pdfSrc" *ngSwitchCase="'pdf'" width="100%" height="100%" allowfullscreen mozallowfullscreen webkitallowfullscreen
oallowfullscreen msallowfullscreen ></iframe>
<div *ngSwitchCase="'other'"><a mat-button-raised href={{resourceSrc}}>Open File</a></div>
<iframe *ngSwitchCase="'HTML'" [src]="pdfSrc"></iframe>
</div>
66 changes: 66 additions & 0 deletions src/app/resources/view-resources/resources-viewer.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Component, Input, OnChanges, OnDestroy, EventEmitter, Output } from '@angular/core';

import { DomSanitizer } from '@angular/platform-browser';
import { environment } from '../../../environments/environment';
import { takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs/Subject';
import { ResourcesService } from '../resources.service';

@Component({
selector: 'planet-resources-viewer',
templateUrl: './resources-viewer.component.html',
styleUrls: [ './resources-viewer.scss' ]
})
export class ResourcesViewerComponent implements OnChanges, OnDestroy {

@Input() resourceId: string;
@Input() resource: any;
@Output() resourceUrl = new EventEmitter<any>();
mediaType: string;
contentType: string;
resourceSrc: string;
urlPrefix = environment.couchAddress + 'resources/';
pdfSrc: any;
private onDestroy$ = new Subject<void>();

constructor(
private sanitizer: DomSanitizer,
private resourcesService: ResourcesService
) { }

ngOnChanges() {
if (this.resource === undefined || this.resource._id !== this.resourceId) {
this.resourcesService.resourcesUpdated$.pipe(takeUntil(this.onDestroy$))
.subscribe((resourceArr) => {
this.setResource(resourceArr[0]);
});
this.resourcesService.updateResources({ resourceIds: [ this.resourceId ] });
} else {
this.setResource(this.resource);
}
}

ngOnDestroy() {
this.onDestroy$.next();
this.onDestroy$.complete();
}

setResource(resource: any) {
this.resource = resource;
// openWhichFile is used to label which file to start with for HTML resources
const filename = resource.openWhichFile || Object.keys(resource._attachments)[0];
this.mediaType = resource.mediaType;
this.contentType = resource._attachments[filename].content_type;
this.resourceSrc = this.urlPrefix + resource._id + '/' + filename;
if (!this.mediaType) {
const mediaTypes = [ 'image', 'pdf', 'audio', 'video', 'zip' ];
this.mediaType = mediaTypes.find((type) => this.contentType.indexOf(type) > -1) || 'other';
}
if (this.mediaType === 'pdf' || this.mediaType === 'HTML') {
this.pdfSrc = this.sanitizer.bypassSecurityTrustResourceUrl(this.resourceSrc);
}
// Emit resource src so parent component can use for links
this.resourceUrl.emit(this.resourceSrc);
}

}
14 changes: 14 additions & 0 deletions src/app/resources/view-resources/resources-viewer.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@import '../../variables';

:host {
* {
max-width: 100%;
}

iframe {
display: block;
border: none;
width: 100%;
height: $view-container-height;
}
}
2 changes: 1 addition & 1 deletion src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ body {
// 352px = 5 * 64px toolbars + 32px of padding
// Overflow must be set on children
.view-container.view-full-height {
height: calc(100vh - 352px);
height: $view-container-height;
}

// Width of a form with 3 standard matInputs (180px + 10px of padding)
Expand Down

0 comments on commit 5606082

Please sign in to comment.