diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts index f0354f17b6..92ab99d366 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.spec.ts @@ -83,7 +83,7 @@ describe('Test ng2-alfresco-tag Tag actions list', () => { it('Tag list relative a single node should be rendered', (done) => { component.nodeId = 'fake-node-id'; - component.resultsEmitter.subscribe(() => { + component.result.subscribe(() => { fixture.detectChanges(); expect(element.querySelector('#tag_name_0').innerHTML).toBe('test1'); @@ -109,7 +109,7 @@ describe('Test ng2-alfresco-tag Tag actions list', () => { it('Tag list click on delete button should delete the tag', (done) => { component.nodeId = 'fake-node-id'; - component.resultsEmitter.subscribe(() => { + component.result.subscribe(() => { fixture.detectChanges(); let deleteButton: any = element.querySelector('#tag_delete_0'); @@ -142,11 +142,11 @@ describe('Test ng2-alfresco-tag Tag actions list', () => { fixture.detectChanges(); - component.addEmitter.subscribe(() => { + component.successAdd.subscribe(() => { done(); }); - component.resultsEmitter.subscribe(() => { + component.result.subscribe(() => { fixture.detectChanges(); let addButton: any = element.querySelector('#add-tag'); @@ -173,12 +173,12 @@ describe('Test ng2-alfresco-tag Tag actions list', () => { fixture.detectChanges(); - component.addEmitter.subscribe(() => { + component.successAdd.subscribe(() => { expect(component.newTagName).toBe(''); done(); }); - component.resultsEmitter.subscribe(() => { + component.result.subscribe(() => { fixture.detectChanges(); let addButton: any = element.querySelector('#add-tag'); @@ -207,11 +207,37 @@ describe('Test ng2-alfresco-tag Tag actions list', () => { done(); }); + it('Add tag should return an error if the tag is already present', (done) => { + component.nodeId = 'fake-node-id'; + component.newTagName = 'test1'; + + fixture.detectChanges(); + + component.error.subscribe(() => { + done(); + }); + + component.result.subscribe(() => { + fixture.detectChanges(); + + let addButton: any = element.querySelector('#add-tag'); + addButton.click(); + }); + + component.ngOnChanges(); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: dataTag + }); + }); + it('Add tag should be disabled if the node id is not a correct node', (done) => { component.nodeId = 'fake-node-id'; component.newTagName = 'fake-tag-name'; - component.resultsEmitter.subscribe(() => { + component.result.subscribe(() => { let addButton: any = element.querySelector('#add-tag'); expect(addButton.disabled).toEqual(true); done(); @@ -228,7 +254,7 @@ describe('Test ng2-alfresco-tag Tag actions list', () => { component.nodeId = 'fake-node-id'; component.newTagName = 'fake-tag-name'; - component.resultsEmitter.subscribe(() => { + component.result.subscribe(() => { fixture.detectChanges(); let addButton: any = element.querySelector('#add-tag'); diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.ts index a3ccbfbbb4..f58f321996 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-actions.component.ts @@ -41,10 +41,13 @@ export class TagActionsComponent { isContextMenu: boolean = false; @Output() - addEmitter: EventEmitter = new EventEmitter(); + successAdd: EventEmitter = new EventEmitter(); @Output() - resultsEmitter = new EventEmitter(); + error: EventEmitter = new EventEmitter(); + + @Output() + result = new EventEmitter(); newTagName: string; @@ -58,6 +61,10 @@ export class TagActionsComponent { if (translateService) { translateService.addTranslationFolder('ng2-alfresco-tag', 'node_modules/ng2-alfresco-tag/src'); } + + this.tagService.refresh.subscribe(() => { + this.refreshTag(); + }); } ngOnChanges() { @@ -68,11 +75,11 @@ export class TagActionsComponent { this.tagService.getTagsByNodeId(this.nodeId).subscribe((data) => { this.tagsEntries = data.list.entries; this.disableAddTag = false; - this.resultsEmitter.emit(this.tagsEntries); + this.result.emit(this.tagsEntries); }, () => { this.tagsEntries = null; this.disableAddTag = true; - this.resultsEmitter.emit(this.tagsEntries); + this.result.emit(this.tagsEntries); }); } @@ -81,13 +88,13 @@ export class TagActionsComponent { this.translateService.get('TAG.MESSAGES.EXIST').subscribe((error) => { this.errorMsg = error; }); + this.error.emit(this.errorMsg); + }else { + this.tagService.addTag(this.nodeId, this.newTagName).subscribe(() => { + this.newTagName = ''; + this.successAdd.emit(this.nodeId); + }); } - - this.tagService.addTag(this.nodeId, this.newTagName).subscribe(() => { - this.newTagName = ''; - this.refreshTag(); - this.addEmitter.emit(this.nodeId); - }); } searchTag(searchTagName: string) { @@ -103,8 +110,6 @@ export class TagActionsComponent { } removeTag(tag: string) { - this.tagService.removeTag(this.nodeId, tag).subscribe(() => { - this.refreshTag(); - }); + this.tagService.removeTag(this.nodeId, tag); } } diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts index 644425d10f..d0b9c0cf3b 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.spec.ts @@ -82,7 +82,7 @@ describe('Test ng2-alfresco-tag Tag list All ECM', () => { it('Tag list relative a single node should be rendered', (done) => { component.nodeId = 'fake-node-id'; - component.resultsEmitter.subscribe(() => { + component.result.subscribe(() => { fixture.detectChanges(); expect(element.querySelector('#tag_name_0').innerHTML).toBe('test1'); diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.ts index 3afe4187f6..e6ff780766 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-list.component.ts @@ -35,23 +35,26 @@ export class TagList { tagsEntries: any; @Output() - resultsEmitter = new EventEmitter(); + result = new EventEmitter(); /** * Constructor * @param tagService */ constructor(private tagService: TagService) { + this.tagService.refresh.subscribe(() => { + this.refreshTag(); + }); } - ngOnInit(changes) { - return this.refreshTagEcm(); + ngOnInit() { + return this.refreshTag(); } - refreshTagEcm() { + refreshTag() { this.tagService.getAllTheTags().subscribe((data) => { this.tagsEntries = data.list.entries; - this.resultsEmitter.emit(this.tagsEntries); + this.result.emit(this.tagsEntries); }); } } diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts index c17ec7a2e3..809c75f794 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.spec.ts @@ -82,7 +82,7 @@ describe('Test ng2-alfresco-tag Tag relative node list', () => { it('Tag list relative a single node should be rendered', (done) => { component.nodeId = 'fake-node-id'; - component.resultsEmitter.subscribe(() => { + component.results.subscribe(() => { fixture.detectChanges(); expect(element.querySelector('#tag_name_0').innerHTML).toBe('test1'); @@ -108,7 +108,7 @@ describe('Test ng2-alfresco-tag Tag relative node list', () => { it('Tag list click on delete button should delete the tag', (done) => { component.nodeId = 'fake-node-id'; - component.resultsEmitter.subscribe(() => { + component.results.subscribe(() => { fixture.detectChanges(); let deleteButton: any = element.querySelector('#tag_delete_0'); diff --git a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.ts b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.ts index 84d16b8c97..e4469b56cd 100644 --- a/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.ts +++ b/ng2-components/ng2-alfresco-tag/src/components/tag-node-list.component.ts @@ -39,13 +39,16 @@ export class TagNodeList { tagsEntries: any; @Output() - resultsEmitter = new EventEmitter(); + results = new EventEmitter(); /** * Constructor * @param tagService */ constructor(private tagService: TagService) { + this.tagService.refresh.subscribe(() => { + this.refreshTag(); + }); } ngOnChanges() { @@ -55,7 +58,7 @@ export class TagNodeList { refreshTag() { this.tagService.getTagsByNodeId(this.nodeId).subscribe((data) => { this.tagsEntries = data.list.entries; - this.resultsEmitter.emit(this.tagsEntries); + this.results.emit(this.tagsEntries); }); } diff --git a/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts b/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts index d60dfcd73a..0e8480629b 100644 --- a/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts +++ b/ng2-components/ng2-alfresco-tag/src/services/tag.service.spec.ts @@ -118,5 +118,30 @@ describe('Tag service', () => { status: 403 }); }); + + it('delete tag should trigger a refresh event', (done) => { + service.refresh.subscribe(() => { + done(); + }); + + service.removeTag('fake-node-id', 'fake-tag'); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200 + }); + }); + + it('add tag should trigger a refresh event', (done) => { + service.refresh.subscribe(() => { + done(); + }); + + service.addTag('fake-node-id', 'fake-tag'); + + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200 + }); + }); }); + }); diff --git a/ng2-components/ng2-alfresco-tag/src/services/tag.service.ts b/ng2-components/ng2-alfresco-tag/src/services/tag.service.ts index 6027115b11..a3bec077b0 100644 --- a/ng2-components/ng2-alfresco-tag/src/services/tag.service.ts +++ b/ng2-components/ng2-alfresco-tag/src/services/tag.service.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { Injectable } from '@angular/core'; +import { Injectable, Output, EventEmitter } from '@angular/core'; import { Observable } from 'rxjs/Rx'; import { AlfrescoApiService, LogService } from 'ng2-alfresco-core'; @@ -25,6 +25,9 @@ import { AlfrescoApiService, LogService } from 'ng2-alfresco-core'; @Injectable() export class TagService { + @Output() + refresh = new EventEmitter(); + constructor(private apiService: AlfrescoApiService, private logService: LogService) { } @@ -44,13 +47,27 @@ export class TagService { let tagBody = new alfrescoApi.core.TagBody(); tagBody.tag = tagName; - return Observable.fromPromise(this.apiService.getInstance().core.tagsApi.addTag(nodeId, tagBody)) - .catch(err => this.handleError(err)); + let promiseAdd = Observable.fromPromise(this.apiService.getInstance().core.tagsApi.addTag(nodeId, tagBody)); + + promiseAdd.subscribe((data) => { + this.refresh.emit(data); + }, (err) => { + this.handleError(err); + }); + + return promiseAdd; } removeTag(nodeId: string, tag: string): any { - return Observable.fromPromise(this.apiService.getInstance().core.tagsApi.removeTag(nodeId, tag)) - .catch(err => this.handleError(err)); + let promiseRemove = Observable.fromPromise(this.apiService.getInstance().core.tagsApi.removeTag(nodeId, tag)); + + promiseRemove.subscribe((data) => { + this.refresh.emit(data); + }, (err) => { + this.handleError(err); + }); + + return promiseRemove; } private handleError(error: any) {