Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
Fix accordion display and ordering bug
Browse files Browse the repository at this point in the history
More than one accordion block could not be displayed. Furthermore, the order of the blocks
was not implemented.
  • Loading branch information
Nicolas Märchy committed May 1, 2018
1 parent c977ea8 commit 9da3a41
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 315 deletions.
15 changes: 4 additions & 11 deletions src/learnplace/directives/accordion/accordion.directive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {ChangeDetectorRef, Component, Input, OnDestroy, OnInit} from "@angular/core";
import {AccordionBlockModel} from "../../services/block.model";
import {animate, state, style, transition, trigger} from "@angular/animations";
import {Observable} from "rxjs/Observable";
import {Subscription} from "rxjs/Subscription";
import {isDefined} from "ionic-angular/es2015/util/util";

Expand All @@ -24,25 +23,19 @@ import {isDefined} from "ionic-angular/es2015/util/util";
export class AccordionBlock implements OnInit, OnDestroy {

@Input("value")
readonly observableAccordion: Observable<AccordionBlockModel>;

accordion: AccordionBlockModel;
readonly accordion: AccordionBlockModel;

private expanded: boolean = false;

private accordionSubscription: Subscription | undefined = undefined;
private accordionSubscription?: Subscription;

constructor(
private readonly detectorRef: ChangeDetectorRef
) {}

ngOnInit(): void {

this.observableAccordion.subscribe(it => {
this.accordion = it;
this.expanded = it.expanded;
this.detectorRef.detectChanges();
});
this.expanded = this.accordion.expanded;
this.accordionSubscription = this.accordion.blocks.subscribe(_ => this.detectorRef.detectChanges())
}

ngOnDestroy(): void {
Expand Down
36 changes: 18 additions & 18 deletions src/learnplace/directives/accordion/accordion.html
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
<ion-card class="accordion" *ngIf="accordion.visible">

<ion-card-content>
<ion-card-content>

<ion-card-title (click)="toggle()">
<ion-card-title (click)="toggle()">

<ion-icon *ngIf="!expanded" name="arrow-forward"></ion-icon>
<ion-icon *ngIf="expanded" name="arrow-down"></ion-icon>
<ion-icon *ngIf="!expanded" name="arrow-forward"></ion-icon>
<ion-icon *ngIf="expanded" name="arrow-down"></ion-icon>

{{accordion.title}}
</ion-card-title>
{{accordion.title}}
</ion-card-title>


<div #expandWrapper [@expanded]="expanded">
<div #expandWrapper [@expanded]="expanded">

<div *ngFor="let block of accordion.blocks">
<text-block *ngIf="(block | async).isRichtext()" [value]="block"></text-block>
<picture-block *ngIf="(block | async).isPicture()" [value]="block"></picture-block>
<video-block *ngIf="(block | async).isVideo()" [value]="block"></video-block>
<link-block *ngIf="(block | async).isLink()" [value]="block"></link-block>
</div>
<div *ngFor="let block of (accordion.blocks | async)">
<text-block *ngIf="block.isRichtext()" [value]="block"></text-block>
<picture-block *ngIf="block.isPicture()" [value]="block"></picture-block>
<video-block *ngIf="block.isVideo()" [value]="block"></video-block>
<link-block *ngIf="block.isLink()" [value]="block"></link-block>
</div>

</div>
</div>

</ion-card-content>
</ion-card-content>

</ion-card>

<ion-card *ngIf="!accordion.visible" color=primary>

<ion-card-content>
{{"learnplace.block.accordion.too_far_away" | translate}}
</ion-card-content>
<ion-card-content>
{{"learnplace.block.accordion.too_far_away" | translate}}
</ion-card-content>

</ion-card>
107 changes: 59 additions & 48 deletions src/learnplace/directives/linkblock/link-block.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {ChangeDetectorRef, Component, Inject, Input, OnDestroy, OnInit} from "@a
import {LinkBlockModel} from "../../services/block.model";
import {LINK_BUILDER, LinkBuilder} from "../../../services/link/link-builder.service";
import {
OPEN_OBJECT_IN_ILIAS_ACTION_FACTORY,
OpenObjectInILIASAction
OPEN_OBJECT_IN_ILIAS_ACTION_FACTORY,
OpenObjectInILIASAction
} from "../../../actions/open-object-in-ilias-action";
import {Builder} from "../../../services/builder.base";
import {ILIASObjectAction} from "../../../actions/object-action";
Expand All @@ -17,67 +17,78 @@ import {Subscription} from "rxjs/Subscription";
import {isDefined} from "ionic-angular/es2015/util/util";

@Component({
selector: "link-block",
templateUrl: "link-block.html"
selector: "link-block",
templateUrl: "link-block.html"
})
export class LinkBlock implements OnInit, OnDestroy {

@Input("value")
readonly observableLinkBlock: Observable<LinkBlockModel>;
@Input("value")
readonly link: LinkBlockModel;

link: LinkBlockModel | undefined = undefined;
linkLabel: string | undefined = undefined;
disableLink: boolean = false;
linkLabel: string | undefined = undefined;
disableLink: boolean = false;

private linkBlockSubscription: Subscription | undefined = undefined;
private linkBlockSubscription: Subscription | undefined = undefined;

private readonly log: Logger = Logging.getLogger(LinkBlock.name);
private readonly log: Logger = Logging.getLogger(LinkBlock.name);

constructor(
@Inject(LINK_BUILDER) private readonly linkBuilder: LinkBuilder,
private readonly translate: TranslateService,
@Inject(OPEN_OBJECT_IN_ILIAS_ACTION_FACTORY)
private readonly openInIliasActionFactory: (title: string, urlBuilder: Builder<Promise<string>>) => OpenObjectInILIASAction,
private readonly detectorRef: ChangeDetectorRef
){}
constructor(
@Inject(LINK_BUILDER) private readonly linkBuilder: LinkBuilder,
private readonly translate: TranslateService,
@Inject(OPEN_OBJECT_IN_ILIAS_ACTION_FACTORY)
private readonly openInIliasActionFactory: (title: string, urlBuilder: Builder<Promise<string>>) => OpenObjectInILIASAction,
private readonly detectorRef: ChangeDetectorRef
) {
}

ngOnInit(): void {

ngOnInit(): void {
// this.linkBlockSubscription = this.observableLinkBlock.subscribe(it => {
// this.link = it;
// this.detectorRef.detectChanges();
// });

this.linkBlockSubscription = this.observableLinkBlock.subscribe(it => {
this.link = it;
this.detectorRef.detectChanges();
});
// because the ref id is immutable, we only want to read the objects title once
User.findActiveUser().then(user => {
return ILIASObject.findByRefId(this.link.refId, user.id);
}).then(obj => {
this.linkLabel = obj.title;
}).catch(_ => {
this.disableLink = true;
this.log.warn(() => `Could not load label for link block with refId: refId=${this.link.refId}`);
this.linkLabel = this.translate.instant("learnplace.block.link.no_access");
});

// because the ref id is immutable, we only want to read the objects title once
this.observableLinkBlock.first().subscribe(it => {

User.findActiveUser().then(user => {
return ILIASObject.findByRefId(it.refId, user.id);
}).then(obj => {
this.linkLabel = obj.title;
}).catch(_ => {
this.disableLink = true;
this.log.warn(() => `Could not load label for link block with refId: refId=${it.refId}`);
this.linkLabel = this.translate.instant("learnplace.block.link.no_access");
});
});
}
// this.observableLinkBlock.first().subscribe(it => {
//
// User.findActiveUser().then(user => {
// return ILIASObject.findByRefId(it.refId, user.id);
// }).then(obj => {
// this.linkLabel = obj.title;
// }).catch(_ => {
// this.disableLink = true;
// this.log.warn(() => `Could not load label for link block with refId: refId=${it.refId}`);
// this.linkLabel = this.translate.instant("learnplace.block.link.no_access");
// });
// });
}

ngOnDestroy(): void {
if(isDefined(this.linkBlockSubscription)) {
this.linkBlockSubscription.unsubscribe();
ngOnDestroy(): void {
if (isDefined(this.linkBlockSubscription)) {
this.linkBlockSubscription.unsubscribe();
}
}
}

open(): void {
open(): void {

if(!this.disableLink) {
const action: ILIASObjectAction = this.openInIliasActionFactory(
this.translate.instant("actions.view_in_ilias"),
this.linkBuilder.default().target(this.link.refId)
);
if (!this.disableLink) {
const action: ILIASObjectAction = this.openInIliasActionFactory(
this.translate.instant("actions.view_in_ilias"),
this.linkBuilder.default().target(this.link.refId)
);

action.execute();
action.execute();
}
}
}
}
92 changes: 36 additions & 56 deletions src/learnplace/directives/pictureblock/pictureblock.directive.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,58 @@
import {ChangeDetectorRef, Component, Input, OnDestroy, OnInit} from "@angular/core";
import {Component, Input, OnDestroy, OnInit} from "@angular/core";
import {PictureBlockModel} from "../../services/block.model";
import {Platform} from "ionic-angular";
import {File} from "@ionic-native/file";
import {PhotoViewer, PhotoViewerOptions} from "@ionic-native/photo-viewer";
import {DomSanitizer, SafeUrl} from "@angular/platform-browser";
import {Logger} from "../../../services/logging/logging.api";
import {Logging} from "../../../services/logging/logging.service";
import {Observable} from "rxjs/Observable";
import {Subscription} from "rxjs/Subscription";
import {isDefined} from "ionic-angular/es2015/util/util";

@Component({
selector: "picture-block",
templateUrl: "picture-block.html"
selector: "picture-block",
templateUrl: "picture-block.html"
})
export class PictureBlock implements OnInit, OnDestroy {
export class PictureBlock implements OnInit {

@Input("value")
readonly observablePicture: Observable<PictureBlockModel>;
@Input("value")
readonly pictureBlock: PictureBlockModel;

pictureBlock: PictureBlockModel | undefined = undefined;
embeddedSrc?: SafeUrl;

embeddedSrc: SafeUrl | undefined = undefined;
private readonly log: Logger = Logging.getLogger(PictureBlock.name);

private pictureBlockSubscription: Subscription | undefined = undefined;

private readonly log: Logger = Logging.getLogger(PictureBlock.name);

constructor(
private readonly platform: Platform,
private readonly file: File,
private readonly photoViewer: PhotoViewer,
private readonly sanitizer: DomSanitizer,
private readonly detectorRef: ChangeDetectorRef
) {}

ngOnInit(): void {
constructor(
private readonly platform: Platform,
private readonly file: File,
private readonly photoViewer: PhotoViewer,
private readonly sanitizer: DomSanitizer
) {
}

this.pictureBlockSubscription = this.observablePicture.subscribe(it => {
this.pictureBlock = it;
this.detectorRef.detectChanges();
});
ngOnInit(): void {

// because the thumbnail is immutable, we only use the first picture block to encode it
this.observablePicture.first().subscribe(it => {
const fileName: string = it.thumbnail.split("/").pop();
const path: string = it.thumbnail.replace(fileName, "");
const fileName: string = this.pictureBlock.thumbnail.split("/").pop();
const path: string = this.pictureBlock.thumbnail.replace(fileName, "");

this.file.readAsDataURL(`${this.getStorageLocation()}${path}`, fileName).then(data => {
this.embeddedSrc = this.sanitizer.bypassSecurityTrustUrl(data);
}).catch(error => {
this.log.warn(() => `Could not load thumbnail: url: ${it.thumbnail}`);
this.log.debug(() => `Thumbnail load error: ${JSON.stringify(error)}`);
});
});
}
this.file.readAsDataURL(`${this.getStorageLocation()}${path}`, fileName).then(data => {
this.embeddedSrc = this.sanitizer.bypassSecurityTrustUrl(data);
}).catch(error => {
this.log.warn(() => `Could not load thumbnail: url: ${this.pictureBlock.thumbnail}`);
this.log.debug(() => `Thumbnail load error: ${JSON.stringify(error)}`);
});
}

ngOnDestroy(): void {
if(isDefined(this.pictureBlockSubscription))
this.pictureBlockSubscription.unsubscribe();
}
show(): void {
this.photoViewer.show(`${this.getStorageLocation()}${this.pictureBlock.url}`, this.pictureBlock.title, <PhotoViewerOptions>{share: false});
}

show(): void {
this.photoViewer.show(`${this.getStorageLocation()}${this.pictureBlock.url}`, this.pictureBlock.title, <PhotoViewerOptions>{share:false});
}
private getStorageLocation(): string {
if (this.platform.is("android")) {
return this.file.externalApplicationStorageDirectory;
}
if (this.platform.is("ios")) {
return this.file.dataDirectory;
}

private getStorageLocation(): string {
if (this.platform.is("android")) {
return this.file.externalApplicationStorageDirectory;
throw new Error("Unsupported platform. Can not return a storage location.");
}
if (this.platform.is("ios")) {
return this.file.dataDirectory;
}

throw new Error("Unsupported platform. Can not return a storage location.");
}
}
12 changes: 5 additions & 7 deletions src/learnplace/directives/textblock/textblock.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import {isDefined} from "ionic-angular/es2015/util/util";
export class TextBlock implements OnInit, OnDestroy {

@Input("value")
readonly observableTextBlock: Observable<TextBlockModel>;

textBlock: TextBlockModel | undefined = undefined;
readonly textBlock: TextBlockModel;

private textBlockSubscription: Subscription | undefined = undefined;

Expand All @@ -22,10 +20,10 @@ export class TextBlock implements OnInit, OnDestroy {
) {}

ngOnInit(): void {
this.textBlockSubscription = this.observableTextBlock.subscribe(it => {
this.textBlock = it;
this.detectorRef.detectChanges();
})
// this.textBlockSubscription = this.observableTextBlock.subscribe(it => {
// this.textBlock = it;
// this.detectorRef.detectChanges();
// })
}

ngOnDestroy(): void {
Expand Down
12 changes: 5 additions & 7 deletions src/learnplace/directives/videoblock/videoblock.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import {isDefined} from "ionic-angular/es2015/util/util";
export class VideoBlock implements OnInit, OnDestroy {

@Input("value")
readonly observableVideoBlock: Observable<VideoBlockModel>;

videoBlock: VideoBlockModel | undefined = undefined;
readonly videoBlock: VideoBlockModel;

private videoBlockSubscription: Subscription | undefined = undefined;

Expand All @@ -29,10 +27,10 @@ export class VideoBlock implements OnInit, OnDestroy {


ngOnInit(): void {
this.videoBlockSubscription = this.observableVideoBlock.subscribe(it => {
this.videoBlock = it;
this.detectorRef.detectChanges();
})
// this.videoBlockSubscription = this.observableVideoBlock.subscribe(it => {
// this.videoBlock = it;
// this.detectorRef.detectChanges();
// })
}

ngOnDestroy(): void {
Expand Down
Loading

0 comments on commit 9da3a41

Please sign in to comment.