user sorting by config

This commit is contained in:
Maximilian Krambach 2019-02-13 17:43:28 +01:00
parent 9785f67562
commit 59ed906a36
6 changed files with 79 additions and 16 deletions

View File

@ -1,4 +1,5 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { BaseRepository } from '../base-repository'; import { BaseRepository } from '../base-repository';
import { ViewUser } from 'app/site/users/models/view-user'; import { ViewUser } from 'app/site/users/models/view-user';
@ -305,4 +306,42 @@ export class UserRepositoryService extends BaseRepository<ViewUser, User> {
public getUserDuplicates(user: ViewUser): ViewUser[] { public getUserDuplicates(user: ViewUser): ViewUser[] {
return this.getViewModelList().filter(existingUser => existingUser.full_name === user.full_name); return this.getViewModelList().filter(existingUser => existingUser.full_name === user.full_name);
} }
/**
* @returns the observable for users sorted according to configuration
*/
public getSortedViewModelListObservable(): Observable<ViewUser[]> {
const subject = new BehaviorSubject<ViewUser[]>([]);
this.getViewModelListObservable().subscribe(users => {
subject.next(this.sortViewUsersByConfig(users));
});
return subject.asObservable();
}
/**
* Sort viewUsers by the configured settings
*
* @param users
* @returns the users sorted by first name, last name or number, according
* to the config setting. Fallthrough and identical cases will be sorted by
* 'short_name'
*/
public sortViewUsersByConfig(users: ViewUser[]): ViewUser[] {
const sort = this.configService.instant<'first_name' | 'last_name' | 'number'>('users_sort_by') || 'last_name';
return users.sort((a, b) => {
if (a[sort] && b[sort]) {
if (a[sort] === b[sort]) {
return a.short_name.localeCompare(b.short_name, this.translate.currentLang);
} else {
return a[sort].localeCompare(b[sort], this.translate.currentLang);
}
} else if (a[sort] && !b[sort]) {
return -1;
} else if (b[sort]) {
return 1;
} else {
return a.short_name.localeCompare(b.short_name);
}
});
}
} }

View File

