Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions apps/angular/anchor-scrolling/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import {
provideRouter,
withInMemoryScrolling,
withViewTransitions,
} from '@angular/router';
import { appRoutes } from './app.routes';

export const appConfig: ApplicationConfig = {
providers: [provideRouter(appRoutes)],
providers: [
provideRouter(
appRoutes,
withViewTransitions(),
withInMemoryScrolling({
anchorScrolling: 'enabled', // Enables scrolling to fragments (#id)
scrollPositionRestoration: 'enabled',
}),
),
],
};
9 changes: 6 additions & 3 deletions apps/angular/anchor-scrolling/src/app/foo.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { NavButtonComponent } from './nav-button.component';

@Component({
standalone: true,
imports: [NavButtonComponent],
imports: [NavButtonComponent, RouterLink],
selector: 'app-foo',
template: `
Welcome to foo page
<nav-button href="home" class="fixed left-1/2 top-3">Home Page</nav-button>
<nav-button [links]="'/home'" class="fixed left-1/2 top-3 snap-start">
Home Page
</nav-button>
<div class="h-screen bg-blue-200">section 1</div>
<div class="h-screen bg-red-200">section 2</div>
<div class="h-screen snap-end bg-red-200">section 2</div>
`,
})
export class FooComponent {}
25 changes: 17 additions & 8 deletions apps/angular/anchor-scrolling/src/app/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { Component } from '@angular/core';
import { Component, Output } from '@angular/core';
import { RouterLink, RouterLinkActive } from '@angular/router';
import { NavButtonComponent } from './nav-button.component';

@Component({
standalone: true,
imports: [NavButtonComponent],
imports: [NavButtonComponent, RouterLink, RouterLinkActive],
selector: 'app-home',
template: `
<nav-button href="/foo" class="fixed left-1/2 top-3">Foo Page</nav-button>
<div id="top" class="h-screen bg-gray-500">
<nav-button [links]="'/foo'" class="fixed left-1/2 top-3">
Foo Page
</nav-button>
<div id="top" class="h-screen snap-start bg-gray-500">
Empty
<nav-button href="#bottom">Scroll Bottom</nav-button>
<nav-button [links]="homePath" [fragment]="'bottom'">
Scroll Bottom
</nav-button>
</div>
<div id="bottom" class="h-screen bg-blue-300">
<div id="bottom" class="h-screen snap-start bg-blue-300">
I want to scroll each
<nav-button href="#top">Scroll Top</nav-button>
<nav-button [links]="homePath" [fragment]="'top'">Scroll Top</nav-button>
</div>
`,
})
export class HomeComponent {}
export class HomeComponent {
@Output() links = '.';
@Output() fragment = '';
homePath = '.';
}
8 changes: 6 additions & 2 deletions apps/angular/anchor-scrolling/src/app/nav-button.component.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/* eslint-disable @angular-eslint/component-selector */
import { Component, Input } from '@angular/core';
import { RouterLink } from '@angular/router';

@Component({
selector: 'nav-button',
standalone: true,
template: `
<a [href]="href">
<a [routerLink]="links" [fragment]="fragment">
<ng-content></ng-content>
</a>
`,
host: {
class: 'block w-fit border border-red-500 rounded-md p-4 m-2',
},
imports: [RouterLink],
})
export class NavButtonComponent {
@Input() href = '';
@Input() links: string | string[] = '';
@Input() fragment = '';
}
8 changes: 8 additions & 0 deletions apps/angular/anchor-scrolling/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
@tailwind utilities;

/* You can add global styles to this file, and also import other style files */
html {
scroll-behavior: smooth;
scroll-snap-type: y mandatory;
}

