File

context-dashboard-state/context-dashboard-state.service.ts

Description

Manages dashboard-level state with dirty tracking and persistence capabilities.

This service provides a centralized state management system for context-aware dashboards. It tracks the original (default) state from the dashboard configuration, maintains a working copy of the state, and determines when changes need to be saved based on deep equality comparison.

Key responsibilities:

  • Extract and store the dashboard's default state on selection
  • Maintain a mutable working state (globalState) that can be updated
  • Track whether current state differs from default (dirty tracking)
  • Enable/disable save functionality based on state changes
  • Persist state changes back to the dashboard via inventory API
Example :
// Dashboard with global time context state
interface TimeContextState {
  dateFrom: string;
  dateTo: string;
  interval: string;
}


const stateService = inject(ContextDashboardStateService<TimeContextState>);


// Set dashboard and extract its default state
stateService.setSelectedDashboard(dashboard);


// Update state (triggers dirty tracking)
stateService.updateGlobalState({ dateFrom: '2024-01-01' });


// Check if save is needed
stateService.isSaveDisabled.subscribe(disabled => {
  if (!disabled) {
    // Save button enabled - state has changed
  }
});

Index

Properties
Methods

Methods

extractStateFromDashboard
extractStateFromDashboard(dashboard: DashboardWithState | null)

Extracts state from the standard dashboard structure.

Retrieves the state object stored at the conventional path: dashboard.c8y_Dashboard.dashboardState

Parameters :
Name Type Optional Description
dashboard DashboardWithState<StateType> | null No
  • Dashboard object with potential state at c8y_Dashboard.dashboardState
Returns : StateType | null

Extracted state object, or null if dashboard is null or has no state

getGlobalState
getGlobalState()

Returns a deep clone of the current working state.

The returned object is a deep copy, so modifications won't affect the internal state. Use this when you need to read state without risking accidental mutations.

Returns : StateType | null

Deep cloned copy of current state, or null if no state exists

resetGlobalState
resetGlobalState()

Reverts the working state back to the dashboard's original default state.

This effectively discards all changes made since the dashboard was selected, resetting to the state that was extracted from c8y_Dashboard.dashboardState. Also disables the save button since state now matches the default.

Use case: Called when user clicks "Cancel" or "Reset" to discard unsaved changes.

Returns : void
Async saveDashboardState
saveDashboardState(mo: any, state: Record)

Persists state changes to the dashboard via the Cumulocity inventory API.

Updates the dashboard's c8y_Dashboard.dashboardState property with the provided state object and saves it to the backend. Creates the c8y_Dashboard object if it doesn't exist.

Note: This method doesn't automatically update the local signals. After a successful save, you should emit to dashboardSaved to notify listeners.

Parameters :
Name Type Optional Description
mo any No
  • Managed object (dashboard) to update
state Record<string | any> No
  • State object to persist to c8y_Dashboard.dashboardState
Returns : Promise<any>

Promise resolving to the updated dashboard object from the API

setSelectedDashboard
setSelectedDashboard(dashboard: DashboardWithState)

Selects a dashboard and initializes state management for it.

This method performs the complete initialization workflow when a dashboard is selected:

  1. Extracts the dashboard's embedded state from c8y_Dashboard.dashboardState
  2. Creates an immutable snapshot as the default state (for dirty tracking)
  3. Creates a working copy as the current global state (for modifications)
  4. Resets the save button state to disabled (no changes yet)

Deep cloning strategy: Both default state and global state are deep cloned to ensure complete isolation. This prevents accidental mutations from affecting the comparison baseline.

Null handling: When null/undefined is passed, all state is cleared and signals/observables are reset.

or null to clear the selection

Parameters :
Name Type Optional Description
dashboard DashboardWithState<StateType> No
  • Dashboard object containing state at c8y_Dashboard.dashboardState, or null to clear the selection
Returns : void
updateGlobalState
updateGlobalState(newState: Partial<StateType>)

Merges partial state updates into the current working state with dirty tracking.

This method applies partial updates to the global state using a shallow merge strategy, then performs deep equality comparison against the default state to determine if save should be enabled.

Example :
// Initial state: { dateFrom: '2024-01-01', dateTo: '2024-01-02' }
stateService.updateGlobalState({ dateFrom: '2024-02-01' });
// Result: { dateFrom: '2024-02-01', dateTo: '2024-01-02' }
// isSaveDisabled = false (changed from default)
Parameters :
Name Type Optional Description
newState Partial<StateType> No
  • Partial state object with properties to merge into current state
Returns : void

Properties

Readonly dashboardDefaultState
Type : unknown
Default value : signal<StateType | null>(null)

Immutable snapshot of the dashboard's original state, used for comparison

Readonly dashboardSaved
Type : unknown
Default value : new BehaviorSubject<any>(null)

Observable emitting the dashboard object after successful save operations

Readonly globalState
Type : unknown
Default value : signal<StateType | null>(null)

Current working state that can be modified, separate from the default state

Readonly inventory
Type : unknown
Default value : inject(InventoryService)
Readonly isSaveDisabled
Type : unknown
Default value : new BehaviorSubject<boolean>(true)

Observable indicating whether save should be disabled (true when no changes detected)

Readonly selected$
Type : unknown
Default value : new BehaviorSubject<DashboardWithState<StateType> | null>(null)

Observable for dashboard selection changes (legacy support)

Readonly selectedDashboard
Type : unknown
Default value : signal<DashboardWithState<StateType> | null>(null)

Currently selected dashboard object with its embedded state

results matching ""

    No results matching ""