Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply widget preferences to search #388

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@
width: 50vw !important;
}

.searchConfig {
width: 50px;
color: var(--toolbar-text-color);
filter: brightness(0.5);
margin-inline-end: 5px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
<mat-label>
Search <mat-icon>search</mat-icon>
</mat-label>
<input *ngIf="searchStringFromConfig" matInput class="searchConfig"
[ngModel]="searchStringFromConfig" disabled="true"
matTooltip="Remove from widget preferences">
<input #searchSnippets matInput type="text" [(ngModel)]="searchString">
<!-- <button mat-button *ngIf="searchString" matSuffix mat-icon-button aria-label="Clear"
(click)="searchString=''">
<mat-icon>close</mat-icon>
</button> -->
</mat-form-field>
</span>
<span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,30 @@ describe('SearchWindowComponent', () => {

[
undefined,
{
general: { type: 'logbook', title: 'Logbook view' },
filter: { targetId: 'id' }
} as WidgetItemConfig,
{config:
{
general: { type: 'logbook', title: 'Logbook view' },
filter: { targetId: 'id' }
} as WidgetItemConfig,
searchStringFromConfig: '',
configOut:
{
general: { type: 'logbook', title: 'Logbook view' },
filter: { targetId: 'id' }
} as WidgetItemConfig,
},
{config:
{
general: { type: 'logbook', title: 'Logbook view' },
filter: { targetId: 'id', tags: ['a', 'b'], excludeTags: ['c', 'd'] }
} as WidgetItemConfig,
searchStringFromConfig: '-#c -#d #a #b',
configOut:
{
general: { type: 'logbook', title: 'Logbook view' },
filter: { targetId: 'id' }
} as WidgetItemConfig,
}
].forEach((t, i) => {
it(`should _prepareConfig ${i}`, () => {
const defaultConfig = {
Expand All @@ -117,8 +137,41 @@ describe('SearchWindowComponent', () => {
}
};
if (t)
component.configsArray = [{ cols: 0, rows: 1, y: 2, x: 3, config: t }];
expect(component["_prepareConfig"]()).toEqual(t ?? defaultConfig);
component.configsArray = [{ cols: 0, rows: 1, y: 2, x: 3, config: t.config }];
expect(component["_prepareConfig"]()).toEqual(t? t.configOut: defaultConfig);
if (t)
expect(component.searchStringFromConfig).toEqual(t.searchStringFromConfig);
});
});

[
{filter: {tags: ['a', 'b'], excludeTags: ['c', 'd'] }, prefix: '#', key: 'tags', output: '#a #b'},
{filter: {tags: ['a', 'b'], excludeTags: ['c', 'd'] }, prefix: '-#', key: 'excludeTags', output: '-#c -#d'},
].forEach((t, i) => {
it(`should tagsToString ${i}`, () => {
expect(component['tagsToString'](t.filter, t.key, t.prefix)).toEqual(t.output);
});
});

it('should composeSearchString', () => {
const tagFilter = {tags: ['a', 'b'], excludeTags: ['c', 'd'] };
expect(component['composeSearchString'](tagFilter)).toEqual('-#c -#d #a #b');
});

[
{searchString: '', searchStringFromConfig: '', output: ''},
{searchString: ' ', searchStringFromConfig: ' ', output: ''},
{searchString: ' ', searchStringFromConfig: 'def', output: 'def'},
{searchString: 'abc', searchStringFromConfig: '', output: 'abc'},
{searchString: 'abc', searchStringFromConfig: ' ', output: 'abc'},
{searchString: 'abc', searchStringFromConfig: 'def', output: 'abc def'},
{searchString: 'abc', searchStringFromConfig: 'def ', output: 'abc def'},
{searchString: ' abc', searchStringFromConfig: 'def ', output: 'abc def'},
].forEach((t, i) => {
it(`should concatSearchStrings ${i}`, () => {
component.searchString = t.searchString;
component.searchStringFromConfig = t.searchStringFromConfig;
expect(component['concatSearchStrings']()).toEqual(t.output);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class SearchWindowComponent implements OnInit {
_sample_user: string = "";
subscriptions: Subscription[] = [];
logbookId?: string;
searchStringFromConfig = '';

constructor(
public userPreferences: UserPreferencesService,
Expand Down Expand Up @@ -68,14 +69,18 @@ export class SearchWindowComponent implements OnInit {
submitSearch() {
this.searched = this.searchString;
if (this.logbookId) {
this.searchScrollService.reset(this.searchString);
this.searchScrollService.reset(this.concatSearchStrings());
return
}
this.logbookIconScrollService.reset(this.searchString);
this.overviewSearch.emit(this.searchString);
this.closeSearch();
}

private concatSearchStrings(): string {
return `${this.searchString} ${this.searchStringFromConfig}`.trim();
}

private async _initialize_help() {

this._sample_user = this.userPreferences.userInfo.username;
Expand Down Expand Up @@ -141,7 +146,10 @@ export class SearchWindowComponent implements OnInit {
}

private _prepareConfig() {
return this._extractConfig() ?? this.defaultConfig;
const config = JSON.parse(JSON.stringify(this._extractConfig() ?? this.defaultConfig));
if (config.filter?.tags || config.filter?.excludeTags)
this.searchStringFromConfig = this.composeSearchString(config.filter);
return config;
}

private _extractConfig() {
Expand All @@ -152,6 +160,16 @@ export class SearchWindowComponent implements OnInit {
)?.[0]?.config;
}

private tagsToString(configFilter: WidgetItemConfig['filter'], tagKey: string, prefix: string) {
const tagsString = `${configFilter?.[tagKey]?.length > 0? `${prefix}` + configFilter?.[tagKey].join(` ${prefix}`): ''}`
delete configFilter[tagKey]
return tagsString
}

private composeSearchString(configFilter: WidgetItemConfig['filter']) {
return `${this.tagsToString(configFilter, 'excludeTags', '-#')} ${this.tagsToString(configFilter, 'tags', '#')}`.trim();
}

ngOnDestroy(): void {
//Called once, before the instance is destroyed.
//Add 'implements OnDestroy' to the class.
Expand Down
Loading