Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[builder-web] Make it so clicking outside the user-nav dropdown makes… #985

Merged
merged 1 commit into from
Jun 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions components/builder-web/app/header/user-nav/UserNavComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {Component} from "angular2/core";
import {Component, ElementRef, Input} from "angular2/core";
import {RouterLink} from "angular2/router";

@Component({
directives: [RouterLink],
inputs: ["isOpen", "isSignedIn", "username", "avatarUrl", "signOut",
"toggleUserNavMenu"],
host: {
"(document:click)": "toggle($event)",
},
selector: "user-nav",
template: `
<div class="main-nav--cta" *ngIf="!isSignedIn">
<a class="button" [routerLink]="['SignIn']">Sign In</a>
</div>
<div class="main-nav--profile" *ngIf="isSignedIn">
<a class="main-nav--avatar" href="#" (click)="toggleUserNavMenu()">
<a class="main-nav--avatar" href="#">
<img height=24 src="{{avatarUrl}}" />
</a>
<ul class="main-nav--dropdown" *ngIf="isOpen">
Expand All @@ -37,4 +38,27 @@ import {RouterLink} from "angular2/router";
</div>`,
})

export class UserNavComponent { }
export class UserNavComponent {
@Input() isOpen: boolean;
@Input() isSignedIn: boolean;
@Input() username: string;
@Input() avatarUrl: string;
@Input() signOut: Function;
@Input() toggleUserNavMenu: Function;

constructor(private element: ElementRef) {}

// This will be triggered on *any* click on the document. Toggle the menu
// if it's already open and clicked on outide the dropdown, or if it's not
// open and we click on this component's elements (the thing that opens the
// dropdown)
//
// This makes it so the dropdown closes if you click somewhere you would
// expect would make it close.
private toggle(event) {
if ((this.isOpen && !event.target.closest(".main-nav--dropdown")) ||
(!this.isOpen && this.element.nativeElement.contains(event.target))) {
this.toggleUserNavMenu();
}
}
}