Commit 0716bd47e66cbf3da6a3289d204353e7f5f38d0d

Authored by Carlos Purificação
1 parent c1e0c406

Added environment component

src/app/environment/environment-home.component.ts 0 → 100644
... ... @@ -0,0 +1,41 @@
  1 +import {Component, Inject, provide} from 'ng-forward';
  2 +import {EnvironmentService} from "../../lib/ng-noosfero-api/http/environment.service";
  3 +import {NotificationService} from "../shared/services/notification.service";
  4 +
  5 +/**
  6 + * @ngdoc controller
  7 + * @name environment.Environment
  8 + * @description
  9 + * This is the environment controller.
  10 + */
  11 +@Component({
  12 + selector: 'environment-home',
  13 + templateUrl: "app/environment/environment-home.html",
  14 + providers: [
  15 + provide('environmentService', { useClass: EnvironmentService }),
  16 + provide('notificationService', { useClass: NotificationService })
  17 + ]
  18 +})
  19 +@Inject(EnvironmentService, "$log", "$sce")
  20 +export class EnvironmentHomeComponent {
  21 +
  22 + environment: noosfero.Environment;
  23 +
  24 + constructor(private environmentService: EnvironmentService, private $sce: ng.ISCEService) {
  25 + console.debug("Constructor from EnvironmentHomeComponent");
  26 +
  27 + environmentService.getByIdentifier("default").then((result: noosfero.Environment) => {
  28 + this.environment = result;
  29 + console.debug("Environment ::", result);
  30 + })
  31 + }
  32 +
  33 + getEnvironmentDescription() {
  34 + if(this.environment && this.environment.settings && this.environment.settings.description){
  35 + return this.$sce.trustAsHtml(this.environment.settings.description);
  36 + }
  37 + else {
  38 + return "";
  39 + }
  40 + }
  41 +}
0 42 \ No newline at end of file
... ...
src/app/environment/environment-home.html 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +<div id="environment-description" ng-bind-html="vm.getEnvironmentDescription()" class="environment-description">
  2 +</div>
0 3 \ No newline at end of file
... ...
src/app/environment/environment.component.ts 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +import {StateConfig, Component, Inject, provide} from 'ng-forward';
  2 +import {EnvironmentService} from "../../lib/ng-noosfero-api/http/environment.service";
  3 +import {NotificationService} from "../shared/services/notification.service";
  4 +import {EnvironmentHomeComponent} from "./environment-home.component";
  5 +
  6 +/**
  7 + * @ngdoc controller
  8 + * @name environment.Environment
  9 + * @description
  10 + * This is the environment controller.
  11 + */
  12 +@Component({
  13 + selector: 'environment',
  14 + templateUrl: "app/environment/environment.html",
  15 + providers: [
  16 + provide('environmentService', { useClass: EnvironmentService }),
  17 + provide('notificationService', { useClass: NotificationService })
  18 + ]
  19 +})
  20 +@StateConfig([
  21 + {
  22 + name: 'main.environment.home',
  23 + url: "",
  24 + component: EnvironmentHomeComponent,
  25 + views: {
  26 + "mainBlockContent": {
  27 + templateUrl: "app/environment/environment-home.html",
  28 + controller: EnvironmentHomeComponent,
  29 + controllerAs: "vm"
  30 + }
  31 + }
  32 + }
  33 +])
  34 +@Inject(EnvironmentService, "$state")
  35 +export class EnvironmentComponent {
  36 +
  37 + boxes: noosfero.Box[];
  38 + environment: noosfero.Environment;
  39 +
  40 + constructor(environmentService: EnvironmentService, $state: ng.ui.IStateService, notificationService: NotificationService) {
  41 + //console.debug("Creating EnvironmentComponent...");
  42 + let boxesPromisse = environmentService.getByIdentifier("default").then((environment: noosfero.Environment) => {
  43 + //console.debug("Set current environment by identifier callback.: ", environment);
  44 + this.environment = environment;
  45 + return environmentService.getBoxes(this.environment.id);
  46 + }).then((boxes: noosfero.Box[]) => {
  47 + //console.debug("Set environment boxes in callback: ", boxes);
  48 + this.boxes = boxes;
  49 + }).catch(() => {
  50 + $state.transitionTo('main');
  51 + notificationService.error({ message: "notification.environment.not_found" });
  52 + });
  53 + }
  54 +}
... ...
src/app/environment/environment.html 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +<div class="environment-container">
  2 + <div class="row">
  3 + <noosfero-boxes [boxes]="vm.boxes" [owner]="vm.environment"></noosfero-boxes>
  4 + </div>
  5 +</div>
