mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
[ADF-983] User Preferences Service (#2044)
* user preferences service * unit tests and code improvements * fix tests * fix tests * fix tests * add missing production settings * readme updates * integrate with the core module
This commit is contained in:
committed by
Eugenio Romano
parent
d7bc86239f
commit
0d1354ebbc
@@ -24,17 +24,14 @@ export class AppConfigService {
|
||||
|
||||
private config: any = {
|
||||
'ecmHost': 'http://{hostname}:{port}/ecm',
|
||||
'bpmHost': 'http://{hostname}:{port}/bpm',
|
||||
'application': {
|
||||
'name': 'Alfresco'
|
||||
}
|
||||
'bpmHost': 'http://{hostname}:{port}/bpm'
|
||||
};
|
||||
|
||||
configFile: string = null;
|
||||
|
||||
constructor(private http: Http) {}
|
||||
|
||||
get<T>(key: string): T {
|
||||
get<T>(key: string, defaultValue?: T): T {
|
||||
let result: any = ObjectUtils.getValue(this.config, key);
|
||||
if (typeof result === 'string') {
|
||||
const map = new Map<string, string>();
|
||||
@@ -42,18 +39,22 @@ export class AppConfigService {
|
||||
map.set('port', location.port);
|
||||
result = this.formatString(result, map);
|
||||
}
|
||||
if (result === undefined) {
|
||||
return defaultValue;
|
||||
}
|
||||
return <T> result;
|
||||
}
|
||||
|
||||
load(resource: string = 'app.config.json'): Promise<any> {
|
||||
load(resource: string = 'app.config.json', values?: {}): Promise<any> {
|
||||
this.configFile = resource;
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise(resolve => {
|
||||
this.config = Object.assign({}, values || {});
|
||||
this.http.get(resource).subscribe(
|
||||
data => {
|
||||
this.config = Object.assign({}, this.config, data.json() || {});
|
||||
resolve(this.config);
|
||||
},
|
||||
(err) => {
|
||||
() => {
|
||||
const errorMessage = `Error loading ${resource}`;
|
||||
console.log(errorMessage);
|
||||
resolve(this.config);
|
||||
@@ -74,11 +75,11 @@ export class AppConfigService {
|
||||
}
|
||||
}
|
||||
|
||||
export function InitAppConfigServiceProvider(resource: string): any {
|
||||
export function InitAppConfigServiceProvider(resource: string, values?: {}): any {
|
||||
return {
|
||||
provide: APP_INITIALIZER,
|
||||
useFactory: (configService: AppConfigService) => {
|
||||
return () => configService.load(resource);
|
||||
return () => configService.load(resource, values);
|
||||
},
|
||||
deps: [
|
||||
AppConfigService
|
||||
@@ -96,12 +97,12 @@ export function InitAppConfigServiceProvider(resource: string): any {
|
||||
]
|
||||
})
|
||||
export class AppConfigModule {
|
||||
static forRoot(resource: string): ModuleWithProviders {
|
||||
static forRoot(resource: string, values?: {}): ModuleWithProviders {
|
||||
return {
|
||||
ngModule: AppConfigModule,
|
||||
providers: [
|
||||
AppConfigService,
|
||||
InitAppConfigServiceProvider(resource)
|
||||
InitAppConfigServiceProvider(resource, values)
|
||||
]
|
||||
};
|
||||
}
|
||||
|
@@ -0,0 +1,95 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { TestBed, async } from '@angular/core/testing';
|
||||
import { AppConfigModule } from './app-config.service';
|
||||
import { StorageService } from './storage.service';
|
||||
import { UserPreferencesService } from './user-preferences.service';
|
||||
|
||||
describe('UserPreferencesService', () => {
|
||||
|
||||
const defaultPaginationSize: number = 10;
|
||||
let preferences: UserPreferencesService;
|
||||
let storage: StorageService;
|
||||
|
||||
beforeEach(async(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [
|
||||
AppConfigModule.forRoot('app.config.json', {
|
||||
'pagination': {
|
||||
'size': defaultPaginationSize
|
||||
}
|
||||
})
|
||||
],
|
||||
providers: [
|
||||
StorageService,
|
||||
UserPreferencesService
|
||||
]
|
||||
}).compileComponents();
|
||||
}));
|
||||
|
||||
beforeEach(() => {
|
||||
preferences = TestBed.get(UserPreferencesService);
|
||||
storage = TestBed.get(StorageService);
|
||||
});
|
||||
|
||||
it('should get default pagination from app config', () => {
|
||||
expect(preferences.paginationSize).toBe(defaultPaginationSize);
|
||||
});
|
||||
|
||||
it('should use [GUEST] as default storage prefix', () => {
|
||||
expect(preferences.getStoragePrefix()).toBe('GUEST');
|
||||
});
|
||||
|
||||
it('should change storage prefix', () => {
|
||||
preferences.setStoragePrefix('USER_A');
|
||||
expect(preferences.getStoragePrefix()).toBe('USER_A');
|
||||
});
|
||||
|
||||
it('should format property key for default prefix', () => {
|
||||
expect(preferences.getPropertyKey('propertyA')).toBe('GUEST__propertyA');
|
||||
});
|
||||
|
||||
it('should format property key for custom prefix', () => {
|
||||
preferences.setStoragePrefix('USER_A');
|
||||
expect(preferences.getPropertyKey('propertyA')).toBe('USER_A__propertyA');
|
||||
});
|
||||
|
||||
it('should save value with default prefix', () => {
|
||||
preferences.set('propertyA', 'valueA');
|
||||
const propertyKey = preferences.getPropertyKey('propertyA');
|
||||
expect(storage.getItem(propertyKey)).toBe('valueA');
|
||||
});
|
||||
|
||||
it('should save value with custom prefix', () => {
|
||||
preferences.setStoragePrefix('USER_A');
|
||||
preferences.set('propertyA', 'valueA');
|
||||
const propertyKey = preferences.getPropertyKey('propertyA');
|
||||
expect(storage.getItem(propertyKey)).toBe('valueA');
|
||||
});
|
||||
|
||||
it('should store custom pagination settings for default prefix', () => {
|
||||
preferences.paginationSize = 5;
|
||||
expect(preferences.paginationSize).toBe(5);
|
||||
});
|
||||
|
||||
it('should return default paginationSize value', () => {
|
||||
preferences.set('PAGINATION_SIZE', 0);
|
||||
expect(preferences.paginationSize).toBe(defaultPaginationSize);
|
||||
});
|
||||
|
||||
});
|
@@ -0,0 +1,71 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright 2016 Alfresco Software, Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AppConfigService } from './app-config.service';
|
||||
import { StorageService } from './storage.service';
|
||||
|
||||
@Injectable()
|
||||
export class UserPreferencesService {
|
||||
|
||||
private defaults = {
|
||||
paginationSize: 25
|
||||
};
|
||||
|
||||
private _storagePrefix: string = 'GUEST';
|
||||
|
||||
getStoragePrefix(): string {
|
||||
return this._storagePrefix;
|
||||
}
|
||||
|
||||
setStoragePrefix(value: string) {
|
||||
this._storagePrefix = value || 'GUEST';
|
||||
}
|
||||
|
||||
constructor(
|
||||
appConfig: AppConfigService,
|
||||
private storage: StorageService) {
|
||||
this.defaults.paginationSize = appConfig.get('pagination.size', 25);
|
||||
}
|
||||
|
||||
getPropertyKey(property: string): string {
|
||||
return `${this.getStoragePrefix()}__${property}`;
|
||||
}
|
||||
|
||||
set(property: string, value: any) {
|
||||
if (!property) { return; }
|
||||
|
||||
this.storage.setItem(
|
||||
this.getPropertyKey(property),
|
||||
value
|
||||
);
|
||||
}
|
||||
|
||||
get(property: string): string {
|
||||
const key = this.getPropertyKey(property);
|
||||
return this.storage.getItem(key);
|
||||
}
|
||||
|
||||
set paginationSize(value: number) {
|
||||
this.set('PAGINATION_SIZE', value);
|
||||
}
|
||||
|
||||
get paginationSize(): number {
|
||||
return Number(this.get('PAGINATION_SIZE')) || this.defaults.paginationSize;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user