Skip to content

Commit

Permalink
refactor(slideshow): set standalone (#56)
Browse files Browse the repository at this point in the history
* refactor(client/ng): turn slideshow as standalone component

* fix(client/ng): @service imports

* fix(client/ng): imports

* fix(client/ng): slideshow navbar

* fix(client/ng): slideshow eggtimer

* refactor(EggTimer): comments, naming
  • Loading branch information
berdal84 committed May 3, 2023
1 parent aa26ca5 commit 349d118
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 231 deletions.
22 changes: 11 additions & 11 deletions client/ng/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@ import { RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { ROUTES } from './app.routes';
import { AppComponent } from './app.component';
import { MenuComponent } from './components/menu/menu.component';
import { SlideshowComponent } from './components/slideshow/slideshow.component';
import { ListComponent } from './components/list/list.component';
import { AdvisesComponent } from './components/advises/advises.component';
import { MoreComponent } from './components/more/more.component';
import { ContributeComponent } from './components/contribute/contribute.component';
import { Error404Component } from './components/error404/error404.component';
import { MenuComponent } from '@components/menu/menu.component';
import { ListComponent } from '@components/list/list.component';
import { AdvisesComponent } from '@components/advises/advises.component';
import { MoreComponent } from '@components/more/more.component';
import { ContributeComponent } from '@components/contribute/contribute.component';
import { Error404Component } from '@components/error404/error404.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { environment } from 'src/environments/environment';
import { AdminModule } from './components/admin/admin.module';
import { PaginationComponent } from './components/pagination/pagination.component';
import { AdminModule } from '@components/admin/admin.module';
import { PaginationComponent } from '@components/pagination/pagination.component';
import {NgOptimizedImage} from "@angular/common";
import {SlideshowComponent} from "@components/slideshow/slideshow.component";

@NgModule({
declarations: [
AppComponent,
MenuComponent,
SlideshowComponent,
ListComponent,
AdvisesComponent,
MoreComponent,
Error404Component,
ContributeComponent,
PaginationComponent
PaginationComponent,
],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
AdminModule,
SlideshowComponent,
RouterModule.forRoot(
ROUTES, {
enableTracing: !environment.production,
Expand Down
2 changes: 1 addition & 1 deletion client/ng/src/app/components/admin/admin.guard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthService } from '@servicesauth.service';
import { AuthService } from '@services/auth.service';

@Injectable()
export class AdminGuard implements CanActivate, CanActivateChild {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService } from '@servicesauth.service';
import { AuthService } from '@services/auth.service';

@Component({
selector: 'app-dashboard',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component } from "@angular/core";
import { FormControl, FormGroup, Validators } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
import { Credentials } from "jeudemots-shared";
import { AuthService } from "@servicesauth.service";
import { AuthService } from "@services/auth.service";

@Component({
selector: "app-login",
Expand Down
2 changes: 1 addition & 1 deletion client/ng/src/app/components/list/list.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnDestroy, OnInit } from "@angular/core";
import { interval, Subscription } from "rxjs";
import { AuthService } from "@servicesauth.service";
import { AuthService } from "@services/auth.service";
import { Joke, Page } from "jeudemots-shared";
import { BackendService } from "@services/backend.service";
import { debounce, filter, map, tap } from "rxjs/operators";
Expand Down
2 changes: 1 addition & 1 deletion client/ng/src/app/components/menu/menu.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component } from '@angular/core';
import { Router } from '@angular/router';
import { AuthService as AuthService } from '@servicesauth.service';
import { AuthService as AuthService } from '@services/auth.service';
import {map} from 'rxjs/operators';
import {LINKS} from './menu.data';
import {combineLatest, Observable, of} from 'rxjs';
Expand Down
69 changes: 69 additions & 0 deletions client/ng/src/app/components/slideshow/egg-timer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {BehaviorSubject, Subject} from "rxjs";

type EggTimerOptions = {
precision?: number;
};

export class EggTimer {
/** in seconds */
private timer: number = 0;
/** in seconds */
private initialTimer = 0;
/** in milliseconds */
private readonly precision;
/** Triggered when paused or started */
isPlaying$ = new BehaviorSubject(false);
/** triggered when the egg time expired */
timeout$ = new Subject<void>();

constructor({precision = 100}: EggTimerOptions = {}) {
this.precision = precision;
}

/**
* Return a number between 0 (just started) and 1 (timeout)
*/
get progress(): number {
return 1 - this.timer / this.initialTimer;
}

/**
* Reset the timer (without pausing/playing)
* @param time the new time in second
*/
reset(time: number) {
this.timer = time;
this.initialTimer = time;
}

/**
* Starts the timer
* @param time the new time in second
*/
start(time?: number) {
if ( time ) this.reset(time);

this.isPlaying$.next(true);

// Function to increment the egg timer and set a new page when necessary
// It relies on setTimeout to be sure next increment cannot happen when page is loading
const incrementEggTimer = async () => {
if ( !this.isPlaying$.value ) return;
this.timer -= this.precision / 1000;
if ( this.timer < 0 ) {
this.timer = 0; // set to 0 to ensure the progress computation won't be negative
this.isPlaying$.next(false);
this.timeout$.next();
} else {
window.setTimeout( incrementEggTimer, this.precision);
}
};

// Bootstrap the first increment
window.setTimeout( incrementEggTimer );
}

pause() {
this.isPlaying$.next(false);
}
}
106 changes: 106 additions & 0 deletions client/ng/src/app/components/slideshow/navbar.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {Component, EventEmitter, Input, Output} from "@angular/core";
import {CommonModule, NgClass, NgIf, NgOptimizedImage} from '@angular/common';
import {ButtonType} from './navbar.types';

@Component({
standalone: true,
selector: 'app-navbar',
imports: [
NgClass,
CommonModule,
NgIf,
NgOptimizedImage,
],
styles: [`
:host {
display: flex;
}
.mirror-x{
scale: -1;
}
img{
transform: rotate(0deg);
width: auto;
height: auto;
filter: invert(0.8) drop-shadow( 1px 1px 2px white);
cursor: pointer;
transition-duration: 0.5s;
}
img:hover {
filter: invert(0.5) drop-shadow( 1px 1px 5px white);
transform: scale( 1.1);
transition-duration: 0.2s;
}
img.disable {
filter: invert(0.8) drop-shadow( 1px 1px 2px white);
cursor: pointer;
transition-duration: 0.5s;
}
`],
template: `
<img
src="assets/rewind.svg"
[ngClass]="{
'disable': !enablePrevious
}"
(click)="clickEvent.emit('first')"
[title]="enablePrevious ? 'Se rendre au début' : 'Vous y êtes déjà'"
alt="go to first"
/>
<img
src="assets/arrow-left.svg"
[ngClass]="{
'disable': !enablePrevious
}"
(click)="clickEvent.emit('previous')"
[title]="enablePrevious ? 'précédent' : 'Vous y êtes déjà'"
alt="previous"
/>
<img
*ngIf="!playing"
src="assets/play.svg"
(click)="clickEvent.emit('play')"
title="Démarrer le diaporama"
alt="play"
/>
<img
*ngIf="playing"
src="assets/pause.svg"
(click)="clickEvent.emit('pause')"
title="Mettre le diaporama en pause"
alt="pause"
/>
<img
src="assets/arrow-left.svg"
class="mirror-x"
[ngClass]="{
'disable': !enableNext
}"
(click)="clickEvent.emit('next')"
[title]="enableNext ? 'suivant' : 'Vous y êtes déjà'"
alt="next"
/>
<img
src="assets/rewind.svg"
class="mirror-x"
[ngClass]="{
'disable': !enableNext
}"
(click)="clickEvent.emit('last')"
[title]="enableNext ? 'Se rendre à la fin' : 'Vous y êtes déjà'"
alt="go to last"
/>
`
})
export class NavBarComponent {
@Output('onClicked') clickEvent = new EventEmitter<ButtonType>();
@Input() playing: boolean = false;
@Input() enablePrevious: boolean = false;
@Input() enableNext: boolean = false;
}
1 change: 1 addition & 0 deletions client/ng/src/app/components/slideshow/navbar.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ButtonType = 'play' | 'pause' | 'previous' | 'next' | 'first' | 'last';
28 changes: 1 addition & 27 deletions client/ng/src/app/components/slideshow/slideshow.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,11 @@
justify-content: space-between;
}

#navbar {
.navbar {
width: 30%;
display: flex;
flex-direction: row;
justify-content: center;
margin: 8px 0;
gap: 2px;
transform: rotate(0deg);
}

.mirror-x{
scale: -1;
}

#navbar > img{
width: auto;
height: auto;
filter: invert(0.8) drop-shadow( 1px 1px 2px white);
cursor: pointer;
transition-duration: 0.5s;
}

#navbar > img:hover {
filter: invert(0.5) drop-shadow( 1px 1px 5px white);
transform: scale( 1.1);
transition-duration: 0.2s;
}

#navbar > img:disable {
filter: invert(0.8) drop-shadow( 1px 1px 2px white);
cursor: pointer;
transition-duration: 0.5s;
}

Loading

0 comments on commit 349d118

Please sign in to comment.