Commit 729a3a63498ec49ce191fbc4d5348a980a3d4496

Authored by Evandro Junior
1 parent d003d3ce
Exists in master and in 1 other branch dev-fixes

migrated navbar for ng-forward syntax. added test for navbar

src/app/components/auth/index.ts
1   -/* Module Index Entry - generated using the script npm run generate-indexes */
  1 +/* Module Index Entry - generated using the script npm run generate-index */
2 2 export * from "./auth_controller";
3 3 export * from "./auth_events";
4 4 export * from "./auth_service";
... ...
src/app/components/navbar/index.ts
1 1 /* Module Index Entry - generated using the script npm run generate-index */
  2 +export * from "./navbar";
... ...
src/app/components/navbar/navbar.spec.ts 0 → 100644
... ... @@ -0,0 +1,56 @@
  1 +import {createComponentFromClass, quickCreateComponent} from "./../../../spec/helpers";
  2 +import {Navbar} from "./";
  3 +import {AUTH_EVENTS} from "./../auth";
  4 +import {User} from "./../../models/interfaces";
  5 +import {Injectable, Provider, provide} from "ng-forward";
  6 +
  7 +
  8 +
  9 +describe("Navbar Component", () => {
  10 +
  11 + let $rootScope: ng.IRootScopeService;
  12 + let user = <User>{
  13 + id: 1,
  14 + login: "user"
  15 + };
  16 +
  17 + beforeEach(angular.mock.module("templates"));
  18 +
  19 + // beforeEach(inject((_$rootScope_: ng.IRootScopeService) => {
  20 + // $rootScope = _$rootScope_;
  21 + // }));
  22 +
  23 + it('should get the loggedIn user', (done: Function) => {
  24 +
  25 +
  26 + let scope = jasmine.createSpyObj("scope", ["$on"]);
  27 +
  28 + let providers = [
  29 +
  30 +
  31 + new Provider('moment', { useValue: {} }),
  32 + new Provider('$modal', { useValue: {} }),
  33 + new Provider('AuthService', { useValue: {} }),
  34 + new Provider('Session', { useValue: {
  35 + getCurrentUser: () => { return user}
  36 + } }),
  37 + new Provider('$scope', { useValue: scope }),
  38 + new Provider('$state', { useValue: {} }),
  39 + new Provider('AUTH_EVENTS', { useValue: {AUTH_EVENTS} })
  40 + ];
  41 +
  42 +
  43 +
  44 + quickCreateComponent({
  45 + providers: providers,
  46 + template: "<acme-navbar></acme-navbar>",
  47 + directives: [Navbar]
  48 + }).then(fixture => {
  49 + let navbarInstance: Navbar = fixture.debugElement.componentViewChildren[0].componentInstance;
  50 + expect(navbarInstance).toBeDefined();
  51 + expect(navbarInstance["currentUser"]).toEqual(user)
  52 + done();
  53 + })
  54 + });
  55 +
  56 +});
0 57 \ No newline at end of file
... ...
src/app/components/navbar/navbar.ts 0 → 100644
... ... @@ -0,0 +1,68 @@
  1 +import {Component, Inject} from "ng-forward";
  2 +
  3 +
  4 +import {Session, AuthService, IAuthEvents} from "./../auth";
  5 +import {User} from "./../../models/interfaces";
  6 +
  7 +@Component({
  8 + selector: "acme-navbar",
  9 + templateUrl: "app/components/navbar/navbar.html"
  10 +})
  11 +@Inject("moment", "$modal", "AuthService", "Session", "$scope", "$state", "AUTH_EVENTS")
  12 +export class Navbar {
  13 +
  14 + private currentUser: User;
  15 + private modalInstance: any = null;
  16 + /**
  17 + *
  18 + */
  19 + constructor(
  20 + private moment: moment.MomentStatic,
  21 + private $modal: any,
  22 + private authService: AuthService,
  23 + private session: Session,
  24 + private $scope: ng.IScope,
  25 + private $state: ng.ui.IStateService,
  26 + private AUTH_EVENTS: IAuthEvents
  27 + ) {
  28 +
  29 + this.currentUser = session.getCurrentUser();
  30 +
  31 + $scope.$on(AUTH_EVENTS.loginSuccess, function() {
  32 + if (this.modalInstance) {
  33 + this.modalInstance.close();
  34 + this.modalInstance = null;
  35 + }
  36 +
  37 + this.$state.go(this.$state.current, {}, { reload: true }); //TODO move to auth
  38 + });
  39 +
  40 + $scope.$on(AUTH_EVENTS.logoutSuccess, () => {
  41 + this.currentUser = this.session.getCurrentUser();
  42 + });
  43 +
  44 + }
  45 +
  46 + openLogin() {
  47 + this.modalInstance = this.$modal.open({
  48 + templateUrl: 'app/components/auth/login.html',
  49 + controller: 'AuthController',
  50 + controllerAs: 'vm',
  51 + bindToController: true
  52 + });
  53 + };
  54 +
  55 + logout() {
  56 + this.authService.logout();
  57 + this.$state.go(this.$state.current, {}, { reload: true }); //TODO move to auth
  58 + };
  59 +
  60 +
  61 +
  62 + activate() {
  63 + if (!this.currentUser) {
  64 + this.openLogin();
  65 + }
  66 + }
  67 +
  68 +}
0 69 \ No newline at end of file
... ...
src/app/components/noosfero-blocks/link-list/index.ts
1 1 /* Module Index Entry - generated using the script npm run generate-index */
  2 +export * from "./link-list.component";
... ...
src/app/index.ts
... ... @@ -19,6 +19,8 @@ import {bootstrap, bundle} from &quot;ng-forward&quot;;
19 19 import {AUTH_EVENTS} from "./components/auth/auth_events";
20 20 import {AuthController} from "./components/auth/auth_controller";
21 21  
  22 +import {Navbar} from "./components/navbar";
  23 +
22 24 declare var moment: any;
23 25  
24 26 let noosferoApp: any = bundle("noosferoApp", Main, ["ngAnimate", "ngCookies", "ngStorage", "ngTouch",
... ... @@ -41,7 +43,6 @@ NoosferoApp.run(noosferoAngularRunBlock);
41 43 NoosferoApp.addController("AuthController", AuthController);
42 44  
43 45  
44   -require("./components/navbar/navbar.directive.js");
45 46 require("./components/noosfero-activities/activities.component.js");
46 47 require("./components/noosfero-activities/activity/activity.component.js");
47 48 require("./components/noosfero-blocks/main-block/main-block.component.js");
... ...
src/app/main/main.component.ts
... ... @@ -12,6 +12,8 @@ import {LinkListBlock} from &quot;../components/noosfero-blocks/link-list/link-list.c
12 12 import {AuthService} from "./../components/auth/auth_service";
13 13 import {Session} from "./../components/auth/session";
14 14  
  15 +import {Navbar} from "../components/navbar";
  16 +
15 17 @Component({
16 18 selector: 'main-content',
17 19 templateUrl: "app/main/main.html",
... ... @@ -24,7 +26,7 @@ export class MainContent {
24 26 @Component({
25 27 selector: 'main',
26 28 template: '<div ng-view></div>',
27   - directives: [NoosferoArticleBlog, ArticleView, Boxes, Block, LinkListBlock],
  29 + directives: [NoosferoArticleBlog, ArticleView, Boxes, Block, LinkListBlock, Navbar],
28 30 providers: [AuthService, Session]
29 31 })
30 32 @StateConfig([
... ...