Commit 450a6b962b97443b7bf72d6d95c8b6ba6f5c769d

Authored by ABNER SILVA DE OLIVEIRA
2 parents f2b55101 6738dc36

merge with branch refactory-auth-service

.vscode/settings.json
... ... @@ -4,5 +4,7 @@
4 4 "**/.git": true,
5 5 "**/.DS_Store": true,
6 6 "src/app/*.js": false
7   - }
  7 + },
  8 + "editor.fontSize": 14,
  9 + "typescript.useCodeSnippetsOnMethodSuggest": true
8 10 }
9 11 \ No newline at end of file
... ...
src/app/cms/cms.component.ts
1 1 import {StateConfig, Component, Inject} from 'ng-forward';
2   -
  2 +import {Profile} from "./../models/interfaces";
3 3 @Component({
4 4 selector: 'cms',
5 5 templateUrl: "app/cms/cms.html"
... ... @@ -10,19 +10,19 @@ export class Cms {
10 10 article: any = {};
11 11 profile: any;
12 12  
13   - constructor(private noosfero, private $stateParams, private $httpParamSerializer, private $state, private SweetAlert) {
  13 + constructor(private noosfero: any/* TODO convert noosferoService */, private $stateParams: ng.ui.IStateParamsService, private $httpParamSerializer: any, private $state: ng.ui.IStateService, private SweetAlert: any) {
14 14  
15 15 }
16 16  
17 17 save() {
18   - this.noosfero.currentProfile.then((profile) => {
  18 + this.noosfero.currentProfile.then((profile: Profile) => {
19 19 return this.noosfero.profiles.one(profile.id).customPOST(
20 20 { article: this.article },
21 21 'articles',
22 22 {},
23 23 { 'Content-Type': 'application/json' }
24 24 )
25   - }).then((response) => {
  25 + }).then((response: restangular.IResponse) => {
26 26 this.$state.transitionTo('main.profile.page', { page: response.data.article.path, profile: response.data.article.profile.identifier });
27 27 this.SweetAlert.swal({
28 28 title: "Good job!",
... ...
src/app/components/auth/auth.controller.js
... ... @@ -1,17 +0,0 @@
1   -(function() {
2   - 'use strict';
3   -
4   - angular
5   - .module('noosferoApp')
6   - .controller('AuthController', AuthController);
7   -
8   -
9   - /** @ngInject */
10   - function AuthController(noosfero, $log, $stateParams, AuthService) {
11   - var vm = this;
12   - vm.credentials = {};
13   - vm.login = function() {
14   - AuthService.login(vm.credentials);
15   - }
16   - }
17   -})();
src/app/components/auth/auth.service.js
... ... @@ -1,88 +0,0 @@
1   -(function() {
2   - 'use strict';
3   -
4   - angular
5   - .module('noosferoApp')
6   - .factory('Session', Session)
7   - .factory('AuthService', AuthService);
8   -
9   - /** @ngInject */
10   - function AuthService($q, $http, $rootScope, Session, $log, AUTH_EVENTS) {
11   -
12   - function login (credentials) {
13   - var url = '/api/v1/login';
14   - var encodedData = 'login=' + credentials.username + '&password=' + credentials.password;
15   - return $http.post(url, encodedData).then(loginSuccessCallback, loginFailedCallback);
16   - }
17   -
18   - function loginFromCookie() {
19   - var url = '/api/v1/login_from_cookie';
20   - return $http.post(url).then(loginSuccessCallback, loginFailedCallback);
21   - }
22   -
23   - function loginSuccessCallback(response) {
24   - $log.debug('AuthService.login [SUCCESS] response', response);
25   - var currentUser = Session.create(response.data);
26   - $rootScope.currentUser = currentUser;
27   - $rootScope.$broadcast(AUTH_EVENTS.loginSuccess, currentUser);
28   - return currentUser;
29   - }
30   -
31   - function loginFailedCallback(response) {
32   - $log.debug('AuthService.login [FAIL] response', response);
33   - $rootScope.$broadcast(AUTH_EVENTS.loginFailed);
34   - // return $q.reject(response);
35   - return null;
36   - }
37   -
38   - function logout () {
39   - Session.destroy();
40   - $rootScope.currentUser = undefined;
41   - $rootScope.$broadcast(AUTH_EVENTS.logoutSuccess);
42   - $http.jsonp('/account/logout'); //FIXME logout from noosfero to sync login state
43   - }
44   -
45   - function isAuthenticated () {
46   - return !!Session.userId;
47   - }
48   -
49   - function isAuthorized (authorizedRoles) {
50   - if (!angular.isArray(authorizedRoles)) {
51   - authorizedRoles = [authorizedRoles];
52   - }
53   - return (service.isAuthenticated() && authorizedRoles.indexOf(Session.userRole) !== -1);
54   - }
55   -
56   - var service = {
57   - login: login,
58   - loginFromCookie: loginFromCookie,
59   - logout: logout,
60   - isAuthenticated: isAuthenticated,
61   - isAuthorized: isAuthorized
62   - };
63   - return service;
64   - }
65   -
66   - /** @ngInject */
67   - function Session($localStorage, $log) {
68   - var service = {};
69   -
70   - service.create = function(data) {
71   - $localStorage.currentUser = data.user;
72   - $log.debug('User session created.', $localStorage.currentUser);
73   - return $localStorage.currentUser;
74   - };
75   -
76   - service.destroy = function() {
77   - delete $localStorage.currentUser;
78   - $log.debug('User session destroyed.');
79   - };
80   -
81   - service.getCurrentUser = function () {
82   - return $localStorage.currentUser;
83   - };
84   -
85   - return service;
86   - }
87   -
88   -})();
src/app/components/auth/auth_controller.ts 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +import {Credentials} from "./../../models/interfaces";
  2 +import {AuthService} from "./auth_service";
  3 +
  4 +export class AuthController {
  5 +
  6 + static $inject = ["noosfero", "$log", "$stateParams", "AuthService"];
  7 +
  8 + constructor(
  9 + private noosfero: any,
  10 + private $log: ng.ILogService,
  11 + private $stateParams: any,
  12 + private AuthService: AuthService
  13 + ) {
  14 +
  15 + }
  16 +
  17 + credentials: Credentials;
  18 +
  19 + login() {
  20 + this.AuthService.login(this.credentials);
  21 + }
  22 +}
0 23 \ No newline at end of file
... ...
src/app/components/auth/auth_events.ts 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +export interface IAuthEvents {
  2 + loginSuccess: string;
  3 + loginFailed: string;
  4 + logoutSuccess: string;
  5 +}
  6 +
  7 +export const AUTH_EVENTS: IAuthEvents = {
  8 + loginSuccess: "auth-login-success",
  9 + loginFailed: "auth-login-failed",
  10 + logoutSuccess: "auth-logout-success"
  11 +};
0 12 \ No newline at end of file
... ...
src/app/components/auth/auth_service.ts 0 → 100644
... ... @@ -0,0 +1,65 @@
  1 +import {Injectable, Inject} from "ng-forward";
  2 +
  3 +import {Credentials, NoosferoRootScope, User, UserResponse} from "./../../models/interfaces";
  4 +import {Session} from "./session";
  5 +
  6 +import {AUTH_EVENTS, IAuthEvents} from "./auth_events";
  7 +
  8 +@Injectable()
  9 +@Inject("$q", "$http", "$rootScope", "Session", "$log", "AUTH_EVENTS")
  10 +export class AuthService {
  11 +
  12 + constructor(private $q: ng.IQService,
  13 + private $http: ng.IHttpService,
  14 + private $rootScope: NoosferoRootScope,
  15 + private session: Session,
  16 + private $log: ng.ILogService,
  17 + private auth_events: IAuthEvents) {
  18 +
  19 + }
  20 +
  21 + loginFromCookie() {
  22 + let url: string = '/api/v1/login_from_cookie';
  23 + return this.$http.post(url, null).then(this.loginSuccessCallback.bind(this), this.loginFailedCallback.bind(this));
  24 + }
  25 +
  26 +
  27 + private loginSuccessCallback(response: ng.IHttpPromiseCallbackArg<UserResponse>) {
  28 + this.$log.debug('AuthService.login [SUCCESS] response', response);
  29 + let currentUser: User = this.session.create(response.data);
  30 + this.$rootScope.currentUser = currentUser;
  31 + this.$rootScope.$broadcast(this.auth_events.loginSuccess, currentUser);
  32 + return currentUser;
  33 + }
  34 +
  35 + login(credentials: Credentials) {
  36 + let url = '/api/v1/login';
  37 + let encodedData = 'login=' + credentials.username + '&password=' + credentials.password;
  38 + return this.$http.post(url, encodedData).then(this.loginSuccessCallback.bind(this), this.loginFailedCallback.bind(this));
  39 + }
  40 +
  41 + private loginFailedCallback(response: ng.IHttpPromiseCallbackArg<any>): any {
  42 + this.$log.debug('AuthService.login [FAIL] response', response);
  43 + this.$rootScope.$broadcast(this.auth_events.loginFailed);
  44 + // return $q.reject(response);
  45 + return null;
  46 + }
  47 +
  48 + public logout() {
  49 + this.session.destroy();
  50 + this.$rootScope.currentUser = undefined;
  51 + this.$rootScope.$broadcast(this.auth_events.logoutSuccess);
  52 + this.$http.jsonp('/account/logout'); // FIXME logout from noosfero to sync login state
  53 + }
  54 +
  55 + public isAuthenticated() {
  56 + return !!this.session.getCurrentUser();
  57 + }
  58 +
  59 + public isAuthorized(authorizedRoles: string | string[]) {
  60 + if (!angular.isArray(authorizedRoles)) {
  61 + authorizedRoles = [<string>authorizedRoles];
  62 + }
  63 + return (this.isAuthenticated() && authorizedRoles.indexOf(this.session.getCurrentUser().userRole) !== -1);
  64 + }
  65 +}
0 66 \ No newline at end of file
... ...
src/app/components/auth/session.ts 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +import {Injectable, Inject} from "ng-forward";
  2 +import {UserResponse, User, INoosferoLocalStorage} from "./../../models/interfaces";
  3 +
  4 +@Injectable()
  5 +@Inject("$localStorage", "$log")
  6 +export class Session {
  7 +
  8 + constructor(private $localStorage: INoosferoLocalStorage, private $log: ng.ILogService) {
  9 +
  10 + }
  11 +
  12 + create(data: UserResponse): User {
  13 + this.$localStorage.currentUser = data.user;
  14 + this.$log.debug('User session created.', this.$localStorage.currentUser);
  15 + return this.$localStorage.currentUser;
  16 + };
  17 +
  18 + destroy() {
  19 + delete this.$localStorage.currentUser;
  20 + this.$log.debug('User session destroyed.');
  21 + };
  22 +
  23 + getCurrentUser(): User {
  24 + return this.$localStorage.currentUser;
  25 + };
  26 +
  27 +}
0 28 \ No newline at end of file
... ...
src/app/components/noosfero-articles/article/article.component.spec.ts
... ... @@ -1,81 +0,0 @@
1   -import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder';
2   -import {Input, provide, Component} from 'ng-forward';
3   -
4   -import {ArticleComponent} from './article.component';
5   -
6   -// Instantiate the Builder, this part is different than ng2.
7   -// In ng2 you inject tcb.
8   -const tcb = new TestComponentBuilder();
9   -
10   -// this htmlTemplate will be re-used between the container components in this spec file
11   -const htmlTemplate: string = '<noosfero-article [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-article>';
12   -
13   -
14   -describe("Article Component", () => {
15   -
16   - // the karma preprocessor html2js transform the templates html into js files which put
17   - // the templates to the templateCache into the module templates
18   - // we need to load the module templates here as the template for the
19   - // component NoosferoArtileComponent will be load on our tests
20   - beforeEach(angular.mock.module("templates"));
21   -
22   - it("receives the article and profile as inputs", done => {
23   -
24   - // Creating a container component (ArticleContainerComponent) to include
25   - // the component under test (ArticleComponent)
26   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleComponent] })
27   - class ArticleContainerComponent {
28   - article = { type: 'anyArticleType' };
29   - profile = { name: 'profile-name' };
30   - constructor() {
31   - }
32   - }
33   -
34   - // uses the TestComponentBuilder instance to initialize the component
35   - tcb
36   - .createAsync(ArticleContainerComponent).then(fixture => {
37   - // and here we can inspect and run the test assertions
38   - let myComponent: ArticleComponent = fixture.componentInstance;
39   -
40   - // assure the article object inside the ArticleComponent matches
41   - // the provided through the parent component
42   - expect(myComponent.article.type).toEqual("anyArticleType");
43   - expect(myComponent.profile.name).toEqual("profile-name");
44   -
45   - // done needs to be called (it isn't really needed, as we can read in
46   - // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync)
47   - // because createAsync in ng-forward is not really async, but as the intention
48   - // here is write tests in angular 2 ways, this is recommended
49   - done();
50   - });
51   - });
52   -
53   -
54   - it("renders a component which matches to the article type", done => {
55   - // NoosferoTinyMceArticle component created to check if it will be used
56   - // when a article with type 'TinyMceArticle' is provided to the noosfero-article (ArticleComponent)
57   - // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider
58   - @Component({ selector: 'noosfero-tiny-mce-article', template: "<h1>TinyMceArticle</h1>" })
59   - class NoosferoTinyMceArticle {
60   - @Input() article: any;
61   - @Input() profile: any;
62   - }
63   -
64   - // Creating a container component (ArticleContainerComponent) to include our NoosferoTinyMceArticle
65   - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleComponent, NoosferoTinyMceArticle] })
66   - class CustomArticleType {
67   - article = { type: 'TinyMceArticle' };
68   - profile = { name: 'profile-name' };
69   - constructor() {
70   - }
71   - }
72   - tcb
73   - .createAsync(CustomArticleType).then(fixture => {
74   - let myComponent: CustomArticleType = fixture.componentInstance;
75   - expect(myComponent.article.type).toEqual("TinyMceArticle");
76   - expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle");
77   - done();
78   - });
79   - });
80   -
81   -});
src/app/components/noosfero-articles/article/article.component.ts
... ... @@ -1,26 +0,0 @@
1   -import { bundle, Input, Inject, Component, Directive } from 'ng-forward';
2   -import {NoosferoArticleBlog} from "../blog/blog.component";
3   -
4   -@Component({
5   - selector: 'noosfero-article',
6   - templateUrl: 'app/components/noosfero-articles/article/article.html',
7   - directives: [NoosferoArticleBlog]
8   -})
9   -@Inject("$element", "$scope", "$injector", "$compile")
10   -export class ArticleComponent {
11   -
12   - @Input() article: any;
13   - @Input() profile: any;
14   -
15   - ngOnInit() {
16   - let specificDirective = 'noosfero' + this.article.type;
17   - if (this.$injector.has(specificDirective + 'Directive')) {
18   - let directiveName = specificDirective.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
19   - this.$element.replaceWith(this.$compile('<' + directiveName + ' [article]="ctrl.article" [profile]="ctrl.profile"></' + directiveName + '>')(this.$scope));
20   - }
21   - }
22   -
23   - constructor(private $element: any, private $scope: ng.IScope, private $injector: ng.auto.IInjectorService, private $compile: ng.ICompileService) {
24   -
25   - }
26   -}
src/app/components/noosfero-articles/article/article_view.spec.ts 0 → 100644
... ... @@ -0,0 +1,108 @@
  1 +import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder';
  2 +import {Input, provide, Component} from 'ng-forward';
  3 +
  4 +import {ArticleView, ArticleDefaultView} from './article_view';
  5 +
  6 +// Instantiate the Builder, this part is different than ng2.
  7 +// In ng2 you inject tcb.
  8 +const tcb = new TestComponentBuilder();
  9 +
  10 +// this htmlTemplate will be re-used between the container components in this spec file
  11 +const htmlTemplate: string = '<noosfero-article [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-article>';
  12 +
  13 +
  14 +describe("ArticleView Component", () => {
  15 +
  16 + // the karma preprocessor html2js transform the templates html into js files which put
  17 + // the templates to the templateCache into the module templates
  18 + // we need to load the module templates here as the template for the
  19 + // component Noosfero ArtileView will be load on our tests
  20 + beforeEach(angular.mock.module("templates"));
  21 +
  22 + it("renders the default component when no specific component is found", (done: Function) => {
  23 + // Creating a container component (ArticleContainerComponent) to include
  24 + // the component under test (ArticleView)
  25 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] })
  26 + class ArticleContainerComponent {
  27 + article = { type: 'anyArticleType' };
  28 + profile = { name: 'profile-name' };
  29 + constructor() {
  30 + }
  31 + }
  32 +
  33 + // uses the TestComponentBuilder instance to initialize the component
  34 + tcb.createAsync(ArticleContainerComponent).then((fixture) => {
  35 + // and here we can inspect and run the test assertions
  36 +
  37 + // gets the children component of ArticleContainerComponent
  38 + let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance;
  39 +
  40 + // and checks if the article View rendered was the Default Article View
  41 + expect(articleView.constructor.prototype).toEqual(ArticleDefaultView.prototype);
  42 +
  43 + // done needs to be called (it isn't really needed, as we can read in
  44 + // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync)
  45 + // because createAsync in ng-forward is not really async, but as the intention
  46 + // here is write tests in angular 2 ways, this is recommended
  47 + done();
  48 + });
  49 + });
  50 +
  51 + it("receives the article and profile as inputs", (done: Function) => {
  52 +
  53 + // Creating a container component (ArticleContainerComponent) to include
  54 + // the component under test (ArticleView)
  55 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] })
  56 + class ArticleContainerComponent {
  57 + article = { type: 'anyArticleType' };
  58 + profile = { name: 'profile-name' };
  59 + constructor() {
  60 + }
  61 + }
  62 +
  63 + // uses the TestComponentBuilder instance to initialize the component
  64 + tcb.createAsync(ArticleContainerComponent).then((fixture) => {
  65 + // and here we can inspect and run the test assertions
  66 + let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance;
  67 +
  68 + // assure the article object inside the ArticleView matches
  69 + // the provided through the parent component
  70 + expect(articleView.article.type).toEqual("anyArticleType");
  71 + expect(articleView.profile.name).toEqual("profile-name");
  72 +
  73 + // done needs to be called (it isn't really needed, as we can read in
  74 + // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync)
  75 + // because createAsync in ng-forward is not really async, but as the intention
  76 + // here is write tests in angular 2 ways, this is recommended
  77 + done();
  78 + });
  79 + });
  80 +
  81 +
  82 + it("renders a article view which matches to the article type", done => {
  83 + // NoosferoTinyMceArticle component created to check if it will be used
  84 + // when a article with type 'TinyMceArticle' is provided to the noosfero-article (ArticleView)
  85 + // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider
  86 + @Component({ selector: 'noosfero-tiny-mce-article', template: "<h1>TinyMceArticle</h1>" })
  87 + class TinyMceArticleView {
  88 + @Input() article: any;
  89 + @Input() profile: any;
  90 + }
  91 +
  92 + // Creating a container component (ArticleContainerComponent) to include our NoosferoTinyMceArticle
  93 + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView, TinyMceArticleView] })
  94 + class CustomArticleType {
  95 + article = { type: 'TinyMceArticle' };
  96 + profile = { name: 'profile-name' };
  97 + constructor() {
  98 + }
  99 + }
  100 + tcb.createAsync(CustomArticleType).then(fixture => {
  101 + let myComponent: CustomArticleType = fixture.componentInstance;
  102 + expect(myComponent.article.type).toEqual("TinyMceArticle");
  103 + expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle");
  104 + done();
  105 + });
  106 + });
  107 +
  108 +});