@ -144,7 +144,7 @@ export class SpeakerListComponent extends BaseViewComponent implements OnInit {
public ngOnInit(): void { public ngOnInit(): void {
// load and observe users // load and observe users
this.users = new BehaviorSubject(this.userRepository.getViewModelList()); this.users = new BehaviorSubject(this.userRepository.getViewModelList());
this.userRepository.getViewModelListObservable().subscribe(users => this.users.next(users)); this.userRepository.getSortedViewModelListObservable().subscribe(users => this.users.next(users));
// detect changes in the form // detect changes in the form
this.addSpeakerForm.valueChanges.subscribe(formResult => { this.addSpeakerForm.valueChanges.subscribe(formResult => {

View File

@ -81,8 +81,10 @@ export class ManageSubmittersComponent extends BaseViewComponent {
this.editSubmitterObservable = this.editSubmitterSubject.asObservable(); this.editSubmitterObservable = this.editSubmitterSubject.asObservable();
// get all users for the submitter add form // get all users for the submitter add form
this.users = new BehaviorSubject<ViewUser[]>(this.userRepository.getViewModelList()); this.users = new BehaviorSubject<ViewUser[]>(
this.userRepository.getViewModelListObservable().subscribe(users => this.users.next(users)); this.userRepository.sortViewUsersByConfig(this.userRepository.getViewModelList())
);
this.userRepository.getSortedViewModelListObservable().subscribe(users => this.users.next(users));
// detect changes in the form // detect changes in the form
this.addSubmitterForm.valueChanges.subscribe(formResult => { this.addSubmitterForm.valueChanges.subscribe(formResult => {

View File

@ -9,7 +9,6 @@ import { TranslateService } from '@ngx-translate/core';
import { ItemRepositoryService } from 'app/core/repositories/agenda/item-repository.service'; import { ItemRepositoryService } from 'app/core/repositories/agenda/item-repository.service';
import { BaseViewComponent } from '../../../base/base-view'; import { BaseViewComponent } from '../../../base/base-view';
import { Category } from 'app/shared/models/motions/category';
import { CategoryRepositoryService } from 'app/core/repositories/motions/category-repository.service'; import { CategoryRepositoryService } from 'app/core/repositories/motions/category-repository.service';
import { ChangeRecommendationRepositoryService } from 'app/core/repositories/motions/change-recommendation-repository.service'; import { ChangeRecommendationRepositoryService } from 'app/core/repositories/motions/change-recommendation-repository.service';
import { ChangeRecoMode, LineNumberingMode, ViewMotion } from '../../models/view-motion'; import { ChangeRecoMode, LineNumberingMode, ViewMotion } from '../../models/view-motion';
@ -32,7 +31,6 @@ import { PersonalNoteContent } from 'app/shared/models/users/personal-note';
import { PersonalNoteService } from 'app/core/ui-services/personal-note.service'; import { PersonalNoteService } from 'app/core/ui-services/personal-note.service';
import { PromptService } from 'app/core/ui-services/prompt.service'; import { PromptService } from 'app/core/ui-services/prompt.service';
import { StatuteParagraphRepositoryService } from 'app/core/repositories/motions/statute-paragraph-repository.service'; import { StatuteParagraphRepositoryService } from 'app/core/repositories/motions/statute-paragraph-repository.service';
import { User } from 'app/shared/models/users/user';
import { ViewMotionChangeRecommendation } from '../../models/view-change-recommendation'; import { ViewMotionChangeRecommendation } from '../../models/view-change-recommendation';
import { ViewCreateMotion } from '../../models/view-create-motion'; import { ViewCreateMotion } from '../../models/view-create-motion';
import { ViewportService } from 'app/core/ui-services/viewport.service'; import { ViewportService } from 'app/core/ui-services/viewport.service';
@ -41,6 +39,7 @@ import { ViewStatuteParagraph } from '../../models/view-statute-paragraph';
import { Workflow } from 'app/shared/models/motions/workflow'; import { Workflow } from 'app/shared/models/motions/workflow';
import { LinenumberingService } from 'app/core/ui-services/linenumbering.service'; import { LinenumberingService } from 'app/core/ui-services/linenumbering.service';
import { Tag } from 'app/shared/models/core/tag'; import { Tag } from 'app/shared/models/core/tag';
import { UserRepositoryService } from 'app/core/repositories/users/user-repository.service';
import { ViewMotionBlock } from '../../models/view-motion-block'; import { ViewMotionBlock } from '../../models/view-motion-block';
import { ViewWorkflow } from '../../models/view-workflow'; import { ViewWorkflow } from '../../models/view-workflow';
import { ViewUser } from 'app/site/users/models/view-user'; import { ViewUser } from 'app/site/users/models/view-user';
@ -340,6 +339,7 @@ export class MotionDetailComponent extends BaseViewComponent implements OnInit {
* @param pdfExport export the motion to pdf * @param pdfExport export the motion to pdf
* @param personalNoteService: personal comments and favorite marker * @param personalNoteService: personal comments and favorite marker
* @param categoryRepo * @param categoryRepo
* @param userRepo
*/ */
public constructor( public constructor(
title: Title, title: Title,
@ -364,13 +364,18 @@ export class MotionDetailComponent extends BaseViewComponent implements OnInit {
private personalNoteService: PersonalNoteService, private personalNoteService: PersonalNoteService,
private linenumberingService: LinenumberingService, private linenumberingService: LinenumberingService,
private viewModelStore: ViewModelStoreService, private viewModelStore: ViewModelStoreService,
private categoryRepo: CategoryRepositoryService private categoryRepo: CategoryRepositoryService,
private userRepo: UserRepositoryService
) { ) {
super(title, translate, matSnackBar); super(title, translate, matSnackBar);
// Initial Filling of the Subjects // Initial Filling of the Subjects
this.submitterObserver = new BehaviorSubject(this.viewModelStore.getAll(ViewUser)); this.submitterObserver = new BehaviorSubject(
this.supporterObserver = new BehaviorSubject(this.viewModelStore.getAll(ViewUser)); this.userRepo.sortViewUsersByConfig(this.userRepo.getViewModelList())
);
this.supporterObserver = new BehaviorSubject(
this.userRepo.sortViewUsersByConfig(this.userRepo.getViewModelList())
);
this.categoryObserver = new BehaviorSubject( this.categoryObserver = new BehaviorSubject(
this.categoryRepo.sortViewCategoriesByConfig(this.viewModelStore.getAll(ViewCategory)) this.categoryRepo.sortViewCategoriesByConfig(this.viewModelStore.getAll(ViewCategory))
); );
@ -381,16 +386,17 @@ export class MotionDetailComponent extends BaseViewComponent implements OnInit {
this.tagObserver = new BehaviorSubject(this.viewModelStore.getAll(ViewTag)); this.tagObserver = new BehaviorSubject(this.viewModelStore.getAll(ViewTag));
this.motionObserver = new BehaviorSubject(this.viewModelStore.getAll(ViewMotion)); this.motionObserver = new BehaviorSubject(this.viewModelStore.getAll(ViewMotion));
this.userRepo.getSortedViewModelListObservable().subscribe(sortedUsers => {
this.submitterObserver.next(sortedUsers);
this.supporterObserver.next(sortedUsers);
});
this.categoryRepo.getSortedViewModelListObservable().subscribe(sortedCategories => {
this.categoryObserver.next(sortedCategories);
});
// Make sure the subjects are updated, when a new Model for the type arrives // Make sure the subjects are updated, when a new Model for the type arrives
// TODO get rid of DS here
this.DS.changeObservable.subscribe(newModel => { this.DS.changeObservable.subscribe(newModel => {
if (newModel instanceof User) { if (newModel instanceof Workflow) {
this.submitterObserver.next(this.viewModelStore.getAll(ViewUser));
this.supporterObserver.next(this.viewModelStore.getAll(ViewUser));
} else if (newModel instanceof Category) {
this.categoryObserver.next(
this.categoryRepo.sortViewCategoriesByConfig(this.viewModelStore.getAll(ViewCategory))
);
} else if (newModel instanceof Workflow) {
this.workflowObserver.next(this.viewModelStore.getAll(ViewWorkflow)); this.workflowObserver.next(this.viewModelStore.getAll(ViewWorkflow));
} else if (newModel instanceof MotionBlock) { } else if (newModel instanceof MotionBlock) {
this.blockObserver.next(this.viewModelStore.getAll(ViewMotionBlock)); this.blockObserver.next(this.viewModelStore.getAll(ViewMotionBlock));

View File

@ -1,6 +1,9 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { BaseSortListService, OsSortingDefinition } from 'app/core/ui-services/base-sort-list.service'; import { BaseSortListService, OsSortingDefinition } from 'app/core/ui-services/base-sort-list.service';
import { ConfigService } from 'app/core/ui-services/config.service';
import { StorageService } from 'app/core/core-services/storage.service';
import { ViewUser } from '../models/view-user'; import { ViewUser } from '../models/view-user';
@Injectable({ @Injectable({
@ -23,4 +26,16 @@ export class UserSortListService extends BaseSortListService<ViewUser> {
] ]
}; };
protected name = 'User'; protected name = 'User';
/**
* Constructor. Sets the default sorting if none is set locally
*
* @param translate
* @param store
* @param config
*/
public constructor(translate: TranslateService, store: StorageService, config: ConfigService) {
super(translate, store);
this.sortOptions.sortProperty = config.instant<keyof ViewUser>('motions_motions_sorting');
}
} }

View File

@ -19,6 +19,7 @@ def get_config_variables():
choices=( choices=(
{"value": "first_name", "display_name": "Given name"}, {"value": "first_name", "display_name": "Given name"},
{"value": "last_name", "display_name": "Surname"}, {"value": "last_name", "display_name": "Surname"},
{"value": "number", "display_name": "Participant number"},
), ),
weight=510, weight=510,
group="Participants", group="Participants",