diff --git a/src/app/components/auth/index.ts b/src/app/components/auth/index.ts index 0f76efa..39bfb1b 100644 --- a/src/app/components/auth/index.ts +++ b/src/app/components/auth/index.ts @@ -1,4 +1,4 @@ -/* Module Index Entry - generated using the script npm run generate-indexes */ +/* Module Index Entry - generated using the script npm run generate-index */ export * from "./auth_controller"; export * from "./auth_events"; export * from "./auth_service"; diff --git a/src/app/components/navbar/index.ts b/src/app/components/navbar/index.ts index 4206e70..dd56b10 100644 --- a/src/app/components/navbar/index.ts +++ b/src/app/components/navbar/index.ts @@ -1 +1,2 @@ /* Module Index Entry - generated using the script npm run generate-index */ +export * from "./navbar"; diff --git a/src/app/components/navbar/navbar.spec.ts b/src/app/components/navbar/navbar.spec.ts new file mode 100644 index 0000000..aa1c612 --- /dev/null +++ b/src/app/components/navbar/navbar.spec.ts @@ -0,0 +1,56 @@ +import {createComponentFromClass, quickCreateComponent} from "./../../../spec/helpers"; +import {Navbar} from "./"; +import {AUTH_EVENTS} from "./../auth"; +import {User} from "./../../models/interfaces"; +import {Injectable, Provider, provide} from "ng-forward"; + + + +describe("Navbar Component", () => { + + let $rootScope: ng.IRootScopeService; + let user = { + id: 1, + login: "user" + }; + + beforeEach(angular.mock.module("templates")); + + // beforeEach(inject((_$rootScope_: ng.IRootScopeService) => { + // $rootScope = _$rootScope_; + // })); + + it('should get the loggedIn user', (done: Function) => { + + + let scope = jasmine.createSpyObj("scope", ["$on"]); + + let providers = [ + + + new Provider('moment', { useValue: {} }), + new Provider('$modal', { useValue: {} }), + new Provider('AuthService', { useValue: {} }), + new Provider('Session', { useValue: { + getCurrentUser: () => { return user} + } }), + new Provider('$scope', { useValue: scope }), + new Provider('$state', { useValue: {} }), + new Provider('AUTH_EVENTS', { useValue: {AUTH_EVENTS} }) + ]; + + + + quickCreateComponent({ + providers: providers, + template: "", + directives: [Navbar] + }).then(fixture => { + let navbarInstance: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance; + expect(navbarInstance).toBeDefined(); + expect(navbarInstance["currentUser"]).toEqual(user) + done(); + }) + }); + +}); \ No newline at end of file diff --git a/src/app/components/navbar/navbar.ts b/src/app/components/navbar/navbar.ts new file mode 100644 index 0000000..fc4eadf --- /dev/null +++ b/src/app/components/navbar/navbar.ts @@ -0,0 +1,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.getCurrentUser(); + + $scope.$on(AUTH_EVENTS.loginSuccess, function() { + 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.getCurrentUser(); + }); + + } + + 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(); + } + } + +} \ No newline at end of file diff --git a/src/app/components/noosfero-blocks/link-list/index.ts b/src/app/components/noosfero-blocks/link-list/index.ts index 4206e70..765aba8 100644 --- a/src/app/components/noosfero-blocks/link-list/index.ts +++ b/src/app/components/noosfero-blocks/link-list/index.ts @@ -1 +1,2 @@ /* Module Index Entry - generated using the script npm run generate-index */ +export * from "./link-list.component"; diff --git a/src/app/index.ts b/src/app/index.ts index b3c5d62..ce1188b 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -19,6 +19,8 @@ import {bootstrap, bundle} from "ng-forward"; import {AUTH_EVENTS} from "./components/auth/auth_events"; import {AuthController} from "./components/auth/auth_controller"; +import {Navbar} from "./components/navbar"; + declare var moment: any; let noosferoApp: any = bundle("noosferoApp", Main, ["ngAnimate", "ngCookies", "ngStorage", "ngTouch", @@ -41,7 +43,6 @@ NoosferoApp.run(noosferoAngularRunBlock); NoosferoApp.addController("AuthController", AuthController); -require("./components/navbar/navbar.directive.js"); require("./components/noosfero-activities/activities.component.js"); require("./components/noosfero-activities/activity/activity.component.js"); require("./components/noosfero-blocks/main-block/main-block.component.js"); diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index 8f610ba..9e452e8 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -12,6 +12,8 @@ import {LinkListBlock} from "../components/noosfero-blocks/link-list/link-list.c import {AuthService} from "./../components/auth/auth_service"; import {Session} from "./../components/auth/session"; +import {Navbar} from "../components/navbar"; + @Component({ selector: 'main-content', templateUrl: "app/main/main.html", @@ -24,7 +26,7 @@ export class MainContent { @Component({ selector: 'main', template: '
', - directives: [NoosferoArticleBlog, ArticleView, Boxes, Block, LinkListBlock], + directives: [NoosferoArticleBlog, ArticleView, Boxes, Block, LinkListBlock, Navbar], providers: [AuthService, Session] }) @StateConfig([ -- libgit2 0.21.2