body {
overflow-y: scroll;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
import { Component, OnInit } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import { FakeHttpService } from '../../data-access/fake-http.service';
import { CardType } from '../../model/card.model';
import { City } from '../../model/city.model';
import { CardComponent } from '../../ui/card/card.component';

@Component({
selector: 'app-city-card',
template: 'TODO City',
template: `
<app-card
[list]="cities"
[type]="cardType"
[image]="image"
customClass="bg-light-red"></app-card>
`,
standalone: true,
imports: [],
imports: [CardComponent],
})
export class CityCardComponent implements OnInit {
constructor() {}
cities: City[] = [];
image = 'city.png';
cardType = CardType.CITY;

ngOnInit(): void {}
constructor(
private http: FakeHttpService,
private store: CityStore,
) {}

ngOnInit(): void {
this.http.fetchCities$.subscribe((s) => this.store.addAll(s));

this.store.cities$.subscribe((s) => (this.cities = s));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CardComponent } from '../../ui/card/card.component';
<app-card
[list]="students"
[type]="cardType"
[image]="image"
customClass="bg-light-green"></app-card>
`,
standalone: true,
Expand All @@ -26,6 +27,7 @@ import { CardComponent } from '../../ui/card/card.component';
export class StudentCardComponent implements OnInit {
students: Student[] = [];
cardType = CardType.STUDENT;
image = 'student.webp';

constructor(
private http: FakeHttpService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CardComponent } from '../../ui/card/card.component';
<app-card
[list]="teachers"
[type]="cardType"
[image]="image"
customClass="bg-light-red"></app-card>
`,
styles: [
Expand All @@ -26,6 +27,7 @@ import { CardComponent } from '../../ui/card/card.component';
export class TeacherCardComponent implements OnInit {
teachers: Teacher[] = [];
cardType = CardType.TEACHER;
image = 'teacher.png';

constructor(
private http: FakeHttpService,
Expand Down
8 changes: 8 additions & 0 deletions apps/angular/projection/src/app/model/card.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ export enum CardType {
STUDENT,
CITY,
}

export interface CardModel {
name: string;
firstName: string;
lastName: string;
subject: string;
id: number;
}
31 changes: 31 additions & 0 deletions apps/angular/projection/src/app/services/card.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from '@angular/core';
import { CityStore } from '../data-access/city.store';
import {
randomCity,
randStudent,
randTeacher,
} from '../data-access/fake-http.service';
import { StudentStore } from '../data-access/student.store';
import { TeacherStore } from '../data-access/teacher.store';
import { CardType } from '../model/card.model';

@Injectable({
providedIn: 'root',
})
export class CardService {
constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
private cityStore: CityStore,
) {}

addOne(type: CardType) {
if (type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
} else if (type === CardType.CITY) {
this.cityStore.addOne(randomCity());
}
}
}
61 changes: 24 additions & 37 deletions apps/angular/projection/src/app/ui/card/card.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { NgFor, NgIf } from '@angular/common';
import { CommonModule } from '@angular/common';
import { Component, Input } from '@angular/core';
import { randStudent, randTeacher } from '../../data-access/fake-http.service';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
import { CardService } from '../../services/card.service';
import { ListItemComponent } from '../list-item/list-item.component';

@Component({
Expand All @@ -12,50 +10,39 @@ import { ListItemComponent } from '../list-item/list-item.component';
<div
class="flex w-fit flex-col gap-3 rounded-md border-2 border-black p-4"
[class]="customClass">
<img
*ngIf="type === CardType.TEACHER"
src="assets/img/teacher.png"
width="200px" />
<img
*ngIf="type === CardType.STUDENT"
src="assets/img/student.webp"
width="200px" />
<ng-template #cardContent>
<img src="assets/img/{{ image }}" width="200px" />

<section>
<app-list-item
*ngFor="let item of list"
[name]="item.firstName"
[id]="item.id"
[type]="type"></app-list-item>
</section>
<section>
<app-list-item
*ngFor="let item of list"
[name]="item.firstName ?? item.name"
[id]="item.id"
[type]="type"></app-list-item>
</section>

<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
<button
class="rounded-sm border border-blue-500 bg-blue-300 p-2"
(click)="addNewItem()">
Add
</button>
</ng-template>

<ng-container *ngTemplateOutlet="cardContent"></ng-container>
</div>
`,
standalone: true,
imports: [NgIf, NgFor, ListItemComponent],
imports: [CommonModule, ListItemComponent],
})
export class CardComponent {
@Input() list: any[] | null = null;
@Input() list!: any[];
@Input() type!: CardType;
@Input() customClass = '';
@Input() image: string = '';

CardType = CardType;

constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
) {}
constructor(private cardService: CardService) {}

addNewItem() {
if (this.type === CardType.TEACHER) {
this.teacherStore.addOne(randTeacher());
} else if (this.type === CardType.STUDENT) {
this.studentStore.addOne(randStudent());
}
this.cardService.addOne(this.type);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component, Input } from '@angular/core';
import { CityStore } from '../../data-access/city.store';
import { StudentStore } from '../../data-access/student.store';
import { TeacherStore } from '../../data-access/teacher.store';
import { CardType } from '../../model/card.model';
Expand All @@ -23,13 +24,16 @@ export class ListItemComponent {
constructor(
private teacherStore: TeacherStore,
private studentStore: StudentStore,
private cityStore: CityStore,
) {}

delete(id: number) {
if (this.type === CardType.TEACHER) {
this.teacherStore.deleteOne(id);
} else if (this.type === CardType.STUDENT) {
this.studentStore.deleteOne(id);
} else if (this.type === CardType.CITY) {
this.cityStore.deleteOne(id);
}
}
}
Loading