... ...
src/app/main/main.component.ts
... ... @@ -6,12 +6,16 @@ import {ArticleViewComponent} from &quot;./../article/article-default-view.component&quot;
6 6 import {ProfileComponent} from "../profile/profile.component";
7 7 import {BoxesComponent} from "../layout/boxes/boxes.component";
8 8 import {BlockComponent} from "../layout/blocks/block.component";
  9 +import {EnvironmentComponent} from "../environment/environment.component";
  10 +import {EnvironmentHomeComponent} from "../environment/environment-home.component";
  11 +
9 12 import {LinkListBlockComponent} from "./../layout/blocks/link-list/link-list.component";
10 13 import {RecentDocumentsBlockComponent} from "../layout/blocks/recent-documents/recent-documents.component";
11 14 import {ProfileImageBlockComponent} from "../layout/blocks/profile-image-block/profile-image-block.component";
12 15 import {RawHTMLBlockComponent} from "../layout/blocks/raw-html/raw-html.component";
13 16  
14 17 import {MembersBlockComponent} from "./../layout/blocks/members-block/members-block.component";
  18 +import {PeopleBlockComponent} from "./../layout/blocks/people-block/people-block.component";
15 19 import {NoosferoTemplate} from "../shared/pipes/noosfero-template.filter";
16 20 import {DateFormat} from "../shared/pipes/date-format.filter";
17 21  
... ... @@ -34,7 +38,7 @@ import {MainBlockComponent} from &quot;../layout/blocks/main-block/main-block.compone
34 38 * This controller actually contains the main content of Noosfero Angular Theme:
35 39 * - the navbar
36 40 * - the {@link Main} view content
37   - *
  41 + *
