Skip to content

Commit 973924a

Browse files
euangoddardchristopherthielen
authored andcommitted
feat(uiSref): Add support for ctrl/middle-clicking on a uiSref generated URL (#175)
Closes #147
1 parent 1a026d2 commit 973924a

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/directives/uiSref.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @ng2api @module directives */
22
/** */
33
import { UIRouter, extend, Obj, TransitionOptions, TargetState } from "@uirouter/core";
4-
import { Directive, Inject, Input, Optional, ElementRef, Renderer2, OnChanges, SimpleChanges } from "@angular/core";
4+
import { Directive, Inject, Input, Optional, ElementRef, Renderer2, OnChanges, SimpleChanges, HostListener } from "@angular/core";
55
import { UIView, ParentUIViewInject } from "./uiView";
66
import { ReplaySubject } from "rxjs/ReplaySubject";
77
import { Subscription } from "rxjs/Subscription";
@@ -69,7 +69,6 @@ export class AnchorUISref {
6969
@Directive({
7070
selector: '[uiSref]',
7171
exportAs: 'uiSref',
72-
host: { '(click)': 'go()' }
7372
})
7473
export class UISref implements OnChanges {
7574

@@ -169,9 +168,10 @@ export class UISref implements OnChanges {
169168
}
170169

171170
/** When triggered by a (click) event, this function transitions to the UISref's target state */
172-
go() {
173-
if (this._anchorUISref && this._anchorUISref.openInNewTab()) {
174-
return ;
171+
@HostListener("click", ["$event.button", "$event.ctrlKey", "$event.metaKey"])
172+
go(button: number, ctrlKey: boolean, metaKey: boolean) {
173+
if (this._anchorUISref && (this._anchorUISref.openInNewTab() || button !== 0 || ctrlKey || metaKey)) {
174+
return;
175175
}
176176

177177
this._router.stateService.go(this.state, this.params, this.getOptions());

test/uiSref/uiSref.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('uiSref', () => {
104104

105105
describe('when clicked', () => {
106106
beforeEach(() => {
107-
des[0].triggerEventHandler('click', {});
107+
des[0].triggerEventHandler('click', {button: 0, metaKey: false, ctrlKey: false});
108108
});
109109

110110
it('should ignore the click event', () => {
@@ -121,7 +121,7 @@ describe('uiSref', () => {
121121

122122
describe('when clicked', () => {
123123
beforeEach(() => {
124-
des[0].triggerEventHandler('click', {});
124+
des[0].triggerEventHandler('click', {button: 0, metaKey: false, ctrlKey: false});
125125
});
126126

127127
it('should navigate to the state', () => {

test/uiSrefStatus/uiSrefStatus.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('uiSrefStatus', () => {
3939
describe('when click on `foo` uiSref', () => {
4040
beforeEach(async(() => {
4141
spyOn(component, 'updated');
42-
de.triggerEventHandler('click', {});
42+
de.triggerEventHandler('click', {button: 0, metaKey: false, ctrlKey: false});
4343
}));
4444

4545
it('should emit a event with a TargetState pointing to `foo`', () => {

0 commit comments

Comments
 (0)