mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-2387] added a stream for pagination size change (#3014)
* [ADF-2387] added a stream for pagination size change * [ADF-2387] added test for page size stream * [ADF-2387] start refactoring adding a general stream for user preference attributes * [ADF-2387] added documentation and test for user values stream
This commit is contained in:
@@ -24,10 +24,11 @@ import { AppConfigModule } from '../app-config/app-config.module';
|
||||
import { StorageService } from './storage.service';
|
||||
import { TranslateLoaderService } from './translate-loader.service';
|
||||
import { UserPreferencesService } from './user-preferences.service';
|
||||
import { UserPreferenceValues } from './user-preferences.service';
|
||||
|
||||
describe('UserPreferencesService', () => {
|
||||
|
||||
const defaultPaginationSize: number = 10;
|
||||
const defaultPaginationSize: number = 25;
|
||||
const supportedPaginationSize = [5, 10, 15, 20];
|
||||
let preferences: UserPreferencesService;
|
||||
let storage: StorageService;
|
||||
@@ -66,6 +67,7 @@ describe('UserPreferencesService', () => {
|
||||
});
|
||||
|
||||
it('should get default pagination from app config', () => {
|
||||
appConfig.config.pagination.size = 0;
|
||||
expect(preferences.defaults.paginationSize).toBe(defaultPaginationSize);
|
||||
});
|
||||
|
||||
@@ -75,6 +77,7 @@ describe('UserPreferencesService', () => {
|
||||
});
|
||||
|
||||
it('should use [GUEST] as default storage prefix', () => {
|
||||
preferences.setStoragePrefix(null);
|
||||
expect(preferences.getStoragePrefix()).toBe('GUEST');
|
||||
});
|
||||
|
||||
@@ -145,4 +148,25 @@ describe('UserPreferencesService', () => {
|
||||
expect(preferences.locale).toBe('fake-store-locate');
|
||||
});
|
||||
|
||||
it('should stream the page size value when is set', async(() => {
|
||||
preferences.paginationSize = 5;
|
||||
preferences.onChange.subscribe((userPreferenceStatus) => {
|
||||
expect(userPreferenceStatus.PAGINATION_SIZE).toBe(5);
|
||||
});
|
||||
}));
|
||||
|
||||
it('should stream the user preference status when changed', async(() => {
|
||||
preferences.set('propertyA', 'valueA');
|
||||
preferences.onChange.subscribe((userPreferenceStatus) => {
|
||||
expect(userPreferenceStatus.propertyA).toBe('valueA');
|
||||
});
|
||||
}));
|
||||
|
||||
it('should stream only the selected attribute changes when using select', async(() => {
|
||||
preferences.disableCSRF = true;
|
||||
preferences.select(UserPreferenceValues.DisableCSRF).subscribe((disableCSRFFlag) => {
|
||||
expect(disableCSRFFlag).toBeTruthy();
|
||||
});
|
||||
}));
|
||||
|
||||
});
|
||||
|
@@ -22,6 +22,14 @@ import { Observable } from 'rxjs/Observable';
|
||||
import { AppConfigService } from '../app-config/app-config.service';
|
||||
import { AlfrescoApiService } from './alfresco-api.service';
|
||||
import { StorageService } from './storage.service';
|
||||
import 'rxjs/add/operator/distinctUntilChanged';
|
||||
|
||||
export enum UserPreferenceValues {
|
||||
PaginationSize = 'PAGINATION_SIZE',
|
||||
DisableCSRF = 'DISABLE_CSRF',
|
||||
Locale = 'LOCALE',
|
||||
SupportedPageSizes = 'supportedPageSizes'
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class UserPreferencesService {
|
||||
@@ -32,8 +40,20 @@ export class UserPreferencesService {
|
||||
locale: 'en'
|
||||
};
|
||||
|
||||
private localeSubject: BehaviorSubject<string> ;
|
||||
private userPreferenceStatus: any = {
|
||||
paginationSize: 25,
|
||||
supportedPageSizes: [5, 10, 15, 20],
|
||||
LOCALE: 'en'
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated we are grouping every value changed on the user preference in a single stream : userPreferenceValue$
|
||||
*/
|
||||
locale$: Observable<string>;
|
||||
private localeSubject: BehaviorSubject<string> ;
|
||||
|
||||
private onChangeSubject: BehaviorSubject<any>;
|
||||
onChange: Observable<any>;
|
||||
|
||||
constructor(
|
||||
public translate: TranslateService,
|
||||
@@ -41,11 +61,22 @@ export class UserPreferencesService {
|
||||
private storage: StorageService,
|
||||
private apiService: AlfrescoApiService
|
||||
) {
|
||||
const currentLocale = this.locale || this.getDefaultLocale();
|
||||
this.localeSubject = new BehaviorSubject(currentLocale);
|
||||
this.initUserPreferenceStatus();
|
||||
this.localeSubject = new BehaviorSubject(this.defaults.locale);
|
||||
this.locale$ = this.localeSubject.asObservable();
|
||||
this.defaults.paginationSize = this.appConfig.get('pagination.size', this.defaults.paginationSize);
|
||||
this.defaults.supportedPageSizes = this.appConfig.get('pagination.supportedPageSizes', this.defaults.supportedPageSizes);
|
||||
this.onChangeSubject = new BehaviorSubject(this.userPreferenceStatus);
|
||||
this.onChange = this.onChangeSubject.asObservable();
|
||||
}
|
||||
|
||||
private initUserPreferenceStatus() {
|
||||
this.userPreferenceStatus[UserPreferenceValues.Locale] = this.locale || this.getDefaultLocale();
|
||||
this.userPreferenceStatus[UserPreferenceValues.PaginationSize] = this.appConfig.get('pagination.size', this.defaults.paginationSize);
|
||||
this.userPreferenceStatus[UserPreferenceValues.SupportedPageSizes] = this.appConfig.get('pagination.supportedPageSizes', this.defaults.supportedPageSizes);
|
||||
this.userPreferenceStatus[UserPreferenceValues.DisableCSRF] = this.disableCSRF;
|
||||
}
|
||||
|
||||
select(property: string): Observable<any> {
|
||||
return this.onChange.map((userPreferenceStatus) => userPreferenceStatus[property]).distinctUntilChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,11 +100,12 @@ export class UserPreferencesService {
|
||||
*/
|
||||
set(property: string, value: any) {
|
||||
if (!property) { return; }
|
||||
|
||||
this.storage.setItem(
|
||||
this.getPropertyKey(property),
|
||||
value
|
||||
);
|
||||
this.userPreferenceStatus[property] = value;
|
||||
this.onChangeSubject.next(this.userPreferenceStatus);
|
||||
}
|
||||
|
||||
/** Gets the active storage prefix for preferences. */
|
||||
|
Reference in New Issue
Block a user