-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAngular-Material-Example
More file actions
363 lines (299 loc) · 10.4 KB
/
Angular-Material-Example
File metadata and controls
363 lines (299 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Angular Material Example
import { Component, Injectable, OnInit, NgModule, Pipe, PipeTransform, Directive, ElementRef, OnDestroy } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
import { MatTableModule, MatTableDataSource } from '@angular/material/table';
import { MatSortModule, MatSort } from '@angular/material/sort';
import { MatPaginatorModule, MatPaginator } from '@angular/material/paginator';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
import { DragDropModule, CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
import { MatStepperModule } from '@angular/material/stepper';
import { BreakpointObserver } from '@angular/cdk/layout';
import { HttpClient, HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable, BehaviorSubject, Subject, of } from 'rxjs';
import { takeUntil, tap } from 'rxjs/operators';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, Routes } from '@angular/router';
import { FormGroup, FormControl, Validators, ReactiveFormsModule } from '@angular/forms';
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { MatButtonHarness } from '@angular/material/button/testing';
import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
// --- Components ---
@Component({
selector: 'app-root',
standalone: true,
imports: [MatButtonModule, MatDialogModule],
template: `
<button mat-button color="primary" (click)="openDialog()">Open Dialog</button>
<button mat-button (click)="showSnackBar()">Show Snackbar</button>
<button mat-button (click)="checkToken()">Check Token</button>
`,
})
export class AppComponent {
constructor(private dialog: MatDialog, private snackBar: MatSnackBar) {}
openDialog() {
this.dialog.open(DialogComponent);
}
showSnackBar() {
this.snackBar.open('Message sent!', 'Close', { duration: 3000, panelClass: ['custom-snackbar'] });
}
checkToken() {
this.dialog.open(LoginDialogComponent);
}
}
@Component({
selector: 'app-dialog',
standalone: true,
imports: [MatButtonModule],
template: `
<h2 mat-dialog-title>Dialog</h2>
<button mat-button mat-dialog-close>Close</button>
`,
})
export class DialogComponent {}
@Component({
selector: 'app-table',
standalone: true,
imports: [MatTableModule, MatSortModule, MatPaginatorModule, MatFormFieldModule, MatInputModule],
template: `
<mat-form-field>
<mat-label>Filter</mat-label>
<input matInput (keyup)="applyFilter($event)" placeholder="Ex. Mia">
</mat-form-field>
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let element">{{element.name}}</td>
</ng-container>
<ng-container matColumnDef="email">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Email</th>
<td mat-cell *matCellDef="let element">{{element.email}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 20]" showFirstLastButtons></mat-paginator>
`,
})
export class TableComponent implements OnInit {
displayedColumns: string[] = ['name', 'email'];
dataSource = new MatTableDataSource<User>([
{ name: 'Mia', email: 'mia@example.com' },
{ name: 'John', email: 'john@example.com' },
]);
constructor() {}
ngOnInit() {
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
}
@ViewChild(MatSort) sort!: MatSort;
@ViewChild(MatPaginator) paginator!: MatPaginator;
applyFilter(event: Event) {
const filterValue = (event.target as HTMLInputElement).value;
this.dataSource.filter = filterValue.trim().toLowerCase();
}
}
interface User {
name: string;
email: string;
}
@Component({
selector: 'app-drag-drop',
standalone: true,
imports: [DragDropModule],
template: `
<div cdkDropList class="example-list" (cdkDropListDropped)="drop($event)">
<div class="example-box" *ngFor="let item of items" cdkDrag>{{item}}</div>
</div>
`,
})
export class DragDropComponent {
items = ['Item 1', 'Item 2', 'Item 3'];
drop(event: CdkDragDrop<string[]>) {
moveItemInArray(this.items, event.previousIndex, event.currentIndex);
}
}
@Component({
selector: 'app-stepper',
standalone: true,
imports: [MatStepperModule, MatButtonModule],
template: `
<mat-horizontal-stepper>
<mat-step label="Step 1">
<p>Step 1 content</p>
<button mat-button matStepperNext>Next</button>
</mat-step>
<mat-step label="Step 2">
<p>Step 2 content</p>
<button mat-button matStepperPrevious>Back</button>
<button mat-button matStepperNext>Next</button>
</mat-step>
<mat-step label="Step 3">
<p>Step 3 content</p>
<button mat-button matStepperPrevious>Back</button>
</mat-step>
</mat-horizontal-stepper>
`,
})
export class StepperComponent {}
@Component({
selector: 'app-responsive',
standalone: true,
imports: [MatCardModule],
template: `<mat-card [ngClass]="{'mobile': isMobile}">Responsive Content</mat-card>`,
})
export class ResponsiveComponent implements OnInit {
isMobile = false;
constructor(private breakpointObserver: BreakpointObserver) {}
ngOnInit() {
this.breakpointObserver.observe(['(max-width: 600px)']).subscribe(result => {
this.isMobile = result.matches;
});
}
}
@Component({
selector: 'app-theme-toggle',
standalone: true,
imports: [MatButtonModule], // Assuming MatSlideToggleModule for real toggle
template: `<button mat-button (change)="toggleDarkMode($event)">Dark Mode</button>`,
})
export class ThemeToggleComponent {
toggleDarkMode(isDark: any) { // Simplified for example
document.body.classList.toggle('dark-theme', isDark);
}
}
@Component({
selector: 'app-login-dialog',
standalone: true,
imports: [MatButtonModule, MatDialogModule],
template: `<h2 mat-dialog-title>Login Required</h2><button mat-button mat-dialog-close>Close</button>`,
})
export class LoginDialogComponent {}
// --- Services ---
@Injectable({ providedIn: 'root' })
export class AuthService {
private isLoggedInSubject = new BehaviorSubject<boolean>(false);
constructor(private http: HttpClient) {
this.isLoggedInSubject.next(!!this.getToken());
}
login(credentials: { username: string; password: string }): Observable<any> {
return this.http.post('/api/login', credentials).pipe(
tap((response: any) => {
this.storeToken(response.token);
this.isLoggedInSubject.next(true);
})
);
}
storeToken(token: string) {
localStorage.setItem('token', token);
}
getToken(): string | null {
return localStorage.getItem('token');
}
logout() {
localStorage.removeItem('token');
this.isLoggedInSubject.next(false);
}
hasRole(role: string): boolean {
const token = this.getToken();
if (token) {
const decoded = JSON.parse(atob(token.split('.')[1]));
return decoded.role === role;
}
return false;
}
get isLoggedIn$() {
return this.isLoggedInSubject.asObservable();
}
verifyOTP(otp: string): Observable<any> {
return of({ success: true }); // Mock for example
}
}
// --- Guards ---
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const role = next.data['role'];
const token = this.authService.getToken();
if (token) {
const userRole = this.extractRoleFromToken(token);
if (!role || userRole === role) return true;
}
this.router.navigate(['/login']);
return false;
}
private extractRoleFromToken(token: string): string {
const decodedToken = JSON.parse(atob(token.split('.')[1]));
return decodedToken.role;
}
}
// --- Interceptors ---
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private authService: AuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token = this.authService.getToken();
if (token) {
req = req.clone({ setHeaders: { Authorization: `Bearer ${token}` } });
}
return next.handle(req);
}
}
@Injectable()
export class CsrfInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const csrfToken = 'mock-csrf-token'; // Simulated
req = req.clone({ setHeaders: { 'X-CSRF-TOKEN': csrfToken } });
return next.handle(req);
}
}
// --- Lazy Loaded Module (Simulated) ---
@NgModule({
imports: [MatButtonModule],
declarations: [],
})
export class LazyLoadedModule {}
// --- Routing (Simulated) ---
const routes: Routes = [
{ path: 'lazy-loaded', loadChildren: () => Promise.resolve(LazyLoadedModule) },
{ path: 'dashboard', component: AppComponent, canActivate: [AuthGuard], data: { role: 'admin' } },
];
// --- Tests ---
describe('ButtonTest', () => {
let loader: HarnessLoader;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MatButtonModule],
}).compileComponents();
loader = TestbedHarnessEnvironment.loader();
});
it('should click button', async () => {
const button = await loader.getHarness(MatButtonHarness);
await button.click();
expect(true).toBe(true); // Placeholder assertion
});
});
// --- Subscription Management Example ---
@Component({
selector: 'app-subscription-example',
standalone: true,
imports: [],
template: `<p>Subscription Example</p>`,
})
export class SubscriptionExampleComponent implements OnInit, OnDestroy {
private destroy$ = new Subject<void>();
constructor(private authService: AuthService) {}
ngOnInit() {
this.authService.isLoggedIn$.pipe(takeUntil(this.destroy$)).subscribe(isLoggedIn => {
console.log('Logged in:', isLoggedIn);
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
}