diff --git a/src/app/article/article-default-view.component.ts b/src/app/article/article-default-view.component.ts index 2f3a684..e5993f0 100644 --- a/src/app/article/article-default-view.component.ts +++ b/src/app/article/article-default-view.component.ts @@ -5,7 +5,7 @@ import {MacroDirective} from "./macro/macro.directive"; import {ArticleToolbarHotspotComponent} from "../hotspot/article-toolbar-hotspot.component"; import {ArticleContentHotspotComponent} from "../hotspot/article-content-hotspot.component"; import {ArticleService} from "./../../lib/ng-noosfero-api/http/article.service"; - +import {NotificationService} from "./../shared/services/notification.service"; /** * @ngdoc controller * @name ArticleDefaultView @@ -17,15 +17,15 @@ import {ArticleService} from "./../../lib/ng-noosfero-api/http/article.service"; selector: 'noosfero-default-article', templateUrl: 'app/article/article.html' }) -@Inject("$state", ArticleService) +@Inject("$state", ArticleService, NotificationService) export class ArticleDefaultViewComponent { @Input() article: noosfero.Article; @Input() profile: noosfero.Profile; - constructor(private $state: ng.ui.IStateService, public articleService: ArticleService) { + constructor(private $state: ng.ui.IStateService, public articleService: ArticleService, protected notificationService: NotificationService) { // Subscribe to the Article Removed Event - this.articleService.subscribeToArticleRemoved((article: noosfero.Article) => { + this.articleService.subscribeToModelRemoved((article: noosfero.Article) => { if (this.article.parent) { this.$state.transitionTo('main.profile.page', { page: this.article.parent.path, profile: this.article.profile.identifier }); } else { @@ -35,8 +35,9 @@ export class ArticleDefaultViewComponent { } delete() { - this.articleService.removeArticle(this.article).catch((cause: any) => { - throw new Error(`Problem removing the article: ${cause}`); + this.articleService.remove(this.article).catch((cause: any) => { + // TODO - Montar mensagem de erro com a causa + this.notificationService.error({ message: "article.default_view.remove.failed"}); }); } diff --git a/src/app/layout/blocks/recent-documents/recent-documents-block.component.ts b/src/app/layout/blocks/recent-documents/recent-documents-block.component.ts index d051a92..dd5202a 100644 --- a/src/app/layout/blocks/recent-documents/recent-documents-block.component.ts +++ b/src/app/layout/blocks/recent-documents/recent-documents-block.component.ts @@ -1,7 +1,7 @@ import {Component, Inject, Input} from "ng-forward"; import {BlockService} from "../../../../lib/ng-noosfero-api/http/block.service"; -import {ArticleService} from "./../../../../lib/ng-noosfero-api/http/article.service" -import {Arrays} from "./../../../../lib/util/arrays" +import {ArticleService} from "./../../../../lib/ng-noosfero-api/http/article.service"; +import {Arrays} from "./../../../../lib/util/arrays"; @Component({ selector: "noosfero-recent-documents-block", @@ -28,12 +28,12 @@ export class RecentDocumentsBlockComponent { }); this.watchArticles(); } - + watchArticles() { this.articleService.subscribeToArticleRemoved((article: noosfero.Article) => { Arrays.remove(this.documents, article); }); - } + } openDocument(article: any) { this.$state.go("main.profile.page", { page: article.path, profile: article.profile.identifier }); diff --git a/src/app/layout/blocks/statistics/statistics-block.component.ts b/src/app/layout/blocks/statistics/statistics-block.component.ts index a724baa..920ddb3 100644 --- a/src/app/layout/blocks/statistics/statistics-block.component.ts +++ b/src/app/layout/blocks/statistics/statistics-block.component.ts @@ -1,11 +1,28 @@ -import {Input, Inject, Component} from "ng-forward"; - +import {Input, Inject, Component, provide} from "ng-forward"; +import {ArticleService} from "./../../../../lib/ng-noosfero-api/http/article.service"; +import {BlockService} from "./../../../../lib/ng-noosfero-api/http/block.service"; @Component({ selector: "noosfero-statistics-block", templateUrl: 'app/layout/blocks/statistics/statistics-block.html' }) +@Inject(ArticleService, BlockService) export class StatisticsBlockComponent { @Input() block: noosfero.StatisticsBlock; @Input() owner: any; + + constructor(articleService: ArticleService, blockService: BlockService) { + // watches for article being removed + // to update comments and tag statistics, which would + // changed after removing an article + articleService.subscribeToModelRemoved(() => { + blockService.getBlock(this.block.id) + .then(blockFromAPI => this.block = blockFromAPI); + }); + + articleService.subscribeToModelAdded(() => { + blockService.getBlock(this.block.id) + .then(blockFromAPI => this.block = blockFromAPI); + }); + } } diff --git a/src/lib/ng-noosfero-api/http/article.service.ts b/src/lib/ng-noosfero-api/http/article.service.ts index c752687..0805339 100644 --- a/src/lib/ng-noosfero-api/http/article.service.ts +++ b/src/lib/ng-noosfero-api/http/article.service.ts @@ -7,8 +7,6 @@ import {NoosferoRootScope} from "./../../../app/shared/models/interfaces"; @Inject("Restangular", "$q", "$log", ProfileService) export class ArticleService extends RestangularService { - private articleRemoved: EventEmitter = new EventEmitter(); - constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService, protected profileService: ProfileService) { super(Restangular, $q, $log); } @@ -24,28 +22,22 @@ export class ArticleService extends RestangularService { }; } - removeArticle(article: noosfero.Article) { - let restRequest: ng.IPromise> = this.remove(article); - let deferred = this.$q.defer>(); - restRequest.then((result: any) => { - this.notifyArticleRemovedListeners(article); - }).catch(this.getHandleErrorFunction(deferred)); - return deferred.promise; - } + // removeArticle(article: noosfero.Article) { + // // let restRequest: ng.IPromise> = this.remove(article); + // // let deferred = this.$q.defer>(); + // // restRequest.then((result: any) => { + // // this.notifyArticleRemovedListeners(article); + // // }).catch(this.getHandleErrorFunction(deferred)); + // // return deferred.promise; + // } /** * Notify listeners that this article has been removed */ - private notifyArticleRemovedListeners(article: noosfero.Article) { - this.articleRemoved.next(article); - } + // private notifyArticleRemovedListeners(article: noosfero.Article) { + // this.modelRemovedEventEmitter.next(article); + // } - /** - * subscribes to the ArticleRemoved event emitter - */ - subscribeToArticleRemoved(fn: Function) { - this.articleRemoved.subscribe(fn); - } updateArticle(article: noosfero.Article) { let headers = { diff --git a/src/lib/ng-noosfero-api/http/block.service.ts b/src/lib/ng-noosfero-api/http/block.service.ts index f8b0378..de768dd 100644 --- a/src/lib/ng-noosfero-api/http/block.service.ts +++ b/src/lib/ng-noosfero-api/http/block.service.ts @@ -37,4 +37,14 @@ export class BlockService extends RestangularService { return apiContentPromise.promise; } + getBlock(blockId: number): ng.IPromise { + let deferred = this.$q.defer(); + this.get(blockId) + .then((result: noosfero.RestResult) => { + deferred.resolve(result.data); + }) + .catch(reason => deferred.reject(reason)); + return deferred.promise; + } + } diff --git a/src/lib/ng-noosfero-api/http/restangular_service.ts b/src/lib/ng-noosfero-api/http/restangular_service.ts index 46db39b..cc8d8ba 100644 --- a/src/lib/ng-noosfero-api/http/restangular_service.ts +++ b/src/lib/ng-noosfero-api/http/restangular_service.ts @@ -1,3 +1,4 @@ +import {EventEmitter} from "ng-forward"; /** * @name RestangularService * Base class to be extended by classes which will provide access @@ -13,6 +14,11 @@ export abstract class RestangularService { private baseResource: restangular.IElement; private currentPromise: ng.IDeferred; + protected modelFoundEventEmitter: EventEmitter = new EventEmitter(); + protected modelAddedEventEmitter: EventEmitter = new EventEmitter(); + protected modelRemovedEventEmitter: EventEmitter = new EventEmitter(); + protected modelUpdatedEventEmitter: EventEmitter = new EventEmitter(); + /** * Creates an instance of RestangularService. * @@ -35,6 +41,22 @@ export abstract class RestangularService { // }); } + subscribeToModelRemoved(fn: ((model: T) => void)) { + this.modelRemovedEventEmitter.subscribe(fn); + } + + subscribeToModelAdded(fn: ((model: T) => void)) { + this.modelAddedEventEmitter.subscribe(fn); + } + + subscribeToModelUpdated(fn: ((model: T) => void)) { + this.modelUpdatedEventEmitter.subscribe(fn); + } + + subscribeToModelFound(fn: ((model: T) => void)) { + this.modelFoundEventEmitter.subscribe(fn); + } + public resetCurrent() { this.currentPromise = this.$q.defer(); } @@ -107,7 +129,7 @@ export abstract class RestangularService { restRequest = this.restangularService.one(this.getResourcePath(), id).get(queryParams, headers); } - restRequest.then(this.getHandleSuccessFunction(deferred)) + restRequest.then(this.getHandleSuccessFunction(deferred, this.modelFoundEventEmitter)) .catch(this.getHandleErrorFunction(deferred)); @@ -201,7 +223,7 @@ export abstract class RestangularService { restRequest = restangularObj.remove(queryParams, headers); restRequest - .then(this.getHandleSuccessFunction(deferred)) + .then(this.getHandleSuccessFunction(deferred, this.modelRemovedEventEmitter)) .catch(this.getHandleErrorFunction(deferred)); return deferred.promise; @@ -226,7 +248,7 @@ export abstract class RestangularService { restRequest = restangularObj.put(queryParams, headers); - restRequest.then(this.getHandleSuccessFunction(deferred)) + restRequest.then(this.getHandleSuccessFunction(deferred, this.modelUpdatedEventEmitter)) .catch(this.getHandleErrorFunction(deferred)); return deferred.promise; @@ -255,7 +277,7 @@ export abstract class RestangularService { restRequest = this.baseResource.post(data, queryParams, headers); } - restRequest.then(this.getHandleSuccessFunction(deferred)) + restRequest.then(this.getHandleSuccessFunction(deferred, this.modelAddedEventEmitter)) .catch(this.getHandleErrorFunction(deferred)); return deferred.promise; @@ -289,7 +311,7 @@ export abstract class RestangularService { } /** HANDLERS */ - protected getHandleSuccessFunction(deferred: ng.IDeferred>, responseKey?: string): (response: restangular.IResponse) => void { + protected getHandleSuccessFunction(deferred: ng.IDeferred>, successEmitter: EventEmitter = null): (response: restangular.IResponse) => void { let self = this; /** @@ -301,7 +323,13 @@ export abstract class RestangularService { if (self.$log) { self.$log.debug("Request successfull executed", response.data, self, response); } - deferred.resolve(this.extractData(response)); + let resultModel: noosfero.RestResult = this.extractData(response); + // resolve the promise with the model returned from the Noosfero API + deferred.resolve(resultModel); + // emits the event if a successEmiter was provided in the successEmitter parameter + if (successEmitter !== null) { + successEmitter.next(resultModel); + } }; return successFunction; } -- libgit2 0.21.2