alfresco-ng2-components/docs/core/services/user-preferences.service.md
Maurizio Vitale 1fa81962a0
👽 Angular 14 rebase 👽 (#7769)
* fix after rebase

* new release strategy for ng next

Signed-off-by: eromano <eugenioromano16@gmail.com>

* peer dep

Signed-off-by: eromano <eugenioromano16@gmail.com>

* Angular 14

fix unit test and storybook

Signed-off-by: eromano <eugenioromano16@gmail.com>

fix after rebase

Signed-off-by: eromano <eugenioromano16@gmail.com>

update pkg.json

Signed-off-by: eromano <eugenioromano16@gmail.com>

missing dep

Signed-off-by: eromano <eugenioromano16@gmail.com>

Fix mistake and missing code

Dream....build only affected libs

Add utility run commands

* Use nx command to run affected tests

* Fix nx test core

fix content tests

Run unit with watch false

core test fixes

reduce test warnings

Fix process cloud unit

Fix adf unit test

Fix lint process cloud

Disable lint next line

Use right core path

Fix insights unit

fix linting insights

Fix process-services unit

fix the extensions test report

fix test warnings

Fix content unit

Fix bunch of content unit

* Produce an adf alpha of 14

* hopefully fixing the content

* Push back the npm publish

* Remove flaky unit

* Fix linting

* Make the branch as root

* Get rid of angualar13

* Remove the travis depth

* Fixing version for npm

* Enabling cache for unit and build

* Fix scss for core and paths

Copy i18 and asset by using ng-packager

Export the theming alias and fix path

Use ng-package to copy assets process-services-cloud

Use ng-package to copy assets process-services

Use ng-package to copy assets content-services

Use ng-package to copy assets insights

* feat: fix api secondary entry point

* fix storybook rebase

* Move dist under dist/libs from lib/dist

* Fix the webstyle

* Use only necessary nrwl deps and improve lint

* Fix unit for libs

* Convert lint.sh to targets - improve performance

* Use latest of angular

* Align alfresco-js-api

Signed-off-by: eromano <eugenioromano16@gmail.com>
Co-authored-by: eromano <eugenioromano16@gmail.com>
Co-authored-by: Mikolaj Serwicki <mikolaj.serwicki@hyland.com>
Co-authored-by: Tomasz <tomasz.gnyp@hyland.com>
2022-08-25 10:50:30 +01:00

5.0 KiB

Title, Added, Status, Last reviewed
Title Added Status Last reviewed
User Preferences Service v2.0.0 Active 2019-01-16

User Preferences Service

Stores preferences for the app and for individual components.

Class members

Methods

  • get(property: string, defaultValue?: string): string
    Gets a preference property.
    • property: string - Name of the property
    • defaultValue: string - (Optional) Default to return if the property is not found
    • Returns string - Preference property
  • getDefaultLocale(): string
    Gets the default locale.
    • Returns string - Default locale language code
  • getPropertyKey(property: string): string
    Gets the full property key with prefix.
    • property: string - The property name
    • Returns string - Property key
  • getStoragePrefix(): string
    Gets the active storage prefix for preferences.
    • Returns string - Storage prefix
  • hasItem(property: string): boolean
    Check if an item is present in the storage
    • property: string - Name of the property
    • Returns boolean - True if the item is present, false otherwise
  • select(property: string): Observable<any>
    Sets up a callback to notify when a property has changed.
    • property: string - The property to watch
    • Returns Observable<any> - Notification callback
  • set(property: string, value: any)
    Sets a preference property.
    • property: string - Name of the property
    • value: any - New value for the property
  • setStoragePrefix(value: string)
    Sets the active storage prefix for preferences.
    • value: string - Name of the prefix
  • setWithoutStore(property: string, value: any)
    Sets a preference property.
    • property: string - Name of the property
    • value: any - New value for the property

Details

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 to the current username:

import { UserPreferencesService, AuthenticationService } from '@alfresco/adf-core';

@Component({...})
class AppComponent {
    constructor(private userPreferences: UserPreferencesService,
                private authService: AuthenticationService) {
    }

    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 a dedicated profile.

You can import the service into your controller and use its APIs as shown below:

@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:

Name Type Description
authType string Authorization type (can be "ECM", "BPM" or "ALL").
disableCSRF boolean Prevents the CSRF Token from being submitted if true. Only valid for Process Services.
paginationSize number Pagination size.
locale string Current locale setting.

User Preference onChange Stream

Whenever a property is set with the user preferences service, an onChange event is sent with the whole set of user properties. This is useful when a component needs to react to a property change:

    userPreferences.paginationSize = 10;
    userPreferences.onChange().subscribe((userStatusValues) => {
        console.log(userStatusValues.PAGINATION_SIZE); //this will be 10
    });

You can also use the select method to get notification when a particular property is changed. A set of basic properties is added into the enumeration UserPreferenceValues which gives you the key value to access the standard user preference service properties : PaginationSize, DisableCSRF, Locale, SupportedPageSizes and ExpandedSideNavStatus.

    userPreferences.disableCSRF = true;
    userPreferences.select(UserPreferenceValues.DisableCSRF).subscribe((CSRFflag) => {
        console.log(CSRFflag); //this will be true;
    });