diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.html b/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.html index 5d2a22bd40..3487f5446d 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.html +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.html @@ -57,6 +57,7 @@ [allowDropFiles]="false" [enablePagination]="!showingSearchResults" (folderChange)="onFolderChange($event)" + (ready)="onFolderLoaded()" data-automation-id="content-node-selector-document-list"> diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.spec.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.spec.ts index 7fe30d8868..26351f63e9 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.spec.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.spec.ts @@ -476,6 +476,33 @@ describe('ContentNodeSelectorComponent', () => { }); })); + it('should reload the original documentlist when clearing the search input', async(() => { + typeToSearchBox('shenron'); + respondWithSearchResults(ONE_FOLDER_RESULT); + + fixture.whenStable().then(() => { + typeToSearchBox(''); + fixture.detectChanges(); + + let documentList = fixture.debugElement.query(By.css('[data-automation-id="content-node-selector-document-list"]')); + expect(documentList.componentInstance.currentFolderId).toBe('cat-girl-nuku-nuku'); + }); + })); + + it('should set the folderIdToShow to the default "currentFolderId" if siteId is undefined', () => { + component.siteChanged( { guid: 'Kame-Sennin Muten Roshi' }); + fixture.detectChanges(); + + let documentList = fixture.debugElement.query(By.css('[data-automation-id="content-node-selector-document-list"]')); + expect(documentList.componentInstance.currentFolderId).toBe('Kame-Sennin Muten Roshi'); + + component.siteChanged( { guid: undefined }); + fixture.detectChanges(); + + documentList = fixture.debugElement.query(By.css('[data-automation-id="content-node-selector-document-list"]')); + expect(documentList.componentInstance.currentFolderId).toBe('cat-girl-nuku-nuku'); + }); + xit('should do something with pagination or with many results', () => { }); @@ -510,6 +537,26 @@ describe('ContentNodeSelectorComponent', () => { expect(chooseButton.nativeElement.disabled).toBe(true); }); + it('should become enabled after loading node with the necessary permissions', () => { + hasPermission = true; + component.documentList.folderNode = entry; + component.documentList.ready.emit(); + fixture.detectChanges(); + + let chooseButton = fixture.debugElement.query(By.css('[data-automation-id="content-node-selector-actions-choose"]')); + expect(chooseButton.nativeElement.disabled).toBe(false); + }); + + it('should remain disabled after loading node without the necessary permissions', () => { + hasPermission = false; + component.documentList.folderNode = entry; + component.documentList.ready.emit(); + fixture.detectChanges(); + + let chooseButton = fixture.debugElement.query(By.css('[data-automation-id="content-node-selector-actions-choose"]')); + expect(chooseButton.nativeElement.disabled).toBe(true); + }); + it('should be enabled when clicking on a node (with the right permissions) in the list (onNodeSelect)', () => { hasPermission = true; @@ -543,18 +590,6 @@ describe('ContentNodeSelectorComponent', () => { expect(chooseButton.nativeElement.disabled).toBe(true); }); - it('should become disabled when changing directory after previously selecting a right node', () => { - hasPermission = true; - component.onNodeSelect({ detail: { node: { entry } } }); - fixture.detectChanges(); - - component.onFolderChange(); - fixture.detectChanges(); - - let chooseButton = fixture.debugElement.query(By.css('[data-automation-id="content-node-selector-actions-choose"]')); - expect(chooseButton.nativeElement.disabled).toBe(true); - }); - it('should be disabled when resetting the chosen node', () => { hasPermission = true; component.onNodeSelect({ detail: { node: { entry: {} } } }); diff --git a/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.ts b/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.ts index d05462ac8c..7ccf305383 100644 --- a/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.ts +++ b/ng2-components/ng2-alfresco-documentlist/src/components/content-node-selector/content-node-selector.component.ts @@ -95,7 +95,7 @@ export class ContentNodeSelectorComponent implements OnInit { */ siteChanged(chosenSite: SiteModel): void { this.siteId = chosenSite.guid; - this.querySearch(); + this.updateResults(); } /** @@ -105,9 +105,12 @@ export class ContentNodeSelectorComponent implements OnInit { */ search(searchTerm: string): void { this.searchTerm = searchTerm; - this.querySearch(); + this.updateResults(); } + /** + * Returns whether breadcrumb has to be shown or not + */ needBreadcrumbs() { const whenInFolderNavigation = !this.showingSearchResults, whenInSelectingSearchResult = this.showingSearchResults && this.chosenNode; @@ -137,6 +140,17 @@ export class ContentNodeSelectorComponent implements OnInit { this.folderIdToShow = this.currentFolderId; } + /** + * Update the result list depending on the criterias + */ + private updateResults() { + if (this.searchTerm.length === 0) { + this.folderIdToShow = this.siteId || this.currentFolderId; + } else { + this.querySearch(); + } + } + /** * Perform the call to searchService with the proper parameters */ @@ -153,8 +167,7 @@ export class ContentNodeSelectorComponent implements OnInit { maxItems: 200, orderBy: null }; - this.searchService - .getNodeQueryResults(searchTerm, searchOpts) + this.searchService.getNodeQueryResults(searchTerm, searchOpts) .subscribe( results => { this.showingSearchResults = true; @@ -181,12 +194,7 @@ export class ContentNodeSelectorComponent implements OnInit { * @param event CustomEvent for node-select */ onNodeSelect(event: any): void { - const entry: MinimalNodeEntryEntity = event.detail.node.entry; - if (this.contentService.hasPermission(entry, 'update')) { - this.chosenNode = entry; - } else { - this.resetChosenNode(); - } + this.attemptNodeSelection(event.detail.node.entry); } /** @@ -194,7 +202,26 @@ export class ContentNodeSelectorComponent implements OnInit { */ onFolderChange() { this.showingSearchResults = false; - this.chosenNode = null; + } + + /** + * Attempts to set the currently loaded node + */ + onFolderLoaded() { + this.attemptNodeSelection(this.documentList.folderNode); + } + + /** + * Selects node as choosen if it has the right permission, clears the selection otherwise + * + * @param entry + */ + private attemptNodeSelection(entry: MinimalNodeEntryEntity): void { + if (this.contentService.hasPermission(entry, 'update')) { + this.chosenNode = entry; + } else { + this.resetChosenNode(); + } } /** @@ -208,7 +235,6 @@ export class ContentNodeSelectorComponent implements OnInit { * Emit event with the chosen node */ choose(): void { - // Multiple selections to be implemented... this.select.next([this.chosenNode]); }