38 42 */
39 43 @Component({
40 44 selector: 'main-content',
... ... @@ -45,6 +49,15 @@ export class MainContentComponent {
45 49  
46 50 }
47 51  
  52 +@Component({
  53 + selector: 'environment-content',
  54 + templateUrl: "app/main/main.html",
  55 + providers: [AuthService, SessionService]
  56 +})
  57 +export class EnvironmentContent {
  58 +
  59 +}
  60 +
48 61 /**
49 62 * @ngdoc controller
50 63 * @name main.Main
... ... @@ -63,19 +76,35 @@ export class MainContentComponent {
63 76 selector: 'main',
64 77 template: '<div ng-view></div>',
65 78 directives: [
66   - ArticleBlogComponent, ArticleViewComponent, BoxesComponent, BlockComponent, LinkListBlockComponent,
  79 + ArticleBlogComponent, ArticleViewComponent, BoxesComponent, BlockComponent,
  80 + EnvironmentComponent,
  81 + LinkListBlockComponent,
67 82 MainBlockComponent, RecentDocumentsBlockComponent, Navbar, ProfileImageBlockComponent,
68   - MembersBlockComponent, NoosferoTemplate, DateFormat, RawHTMLBlockComponent
  83 + MembersBlockComponent, PeopleBlockComponent, NoosferoTemplate, DateFormat, RawHTMLBlockComponent
69 84 ],
70 85 providers: [AuthService, SessionService, NotificationService]
71 86 })
72 87 @StateConfig([
73 88 {
74   - url: '/',
75   - component: MainContentComponent,
  89 + url: '',
  90 + component: MainContentComponent,
  91 + abstract: true,
76 92 name: 'main',
77 93 },
78 94 {
  95 + url: '/',
  96 + component: EnvironmentComponent,
  97 + name: 'main.environment',
  98 + abstract: true,
  99 + views: {
  100 + "content": {
  101 + templateUrl: "app/environment/environment.html",
  102 + controller: EnvironmentComponent,
  103 + controllerAs: "vm"
  104 + }
  105 + }
  106 + },
  107 + {
79 108 url: "^/:profile",
80 109 abstract: true,
81 110 component: ProfileComponent,
... ...
src/lib/ng-noosfero-api/http/environment.service.ts 0 → 100644
... ... @@ -0,0 +1,98 @@
  1 +import { Injectable, Inject } from "ng-forward";
  2 +
  3 +@Injectable()
  4 +@Inject("Restangular", "$q")
  5 +export class EnvironmentService {
  6 +
  7 + private _currentEnvironmentPromise: ng.IDeferred<noosfero.Environment>;
  8 +
  9 + constructor(private restangular: restangular.IService, private $q: ng.IQService) {
  10 +
  11 + }
  12 +
  13 + getEnvironmentPeople(params: any) : ng.IPromise<noosfero.Person[]> {
  14 + let p = this.restangular.one('people').get(params);
  15 + let deferred = this.$q.defer<noosfero.Person[]>();
  16 + p.then(this.getHandleSuccessFunctionKeyArray<noosfero.Person[]>("people", deferred));
  17 + p.catch(this.getHandleErrorFunction<noosfero.Person[]>(deferred));
  18 + return deferred.promise;
  19 + }
  20 +
  21 + getByIdentifier(identifier: string): ng.IPromise<noosfero.Environment> {
  22 + console.debug("Getting the current environment by identifier in service: " + identifier);
  23 + let p = this.restangular.one('environment').customGET(identifier);
  24 + console.debug("Return promise: ", p);
  25 +
  26 + let deferred = this.$q.defer<noosfero.Environment>();
  27 + p.then(this.getHandleSuccessFunction<noosfero.Environment>(deferred));
  28 + p.catch(this.getHandleErrorFunction<noosfero.Environment>(deferred));
  29 + return deferred.promise;
  30 + }
  31 +
  32 + getBoxes(id: number) {
  33 + console.debug("Getting the environment [${id}] boxes in service", id);
  34 + let p = this.restangular.one('environments', id).customGET("boxes");
  35 + console.debug("Return boxes promise in service: ", p);
  36 +
  37 + let deferred = this.$q.defer<noosfero.Box[]>();
  38 + p.then(this.getHandleSuccessFunctionKeyArray<noosfero.Box[]>("boxes", deferred));
  39 + p.catch(this.getHandleErrorFunction<noosfero.Box[]>(deferred));
  40 + return deferred.promise;
  41 + }
  42 +
  43 + /** TODO - Please, use the base class RestangularService
  44 + * (description)
  45 + *
  46 + * @template T
  47 + * @param {ng.IDeferred<T>} deferred (description)
  48 + * @returns {(response: restangular.IResponse) => void} (description)
  49 + */
  50 + getHandleErrorFunction<T>(deferred: ng.IDeferred<T>): (response: restangular.IResponse) => void {
  51 + let self = this;
  52 + /**
  53 + * (description)
  54 + *
  55 + * @param {restangular.IResponse} response (description)
  56 + */
  57 + let errorFunction = (response: restangular.IResponse): void => {
  58 + deferred.reject(response);
  59 + };
  60 + return errorFunction;
  61 + }
  62 +
  63 + /**
  64 + * TODO - use restangular service as base class, and this will not be necessary here anymore
  65 + */
  66 + protected getHandleSuccessFunction<C>(deferred: ng.IDeferred<C>, responseKey?: string): (response: restangular.IResponse) => void {
  67 + let self = this;
  68 +
  69 + /**
  70 + * (description)
  71 + *
  72 + * @param {restangular.IResponse} response (description)
  73 + */
  74 + let successFunction = (response: restangular.IResponse): void => {
  75 + let data = this.restangular.stripRestangular(response.data)
  76 + deferred.resolve(data);
  77 + };
  78 + return successFunction;
  79 + }
  80 +
  81 + /**
  82 + * TODO - use restangular service as base class, and this will not be necessary here anymore
  83 + */
  84 + protected getHandleSuccessFunctionKeyArray<C>(key: string, deferred: ng.IDeferred<C>, responseKey?: string): (response: restangular.IResponse) => void {
  85 + let self = this;
  86 +
  87 + /**
  88 + * (description)
  89 + *
  90 + * @param {restangular.IResponse} response (description)
  91 + */
  92 + let successFunction = (response: restangular.IResponse): void => {
  93 + let data = this.restangular.stripRestangular(response.data[key]);
  94 + deferred.resolve(data);
  95 + };
  96 + return successFunction;
  97 + }
  98 +}
... ...
src/lib/ng-noosfero-api/interfaces/environment.ts
1 1  
2 2 namespace noosfero {
  3 + /**
  4 + * @ngdoc interface
  5 + * @name noofero.Environment
  6 + * @description
  7 + * A representation of a Noosfero Environment.
  8 + */
3 9 export interface Environment extends RestModel {
  10 + /**
  11 + * @ngdoc property
  12 + * @name id
  13 + * @propertyOf noofero.Environment
  14 + * @returns {number} The Environment id
  15 + */
  16 + id: number;
  17 + settings: any
4 18 }
5 19 }
6 20 \ No newline at end of file
... ...