-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(directives): Added linkTo directive
- Loading branch information
1 parent
fc5d39e
commit 7703ba2
Showing
3 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
Directive, | ||
HostBinding, | ||
HostListener, | ||
Input, | ||
PLATFORM_DIRECTIVES, | ||
provide, | ||
Provider | ||
} from 'angular2/core'; | ||
import { Location } from './location'; | ||
|
||
/** | ||
* The LinkTo directive links to routes in your app | ||
* | ||
* Links are pushed to the `Location` service to trigger a route change. | ||
* Query params can be represented as an object or a string of names/values | ||
* | ||
* <a linkTo="/home/page" [queryParams]="{ id: 123 }">Home Page</a> | ||
* <a [linkTo]="'/pages' + page.id">Page 1</a> | ||
*/ | ||
@Directive({ selector: '[linkTo]' }) | ||
export class LinkTo { | ||
@Input() target: string; | ||
@HostBinding('attr.href') linkHref; | ||
|
||
@Input() set linkTo(href: string){ | ||
this._href = href; | ||
this._updateHref(); | ||
} | ||
|
||
@Input() set queryParams(params: string | Object) { | ||
this._query = params; | ||
this._updateHref(); | ||
} | ||
|
||
private _href: string; | ||
private _query: string | Object; | ||
|
||
constructor(private _location: Location) {} | ||
|
||
/** | ||
* Handles click events on the associated link | ||
* Prevents default action for non-combination click events without a target | ||
*/ | ||
@HostListener('click', ['$event']) | ||
onClick(event) { | ||
if (!this._comboClick(event) && !this.target) { | ||
this._location.go(this._href, this._query); | ||
|
||
event.preventDefault(); | ||
} | ||
} | ||
|
||
private _updateHref() { | ||
this.linkHref = this._location.prepareExternalUrl(this._href, this._query); | ||
} | ||
|
||
/** | ||
* Determines whether the click event happened with a combination of other keys | ||
*/ | ||
private _comboClick(event) { | ||
let buttonEvent = event.which || event.button; | ||
|
||
return (buttonEvent > 1 || event.ctrlKey || event.metaKey || event.shiftKey); | ||
} | ||
} | ||
|
||
export const LINK_TO_PROVIDERS = [ | ||
provide(PLATFORM_DIRECTIVES, { | ||
multi: true, | ||
useValue: [ LinkTo ] | ||
}) | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters