From 6738dc3655eb451131e01fa3381722dcd9c2021b Mon Sep 17 00:00:00 2001 From: ABNER SILVA DE OLIVEIRA Date: Wed, 2 Mar 2016 13:32:12 -0300 Subject: [PATCH] migrated auth_service, session and auth controller to typescript. renamed articledirective to articleView. fixed articleView test. --- .vscode/settings.json | 4 +++- src/app/cms/cms.component.ts | 8 ++++---- src/app/components/auth/auth.controller.js | 17 ----------------- src/app/components/auth/auth_controller.ts | 22 ++++++++++++++++++++++ src/app/components/auth/auth_events.ts | 2 -- src/app/components/auth/auth_service.ts | 45 ++++++++++++++++++++++++++------------------- src/app/components/auth/noosfero_root_scope.ts | 5 ----- src/app/components/auth/session.ts | 10 ++++++---- src/app/components/noosfero-articles/article/article.directive.spec.ts | 85 ------------------------------------------------------------------------------------- src/app/components/noosfero-articles/article/article.directive.ts | 43 ------------------------------------------- src/app/components/noosfero-articles/article/article_view.spec.ts | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/app/components/noosfero-articles/article/article_view.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/app/components/noosfero-articles/blog/blog.component.ts | 11 ++++++----- src/app/components/noosfero-boxes/boxes.component.ts | 10 +++++----- src/app/content-viewer/content-viewer-actions.component.ts | 5 +++-- src/app/content-viewer/content-viewer.component.ts | 9 ++++----- src/app/index.config.ts | 10 ++++++---- src/app/index.module.ts | 10 +++++++--- src/app/index.route.ts | 2 +- src/app/index.run.ts | 9 +++++---- src/app/index.ts | 8 ++++++-- src/app/main/main.component.ts | 14 ++++++++++---- src/app/models/interfaces.ts | 21 +++++++++++++++++++++ src/app/profile-info/profile-info.component.ts | 8 +++++--- src/app/profile/profile-home.component.ts | 10 ++++++---- src/app/profile/profile.component.ts | 18 ++++++++++-------- src/lib/ng-noosfero-api/api_descriptor.ts | 4 ++++ src/lib/ng-noosfero-api/http/http_client.ts | 7 +++---- src/lib/ng-noosfero-api/http/http_config_provider.ts | 11 +++++------ tsconfig.json | 2 +- 30 files changed, 320 insertions(+), 241 deletions(-) delete mode 100644 src/app/components/auth/auth.controller.js create mode 100644 src/app/components/auth/auth_controller.ts delete mode 100644 src/app/components/auth/noosfero_root_scope.ts delete mode 100644 src/app/components/noosfero-articles/article/article.directive.spec.ts delete mode 100644 src/app/components/noosfero-articles/article/article.directive.ts create mode 100644 src/app/components/noosfero-articles/article/article_view.spec.ts create mode 100644 src/app/components/noosfero-articles/article/article_view.ts create mode 100644 src/lib/ng-noosfero-api/api_descriptor.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 35f4396..7e91822 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,7 @@ "**/.git": true, "**/.DS_Store": true, "src/app/*.js": false - } + }, + "editor.fontSize": 14, + "typescript.useCodeSnippetsOnMethodSuggest": true } \ No newline at end of file diff --git a/src/app/cms/cms.component.ts b/src/app/cms/cms.component.ts index 7c35f21..b4ce620 100644 --- a/src/app/cms/cms.component.ts +++ b/src/app/cms/cms.component.ts @@ -1,5 +1,5 @@ import {StateConfig, Component, Inject} from 'ng-forward'; - +import {Profile} from "./../models/interfaces"; @Component({ selector: 'cms', templateUrl: "app/cms/cms.html" @@ -10,19 +10,19 @@ export class Cms { article: any = {}; profile: any; - constructor(private noosfero, private $stateParams, private $httpParamSerializer, private $state, private SweetAlert) { + constructor(private noosfero: any/* TODO convert noosferoService */, private $stateParams: ng.ui.IStateParamsService, private $httpParamSerializer: any, private $state: ng.ui.IStateService, private SweetAlert: any) { } save() { - this.noosfero.currentProfile.then((profile) => { + this.noosfero.currentProfile.then((profile: Profile) => { return this.noosfero.profiles.one(profile.id).customPOST( { article: this.article }, 'articles', {}, { 'Content-Type': 'application/json' } ) - }).then((response) => { + }).then((response: restangular.IResponse) => { this.$state.transitionTo('main.profile.page', { page: response.data.article.path, profile: response.data.article.profile.identifier }); this.SweetAlert.swal({ title: "Good job!", diff --git a/src/app/components/auth/auth.controller.js b/src/app/components/auth/auth.controller.js deleted file mode 100644 index 1083a12..0000000 --- a/src/app/components/auth/auth.controller.js +++ /dev/null @@ -1,17 +0,0 @@ -(function() { - 'use strict'; - - angular - .module('noosferoApp') - .controller('AuthController', AuthController); - - - /** @ngInject */ - function AuthController(noosfero, $log, $stateParams, AuthService) { - var vm = this; - vm.credentials = {}; - vm.login = function() { - AuthService.login(vm.credentials); - } - } -})(); diff --git a/src/app/components/auth/auth_controller.ts b/src/app/components/auth/auth_controller.ts new file mode 100644 index 0000000..71e65e7 --- /dev/null +++ b/src/app/components/auth/auth_controller.ts @@ -0,0 +1,22 @@ +import {Credentials} from "./../../models/interfaces"; +import {AuthService} from "./auth_service"; + +export class AuthController { + + static $inject = ["noosfero", "$log", "$stateParams", "AuthService"]; + + constructor( + private noosfero: any, + private $log: ng.ILogService, + private $stateParams: any, + private AuthService: AuthService + ) { + + } + + credentials: Credentials; + + login() { + this.AuthService.login(this.credentials); + } +} \ No newline at end of file diff --git a/src/app/components/auth/auth_events.ts b/src/app/components/auth/auth_events.ts index db69755..875d139 100644 --- a/src/app/components/auth/auth_events.ts +++ b/src/app/components/auth/auth_events.ts @@ -1,5 +1,3 @@ - - export interface IAuthEvents { loginSuccess: string; loginFailed: string; diff --git a/src/app/components/auth/auth_service.ts b/src/app/components/auth/auth_service.ts index fc76393..834fc83 100644 --- a/src/app/components/auth/auth_service.ts +++ b/src/app/components/auth/auth_service.ts @@ -1,58 +1,65 @@ import {Injectable, Inject} from "ng-forward"; -import {Credentials} from "./../../models/interfaces"; +import {Credentials, NoosferoRootScope, User, UserResponse} from "./../../models/interfaces"; import {Session} from "./session"; -import {NoosferoRootScope} from "./noosfero_root_scope"; + import {AUTH_EVENTS, IAuthEvents} from "./auth_events"; -@Injectable -@Inject("$q", "$http", "$rootScope", Session, "$log", "AUTH_EVENTS") +@Injectable() +@Inject("$q", "$http", "$rootScope", "Session", "$log", "AUTH_EVENTS") export class AuthService { + constructor(private $q: ng.IQService, private $http: ng.IHttpService, private $rootScope: NoosferoRootScope, - private Session: Session, + private session: Session, private $log: ng.ILogService, - private AUTH_EVENTS: IAuthEvents) { + private auth_events: IAuthEvents) { } - private loginSuccessCallback(response) { + loginFromCookie() { + let url: string = '/api/v1/login_from_cookie'; + return this.$http.post(url, null).then(this.loginSuccessCallback.bind(this), this.loginFailedCallback.bind(this)); + } + + + private loginSuccessCallback(response: ng.IHttpPromiseCallbackArg) { this.$log.debug('AuthService.login [SUCCESS] response', response); - let currentUser = this.Session.create(response.data); + let currentUser: User = this.session.create(response.data); this.$rootScope.currentUser = currentUser; - this.$rootScope.$broadcast(this.AUTH_EVENTS.loginSuccess, currentUser); + this.$rootScope.$broadcast(this.auth_events.loginSuccess, currentUser); return currentUser; } login(credentials: Credentials) { let url = '/api/v1/login'; let encodedData = 'login=' + credentials.username + '&password=' + credentials.password; - return this.$http.post(url, encodedData).then(this.loginSuccessCallback, this.loginFailedCallback); + return this.$http.post(url, encodedData).then(this.loginSuccessCallback.bind(this), this.loginFailedCallback.bind(this)); } - private loginFailedCallback(response) { + private loginFailedCallback(response: ng.IHttpPromiseCallbackArg): any { this.$log.debug('AuthService.login [FAIL] response', response); - this.$rootScope.$broadcast(AUTH_EVENTS.loginFailed); + this.$rootScope.$broadcast(this.auth_events.loginFailed); // return $q.reject(response); return null; } public logout() { - this.Session.destroy(); + this.session.destroy(); this.$rootScope.currentUser = undefined; - this.$rootScope.$broadcast(AUTH_EVENTS.logoutSuccess); - this.$http.jsonp('/account/logout'); //FIXME logout from noosfero to sync login state + this.$rootScope.$broadcast(this.auth_events.logoutSuccess); + this.$http.jsonp('/account/logout'); // FIXME logout from noosfero to sync login state } public isAuthenticated() { - return !!this.Session.getCurrentUser(); + return !!this.session.getCurrentUser(); } - public isAuthorized(authorizedRoles) { + public isAuthorized(authorizedRoles: string | string[]) { if (!angular.isArray(authorizedRoles)) { - authorizedRoles = [authorizedRoles]; + authorizedRoles = [authorizedRoles]; } - return (this.isAuthenticated() && authorizedRoles.indexOf(this.Session.getCurrentUser().userRole) !== -1); + return (this.isAuthenticated() && authorizedRoles.indexOf(this.session.getCurrentUser().userRole) !== -1); } } \ No newline at end of file diff --git a/src/app/components/auth/noosfero_root_scope.ts b/src/app/components/auth/noosfero_root_scope.ts deleted file mode 100644 index 39a72b8..0000000 --- a/src/app/components/auth/noosfero_root_scope.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {User} from "./../../models/interfaces"; - -export interface NoosferoRootScope extends ng.IScope { - currentUser: User; -} \ No newline at end of file diff --git a/src/app/components/auth/session.ts b/src/app/components/auth/session.ts index 80b39a3..ce818f4 100644 --- a/src/app/components/auth/session.ts +++ b/src/app/components/auth/session.ts @@ -1,24 +1,26 @@ import {Injectable, Inject} from "ng-forward"; +import {UserResponse, User, INoosferoLocalStorage} from "./../../models/interfaces"; @Injectable() @Inject("$localStorage", "$log") export class Session { - constructor(private $localStorage, $log) { + + constructor(private $localStorage: INoosferoLocalStorage, private $log: ng.ILogService) { } - create = function(data) { + create(data: UserResponse): User { this.$localStorage.currentUser = data.user; this.$log.debug('User session created.', this.$localStorage.currentUser); return this.$localStorage.currentUser; }; - destroy = function() { + destroy() { delete this.$localStorage.currentUser; this.$log.debug('User session destroyed.'); }; - getCurrentUser = function() { + getCurrentUser(): User { return this.$localStorage.currentUser; }; diff --git a/src/app/components/noosfero-articles/article/article.directive.spec.ts b/src/app/components/noosfero-articles/article/article.directive.spec.ts deleted file mode 100644 index 98a567f..0000000 --- a/src/app/components/noosfero-articles/article/article.directive.spec.ts +++ /dev/null @@ -1,85 +0,0 @@ -import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder'; -import {Input, provide, Component} from 'ng-forward'; - -import {ArticleDirective} from './article.directive'; - -// Instantiate the Builder, this part is different than ng2. -// In ng2 you inject tcb. -const tcb = new TestComponentBuilder(); - -// this htmlTemplate will be re-used between the container components in this spec file -const htmlTemplate: string = ''; - - -describe("Article Directive", () => { - - - // the karma preprocessor html2js transform the templates html into js files which put - // the templates to the templateCache into the module templates - // we need to load the module templates here as the template for the - // component NoosferoArtileDirective will be load on our tests - beforeEach(angular.mock.module("templates")); - - it("receives the article and profile as inputs", done => { - - // Creating a container component (ArticleContainerComponent) to include - // the component under test (ArticleDirective) - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleDirective] }) - class ArticleContainerComponent { - article = { type: 'anyArticleType' }; - profile = { name: 'profile-name' }; - constructor() { - } - } - - // uses the TestComponentBuilder instance to initialize the component - tcb.createAsync(ArticleContainerComponent).then((fixture) => { - // and here we can inspect and run the test assertions - let myComponent: ArticleContainerComponent = fixture.componentInstance; - - console.log(myComponent); - - // assure the article object inside the ArticleDirective matches - // the provided through the parent component - expect(myComponent.article.type).toEqual("anyArticleType"); - expect(myComponent.profile.name).toEqual("profile-name"); - - myComponent.metodoAsync(() => { - done(); - }); - // done needs to be called (it isn't really needed, as we can read in - // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync) - // because createAsync in ng-forward is not really async, but as the intention - // here is write tests in angular 2 ways, this is recommended - done(); - }); - }); - - - it("renders a directive which matches to the article type", done => { - // NoosferoTinyMceArticle component created to check if it will be used - // when a article with type 'TinyMceArticle' is provided to the noosfero-article (ArticleDirective) - // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider - @Component({ selector: 'noosfero-tiny-mce-article', template: "

TinyMceArticle

" }) - class NoosferoTinyMceArticle { - @Input() article: any; - @Input() profile: any; - } - - // Creating a container component (ArticleContainerComponent) to include our NoosferoTinyMceArticle - @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleDirective, NoosferoTinyMceArticle] }) - class CustomArticleType { - article = { type: 'TinyMceArticle' }; - profile = { name: 'profile-name' }; - constructor() { - } - } - tcb.createAsync(CustomArticleType).then(fixture => { - let myComponent: CustomArticleType = fixture.componentInstance; - expect(myComponent.article.type).toEqual("TinyMceArticle"); - expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle"); - done(); - }); - }); - -}); \ No newline at end of file diff --git a/src/app/components/noosfero-articles/article/article.directive.ts b/src/app/components/noosfero-articles/article/article.directive.ts deleted file mode 100644 index 08d8d46..0000000 --- a/src/app/components/noosfero-articles/article/article.directive.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { bundle, Input, Inject, Component, Directive } from 'ng-forward'; -import {NoosferoArticleBlog} from "../blog/blog.component"; - -@Component({ - selector: 'noosfero-default-article', - templateUrl: 'app/components/noosfero-articles/article/article.html' -}) -export class ArticleView { - - @Input() article: any; - @Input() profile: any; - -} - -@Component({ - selector: 'noosfero-article', - template: 'not-used', - directives: [ArticleView, NoosferoArticleBlog] -}) -@Inject("$element", "$scope", "$injector", "$compile") -export class ArticleDirective { - - @Input() article: any; - @Input() profile: any; - directiveName: string; - - ngOnInit() { - let specificDirective = 'noosfero' + this.article.type; - this.directiveName = "noosfero-default-article"; - if (this.$injector.has(specificDirective + 'Directive')) { - this.directiveName = specificDirective.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); - } - this.$element.replaceWith(this.$compile('<' + this.directiveName + ' [article]="ctrl.article" [profile]="ctrl.profile">')(this.$scope)); - } - - constructor( - private $element: any, - private $scope: ng.IScope, - private $injector: ng.auto.IInjectorService, - private $compile: ng.ICompileService) { - - } -} diff --git a/src/app/components/noosfero-articles/article/article_view.spec.ts b/src/app/components/noosfero-articles/article/article_view.spec.ts new file mode 100644 index 0000000..7f27b15 --- /dev/null +++ b/src/app/components/noosfero-articles/article/article_view.spec.ts @@ -0,0 +1,108 @@ +import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder'; +import {Input, provide, Component} from 'ng-forward'; + +import {ArticleView, ArticleDefaultView} from './article_view'; + +// Instantiate the Builder, this part is different than ng2. +// In ng2 you inject tcb. +const tcb = new TestComponentBuilder(); + +// this htmlTemplate will be re-used between the container components in this spec file +const htmlTemplate: string = ''; + + +describe("ArticleView Component", () => { + + // the karma preprocessor html2js transform the templates html into js files which put + // the templates to the templateCache into the module templates + // we need to load the module templates here as the template for the + // component Noosfero ArtileView will be load on our tests + beforeEach(angular.mock.module("templates")); + + it("renders the default component when no specific component is found", (done: Function) => { + // Creating a container component (ArticleContainerComponent) to include + // the component under test (ArticleView) + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] }) + class ArticleContainerComponent { + article = { type: 'anyArticleType' }; + profile = { name: 'profile-name' }; + constructor() { + } + } + + // uses the TestComponentBuilder instance to initialize the component + tcb.createAsync(ArticleContainerComponent).then((fixture) => { + // and here we can inspect and run the test assertions + + // gets the children component of ArticleContainerComponent + let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance; + + // and checks if the article View rendered was the Default Article View + expect(articleView.constructor.prototype).toEqual(ArticleDefaultView.prototype); + + // done needs to be called (it isn't really needed, as we can read in + // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync) + // because createAsync in ng-forward is not really async, but as the intention + // here is write tests in angular 2 ways, this is recommended + done(); + }); + }); + + it("receives the article and profile as inputs", (done: Function) => { + + // Creating a container component (ArticleContainerComponent) to include + // the component under test (ArticleView) + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView] }) + class ArticleContainerComponent { + article = { type: 'anyArticleType' }; + profile = { name: 'profile-name' }; + constructor() { + } + } + + // uses the TestComponentBuilder instance to initialize the component + tcb.createAsync(ArticleContainerComponent).then((fixture) => { + // and here we can inspect and run the test assertions + let articleView: ArticleView = fixture.debugElement.componentViewChildren[0].componentInstance; + + // assure the article object inside the ArticleView matches + // the provided through the parent component + expect(articleView.article.type).toEqual("anyArticleType"); + expect(articleView.profile.name).toEqual("profile-name"); + + // done needs to be called (it isn't really needed, as we can read in + // here (https://github.com/ngUpgraders/ng-forward/blob/master/API.md#createasync) + // because createAsync in ng-forward is not really async, but as the intention + // here is write tests in angular 2 ways, this is recommended + done(); + }); + }); + + + it("renders a article view which matches to the article type", done => { + // NoosferoTinyMceArticle component created to check if it will be used + // when a article with type 'TinyMceArticle' is provided to the noosfero-article (ArticleView) + // *** Important *** - the selector is what ng-forward uses to define the name of the directive provider + @Component({ selector: 'noosfero-tiny-mce-article', template: "

TinyMceArticle

" }) + class TinyMceArticleView { + @Input() article: any; + @Input() profile: any; + } + + // Creating a container component (ArticleContainerComponent) to include our NoosferoTinyMceArticle + @Component({ selector: 'test-container-component', template: htmlTemplate, directives: [ArticleView, TinyMceArticleView] }) + class CustomArticleType { + article = { type: 'TinyMceArticle' }; + profile = { name: 'profile-name' }; + constructor() { + } + } + tcb.createAsync(CustomArticleType).then(fixture => { + let myComponent: CustomArticleType = fixture.componentInstance; + expect(myComponent.article.type).toEqual("TinyMceArticle"); + expect(fixture.debugElement.componentViewChildren[0].text()).toEqual("TinyMceArticle"); + done(); + }); + }); + +}); \ No newline at end of file diff --git a/src/app/components/noosfero-articles/article/article_view.ts b/src/app/components/noosfero-articles/article/article_view.ts new file mode 100644 index 0000000..78982a6 --- /dev/null +++ b/src/app/components/noosfero-articles/article/article_view.ts @@ -0,0 +1,43 @@ +import { bundle, Input, Inject, Component, Directive } from 'ng-forward'; +import {NoosferoArticleBlog} from "../blog/blog.component"; + +@Component({ + selector: 'noosfero-default-article', + templateUrl: 'app/components/noosfero-articles/article/article.html' +}) +export class ArticleDefaultView { + + @Input() article: any; + @Input() profile: any; + +} + +@Component({ + selector: 'noosfero-article', + template: 'not-used', + directives: [ArticleDefaultView, NoosferoArticleBlog] +}) +@Inject("$element", "$scope", "$injector", "$compile") +export class ArticleView { + + @Input() article: any; + @Input() profile: any; + directiveName: string; + + ngOnInit() { + let specificDirective = 'noosfero' + this.article.type; + this.directiveName = "noosfero-default-article"; + if (this.$injector.has(specificDirective + 'Directive')) { + this.directiveName = specificDirective.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); + } + this.$element.replaceWith(this.$compile('<' + this.directiveName + ' [article]="ctrl.article" [profile]="ctrl.profile">')(this.$scope)); + } + + constructor( + private $element: any, + private $scope: ng.IScope, + private $injector: ng.auto.IInjectorService, + private $compile: ng.ICompileService) { + + } +} diff --git a/src/app/components/noosfero-articles/blog/blog.component.ts b/src/app/components/noosfero-articles/blog/blog.component.ts index f907525..0383661 100644 --- a/src/app/components/noosfero-articles/blog/blog.component.ts +++ b/src/app/components/noosfero-articles/blog/blog.component.ts @@ -1,5 +1,6 @@ import {Component, Input, Inject} from "ng-forward"; +import {Article, Profile} from "./../../../models/interfaces"; @Component({ selector: "noosfero-blog", @@ -8,15 +9,15 @@ import {Component, Input, Inject} from "ng-forward"; @Inject("noosfero", "$scope") export class NoosferoArticleBlog { - @Input() article; - @Input() profile; + @Input() article: Article; + @Input() profile: Profile; private posts: any[]; private perPage: number = 3; private currentPage: number; private totalPosts: number = 0; - constructor(private noosfero: any, private $scope) { + constructor(private noosfero: any, private $scope: ng.IScope) { } ngOnInit() { @@ -28,8 +29,8 @@ export class NoosferoArticleBlog { content_type: "TinyMceArticle", per_page: this.perPage, page: this.currentPage - }).then((response) => { - this.totalPosts = response.headers("total"); + }).then((response: restangular.IResponse) => { + this.totalPosts = (response.headers("total")); this.posts = response.data.articles; }); } diff --git a/src/app/components/noosfero-boxes/boxes.component.ts b/src/app/components/noosfero-boxes/boxes.component.ts index 1378163..ddce12f 100644 --- a/src/app/components/noosfero-boxes/boxes.component.ts +++ b/src/app/components/noosfero-boxes/boxes.component.ts @@ -1,16 +1,16 @@ import { Input, Inject, Component } from 'ng-forward'; - +import {Box} from "./../../models/interfaces"; @Component({ selector: "noosfero-boxes", templateUrl: "app/components/noosfero-boxes/boxes.html" }) export class Boxes { - @Input() boxes: any - @Input() owner: any + @Input() boxes: any; + @Input() owner: any; - boxesOrder(box) { - if (box.position == 2) return 0; + boxesOrder(box: Box) { + if (box.position === 2) return 0; return box.position; } } diff --git a/src/app/content-viewer/content-viewer-actions.component.ts b/src/app/content-viewer/content-viewer-actions.component.ts index a6d07a4..cc0c675 100644 --- a/src/app/content-viewer/content-viewer-actions.component.ts +++ b/src/app/content-viewer/content-viewer-actions.component.ts @@ -1,5 +1,6 @@ import {Component, Inject} from "ng-forward"; +import {Profile} from "./../models/interfaces"; @Component({ selector: "content-viewer-actions", templateUrl: "app/content-viewer/navbar-actions.html", @@ -10,8 +11,8 @@ export class ContentViewerActions { article: any; profile: any; - constructor(noosfero) { - noosfero.currentProfile.then((profile) => { + constructor(noosfero: any) { + noosfero.currentProfile.then((profile: Profile) => { this.profile = profile; }); } diff --git a/src/app/content-viewer/content-viewer.component.ts b/src/app/content-viewer/content-viewer.component.ts index d34ffa4..dfae27a 100644 --- a/src/app/content-viewer/content-viewer.component.ts +++ b/src/app/content-viewer/content-viewer.component.ts @@ -1,6 +1,6 @@ import * as noosfero from "../models/interfaces"; -import {ArticleDirective, ArticleView} from "../components/noosfero-articles/article/article.directive"; +import {ArticleDefaultView, ArticleView} from "../components/noosfero-articles/article/article_view"; import {Input, Component, StateConfig, Inject} from "ng-forward"; import {NoosferoArticleBlog} from "./../components/noosfero-articles/blog/blog.component"; @@ -8,7 +8,7 @@ import {NoosferoArticleBlog} from "./../components/noosfero-articles/blog/blog.c @Component({ selector: "content-viewer", templateUrl: "app/content-viewer/page.html", - directives: [NoosferoArticleBlog, ArticleView, ArticleDirective] + directives: [NoosferoArticleBlog, ArticleView, ArticleDefaultView] }) @Inject("noosfero", "$log", "$stateParams") export class ContentViewer { @@ -24,11 +24,10 @@ export class ContentViewer { } activate() { - console.log("HERE CONTENT VIEWER"); - this.noosfero.currentProfile.then((profile) => { + this.noosfero.currentProfile.then((profile: noosfero.Profile) => { this.profile = profile; return this.noosfero.profiles.one(this.profile.id).one("articles").get({ path: this.$stateParams["page"] }); - }).then((response) => { + }).then((response: restangular.IResponse) => { this.article = response.data.article; }); } diff --git a/src/app/index.config.ts b/src/app/index.config.ts index 1fb8a70..f55d98a 100644 --- a/src/app/index.config.ts +++ b/src/app/index.config.ts @@ -1,14 +1,16 @@ /** @ngInject */ -export function noosferoModuleConfig($logProvider, $locationProvider, RestangularProvider, $httpProvider, $provide) { +export function noosferoModuleConfig($logProvider: ng.ILogProvider, $locationProvider: ng.ILocationProvider, RestangularProvider: restangular.IProvider, $httpProvider: ng.IHttpProvider, $provide: ng.auto.IProvideService) { $logProvider.debugEnabled(true); $locationProvider.html5Mode({ enabled: true }); RestangularProvider.setBaseUrl("/api/v1"); RestangularProvider.setFullResponse(true); - $httpProvider.defaults.headers.post = { "Content-Type": "application/x-www-form-urlencoded; charset=utf-8" }; - $provide.decorator("$uiViewScroll", function($delegate, $document) { - return function(uiViewElement) { + ($httpProvider.defaults.headers.post)["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8"; + + + $provide.decorator("$uiViewScroll", function($delegate: any, $document: any) { + return function(uiViewElement: any) { $document.scrollToElementAnimated(uiViewElement); }; }); diff --git a/src/app/index.module.ts b/src/app/index.module.ts index dd154bf..26d6aaa 100644 --- a/src/app/index.module.ts +++ b/src/app/index.module.ts @@ -2,14 +2,14 @@ export class NoosferoApp { static appName: string = "noosferoApp"; static angularModule: any; - //static init(angularModule: any) { - //NoosferoApp.angularModule + // static init(angularModule: any) { + // NoosferoApp.angularModule // angular.module(NoosferoApp.appName, ["ngAnimate", "ngCookies", "ngStorage", "ngTouch", // "ngSanitize", "ngMessages", "ngAria", "restangular", // "ui.router", "ui.bootstrap", "toastr", // "angularMoment", "angular.filter", "akoenig.deckgrid", // "angular-timeline", "duScroll", "oitozero.ngSweetAlert"]); - //} + // } static addConfig(configFunc: Function) { NoosferoApp.angularModule.config(configFunc); @@ -23,6 +23,10 @@ export class NoosferoApp { NoosferoApp.angularModule.service(serviceName, value); } + static addFactory(factoryName: string, value: any) { + NoosferoApp.angularModule.factory(factoryName, value); + } + static addController(controllerName: string, value: any) { NoosferoApp.angularModule.controller(controllerName, value); } diff --git a/src/app/index.route.ts b/src/app/index.route.ts index 58b7ba0..648118e 100644 --- a/src/app/index.route.ts +++ b/src/app/index.route.ts @@ -1,7 +1,7 @@ /** @ngInject */ -export function routeConfig($stateProvider, $urlRouterProvider) { +export function routeConfig($stateProvider: ng.ui.IStateProvider, $urlRouterProvider: ng.ui.IUrlRouterProvider) { $stateProvider .state("main.profile.settings", { url: "^/myprofile/:profile" diff --git a/src/app/index.run.ts b/src/app/index.run.ts index 58c04c0..27038c0 100644 --- a/src/app/index.run.ts +++ b/src/app/index.run.ts @@ -1,11 +1,12 @@ +import {Session} from "./components/auth/session"; /** @ngInject */ -export function noosferoAngularRunBlock($log, Restangular, Session) { - Restangular.addFullRequestInterceptor(function(element, operation, route, url, headers) { +export function noosferoAngularRunBlock($log: ng.ILogService, Restangular: restangular.IService, Session: Session) { + Restangular.addFullRequestInterceptor((element: any, operation: string, route: string, url: string, headers: string) => { if (Session.getCurrentUser()) { - headers["Private-Token"] = Session.getCurrentUser().private_token; + (headers)["Private-Token"] = Session.getCurrentUser().private_token; } - return { headers: headers }; + return { headers: headers }; }); } diff --git a/src/app/index.ts b/src/app/index.ts index c45273b..f77701a 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -17,6 +17,7 @@ import {Main} from "./main/main.component"; import {bootstrap, bundle} from "ng-forward"; import {AUTH_EVENTS} from "./components/auth/auth_events"; +import {AuthController} from "./components/auth/auth_controller"; declare var moment: any; @@ -35,8 +36,11 @@ NoosferoApp.addConstants("AUTH_EVENTS", AUTH_EVENTS); NoosferoApp.addConfig(noosferoModuleConfig); NoosferoApp.run(noosferoAngularRunBlock); -require("./components/auth/auth.controller.js"); -require("./components/auth/auth.service.js"); +// NoosferoApp.addService("Session", Session); +// NoosferoApp.addService("AuthService", AuthService); +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"); diff --git a/src/app/main/main.component.ts b/src/app/main/main.component.ts index 0419d27..c9e3a21 100644 --- a/src/app/main/main.component.ts +++ b/src/app/main/main.component.ts @@ -1,13 +1,18 @@ import {bundle, Component, StateConfig} from "ng-forward"; import {NoosferoArticleBlog} from "./../components/noosfero-articles/blog/blog.component.ts"; -import {ArticleDirective} from "../components/noosfero-articles/article/article.directive.ts"; +import {ArticleView} from "../components/noosfero-articles/article/article_view.ts"; import {Profile} from "../profile/profile.component"; import {Boxes} from "../components/noosfero-boxes/boxes.component"; import {Block} from "../components/noosfero-blocks/block.component"; + +import {AuthService} from "./../components/auth/auth_service"; +import {Session} from "./../components/auth/session"; + @Component({ selector: 'main-content', templateUrl: "app/main/main.html", + providers: [AuthService, Session] }) export class MainContent { @@ -15,8 +20,9 @@ export class MainContent { @Component({ selector: 'main', - template: 'not-used', - directives: [NoosferoArticleBlog, ArticleDirective, Boxes, Block] + template: '
', + directives: [NoosferoArticleBlog, ArticleView, Boxes, Block], + providers: [AuthService, Session] }) @StateConfig([ { @@ -24,7 +30,7 @@ export class MainContent { component: MainContent, name: 'main', resolve: { - currentUser: function(AuthService) { + currentUser: function(AuthService: AuthService) { return AuthService.loginFromCookie(); } } diff --git a/src/app/models/interfaces.ts b/src/app/models/interfaces.ts index 5bef04b..cfc0641 100644 --- a/src/app/models/interfaces.ts +++ b/src/app/models/interfaces.ts @@ -1,3 +1,7 @@ +export interface NoosferoRootScope extends ng.IScope { + currentUser: User; +} + export interface Event extends Article { id: number; } @@ -8,6 +12,7 @@ export interface Article { export interface Profile { id: number; + identifier: string; } export interface Person extends Profile { @@ -32,4 +37,20 @@ export interface User { login: string; email: string; person: Person; + private_token: string; + userRole: string; } + +export interface UserResponse { + user: User; +} + + +export interface Box { + id: number; + position: number; +} + +export interface INoosferoLocalStorage extends angular.storage.ILocalStorageService { + currentUser: User; +} \ No newline at end of file diff --git a/src/app/profile-info/profile-info.component.ts b/src/app/profile-info/profile-info.component.ts index e412690..e9fae9f 100644 --- a/src/app/profile-info/profile-info.component.ts +++ b/src/app/profile-info/profile-info.component.ts @@ -1,5 +1,7 @@ import {StateConfig, Component, Inject} from 'ng-forward'; +import {Profile} from "./../models/interfaces"; + @Component({ selector: 'profile', templateUrl: "app/profile-info/profile-info.html" @@ -10,15 +12,15 @@ export class ProfileInfo { activities: any profile: any - constructor(private noosfero) { + constructor(private noosfero: any) { this.activate(); } activate() { - this.noosfero.currentProfile.then((profile) => { + this.noosfero.currentProfile.then((profile: Profile) => { this.profile = profile; return this.noosfero.profiles.one(this.profile.id).one('activities').get(); - }).then((response) => { + }).then((response: restangular.IResponse) => { this.activities = response.data.activities; }); } diff --git a/src/app/profile/profile-home.component.ts b/src/app/profile/profile-home.component.ts index 92d3402..e2d64e9 100644 --- a/src/app/profile/profile-home.component.ts +++ b/src/app/profile/profile-home.component.ts @@ -1,5 +1,7 @@ import {StateConfig, Component, Inject} from 'ng-forward'; +import {Profile} from "./../models/interfaces"; + @Component({ selector: 'profile-home', template: "
" @@ -7,13 +9,13 @@ import {StateConfig, Component, Inject} from 'ng-forward'; @Inject("noosfero", "$log", "$stateParams", "$scope", "$state") export class ProfileHome { - profile: any; + profile: Profile; - constructor(noosfero, $log, $stateParams, $scope, $state) { - noosfero.currentProfile.then((profile) => { + constructor(noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService, $scope: ng.IScope, $state: ng.ui.IStateService) { + noosfero.currentProfile.then((profile: Profile) => { this.profile = profile; return noosfero.profile(this.profile.id).customGET('home_page', { fields: 'path' }); - }).then((response) => { + }).then((response: restangular.IResponse) => { if (response.data.article) { $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false }); } else { diff --git a/src/app/profile/profile.component.ts b/src/app/profile/profile.component.ts index c9ae887..c1c3019 100644 --- a/src/app/profile/profile.component.ts +++ b/src/app/profile/profile.component.ts @@ -1,9 +1,11 @@ import {StateConfig, Component, Inject} from 'ng-forward'; -import {ProfileInfo} from '../profile-info/profile-info.component' -import {ProfileHome} from '../profile/profile-home.component' -import {Cms} from '../cms/cms.component' +import {ProfileInfo} from '../profile-info/profile-info.component'; +import {ProfileHome} from '../profile/profile-home.component'; +import {Cms} from '../cms/cms.component'; import {ContentViewer} from "../content-viewer/content-viewer.component"; +import * as noosferoModels from "./../models/interfaces"; + @Component({ selector: 'profile', templateUrl: "app/profile/profile.html" @@ -65,15 +67,15 @@ import {ContentViewer} from "../content-viewer/content-viewer.component"; @Inject("noosfero", "$log", "$stateParams") export class Profile { - boxes: any - profile: any + boxes: noosferoModels.Box[]; + profile: noosferoModels.Profile; - constructor(noosfero, $log, $stateParams) { - noosfero.profiles.one().get({ identifier: $stateParams.profile }).then((response) => { + constructor(noosfero: any, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService) { + noosfero.profiles.one().get({ identifier: $stateParams["profile"] }).then((response: restangular.IResponse) => { this.profile = response.data[0]; noosfero.setCurrentProfile(this.profile); return noosfero.boxes(this.profile.id).one().get(); - }).then((response) => { + }).then((response: restangular.IResponse) => { this.boxes = response.data.boxes; }); } diff --git a/src/lib/ng-noosfero-api/api_descriptor.ts b/src/lib/ng-noosfero-api/api_descriptor.ts new file mode 100644 index 0000000..7d5422e --- /dev/null +++ b/src/lib/ng-noosfero-api/api_descriptor.ts @@ -0,0 +1,4 @@ + + +// TODO - see code generation using swagger resource declaration +// -> https://github.com/swagger-api/swagger-codegen diff --git a/src/lib/ng-noosfero-api/http/http_client.ts b/src/lib/ng-noosfero-api/http/http_client.ts index d09e3e2..7866a12 100644 --- a/src/lib/ng-noosfero-api/http/http_client.ts +++ b/src/lib/ng-noosfero-api/http/http_client.ts @@ -1,12 +1,11 @@ -module NgNoosferoAPI { +namespace NgNoosferoAPI { export class NoosferoHttpClient { static $inject = ['$http', '$q']; - constructor(private $http, private $q) { + constructor(private $http: ng.IHttpService, private $q: ng.IQService) { } } NgNoosferoAPI.ngModule.service(NoosferoHttpClient); -} - +} \ No newline at end of file diff --git a/src/lib/ng-noosfero-api/http/http_config_provider.ts b/src/lib/ng-noosfero-api/http/http_config_provider.ts index 2e351c5..4c90b1b 100644 --- a/src/lib/ng-noosfero-api/http/http_config_provider.ts +++ b/src/lib/ng-noosfero-api/http/http_config_provider.ts @@ -1,5 +1,4 @@ -module NgNoosferoAPI { - +namespace NgNoosferoAPI { // The following class represents the provider export class NoosferoHttpServiceConfigProvider implements ng.IServiceProvider { @@ -12,19 +11,19 @@ module NgNoosferoAPI { contentTypeHeader: "application/json", textEncoding: "UTF-8" }; - - + + // Configuration function public setConfig(config: NoosferoHttpServiceConfig) { this.config = config; } - + // Provider's factory function public $get(): NoosferoHttpServiceConfig { return this.config; } } - + ngModule.provider("NoosferoHttpServiceConfig", NoosferoHttpServiceConfigProvider); } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 2ac2af1..95522c7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "module": "commonjs", "target": "es5", - "noImplicitAny": false, + "noImplicitAny": true, "sourceMap": true, "experimentalDecorators": true }, -- libgit2 0.21.2