0 109 \ No newline at end of file
... ...
src/app/components/noosfero-articles/article/article_view.ts 0 → 100644
... ... @@ -0,0 +1,43 @@
  1 +import { bundle, Input, Inject, Component, Directive } from 'ng-forward';
  2 +import {NoosferoArticleBlog} from "../blog/blog.component";
  3 +
  4 +@Component({
  5 + selector: 'noosfero-default-article',
  6 + templateUrl: 'app/components/noosfero-articles/article/article.html'
  7 +})
  8 +export class ArticleDefaultView {
  9 +
  10 + @Input() article: any;
  11 + @Input() profile: any;
  12 +
  13 +}
  14 +
  15 +@Component({
  16 + selector: 'noosfero-article',
  17 + template: 'not-used',
  18 + directives: [ArticleDefaultView, NoosferoArticleBlog]
  19 +})
  20 +@Inject("$element", "$scope", "$injector", "$compile")
  21 +export class ArticleView {
  22 +
  23 + @Input() article: any;
  24 + @Input() profile: any;
  25 + directiveName: string;
  26 +
  27 + ngOnInit() {
  28 + let specificDirective = 'noosfero' + this.article.type;
  29 + this.directiveName = "noosfero-default-article";
  30 + if (this.$injector.has(specificDirective + 'Directive')) {
  31 + this.directiveName = specificDirective.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
  32 + }
  33 + this.$element.replaceWith(this.$compile('<' + this.directiveName + ' [article]="ctrl.article" [profile]="ctrl.profile"></' + this.directiveName + '>')(this.$scope));
  34 + }
  35 +
  36 + constructor(
  37 + private $element: any,
  38 + private $scope: ng.IScope,
  39 + private $injector: ng.auto.IInjectorService,
  40 + private $compile: ng.ICompileService) {
  41 +
  42 + }
  43 +}
