Skip to content

Commit

Permalink
Pass config to search
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Apr 17, 2024
1 parent 052cfce commit 90fea3a
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LogbookInfoService } from '@shared/logbook-info.service';
import { AppConfigService } from 'src/app/app-config.service';

import { SearchWindowComponent } from './search-window.component';
import { WidgetItemConfig } from 'src/app/core/model/config';

const getConfig = () => ({});

Expand Down Expand Up @@ -34,21 +35,6 @@ describe('SearchWindowComponent', () => {
expect(component).toBeTruthy();
});

it('should _parseSearchString', () => {
component.searchString = 'someSearch';
expect(component['_parseSearchString']()).toEqual(
{
location: ['id'],
tags: [],
ownerGroup: "",
createdBy: "",
startDate: "",
endDate: "",
}
);
expect(component.searchString).toEqual('someSearch');
});

it('should submitSearch logbook', () => {
const resetSpy = spyOn(component['searchScrollService'], 'reset');
const search = 'some';
Expand All @@ -71,4 +57,69 @@ describe('SearchWindowComponent', () => {
expect(closeSearchSpy).toHaveBeenCalled();
});

it('should defaultConfig', () => {
expect(component.defaultConfig).toEqual({
filter: {
targetId: 'id',
},
general: {
type: "logbook",
readonly: true
},
view: {
order: ["defaultOrder DESC"],
hideMetadata: false,
showSnippetHeader: false
}
});
});

it('should _extractConfig', () => {
const firstConfig = {
general: {
type: 'logbook',
title: 'some different',
},
filter: {
targetId: 'id'
}
};
const secondConfig = JSON.parse(JSON.stringify(firstConfig));
secondConfig.general.title = 'Logbook view';
const positions = { cols: 0, rows: 1, y: 2, x: 3 };
component.configsArray = [
{ ...positions, config: firstConfig },
{ ...positions, config: secondConfig },
];
expect(component["_extractConfig"]()).toEqual(secondConfig);
});

[
undefined,
{
general: { type: 'logbook', title: 'Logbook view' },
filter: { targetId: 'id' }
} as WidgetItemConfig,
].forEach((t, i) => {
it(`should _prepareConfig ${i}`, () => {
const defaultConfig = {
filter: {
targetId: "id",
},
general: {
type: "logbook",
readonly: true
},
view: {
order: ["defaultOrder DESC"],
hideMetadata: false,
showSnippetHeader: false
}
};
if (t)
component.configsArray = [{ cols: 0, rows: 1, y: 2, x: 3, config: t }];
expect(component["_prepareConfig"]()).toEqual(t ?? defaultConfig);
});
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ import { Hotkeys } from '@shared/hotkeys.service';
import { LogbookIconScrollService } from 'src/app/overview/logbook-icon-scroll-service.service';
import { SearchScrollService } from 'src/app/core/search-scroll.service';

interface SearchResult {
location: string[],
tags: string[],
ownerGroup: string,
createdBy: string,
startDate: string,
endDate: string,
}

@Component({
selector: 'search-window',
templateUrl: './search-window.component.html',
Expand Down Expand Up @@ -132,43 +123,33 @@ export class SearchWindowComponent implements OnInit {
return this._searchString;
}

private _parseSearchString(): SearchResult {
let searchResult: SearchResult = {
location: [],
tags: [],
ownerGroup: "",
createdBy: "",
startDate: "",
endDate: "",
}
console.log("local search")
if (this.logbookId) searchResult.location.push(this.logbookId);
console.log("Search value: ", this.searchString);
console.log("Search config: ", searchResult)
return searchResult;
}

private _prepareConfig() {
let searchResult = this._parseSearchString();
let _config: WidgetItemConfig = {
get defaultConfig() {
return {
filter: {
targetId: "",
additionalLogbooks: searchResult.location,
tags: searchResult.tags,
ownerGroup: searchResult.ownerGroup,
targetId: this.logbookId,
},
general: {
type: "logbook",
title: "",
readonly: true
},
view: {
order: ["defaultOrder DESC"],
hideMetadata: false,
showSnippetHeader: false
}
}
return _config;
};
}

private _prepareConfig() {
return this._extractConfig() ?? this.defaultConfig;
}

private _extractConfig() {
return this.configsArray?.filter?.(configItem =>
configItem?.config?.filter?.targetId === this.logbookId &&
configItem?.config?.general?.type === 'logbook' &&
configItem?.config?.general?.title === 'Logbook view'
)?.[0]?.config;
}

ngOnDestroy(): void {
Expand Down
6 changes: 5 additions & 1 deletion scilog/src/app/logbook/core/search/search.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ describe('SearchComponent', () => {

it('should selectedSnippet', () => {
const emitSpy = spyOn(component.close, 'emit');
component.selectedSnippet('');
component.selectedSnippet('event');
expect(emitSpy).toHaveBeenCalled();
expect(component['scrollToElementService'].selectedItem).toEqual({
event: 'event',
config: undefined,
});
});

});
5 changes: 4 additions & 1 deletion scilog/src/app/logbook/core/search/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export class SearchComponent implements OnInit {

selectedSnippet($event) {
console.log($event);
this.scrollToElementService.selectedItem = $event;
this.scrollToElementService.selectedItem = {
event: $event,
config: this.config
};
this.close.emit();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -690,4 +690,16 @@ describe('LogbookItemComponent', () => {
expect(dialogOpenMock.calls.mostRecent().args[1].data['defaultTags']).toEqual(undefined);
});

it('should scrollToElement', async () => {
const _id = '123';
const config = { general: { title: 'Logbook view' } };
const componentConfig = component.config;
component['scrollToElementService']['$selectedItem'] =
of({ event: { id: _id }, config: config });
const getIndexSpy = spyOn(component['logbookItemDataService'], 'getIndex');
component['scrollToElement']();
fixture.detectChanges();
expect(getIndexSpy).toHaveBeenCalledWith(_id, componentConfig);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,25 @@ export class LogbookItemComponent implements OnInit {

this.updateViewSubscription();

this.subscriptions.push(this.scrollToElementService.$selectedItem.subscribe(async (element) => {
this.scrollToElement();
}

private scrollToElement() {
this.subscriptions.push(this.scrollToElementService.$selectedItem.subscribe(async (event) => {
if (this.config?.general?.title !== event?.config?.general?.title) return;
const element = event.event;
if (element != null) {
if (typeof element.id == 'string') {
let index = await this.logbookItemDataService.getIndex(element.id, this.config);
console.log("snippet:", element);
console.log("index: ", index)
console.log("index: ", index);
console.log(this.config);
if (index >= 0) {
this.goToSnippetIndex(index);
}
}
}
}))
}));
}

updateViewSubscription() {
Expand Down

0 comments on commit 90fea3a

Please sign in to comment.