navbar.ts
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {Component, Inject} from "ng-forward";
import {Session, AuthService, IAuthEvents} from "./../auth";
import {User} from "./../../models/interfaces";
@Component({
selector: "acme-navbar",
templateUrl: "app/components/navbar/navbar.html"
})
@Inject("moment", "$modal", "AuthService", "Session", "$scope", "$state", "AUTH_EVENTS")
export class Navbar {
private currentUser: User;
private modalInstance: any = null;
/**
*
*/
constructor(
private moment: moment.MomentStatic,
private $modal: any,
private authService: AuthService,
private session: Session,
private $scope: ng.IScope,
private $state: ng.ui.IStateService,
private AUTH_EVENTS: IAuthEvents
) {
this.currentUser = session.currentUser();
$scope.$on(AUTH_EVENTS.loginSuccess, () => {
if (this.modalInstance) {
this.modalInstance.close();
this.modalInstance = null;
}
this.$state.go(this.$state.current, {}, { reload: true }); //TODO move to auth
});
$scope.$on(AUTH_EVENTS.logoutSuccess, () => {
this.currentUser = this.session.currentUser();
});
}
openLogin() {
this.modalInstance = this.$modal.open({
templateUrl: 'app/components/auth/login.html',
controller: 'AuthController',
controllerAs: 'vm',
bindToController: true
});
};
logout() {
this.authService.logout();
this.$state.go(this.$state.current, {}, { reload: true }); //TODO move to auth
};
activate() {
if (!this.currentUser) {
this.openLogin();
}
}
}