[ADF-4582] User preference - set layout orientation based on locale (#4744)

* set textOrientation based on locale

* remove manually setting textOrientation

* tests

* revert to getBrowserCultureLang

* match languages keys

* update tests

* fix types
This commit is contained in:
Cilibiu Bogdan
2019-05-22 16:02:51 +03:00
committed by Denys Vuika
parent 26a495c95d
commit 58cf10be5f
3 changed files with 77 additions and 8 deletions

View File

@@ -130,8 +130,6 @@ export class AppLayoutComponent implements OnInit {
if (this.alfrescoApiService.getInstance().isOauthConfiguration()) {
this.enableRedirect = false;
}
this.userPreferencesService.set('textOrientation', this.direction);
}
setState(state) {

View File

@@ -15,7 +15,7 @@
* limitations under the License.
*/
import { TestBed } from '@angular/core/testing';
import { TestBed, async } from '@angular/core/testing';
import { TranslateService } from '@ngx-translate/core';
import { AppConfigService } from '../app-config/app-config.service';
import { StorageService } from './storage.service';
@@ -48,7 +48,7 @@ describe('UserPreferencesService', () => {
'supportedPageSizes': [5, 10, 15, 20]
}
};
preferences = TestBed.get(UserPreferencesService);
storage = TestBed.get(StorageService);
translate = TestBed.get(TranslateService);
});
@@ -57,9 +57,12 @@ describe('UserPreferencesService', () => {
if (changeDisposable) {
changeDisposable.unsubscribe();
}
storage.clear();
});
it('should get default pagination from app config', (done) => {
preferences = new UserPreferencesService(translate, appConfig, storage);
appConfig.config.pagination.size = 0;
appConfig.load().then(() => {
expect(preferences.paginationSize).toBe(0);
@@ -68,44 +71,52 @@ describe('UserPreferencesService', () => {
});
it('should return supported page sizes defined in the app config', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
const supportedPages = preferences.supportedPageSizes;
appConfig.load();
expect(supportedPages).toEqual(supportedPaginationSize);
});
it('should use [GUEST] as default storage prefix', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.setStoragePrefix(null);
expect(preferences.getStoragePrefix()).toBe('GUEST');
});
it('should change storage prefix', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.setStoragePrefix('USER_A');
expect(preferences.getStoragePrefix()).toBe('USER_A');
});
it('should format property key for default prefix', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.setStoragePrefix(null);
expect(preferences.getPropertyKey('propertyA')).toBe('GUEST__propertyA');
});
it('should format property key for custom prefix', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.setStoragePrefix('USER_A');
expect(preferences.getPropertyKey('propertyA')).toBe('USER_A__propertyA');
});
it('should save value with default prefix', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.set('propertyA', 'valueA');
const propertyKey = preferences.getPropertyKey('propertyA');
expect(storage.getItem(propertyKey)).toBe('valueA');
});
it('should null value return default prefix', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
storage.setItem('paginationSize', null);
const paginationSize = preferences.getPropertyKey('paginationSize');
expect(preferences.get(paginationSize, 'default')).toBe('default');
});
it('should save value with custom prefix', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.setStoragePrefix('USER_A');
preferences.set('propertyA', 'valueA');
const propertyKey = preferences.getPropertyKey('propertyA');
@@ -113,37 +124,82 @@ describe('UserPreferencesService', () => {
});
it('should return as default locale the app.config locate as first', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
appConfig.config.locale = 'fake-locate-config';
spyOn(translate, 'getBrowserCultureLang').and.returnValue('fake-locate-browser');
expect(preferences.getDefaultLocale()).toBe('fake-locate-config');
});
it('should return as default locale the browser locale as second', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
spyOn(translate, 'getBrowserCultureLang').and.returnValue('fake-locate-browser');
expect(preferences.getDefaultLocale()).toBe('fake-locate-browser');
});
it('should return as default locale the component property as third ', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
spyOn(translate, 'getBrowserCultureLang').and.stub();
expect(preferences.getDefaultLocale()).toBe('en');
});
it('should return as locale the store locate', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.locale = 'fake-store-locate';
appConfig.config.locale = 'fake-locate-config';
spyOn(translate, 'getBrowserCultureLang').and.returnValue('fake-locate-browser');
expect(preferences.locale).toBe('fake-store-locate');
});
it('should store default textOrientation based on language ', async(() => {
appConfig.config.languages = [
{
key: 'fake-locale-config'
}
];
appConfig.config.locale = 'fake-locale-config';
preferences = new UserPreferencesService(translate, appConfig, storage);
appConfig.load();
const textOrientation = preferences.getPropertyKey('textOrientation');
expect(storage.getItem(textOrientation)).toBe('ltr');
}));
it('should store textOrientation based on language config direction', async(() => {
appConfig.config.languages = [
{
key: 'fake-locale-config',
direction: 'rtl'
}
];
appConfig.config.locale = 'fake-locale-config';
preferences = new UserPreferencesService(translate, appConfig, storage);
appConfig.load();
const textOrientation = preferences.getPropertyKey('textOrientation');
expect(storage.getItem(textOrientation)).toBe('rtl');
}));
it('should not store textOrientation based on language ', async(() => {
appConfig.config.languages = [
{
key: 'fake-locale-browser'
}
];
preferences = new UserPreferencesService(translate, appConfig, storage);
appConfig.load();
const textOrientation = preferences.getPropertyKey('textOrientation');
expect(storage.getItem(textOrientation)).toBe(null);
}));
it('should not store in the storage the locale if the app.config.json does not have a value', () => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.locale = 'fake-store-locate';
spyOn(translate, 'getBrowserCultureLang').and.returnValue('fake-locate-browser');
expect(preferences.locale).toBe('fake-store-locate');
expect(storage.getItem(UserPreferenceValues.Locale)).toBe(null);
});
it('should stream the page size value when is set', (done) => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.paginationSize = 5;
changeDisposable = preferences.onChange.subscribe((userPreferenceStatus) => {
expect(userPreferenceStatus[UserPreferenceValues.PaginationSize]).toBe(5);
@@ -152,6 +208,7 @@ describe('UserPreferencesService', () => {
});
it('should stream the user preference status when changed', (done) => {
preferences = new UserPreferencesService(translate, appConfig, storage);
preferences.set('propertyA', 'valueA');
changeDisposable = preferences.onChange.subscribe((userPreferenceStatus) => {
expect(userPreferenceStatus.propertyA).toBe('valueA');

View File

@@ -18,7 +18,8 @@
import { Injectable } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { Observable, BehaviorSubject } from 'rxjs';
import { AppConfigService } from '../app-config/app-config.service';
import { AppConfigService, AppConfigValues } from '../app-config/app-config.service';
import { LanguageItem } from '../language-menu/language.interface';
import { StorageService } from './storage.service';
import { distinctUntilChanged, map } from 'rxjs/operators';
@@ -61,9 +62,15 @@ export class UserPreferencesService {
private initUserLanguage() {
if (this.locale || this.appConfig.get<string>(UserPreferenceValues.Locale)) {
this.set(UserPreferenceValues.Locale, (this.locale || this.getDefaultLocale()));
const locale = this.locale || this.getDefaultLocale();
this.set(UserPreferenceValues.Locale, locale);
this.set('textOrientation', this.getLanguageByKey(locale).direction || 'ltr');
} else {
this.setWithoutStore(UserPreferenceValues.Locale, (this.locale || this.getDefaultLocale()));
const locale = this.locale || this.getDefaultLocale();
this.setWithoutStore(UserPreferenceValues.Locale, locale);
this.setWithoutStore('textOrientation', (this.locale || this.getDefaultLocale()));
}
}
@@ -215,4 +222,11 @@ export class UserPreferencesService {
return this.appConfig.get<string>(UserPreferenceValues.Locale) || this.translate.getBrowserCultureLang() || 'en';
}
private getLanguageByKey(key: string): LanguageItem {
return (
this.appConfig
.get<Array<LanguageItem>>(AppConfigValues.APP_CONFIG_LANGUAGES_KEY)
.find((language) => key.includes(language.key)) || <LanguageItem> { key: 'en' }
);
}
}