[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:
Denys Vuika
2017-07-05 10:39:50 +01:00
committed by Eugenio Romano
parent ac9b660e83
commit 70a3c863e6
16 changed files with 323 additions and 32 deletions

View File

@@ -375,6 +375,80 @@ The supported variables are:
| hostname | `location.hostname` |
| port | `location.port` |
### Unit testing
You can also provide custom values for the entire service.
This might become handy when creating unit tests.
```ts
describe('MyTest', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
CoreModule.forRoot(),
AppConfigModule.forRoot('app.config.json', {
ecmHost: 'http://localhost:9876/ecm'
})
]
});
});
});
```
In the example above custom values are applied on the top of all the values the `AppConfigService` has previously loaded.
If there is an 'app.config.json' file loaded at unit test run time then your custom values will overwrite exiting values with the same keys if present.
## User Preferences Service
The `UserPreferencesService` allows you to store preferences for the components.
The preferences are bound to a particular `prefix` so the application can switch between different profiles on demand.
For example upon login you can set the `prefix` as current username:
```ts
import { UserPreferencesService, AlfrescoAuthenticationService } from 'ng2-alfresco-core';
@Component({...})
class AppComponent {
constructor(private userPreferences: UserPreferencesService,
private authService: AlfrescoAuthenticationService) {
}
onLoggedIn() {
this.userPreferences.setStoragePrefix(
this.authService.getEcmUsername()
);
}
}
```
As soon as you assign the storage prefix all settings that you get or set via the `UserPreferencesService` will be saved to dedicated profile.
You can import the service in your controller an use its APIs like below:
```ts
@Component({...})
class AppComponent {
constructor(userPreferences: UserPreferencesService) {
userPreferences.set('myProperty1', 'value1');
userPreferences.set('myProperty2', 'value2');
console.log(
userPreferences.get('myProperty1')
);
}
}
```
The service also provides quick access to a set of the "known" properties used across ADF components.
Known properties:
- paginationSize (number) - gets or sets the preferred pagination size
## Notification Service
The Notification Service is implemented on top of the Angular 2 Material Design snackbar.

View File

@@ -28,6 +28,7 @@ import { CardViewModule } from './src/components/view/card-view.module';
import { CollapsableModule } from './src/components/collapsable/collapsable.module';
import { AdfToolbarComponent } from './src/components/toolbar/toolbar.component';
import { AppConfigModule } from './src/services/app-config.service';
import { UserPreferencesService } from './src/services/user-preferences.service';
import {
AlfrescoAuthenticationService,
@@ -72,6 +73,7 @@ export * from './src/events/base.event';
export * from './src/events/base-ui.event';
export * from './src/events/folder-created.event';
export * from './src/models/index';
export { UserPreferencesService } from './src/services/user-preferences.service';
export * from './src/models/index';

View File

@@ -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)
]
};
}

View File

@@ -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);
});
});

View File

@@ -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;
}
}

View File

@@ -43,7 +43,9 @@
"es2015",
"dom"
],
"suppressImplicitAnyIndexErrors": true
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"exclude": [
"demo",