Skip to content

Commit

Permalink
feat: updated router (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts committed Jun 15, 2020
1 parent e3ce5a5 commit 8cf5f73
Show file tree
Hide file tree
Showing 53 changed files with 2,814 additions and 703 deletions.
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/dist
/tmp
package-lock.json
package.json
yarn.lock
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ yarn
ng serve
```

Navigate to `http://localhost:4200/`.

Navigate to `http://localhost:4200/`.

## Build

```sh
ng build
```

28 changes: 7 additions & 21 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets",
"src/content"
],
"assets": ["src/favicon.ico", "src/assets", "src/content"],
"styles": [
"src/styles.scss",
"node_modules/prism-themes/themes/prism-atom-dark.css"
Expand All @@ -67,7 +63,7 @@
"src/assets",
"src/content",
"src/robots.txt"
],
],
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
Expand Down Expand Up @@ -126,22 +122,14 @@
"src/styles.scss"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
"assets": ["src/favicon.ico", "src/assets"]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
"tsConfig": ["src/tsconfig.app.json", "src/tsconfig.spec.json"],
"exclude": ["**/node_modules/**"]
}
}
}
Expand Down Expand Up @@ -179,13 +167,11 @@
"projects/router/tsconfig.lib.json",
"projects/router/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
"exclude": ["**/node_modules/**"]
}
}
}
}
},
"defaultProject": "blog"
}
}
6 changes: 1 addition & 5 deletions firebase.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"hosting": {
"public": "dist/blog",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"deploy": "yarn build:prod && firebase deploy --project brandonroberts-website"
"deploy": "yarn build:prod && firebase deploy --project brandonroberts-website",
"format": "prettier --write ."
},
"private": true,
"dependencies": {
Expand Down Expand Up @@ -39,7 +40,10 @@
"@angular/language-service": "~9.1.4",
"@types/node": "^12.11.1",
"codelyzer": "^5.1.2",
"firebase-tools": "^8.4.2",
"ng-packagr": "^9.0.0",
"prettier": "^2.0.5",
"query-string": "^6.13.1",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.8.3"
Expand Down
4 changes: 4 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
singleQuote: true,
trailingComma: 'es5',
};
3 changes: 2 additions & 1 deletion projects/router/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ This library was generated with [Angular CLI](https://github.com/angular/angular
## Code scaffolding

Run `ng generate component component-name --project router` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project router`.
> Note: Don't forget to add `--project router` or else it will be added to the default project in your `angular.json` file.

> Note: Don't forget to add `--project router` or else it will be added to the default project in your `angular.json` file.
## Build

Expand Down
8 changes: 4 additions & 4 deletions projects/router/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ module.exports = function (config) {
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular-devkit/build-angular/plugins/karma')
require('@angular-devkit/build-angular/plugins/karma'),
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
clearContext: false, // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../../coverage/router'),
reports: ['html', 'lcovonly', 'text-summary'],
fixWebpackSourcePaths: true
fixWebpackSourcePaths: true,
},
reporters: ['progress', 'kjhtml'],
port: 9876,
Expand All @@ -27,6 +27,6 @@ module.exports = function (config) {
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
restartOnFileChange: true
restartOnFileChange: true,
});
};
2 changes: 1 addition & 1 deletion projects/router/ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"lib": {
"entryFile": "src/public-api.ts"
}
}
}
5 changes: 4 additions & 1 deletion projects/router/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{
"name": "router",
"name": "@reactiveangular/router",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^9.1.4",
"@angular/core": "^9.1.4",
"tslib": "^1.10.0"
},
"dependencies": {
"path-to-regexp": "^6.1.0"
}
}
38 changes: 27 additions & 11 deletions projects/router/src/lib/link.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Directive, HostBinding, HostListener, Input, Output, EventEmitter } from '@angular/core';
import {
Directive,
HostBinding,
HostListener,
Input,
Output,
EventEmitter,
} from '@angular/core';
import { Router } from './router.service';
import { Params } from './route-params.service';

/**
* The LinkTo directive links to routes in your app
Expand All @@ -10,25 +18,31 @@ import { Router } from './router.service';
* <a linkTo="/home/page" [queryParams]="{ id: 123 }">Home Page</a>
* <a [linkTo]="'/pages' + page.id">Page 1</a>
*/
@Directive({ selector: '[linkTo]' })
@Directive({ selector: 'a[linkTo]' })
export class LinkTo {
@Input() target: string;
@HostBinding('href') linkHref;
@HostBinding('href') linkHref: string;

@Input() set linkTo(href: string){
@Input() set linkTo(href: string) {
this._href = href;
this._updateHref();
}

@Input() set queryParams(params: string) {
@Input() set queryParams(params: Params) {
this._query = params;
this._updateHref();
}

@Input() set fragment(hash: string) {
this._hash = hash;
this._updateHref();
}

@Output() hrefUpdated: EventEmitter<string> = new EventEmitter<string>();

private _href: string;
private _query: string;
private _query: Params;
private _hash: string;

constructor(private router: Router) {}

Expand All @@ -37,9 +51,9 @@ export class LinkTo {
* Prevents default action for non-combination click events without a target
*/
@HostListener('click', ['$event'])
onClick(event) {
onClick(event: any) {
if (!this._comboClick(event) && !this.target) {
this.router.go(this._href, this._query);
this.router.go(this._href, this._query, this._hash);

event.preventDefault();
}
Expand All @@ -48,7 +62,9 @@ export class LinkTo {
private _updateHref() {
let path = this._cleanUpHref(this._href);

this.linkHref = this.router.getExternalUrl(path);
let url = this.router.serializeUrl(path, this._query, this._hash);
this.linkHref = url;

this.hrefUpdated.emit(this.linkHref);
}

Expand All @@ -58,7 +74,7 @@ export class LinkTo {
private _comboClick(event) {
let buttonEvent = event.which || event.button;

return (buttonEvent > 1 || event.ctrlKey || event.metaKey || event.shiftKey);
return buttonEvent > 1 || event.ctrlKey || event.metaKey || event.shiftKey;
}

private _cleanUpHref(href: string = ''): string {
Expand All @@ -70,4 +86,4 @@ export class LinkTo {

return href;
}
}
}
8 changes: 8 additions & 0 deletions projects/router/src/lib/route-component.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Directive, Input } from '@angular/core';

@Directive({
selector: '[routeComponent]',
})
export class RouteComponentTemplate {
@Input() routeComponent: any;
}
4 changes: 3 additions & 1 deletion projects/router/src/lib/route-params.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export interface Params {
[param: string]: any;
}

export class RouteParams<T extends Params = Params> extends Observable<T> {}
export class RouteParams<T extends Params = Params> extends Observable<T> {}

export class QueryParams<T extends Params = Params> extends Observable<T> {}

0 comments on commit 8cf5f73

Please sign in to comment.