docs: add angular file table example

This commit is contained in:
Corbin Crutchley
2023-12-30 02:06:54 -07:00
parent 2dc9e28381
commit 2fcbab33f3
8 changed files with 13185 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
# Compiled output
/dist
/tmp
/out-tsc
/bazel-out
# Node
/node_modules
npm-debug.log
yarn-error.log
# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*
# Miscellaneous
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings
# System files
.DS_Store
Thumbs.db

View File

@@ -0,0 +1,71 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"DemoApp": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/demo-app",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": ["zone.js"],
"tsConfig": "tsconfig.app.json",
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "DemoApp:build:production"
},
"development": {
"browserTarget": "DemoApp:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "DemoApp:build"
}
}
}
}
}
}

View File

@@ -0,0 +1,31 @@
{
"name": "@ffg-fundamentals/angular-file-table-58",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"dev": "ng serve",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development"
},
"private": true,
"dependencies": {
"@angular/animations": "^16.2.0",
"@angular/common": "^16.2.0",
"@angular/compiler": "^16.2.0",
"@angular/core": "^16.2.0",
"@angular/forms": "^16.2.0",
"@angular/platform-browser": "^16.2.0",
"@angular/platform-browser-dynamic": "^16.2.0",
"@angular/router": "^16.2.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.13.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^16.2.10",
"@angular/cli": "^16.2.10",
"@angular/compiler-cli": "^16.2.0",
"typescript": "~5.1.3"
}
}

View File

@@ -0,0 +1,8 @@
<html>
<head>
<title>Angular File Table - Example #58 - FFG Fundamentals</title>
</head>
<body>
<app-root></app-root>
</body>
</html>

View File

@@ -0,0 +1,178 @@
import "zone.js";
import { bootstrapApplication } from "@angular/platform-browser";
import {
Component,
Input,
EventEmitter,
Output,
OnInit,
OnDestroy,
} from "@angular/core";
import { NgFor, NgIf, DatePipe } from "@angular/common";
@Component({
selector: "file-date",
standalone: true,
imports: [DatePipe],
template: `
<span [attr.aria-label]="inputDate | date: 'MMMM d, Y'">
{{ inputDate | date }}
</span>
`,
})
class FileDateComponent {
@Input() inputDate!: Date;
}
@Component({
selector: "tr[file-item]",
standalone: true,
imports: [FileDateComponent, NgIf],
host: {
"[attr.aria-selected]": "isSelected",
"(click)": "selected.emit()",
"[style]": `
isSelected ?
'background-color: blue; color: white' :
'background-color: white; color: blue'
`,
},
template: `
<td>
<a [href]="href" style="color: inherit">{{ fileName }}</a>
</td>
<td *ngIf="isFolder; else fileDisplay">Type: Folder</td>
<ng-template #fileDisplay><td>Type: File</td></ng-template>
<td><file-date *ngIf="!isFolder" [inputDate]="inputDate" /></td>
`,
})
class FileComponent implements OnInit, OnDestroy {
@Input() fileName!: string;
@Input() href!: string;
@Input() isSelected!: boolean;
@Input() isFolder!: boolean;
@Output() selected = new EventEmitter();
inputDate = new Date();
interval: any = null;
ngOnInit() {
// Check if it's a new day every 10 minutes
this.interval = setInterval(
() => {
const newDate = new Date();
if (this.inputDate.getDate() === newDate.getDate()) return;
this.inputDate = newDate;
},
10 * 60 * 1000,
);
}
ngOnDestroy() {
clearInterval(this.interval);
}
}
@Component({
selector: "tbody[file-table-body]",
standalone: true,
imports: [NgFor, NgIf, FileComponent],
template: `
<ng-container
*ngFor="let file of filesArray; let i = index; trackBy: fileTrackBy"
>
<tr
file-item
*ngIf="!file.isFolder"
(selected)="onSelected(i)"
[isSelected]="selectedIndex === i"
[fileName]="file.fileName"
[href]="file.href"
[isFolder]="file.isFolder"
></tr>
</ng-container>
`,
})
class FileTableBody {
selectedIndex = -1;
fileTrackBy(index: number, file: File) {
return file.id;
}
onSelected(idx: number) {
if (this.selectedIndex === idx) {
this.selectedIndex = -1;
return;
}
this.selectedIndex = idx;
}
onlyShowFiles = false;
toggleOnlyShow() {
this.onlyShowFiles = !this.onlyShowFiles;
}
filesArray: File[] = [
{
fileName: "File one",
href: "/file/file_one",
isFolder: false,
id: 1,
},
{
fileName: "File two",
href: "/file/file_two",
isFolder: false,
id: 2,
},
{
fileName: "File three",
href: "/file/file_three",
isFolder: false,
id: 3,
},
{
fileName: "Folder one",
href: "/file/folder_one/",
isFolder: true,
id: 4,
},
{
fileName: "Folder two",
href: "/file/folder_two/",
isFolder: true,
id: 5,
},
];
}
@Component({
selector: "file-table",
standalone: true,
imports: [NgFor, NgIf, FileTableBody],
template: `
<table style="border-collapse: collapse;">
<tbody file-table-body></tbody>
</table>
`,
})
class FileTableComponent {}
@Component({
selector: "app-root",
standalone: true,
imports: [FileTableComponent],
template: `<file-table />`,
})
class AppComponent {}
interface File {
fileName: string;
href: string;
isFolder: boolean;
id: number;
}
bootstrapApplication(AppComponent);

View File

@@ -0,0 +1,10 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"]
}

View File

@@ -0,0 +1,30 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "ES2022",
"module": "ES2022",
"useDefineForClassFields": false,
"lib": ["ES2022", "dom"]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}