diff --git a/src/app/article/comment/comment.component.spec.ts b/src/app/article/comment/comment.component.spec.ts
index e0f2fca..c5bf006 100644
--- a/src/app/article/comment/comment.component.spec.ts
+++ b/src/app/article/comment/comment.component.spec.ts
@@ -1,8 +1,6 @@
import {Provider, provide, Component} from 'ng-forward';
import * as helpers from "../../../spec/helpers";
-
import {CommentComponent} from './comment.component';
-import {PostCommentComponent} from './post-comment/post-comment.component';
const htmlTemplate: string = '';
@@ -11,12 +9,9 @@ describe("Components", () => {
beforeEach(angular.mock.module("templates"));
- let postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["subscribe"]);
function createComponent() {
- let providers = [
- new Provider('PostCommentEventService', { useValue: postCommentEventService })
- ].concat(helpers.provideFilters("translateFilter"));
+ let providers = helpers.provideFilters("translateFilter");
@Component({ selector: 'test-container-component', directives: [CommentComponent], template: htmlTemplate, providers: providers })
class ContainerComponent {
@@ -44,21 +39,16 @@ describe("Components", () => {
createComponent().then(fixture => {
let component: CommentComponent = fixture.debugElement.componentViewChildren[0].componentInstance;
component.reply();
- expect(component.showReply).toBeTruthy(1);
+ expect(component.showReply()).toBeTruthy("Reply was expected to be true");
done();
});
});
- it("close form when receive a reply", done => {
- let func: Function;
- postCommentEventService.subscribe = (fn: Function) => {
- func = fn;
- };
+ it("show reply relies on current comment __showReply attribute", done => {
createComponent().then(fixture => {
let component = fixture.debugElement.componentViewChildren[0];
- component.componentInstance.showReply = true;
- func({});
- expect(component.componentInstance.showReply).toEqual(false);
+ component.componentInstance.comment.__showReply = false;
+ expect(component.componentInstance.showReply()).toEqual(false);
done();
});
});
diff --git a/src/app/article/comment/comment.component.ts b/src/app/article/comment/comment.component.ts
index ac74189..a6092f5 100644
--- a/src/app/article/comment/comment.component.ts
+++ b/src/app/article/comment/comment.component.ts
@@ -1,27 +1,24 @@
-import { Inject, Input, Component } from 'ng-forward';
+import { Inject, Input, Component, Output, EventEmitter } from 'ng-forward';
import { PostCommentComponent } from "./post-comment/post-comment.component";
-import { PostCommentEventService } from "./post-comment/post-comment-event.service";
@Component({
selector: 'noosfero-comment',
templateUrl: 'app/article/comment/comment.html'
})
-@Inject(PostCommentEventService, "$scope")
export class CommentComponent {
- @Input() comment: noosfero.Comment;
+ @Input() comment: noosfero.CommentViewModel;
@Input() article: noosfero.Article;
- showReply: boolean = false;
+ showReply() {
+ return this.comment && this.comment.__show_reply === true;
+ }
- constructor(postCommentEventService: PostCommentEventService, private $scope: ng.IScope) {
- postCommentEventService.subscribe((comment: noosfero.Comment) => {
- this.showReply = false;
- this.$scope.$apply();
- });
+ constructor() {
}
+
reply() {
- this.showReply = !this.showReply;
+ this.comment.__show_reply = !this.comment.__show_reply;
}
}
diff --git a/src/app/article/comment/comment.html b/src/app/article/comment/comment.html
index 7cf9b96..d23f5f6 100644
--- a/src/app/article/comment/comment.html
+++ b/src/app/article/comment/comment.html
@@ -17,5 +17,5 @@
{{"comment.reply" | translate}}
-
+
diff --git a/src/app/article/comment/comments.component.spec.ts b/src/app/article/comment/comments.component.spec.ts
index 17541af..2c33512 100644
--- a/src/app/article/comment/comments.component.spec.ts
+++ b/src/app/article/comment/comments.component.spec.ts
@@ -17,21 +17,13 @@ describe("Components", () => {
commentService.getByArticle = jasmine.createSpy("getByArticle")
.and.returnValue(helpers.mocks.promiseResultTemplate({ data: comments }));
- let emitEvent: Function;
- let postCommentEventService = {
- subscribe: (fn: Function) => {
- emitEvent = fn;
- }
- };
-
let properties = { article: { id: 1 }, parent: null };
function createComponent() {
// postCommentEventService = jasmine.createSpyObj("postCommentEventService", ["subscribe"]);
let providers = [
helpers.createProviderToValue('CommentService', commentService),
helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService),
- helpers.createProviderToValue('SessionService', helpers.mocks.sessionWithCurrentUser({})),
- new Provider('PostCommentEventService', { useValue: postCommentEventService })
+ helpers.createProviderToValue('SessionService', helpers.mocks.sessionWithCurrentUser({}))
].concat(helpers.provideFilters("translateFilter"));
return helpers.quickCreateComponent({
@@ -60,7 +52,8 @@ describe("Components", () => {
it("update comments list when receive an reply", done => {
properties.parent = { id: 3 };
createComponent().then(fixture => {
- emitEvent({ id: 1, reply_of: { id: 3 } });
+ (fixture.debugElement.componentViewChildren[0].componentInstance).commentAdded({ id: 1, reply_of: { id: 3 } });
+ fixture.detectChanges();
expect(fixture.debugElement.queryAll("noosfero-comment").length).toEqual(3);
done();
});
diff --git a/src/app/article/comment/comments.component.ts b/src/app/article/comment/comments.component.ts
index f1612e9..3317ae7 100644
--- a/src/app/article/comment/comments.component.ts
+++ b/src/app/article/comment/comments.component.ts
@@ -1,26 +1,30 @@
-import { Inject, Input, Component, provide } from 'ng-forward';
+import { Inject, Input, Output, Component, provide, EventEmitter } from 'ng-forward';
+import {INgForwardJQuery} from "ng-forward/cjs/util/jqlite-extensions";
+
+
import { PostCommentComponent } from "./post-comment/post-comment.component";
import { CommentService } from "../../../lib/ng-noosfero-api/http/comment.service";
import { CommentComponent } from "./comment.component";
-import { PostCommentEventService } from "./post-comment/post-comment-event.service";
@Component({
selector: 'noosfero-comments',
templateUrl: 'app/article/comment/comments.html',
- directives: [PostCommentComponent, CommentComponent]
+ directives: [PostCommentComponent, CommentComponent],
+ outputs: ['commentAdded']
})
-@Inject(CommentService, PostCommentEventService, "$scope")
+@Inject(CommentService, "$element")
export class CommentsComponent {
- comments: noosfero.Comment[] = [];
+ comments: noosfero.CommentViewModel[] = [];
@Input() showForm = true;
@Input() article: noosfero.Article;
- @Input() parent: noosfero.Comment;
+ @Input() parent: noosfero.CommentViewModel;
+
protected page = 1;
protected perPage = 5;
protected total = 0;
- constructor(protected commentService: CommentService, private postCommentEventService: PostCommentEventService, private $scope: ng.IScope) { }
+ constructor(protected commentService: CommentService) { }
ngOnInit() {
if (this.parent) {
@@ -28,13 +32,20 @@ export class CommentsComponent {
} else {
this.loadNextPage();
}
- this.postCommentEventService.subscribe((comment: noosfero.Comment) => {
- if ((!this.parent && !comment.reply_of) || (comment.reply_of && this.parent && comment.reply_of.id === this.parent.id)) {
- if (!this.comments) this.comments = [];
- this.comments.push(comment);
- this.$scope.$apply();
- }
+ }
+
+ commentAdded(comment: noosfero.Comment): void {
+ this.comments.push(comment);
+ this.resetShowReply();
+ }
+
+ private resetShowReply() {
+ this.comments.forEach((comment: noosfero.CommentViewModel) => {
+ comment.__show_reply = false;
});
+ if (this.parent) {
+ this.parent.__show_reply = false;
+ }
}
loadComments() {
diff --git a/src/app/article/comment/comments.html b/src/app/article/comment/comments.html
index 09137da..347da4b 100644
--- a/src/app/article/comment/comments.html
+++ b/src/app/article/comment/comments.html
@@ -1,5 +1,5 @@