From c156e1ca6f57c9c914a772e349ea8182ff54ad73 Mon Sep 17 00:00:00 2001 From: Koen Pauwels Date: Wed, 4 Jan 2023 11:43:15 +0100 Subject: [PATCH] 97732 Context help model, actions and reducer --- src/app/shared/context-help.actions.ts | 81 +++++++++++++++++++++ src/app/shared/context-help.model.ts | 4 + src/app/shared/context-help.reducer.ts | 47 ++++++++++++ src/app/shared/context-help.service.spec.ts | 16 ++++ src/app/shared/context-help.service.ts | 33 +++++++++ 5 files changed, 181 insertions(+) create mode 100644 src/app/shared/context-help.actions.ts create mode 100644 src/app/shared/context-help.model.ts create mode 100644 src/app/shared/context-help.reducer.ts create mode 100644 src/app/shared/context-help.service.spec.ts create mode 100644 src/app/shared/context-help.service.ts diff --git a/src/app/shared/context-help.actions.ts b/src/app/shared/context-help.actions.ts new file mode 100644 index 0000000000..2bd8b6a163 --- /dev/null +++ b/src/app/shared/context-help.actions.ts @@ -0,0 +1,81 @@ +import { Action } from '@ngrx/store'; +import { type } from './ngrx/type'; +import { ContextHelpModel } from './context-help.model'; + +export const ContextHelpActionTypes = { + 'CONTEXT_HELP_TOGGLE_ICONS': type('dspace/context-help/CONTEXT_HELP_TOGGLE_ICONS'), + 'CONTEXT_HELP_ADD': type('dspace/context-help/CONTEXT_HELP_ADD'), + 'CONTEXT_HELP_REMOVE': type('dspace/context-help/CONTEXT_HELP_REMOVE'), + 'CONTEXT_HELP_TOGGLE_TOOLTIP': type('dspace/context-help/CONTEXT_HELP_TOGGLE_TOOLTIP'), + 'CONTEXT_HELP_SHOW_TOOLTIP': type('dspace/context-help/CONTEXT_HELP_SHOW_TOOLTIP'), + 'CONTEXT_HELP_HIDE_TOOLTIP' : type('dspace/context-help/CONTEXT_HELP_HIDE_TOOLTIP'), +}; + +/** + * Toggles the visibility of all context help icons. + */ +export class ContextHelpToggleIconsAction implements Action { + type = ContextHelpActionTypes.CONTEXT_HELP_TOGGLE_ICONS; +} + +/** + * Registers a new context help icon to the store. + */ +export class ContextHelpAddAction implements Action { + type = ContextHelpActionTypes.CONTEXT_HELP_ADD; + model: ContextHelpModel; + + constructor (model: ContextHelpModel) { + this.model = model; + } +} + +/** + * Removes a context help icon from the store. + */ +export class ContextHelpRemoveAction implements Action { + type = ContextHelpActionTypes.CONTEXT_HELP_REMOVE; + id: string; + + constructor(id: string) { + this.id = id; + } +} + +export abstract class ContextHelpTooltipAction implements Action { + type; + id: string; + + constructor(id: string) { + this.id = id; + } +} + +/** + * Toggles the tooltip of a single context help icon. + */ +export class ContextHelpToggleTooltipAction extends ContextHelpTooltipAction { + type = ContextHelpActionTypes.CONTEXT_HELP_TOGGLE_TOOLTIP; +} + +/** + * Shows the tooltip of a single context help icon. + */ +export class ContextHelpShowTooltipAction extends ContextHelpTooltipAction { + type = ContextHelpActionTypes.CONTEXT_HELP_SHOW_TOOLTIP; +} + +/** + * Hides the tooltip of a single context help icon. + */ +export class ContextHelpHideTooltipAction extends ContextHelpTooltipAction { + type = ContextHelpActionTypes.CONTEXT_HELP_HIDE_TOOLTIP; +} + +export type ContextHelpAction + = ContextHelpToggleIconsAction + | ContextHelpAddAction + | ContextHelpRemoveAction + | ContextHelpToggleTooltipAction + | ContextHelpShowTooltipAction + | ContextHelpHideTooltipAction; diff --git a/src/app/shared/context-help.model.ts b/src/app/shared/context-help.model.ts new file mode 100644 index 0000000000..4b2b344f60 --- /dev/null +++ b/src/app/shared/context-help.model.ts @@ -0,0 +1,4 @@ +export class ContextHelpModel { + id: string; + tooltipVisible?: boolean = false; +} diff --git a/src/app/shared/context-help.reducer.ts b/src/app/shared/context-help.reducer.ts new file mode 100644 index 0000000000..b5f683956f --- /dev/null +++ b/src/app/shared/context-help.reducer.ts @@ -0,0 +1,47 @@ +import { ContextHelpModel } from './context-help.model'; +import { ContextHelpAction, ContextHelpActionTypes } from './context-help.actions'; + +export type ContextHelpModels = { + [id: string]: ContextHelpModel; +}; + +export interface ContextHelpState { + allIconsVisible: boolean; + models: ContextHelpModels; +} + +export function contextHelpReducer(state: ContextHelpState, action: ContextHelpAction): ContextHelpState { + switch (action.type) { + case ContextHelpActionTypes.CONTEXT_HELP_TOGGLE_ICONS: { + return {...state, allIconsVisible: true}; + } + case ContextHelpActionTypes.CONTEXT_HELP_ADD: { + const newModels = {...state.models, [action.model.id]: action.model}; + return {...state, models: newModels}; + } + case ContextHelpActionTypes.CONTEXT_HELP_REMOVE: { + const {[action.id]: _, ...remainingModels} = state.models; + return {...state, models: remainingModels}; + } + case ContextHelpActionTypes.CONTEXT_HELP_TOGGLE_TOOLTIP: { + return modifyTooltipVisibility(state, action.id, v => !v); + } + case ContextHelpActionTypes.CONTEXT_HELP_SHOW_TOOLTIP: { + return modifyTooltipVisibility(state, action.id, _ => true); + } + case ContextHelpActionTypes.CONTEXT_HELP_HIDE_TOOLTIP: { + return modifyTooltipVisibility(state, action.id, _ => false); + } + default: { + return state; + } + } +} + +function modifyTooltipVisibility(state: ContextHelpState, id: string, modify: (vis: boolean) => boolean) + : ContextHelpState { + const {[id]: matchingModel, ...otherModels} = state.models; + const modifiedModel = {...matchingModel, tooltipVisible: modify(matchingModel.tooltipVisible)}; + const newModels = {...otherModels, [id]: modifiedModel}; + return {...state, models: newModels}; +} diff --git a/src/app/shared/context-help.service.spec.ts b/src/app/shared/context-help.service.spec.ts new file mode 100644 index 0000000000..d2d29121b0 --- /dev/null +++ b/src/app/shared/context-help.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { ContextHelpService } from './context-help.service'; + +describe('ContextHelpService', () => { + let service: ContextHelpService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(ContextHelpService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/shared/context-help.service.ts b/src/app/shared/context-help.service.ts new file mode 100644 index 0000000000..071308c713 --- /dev/null +++ b/src/app/shared/context-help.service.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@angular/core'; +import { ContextHelpModel } from './context-help.model'; + +@Injectable({ + providedIn: 'root' +}) +export class ContextHelpService { + constructor() { } + + toggleIcons() { + + } + + add(contextHelp: ContextHelpModel) { + + } + + remove(id: string) { + + } + + showTooltip(id: string) { + + } + + hideTooltip(id: string) { + + } + + toggleTooltip(id: string) { + + } +}