diff --git a/components/builder-web/app/header/user-nav/UserNavComponent.ts b/components/builder-web/app/header/user-nav/UserNavComponent.ts index fcec61f586..b2bb21c016 100644 --- a/components/builder-web/app/header/user-nav/UserNavComponent.ts +++ b/components/builder-web/app/header/user-nav/UserNavComponent.ts @@ -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: `
`, }) -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(); + } + } +}