mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-03-12 01:32:12 +00:00
feat: show menu for multiple days for canteens and cafes
Closes #19, #79
This commit is contained in:
committed by
Jovan Krunić
parent
66b8720da0
commit
3c079cd189
@@ -21,7 +21,7 @@ import {TranslateModule,} from '@ngx-translate/core';
|
||||
import {SCFacet, SCThingType} from '@openstapps/core';
|
||||
import {ContextMenuComponent} from './context-menu.component';
|
||||
import {SettingsModule} from '../../settings/settings.module';
|
||||
import {MenuService} from '../menu.service';
|
||||
import {ContextMenuService} from '../context/context-menu.service';
|
||||
import {FilterContext, SortContext} from './context-type';
|
||||
|
||||
describe('ContextMenuComponent', async () => {
|
||||
@@ -36,7 +36,7 @@ describe('ContextMenuComponent', async () => {
|
||||
ChildrenOutletContexts,
|
||||
Location,
|
||||
UrlSerializer,
|
||||
MenuService,
|
||||
ContextMenuService,
|
||||
{provide: LocationStrategy, useClass: PathLocationStrategy},
|
||||
{provide: APP_BASE_HREF, useValue: '/'},
|
||||
],
|
||||
|
||||
@@ -20,8 +20,9 @@ import {
|
||||
SCThingType,
|
||||
SCTranslations,
|
||||
} from '@openstapps/core';
|
||||
import {MenuService} from '../menu.service';
|
||||
import {ContextMenuService} from './context-menu.service';
|
||||
import {FilterContext, SortContext, SortContextOption} from './context-type';
|
||||
import {Subscription} from 'rxjs';
|
||||
|
||||
/**
|
||||
* The context menu
|
||||
@@ -68,30 +69,45 @@ export class ContextMenuComponent {
|
||||
*/
|
||||
translator: SCThingTranslator;
|
||||
|
||||
/**
|
||||
* Array of all Subscriptions
|
||||
*/
|
||||
subscriptions: Subscription[] = [];
|
||||
|
||||
|
||||
constructor(private translateService: TranslateService,
|
||||
private readonly menuService: MenuService) {
|
||||
private readonly contextMenuService: ContextMenuService) {
|
||||
this.language = this.translateService.currentLang as keyof SCTranslations<SCLanguage>;
|
||||
this.translator = new SCThingTranslator(this.language);
|
||||
|
||||
this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
|
||||
this.subscriptions.push(this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
|
||||
this.language = event.lang as keyof SCTranslations<SCLanguage>;
|
||||
this.translator = new SCThingTranslator(this.language);
|
||||
});
|
||||
}));
|
||||
|
||||
this.menuService.filterContextChanged$.subscribe((filterContext) => {
|
||||
this.subscriptions.push(this.contextMenuService.filterContextChanged$.subscribe((filterContext) => {
|
||||
this.filterOption = filterContext;
|
||||
});
|
||||
}));
|
||||
|
||||
this.menuService.sortOptions.subscribe((sortContext) => {
|
||||
this.subscriptions.push(this.contextMenuService.sortOptions.subscribe((sortContext) => {
|
||||
this.sortOption = sortContext;
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribe from Observables
|
||||
*/
|
||||
ngOnDestroy() {
|
||||
for (let sub of this.subscriptions) {
|
||||
sub.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets selected filter options and updates listener
|
||||
*/
|
||||
filterChanged = () => {
|
||||
this.menuService.contextFilterChanged(this.filterOption);
|
||||
this.contextMenuService.contextFilterChanged(this.filterOption);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,7 +133,7 @@ export class ContextMenuComponent {
|
||||
option.options.forEach((filterFacet) => filterFacet.buckets.forEach((filterBucket) => {
|
||||
filterBucket.checked = false;
|
||||
}));
|
||||
this.menuService.contextFilterChanged(this.filterOption);
|
||||
this.contextMenuService.contextFilterChanged(this.filterOption);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,6 +150,6 @@ export class ContextMenuComponent {
|
||||
option.reversed = false;
|
||||
}
|
||||
}
|
||||
this.menuService.contextSortChanged(option);
|
||||
this.contextMenuService.contextSortChanged(option);
|
||||
}
|
||||
}
|
||||
|
||||
148
src/app/modules/menu/context/context-menu.service.spec.ts
Normal file
148
src/app/modules/menu/context/context-menu.service.spec.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
|
||||
import {ContextMenuService} from './context-menu.service';
|
||||
import {SCFacet} from '@openstapps/core';
|
||||
import {FilterContext, SortContext} from './context-type';
|
||||
|
||||
describe('ContextMenuService', () => {
|
||||
let service: ContextMenuService;
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [ContextMenuService]
|
||||
});
|
||||
service = TestBed.get(ContextMenuService);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(service).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should update filterOptions', (done) => {
|
||||
service.filterContextChanged$.subscribe((result) => {
|
||||
expect(result).toBeDefined();
|
||||
done();
|
||||
});
|
||||
service.updateContextFilter(facetsMock);
|
||||
});
|
||||
|
||||
it('should update filterQuery', (done) => {
|
||||
service.filterContextChanged$.subscribe(() => {
|
||||
service.contextFilterChanged(filterContext);
|
||||
});
|
||||
service.filterQueryChanged$.subscribe((result) => {
|
||||
expect(result).toBeDefined();
|
||||
done();
|
||||
});
|
||||
service.updateContextFilter(facetsMock);
|
||||
});
|
||||
|
||||
it('should update sortOptions', (done) => {
|
||||
service.sortContextChanged$.subscribe((result) => {
|
||||
expect(result).toBeDefined();
|
||||
done();
|
||||
});
|
||||
service.setContextSort(sortContext);
|
||||
});
|
||||
|
||||
it('should update sortQuery', (done) => {
|
||||
service.sortContextChanged$.subscribe(() => {
|
||||
service.contextSortChanged(sortContext);
|
||||
});
|
||||
service.sortQueryChanged$.subscribe((result) => {
|
||||
expect(result).toBeDefined();
|
||||
done();
|
||||
});
|
||||
service.setContextSort(sortContext);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
const facetsMock: SCFacet[] =
|
||||
[{
|
||||
'buckets': [
|
||||
{
|
||||
'count': 60,
|
||||
'key': 'academic event',
|
||||
},
|
||||
{
|
||||
'count': 160,
|
||||
'key': 'message',
|
||||
},
|
||||
{
|
||||
'count': 151,
|
||||
'key': 'date series',
|
||||
},
|
||||
{
|
||||
'count': 106,
|
||||
'key': 'dish',
|
||||
},
|
||||
{
|
||||
'count': 20,
|
||||
'key': 'building',
|
||||
},
|
||||
{
|
||||
'count': 20,
|
||||
'key': 'semester',
|
||||
},
|
||||
],
|
||||
'field': 'type',
|
||||
}];
|
||||
|
||||
const filterContext: FilterContext = {
|
||||
name: 'filter',
|
||||
options: [
|
||||
{
|
||||
buckets: [
|
||||
{
|
||||
checked: true,
|
||||
count: 60,
|
||||
key: 'academic event'
|
||||
}, {
|
||||
checked: false,
|
||||
count: 160,
|
||||
key: 'message',
|
||||
},
|
||||
{
|
||||
checked: false,
|
||||
count: 151,
|
||||
key: 'date series'
|
||||
}, {
|
||||
checked: false,
|
||||
count: 106,
|
||||
key: 'dish'
|
||||
},
|
||||
{
|
||||
checked: false,
|
||||
count: 20,
|
||||
key: 'building'
|
||||
},
|
||||
{
|
||||
checked: false,
|
||||
count: 20,
|
||||
key: 'semester'
|
||||
}
|
||||
],
|
||||
field: 'type'
|
||||
}]
|
||||
}
|
||||
|
||||
const sortContext: SortContext = {
|
||||
name: 'sort',
|
||||
reversed: false,
|
||||
value: 'name',
|
||||
values: [
|
||||
{
|
||||
reversible: false,
|
||||
value: 'relevance',
|
||||
},
|
||||
{
|
||||
reversible: true,
|
||||
value: 'name',
|
||||
},
|
||||
{
|
||||
reversible: true,
|
||||
value: 'type',
|
||||
},
|
||||
],
|
||||
}
|
||||
231
src/app/modules/menu/context/context-menu.service.ts
Normal file
231
src/app/modules/menu/context/context-menu.service.ts
Normal file
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* Copyright (C) 2020 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Injectable} from '@angular/core';
|
||||
import {SCFacet, SCFacetBucket, SCSearchFilter, SCSearchSort} from '@openstapps/core';
|
||||
import {Subject} from 'rxjs';
|
||||
import {FilterBucket, FilterContext, FilterFacet, SortContext} from './context-type';
|
||||
|
||||
/**
|
||||
* ContextMenuService provides bidirectional communication of context menu options and search queries
|
||||
*/
|
||||
@Injectable()
|
||||
export class ContextMenuService {
|
||||
|
||||
/**
|
||||
* Local filter context object
|
||||
*/
|
||||
contextFilter: FilterContext;
|
||||
|
||||
/**
|
||||
* Container for the filter context
|
||||
*/
|
||||
// tslint:disable-next-line:member-ordering
|
||||
filterOptions = new Subject<FilterContext>();
|
||||
|
||||
/**
|
||||
* Observable filterContext streams
|
||||
*/
|
||||
// tslint:disable-next-line:member-ordering
|
||||
filterContextChanged$ = this.filterOptions.asObservable();
|
||||
|
||||
/**
|
||||
* Container for the filter query (SCSearchFilter)
|
||||
*/
|
||||
filterQuery = new Subject<SCSearchFilter>();
|
||||
|
||||
/**
|
||||
* Observable filterContext streams
|
||||
*/
|
||||
// tslint:disable-next-line:member-ordering
|
||||
filterQueryChanged$ = this.filterQuery.asObservable();
|
||||
|
||||
/**
|
||||
* Container for the sort context
|
||||
*/
|
||||
// tslint:disable-next-line:member-ordering
|
||||
sortOptions = new Subject<SortContext>();
|
||||
|
||||
/**
|
||||
* Observable SortContext streams
|
||||
*/
|
||||
// tslint:disable-next-line:member-ordering
|
||||
sortContextChanged$ = this.sortOptions.asObservable();
|
||||
|
||||
/**
|
||||
* Container for the sort query
|
||||
*/
|
||||
sortQuery = new Subject<SCSearchSort>();
|
||||
|
||||
/**
|
||||
* Observable SortContext streams
|
||||
*/
|
||||
// tslint:disable-next-line:member-ordering
|
||||
sortQueryChanged$ = this.sortQuery.asObservable();
|
||||
|
||||
/**
|
||||
* Returns SCSearchFilter if filterContext value is set, undefined otherwise
|
||||
* @param filterContext FilterContext to build SCSearchFilter from
|
||||
*/
|
||||
buildFilterQuery = (filterContext: FilterContext): SCSearchFilter | undefined => {
|
||||
const filters: SCSearchFilter[] = [];
|
||||
|
||||
filterContext.options.forEach((filterFacet) => {
|
||||
const optionFilters: SCSearchFilter[] = [];
|
||||
filterFacet.buckets.forEach((filterBucket) => {
|
||||
if (filterBucket.checked) {
|
||||
optionFilters.push(
|
||||
{
|
||||
arguments: {
|
||||
field: filterFacet.field,
|
||||
value: filterBucket.key,
|
||||
},
|
||||
type: 'value',
|
||||
});
|
||||
}
|
||||
});
|
||||
if (optionFilters.length > 0) {
|
||||
filters.push({
|
||||
arguments: {
|
||||
filters: optionFilters,
|
||||
operation: 'or',
|
||||
},
|
||||
type: 'boolean',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if (filters.length > 0) {
|
||||
return {
|
||||
arguments: {
|
||||
filters: filters,
|
||||
operation: 'and',
|
||||
},
|
||||
type: 'boolean',
|
||||
};
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns SCSearchSort if sorting value is set, undefined otherwise
|
||||
* @param sortContext SortContext to build SCSearchSort from
|
||||
*/
|
||||
buildSortQuery = (sortContext: SortContext): SCSearchSort | undefined => {
|
||||
if (sortContext.value && sortContext.value.length > 0) {
|
||||
if (sortContext.value === 'name' || sortContext.value === 'type') {
|
||||
return {
|
||||
arguments: {
|
||||
field: sortContext.value,
|
||||
position: 0,
|
||||
},
|
||||
order: sortContext.reversed ? 'desc' : 'asc',
|
||||
type: 'ducet',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates filter query from filterContext
|
||||
*/
|
||||
contextFilterChanged(filterContext: FilterContext) {
|
||||
this.filterQuery.next(this.buildFilterQuery(filterContext));
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates sort query from sortContext
|
||||
*/
|
||||
contextSortChanged(sortContext: SortContext) {
|
||||
this.sortQuery.next(this.buildSortQuery(sortContext));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets sort context
|
||||
*/
|
||||
setContextSort(sortContext: SortContext) {
|
||||
this.sortOptions.next(sortContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the filter context options from given facets
|
||||
*/
|
||||
updateContextFilter(facets: SCFacet[]) {
|
||||
// arrange facet field "type" to first position
|
||||
facets.sort((a: SCFacet, b: SCFacet) => {
|
||||
if (a.field === 'type') {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (b.field === 'type') {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
|
||||
if (!this.contextFilter) {
|
||||
this.contextFilter = {
|
||||
name: 'filter',
|
||||
options: [],
|
||||
};
|
||||
}
|
||||
|
||||
this.updateContextFilterOptions(this.contextFilter, facets);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates context filter with new facets.
|
||||
* It preserves the checked status of existing filter options
|
||||
*/
|
||||
updateContextFilterOptions = (contextFilter: FilterContext, facets: SCFacet[]) => {
|
||||
const newFilterOptions: FilterFacet[] = [];
|
||||
|
||||
// iterate new facets
|
||||
for (const facet of facets) {
|
||||
if (facet.buckets.length > 0) {
|
||||
const newFilterFacet: FilterFacet = {
|
||||
buckets: [],
|
||||
field: facet.field,
|
||||
onlyOnType: facet.onlyOnType,
|
||||
};
|
||||
newFilterOptions.push(newFilterFacet);
|
||||
|
||||
// search existing filterOption
|
||||
const filterOption = contextFilter.options.find((contextFacet: FilterFacet) =>
|
||||
contextFacet.field === facet.field && contextFacet.onlyOnType === facet.onlyOnType);
|
||||
facet.buckets.forEach((bucket: SCFacetBucket) => {
|
||||
// search existing bucket to preserve checked status
|
||||
const existingFilterBucket = filterOption ? filterOption.buckets
|
||||
.find((contextBucket: FilterBucket) => contextBucket.key === bucket.key) : undefined;
|
||||
const filterBucket: FilterBucket = {
|
||||
checked: existingFilterBucket ? existingFilterBucket.checked : false,
|
||||
count: bucket.count,
|
||||
key: bucket.key,
|
||||
};
|
||||
newFilterFacet.buckets.push(filterBucket);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// update filter options
|
||||
contextFilter.options = newFilterOptions;
|
||||
this.contextFilter = contextFilter;
|
||||
|
||||
this.filterOptions.next(contextFilter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user