... ...
src/app/components/noosfero-articles/blog/blog.component.ts
1 1 import {Component, Input, Inject} from "ng-forward";
2 2  
  3 +import {Article, Profile} from "./../../../models/interfaces";
3 4  
4 5 @Component({
5 6 selector: "noosfero-blog",
... ... @@ -8,15 +9,15 @@ import {Component, Input, Inject} from &quot;ng-forward&quot;;
8 9 @Inject("noosfero", "$scope")
9 10 export class NoosferoArticleBlog {
10 11  
11   - @Input() article;
12   - @Input() profile;
  12 + @Input() article: Article;
  13 + @Input() profile: Profile;
13 14  
14 15 private posts: any[];
15 16 private perPage: number = 3;
16 17 private currentPage: number;
17 18 private totalPosts: number = 0;
18 19  
19   - constructor(private noosfero: any, private $scope) {
  20 + constructor(private noosfero: any, private $scope: ng.IScope) {
20 21 }
21 22  
22 23 ngOnInit() {
... ... @@ -28,8 +29,8 @@ export class NoosferoArticleBlog {
28 29 content_type: "TinyMceArticle",
29 30 per_page: this.perPage,
30 31 page: this.currentPage
31   - }).then((response) => {
32   - this.totalPosts = response.headers("total");
  32 + }).then((response: restangular.IResponse) => {
  33 + this.totalPosts = <number>(<any>response.headers("total"));
33 34 this.posts = response.data.articles;
34 35 });
35 36 }
... ...
src/app/components/noosfero-blocks/block.component.spec.ts
... ... @@ -73,8 +73,8 @@ describe(&quot;Block Component&quot;, () =&gt; {
73 73 it("renders the default block when hasn't defined a block type", done => {
74 74 @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [Block] })
75 75 class CustomBlockType {
76   - block = { type: null };
77   - owner = { name: 'profile-name' };
  76 + block: any = { type: null };
  77 + owner: any = { name: 'profile-name' };
78 78 constructor() {
79 79 }
80 80 }
... ...
src/app/components/noosfero-boxes/boxes.component.ts
1 1 import { Input, Inject, Component } from 'ng-forward';
2   -
  2 +import {Box} from "./../../models/interfaces";
3 3 @Component({
4 4 selector: "noosfero-boxes",
5 5 templateUrl: "app/components/noosfero-boxes/boxes.html"
6 6 })
7 7 export class Boxes {
8 8  
9   - @Input() boxes: any
10   - @Input() owner: any
  9 + @Input() boxes: any;
  10 + @Input() owner: any;
11 11  
12   - boxesOrder(box) {
13   - if (box.position == 2) return 0;
  12 + boxesOrder(box: Box) {
  13 + if (box.position === 2) return 0;
14 14 return box.position;
15 15 }
16 16 }
... ...
src/app/content-viewer/content-viewer-actions.component.ts
1 1 import {Component, Inject} from "ng-forward";
2 2  
  3 +import {Profile} from "./../models/interfaces";
3 4 @Component({
4 5 selector: "content-viewer-actions",
5 6 templateUrl: "app/content-viewer/navbar-actions.html",
... ... @@ -10,8 +11,8 @@ export class ContentViewerActions {
10 11 article: any;
11 12 profile: any;
12 13  
13   - constructor(noosfero) {
14   - noosfero.currentProfile.then((profile) => {
  14 + constructor(noosfero: any) {
  15 + noosfero.currentProfile.then((profile: Profile) => {
15 16 this.profile = profile;
16 17 });
17 18 }
... ...
src/app/content-viewer/content-viewer.component.ts
1 1 import * as noosfero from "../models/interfaces";
2 2  
3   -import {ArticleComponent} from "../components/noosfero-articles/article/article.component";
  3 +
  4 +import {ArticleView} from "../components/noosfero-articles/article/article_view";
4 5 import {Input, Component, StateConfig, Inject} from "ng-forward";
5 6  
6 7 import {NoosferoArticleBlog} from "./../components/noosfero-articles/blog/blog.component";
... ... @@ -8,7 +9,7 @@ import {NoosferoArticleBlog} from &quot;./../components/noosfero-articles/blog/blog.c
8 9 @Component({
9 10 selector: "content-viewer",
10 11 templateUrl: "app/content-viewer/page.html",
11   - directives: [NoosferoArticleBlog, ArticleComponent]
  12 + directives: [NoosferoArticleBlog, ArticleView]
12 13 })
13 14 @Inject("noosfero", "$log", "$stateParams")
14 15 export class ContentViewer {
... ... @@ -24,11 +25,10 @@ export class ContentViewer {
24 25 }
25 26  
26 27 activate() {
27   - console.log("HERE CONTENT VIEWER");
28   - this.noosfero.currentProfile.then((profile) => {
  28 + this.noosfero.currentProfile.then((profile: noosfero.Profile) => {
29 29 this.profile = profile;
30 30 return this.noosfero.profiles.one(this.profile.id).one("articles").get({ path: this.$stateParams["page"] });
31   - }).then((response) => {
  31 + }).then((response: restangular.IResponse) => {
32 32 this.article = response.data.article;
33 33 });
34 34 }
... ...
src/app/index.config.ts
1 1  
2 2 /** @ngInject */
3   -export function noosferoModuleConfig($logProvider, $locationProvider, RestangularProvider, $httpProvider, $provide) {
  3 +export function noosferoModuleConfig($logProvider: ng.ILogProvider, $locationProvider: ng.ILocationProvider, RestangularProvider: restangular.IProvider, $httpProvider: ng.IHttpProvider, $provide: ng.auto.IProvideService) {
4 4 $logProvider.debugEnabled(true);
5 5 $locationProvider.html5Mode({ enabled: true });
6 6 RestangularProvider.setBaseUrl("/api/v1");
7 7 RestangularProvider.setFullResponse(true);
8   - $httpProvider.defaults.headers.post = { "Content-Type": "application/x-www-form-urlencoded; charset=utf-8" };
9 8  
10   - $provide.decorator("$uiViewScroll", function($delegate, $document) {
11   - return function(uiViewElement) {
  9 + (<any>$httpProvider.defaults.headers.post)["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8";
  10 +
  11 +
  12 + $provide.decorator("$uiViewScroll", function($delegate: any, $document: any) {
  13 + return function(uiViewElement: any) {
12 14 $document.scrollToElementAnimated(uiViewElement);
13 15 };
14 16 });
... ...
src/app/index.module.ts
... ... @@ -2,14 +2,14 @@ export class NoosferoApp {
2 2  
3 3 static appName: string = "noosferoApp";
4 4 static angularModule: any;
5   - //static init(angularModule: any) {
6   - //NoosferoApp.angularModule
  5 + // static init(angularModule: any) {
  6 + // NoosferoApp.angularModule
7 7 // angular.module(NoosferoApp.appName, ["ngAnimate", "ngCookies", "ngStorage", "ngTouch",
8 8 // "ngSanitize", "ngMessages", "ngAria", "restangular",
9 9 // "ui.router", "ui.bootstrap", "toastr",
10 10 // "angularMoment", "angular.filter", "akoenig.deckgrid",
11 11 // "angular-timeline", "duScroll", "oitozero.ngSweetAlert"]);
12   - //}
  12 + // }
13 13  
14 14 static addConfig(configFunc: Function) {
15 15 NoosferoApp.angularModule.config(configFunc);
... ... @@ -23,6 +23,10 @@ export class NoosferoApp {
23 23 NoosferoApp.angularModule.service(serviceName, value);
24 24 }
25 25  
  26 + static addFactory(factoryName: string, value: any) {
  27 + NoosferoApp.angularModule.factory(factoryName, value);
  28 + }
  29 +
26 30 static addController(controllerName: string, value: any) {
27 31 NoosferoApp.angularModule.controller(controllerName, value);
28 32 }
... ...
src/app/index.route.ts
1 1  
2 2  
3 3 /** @ngInject */
4   -export function routeConfig($stateProvider, $urlRouterProvider) {
  4 +export function routeConfig($stateProvider: ng.ui.IStateProvider, $urlRouterProvider: ng.ui.IUrlRouterProvider) {
5 5 $stateProvider
6 6 .state("main.profile.settings", {
7 7 url: "^/myprofile/:profile"
... ...
src/app/index.run.ts
  1 +import {Session} from "./components/auth/session";
1 2  
2 3 /** @ngInject */
3   -export function noosferoAngularRunBlock($log, Restangular, Session) {
4   - Restangular.addFullRequestInterceptor(function(element, operation, route, url, headers) {
  4 +export function noosferoAngularRunBlock($log: ng.ILogService, Restangular: restangular.IService, Session: Session) {
  5 + Restangular.addFullRequestInterceptor((element: any, operation: string, route: string, url: string, headers: string) => {
5 6 if (Session.getCurrentUser()) {
6   - headers["Private-Token"] = Session.getCurrentUser().private_token;
  7 + (<any>headers)["Private-Token"] = Session.getCurrentUser().private_token;
7 8 }
8   - return { headers: headers };
  9 + return <any>{ headers: <any>headers };
9 10 });
10 11 }
11 12  
... ...
src/app/index.ts
... ... @@ -16,7 +16,8 @@ import {Cms as noosferoCms} from &quot;./cms/cms.component&quot;;
16 16 import {Main} from "./main/main.component";
17 17 import {bootstrap, bundle} from "ng-forward";
18 18  
19   -
  19 +import {AUTH_EVENTS} from "./components/auth/auth_events";
  20 +import {AuthController} from "./components/auth/auth_controller";
20 21  
21 22 declare var moment: any;
22 23  
... ... @@ -28,18 +29,18 @@ let noosferoApp: any = bundle(&quot;noosferoApp&quot;, Main, [&quot;ngAnimate&quot;, &quot;ngCookies&quot;, &quot;n
28 29  
29 30 NoosferoApp.angularModule = noosferoApp;
30 31  
  32 +
31 33 NoosferoApp.addConstants("moment", moment);
32   -NoosferoApp.addConstants("AUTH_EVENTS", {
33   - loginSuccess: "auth-login-success",
34   - loginFailed: "auth-login-failed",
35   - logoutSuccess: "auth-logout-success"
36   -});
  34 +NoosferoApp.addConstants("AUTH_EVENTS", AUTH_EVENTS);
37 35  
38 36 NoosferoApp.addConfig(noosferoModuleConfig);
39 37 NoosferoApp.run(noosferoAngularRunBlock);
40 38  
41   -require("./components/auth/auth.controller.js");
42   -require("./components/auth/auth.service.js");
  39 +// NoosferoApp.addService("Session", Session);
  40 +// NoosferoApp.addService("AuthService", AuthService);
  41 +NoosferoApp.addController("AuthController", AuthController);
  42 +
  43 +
43 44 require("./components/navbar/navbar.directive.js");
44 45 require("./components/noosfero-activities/activities.component.js");
45 46 require("./components/noosfero-activities/activity/activity.component.js");
... ...
src/app/main/main.component.ts
1 1 import {bundle, Component, StateConfig} from "ng-forward";
2 2 import {NoosferoArticleBlog} from "./../components/noosfero-articles/blog/blog.component.ts";
3   -import {ArticleComponent} from "../components/noosfero-articles/article/article.component";
  3 +
  4 +import {ArticleView} from "../components/noosfero-articles/article/article_view";
  5 +
4 6 import {Profile} from "../profile/profile.component";
5 7 import {Boxes} from "../components/noosfero-boxes/boxes.component";
6 8 import {Block} from "../components/noosfero-blocks/block.component";
7 9  
  10 +
  11 +import {AuthService} from "./../components/auth/auth_service";
  12 +import {Session} from "./../components/auth/session";
  13 +
8 14 @Component({
9 15 selector: 'main-content',
10 16 templateUrl: "app/main/main.html",
  17 + providers: [AuthService, Session]
11 18 })
12 19 export class MainContent {
13 20  
... ... @@ -16,7 +23,8 @@ export class MainContent {
16 23 @Component({
17 24 selector: 'main',
18 25 template: '<div ng-view></div>',
19   - directives: [NoosferoArticleBlog, ArticleComponent, Boxes, Block]
  26 + directives: [NoosferoArticleBlog, ArticleView, Boxes, Block],
  27 + providers: [AuthService, Session]
20 28 })
21 29 @StateConfig([
22 30 {
... ... @@ -24,7 +32,7 @@ export class MainContent {
24 32 component: MainContent,
25 33 name: 'main',
26 34 resolve: {
27   - currentUser: function(AuthService) {
  35 + currentUser: function(AuthService: AuthService) {
28 36 return AuthService.loginFromCookie();
29 37 }
30 38 }
... ...
src/app/models/interfaces.ts
  1 +export interface NoosferoRootScope extends ng.IScope {
  2 + currentUser: User;
  3 +}
  4 +
1 5 export interface Event extends Article {
2 6 id: number;
3 7 }
... ... @@ -8,6 +12,7 @@ export interface Article {
8 12  
9 13 export interface Profile {
10 14 id: number;
  15 + identifier: string;
11 16 }
12 17  
13 18 export interface Person extends Profile {
... ... @@ -21,3 +26,31 @@ export interface TynyMceArticle extends Article {
21 26 export interface Blog extends Article {
22 27 id: number;
23 28 }
  29 +
  30 +export interface Credentials {
  31 + username: string;
  32 + password: string;
  33 +}
  34 +
  35 +export interface User {
  36 + id: number;
  37 + login: string;
  38 + email: string;
  39 + person: Person;
  40 + private_token: string;
  41 + userRole: string;
  42 +}
  43 +
  44 +export interface UserResponse {
  45 + user: User;
  46 +}
  47 +
  48 +
  49 +export interface Box {
  50 + id: number;
  51 + position: number;
  52 +}
  53 +
  54 +export interface INoosferoLocalStorage extends angular.storage.ILocalStorageService {
  55 + currentUser: User;
  56 +}
24 57 \ No newline at end of file
... ...
src/app/profile-info/profile-info.component.ts
1 1 import {StateConfig, Component, Inject} from 'ng-forward';
2 2  
  3 +import {Profile} from "./../models/interfaces";
  4 +
3 5 @Component({
4 6 selector: 'profile',
5 7 templateUrl: "app/profile-info/profile-info.html"
... ... @@ -10,15 +12,15 @@ export class ProfileInfo {
10 12 activities: any
11 13 profile: any
12 14  
13   - constructor(private noosfero) {
  15 + constructor(private noosfero: any) {
14 16 this.activate();
15 17 }
16 18  
17 19 activate() {
18   - this.noosfero.currentProfile.then((profile) => {
  20 + this.noosfero.currentProfile.then((profile: Profile) => {
19 21 this.profile = profile;
20 22 return this.noosfero.profiles.one(this.profile.id).one('activities').get();
21   - }).then((response) => {
  23 + }).then((response: restangular.IResponse) => {
22 24 this.activities = response.data.activities;
23 25 });
24 26 }
... ...
src/app/profile/profile-home.component.ts
1 1 import {StateConfig, Component, Inject} from 'ng-forward';
2 2  
  3 +import {Profile} from "./../models/interfaces";
  4 +
3 5 @Component({
4 6 selector: 'profile-home',
5 7 template: "<div></div>"
... ... @@ -7,13 +9,13 @@ import {StateConfig, Component, Inject} from &#39;ng-forward&#39;;
7 9 @Inject("noosfero", "$log", "$stateParams", "$scope", "$state")
8 10 export class ProfileHome {
9 11  
10   - profile: any;
  12 + profile: Profile;
11 13  
12   - constructor(noosfero, $log, $stateParams, $scope, $state) {
13   - noosfero.currentProfile.then((profile) => {
  14 + constructor(noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService, $scope: ng.IScope, $state: ng.ui.IStateService) {
  15 + noosfero.currentProfile.then((profile: Profile) => {
14 16 this.profile = profile;
15 17 return noosfero.profile(this.profile.id).customGET('home_page', { fields: 'path' });
16   - }).then((response) => {
  18 + }).then((response: restangular.IResponse) => {
17 19 if (response.data.article) {
18 20 $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false });
19 21 } else {
... ...
src/app/profile/profile.component.ts
1 1 import {StateConfig, Component, Inject} from 'ng-forward';
2   -import {ProfileInfo} from '../profile-info/profile-info.component'
3   -import {ProfileHome} from '../profile/profile-home.component'
4   -import {Cms} from '../cms/cms.component'
  2 +import {ProfileInfo} from '../profile-info/profile-info.component';
  3 +import {ProfileHome} from '../profile/profile-home.component';
  4 +import {Cms} from '../cms/cms.component';
5 5 import {ContentViewer} from "../content-viewer/content-viewer.component";
6 6  
  7 +import * as noosferoModels from "./../models/interfaces";
  8 +
7 9 @Component({
8 10 selector: 'profile',
9 11 templateUrl: "app/profile/profile.html"
... ... @@ -65,15 +67,15 @@ import {ContentViewer} from &quot;../content-viewer/content-viewer.component&quot;;
65 67 @Inject("noosfero", "$log", "$stateParams")
66 68 export class Profile {
67 69  
68   - boxes: any
69   - profile: any
  70 + boxes: noosferoModels.Box[];
  71 + profile: noosferoModels.Profile;
70 72  
71   - constructor(noosfero, $log, $stateParams) {
72   - noosfero.profiles.one().get({ identifier: $stateParams.profile }).then((response) => {
  73 + constructor(noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService) {
  74 + noosfero.profiles.one().get({ identifier: $stateParams["profile"] }).then((response: restangular.IResponse) => {
73 75 this.profile = response.data[0];
74 76 noosfero.setCurrentProfile(this.profile);
75 77 return noosfero.boxes(this.profile.id).one().get();
76   - }).then((response) => {
  78 + }).then((response: restangular.IResponse) => {
77 79 this.boxes = response.data.boxes;
78 80 });
79 81 }
... ...
src/lib/ng-noosfero-api/api_descriptor.ts 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +
  2 +
  3 +// TODO - see code generation using swagger resource declaration
  4 +// -> https://github.com/swagger-api/swagger-codegen
... ...
src/lib/ng-noosfero-api/http/http_client.ts 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +namespace NgNoosferoAPI {
  2 + export class NoosferoHttpClient {
  3 + static $inject = ['$http', '$q'];
  4 +
  5 + constructor(private $http: ng.IHttpService, private $q: ng.IQService) {
  6 +
  7 + }
  8 + }
  9 +
  10 + NgNoosferoAPI.ngModule.service(NoosferoHttpClient);
  11 +}
0 12 \ No newline at end of file
... ...
src/lib/ng-noosfero-api/http/http_config.ts 0 → 100644
... ... @@ -0,0 +1,26 @@
  1 +
  2 +module NgNoosferoAPI {
  3 + export interface NoosferoHttpServiceConfig {
  4 + protocol: string;
  5 + hostname: string;
  6 + port: number;
  7 + apiPath: string;
  8 + acceptHeader: string;
  9 + contentTypeHeader: string;
  10 + textEncoding: string;
  11 +
  12 + }
  13 +
  14 + export function configNoosferoHttpService(hostname: string, port?: number, protocol?: string) {
  15 + return <NoosferoHttpServiceConfig>{
  16 + hostname: hostname,
  17 + port: port || 80,
  18 + apiPath: "/api/v1",
  19 + protocol: protocol || "http",
  20 + acceptHeader: "application/json",
  21 + contentTypeHeader: "application/json",
  22 + textEncoding: "UTF-8"
  23 + };
  24 + }
  25 +
  26 +}
0 27 \ No newline at end of file
... ...
src/lib/ng-noosfero-api/http/http_config_provider.ts 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +namespace NgNoosferoAPI {
  2 +
  3 + // The following class represents the provider
  4 + export class NoosferoHttpServiceConfigProvider implements ng.IServiceProvider {
  5 + private config = <NoosferoHttpServiceConfig>{
  6 + hostname: "localhost",
  7 + protocol: "http",
  8 + port: 3000,
  9 + apiPath: "/api/v1",
  10 + acceptHeader: "application/json",
  11 + contentTypeHeader: "application/json",
  12 + textEncoding: "UTF-8"
  13 + };
  14 +
  15 +
  16 + // Configuration function
  17 + public setConfig(config: NoosferoHttpServiceConfig) {
  18 + this.config = config;
  19 + }
  20 +
  21 + // Provider's factory function
  22 + public $get(): NoosferoHttpServiceConfig {
  23 + return this.config;
  24 + }
  25 + }
  26 +
  27 + ngModule.provider("NoosferoHttpServiceConfig", NoosferoHttpServiceConfigProvider);
  28 +
  29 +}
0 30 \ No newline at end of file
... ...
src/lib/ng-noosfero-api/module.ts 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +
  2 +
  3 +module NgNoosferoAPI {
  4 + "use strict";
  5 + export var ngModule: ng.IModule = angular.module("ngNoosferoAPI", []);
  6 +
  7 +}
  8 +
... ...
tsconfig.json
... ... @@ -2,7 +2,7 @@
2 2 "compilerOptions": {
3 3 "module": "commonjs",
4 4 "target": "es5",
5   - "noImplicitAny": false,
  5 + "noImplicitAny": true,
6 6 "sourceMap": true,
7 7 "experimentalDecorators": true
8 8 },
... ...