-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cockpit-actions: Add data-lake library to support sharing parameters …
…for actions
- Loading branch information
1 parent
a91febd
commit 417cf85
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* A parameter for a Cockpit action | ||
* @param { string } name - The name of the parameter | ||
* @param { 'string' | 'number' | 'boolean' } type - The type of the parameter (string, number or boolean) | ||
* @param { string } description - What the parameter does or means | ||
* @param { string | number | boolean } defaultValue - The default value of the parameter | ||
* @param { boolean } required - Whether the parameter is required or not | ||
* @param { (string | number)[]? } options - The options for the parameter (only if type is string or number). | ||
* @param { number? } min - The minimum value for the parameter (only if type is number). | ||
* @param { number? } max - The maximum value for the parameter (only if type is number). | ||
*/ | ||
class CockpitActionParameter { | ||
id: string | ||
name: string | ||
type: 'string' | 'number' | 'boolean' | ||
required: boolean | ||
description?: string | ||
defaultValue?: string | number | boolean | ||
options?: (string | number)[] | ||
min?: number | ||
max?: number | ||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
constructor( | ||
id: string, | ||
name: string, | ||
type: 'string' | 'number' | 'boolean', | ||
required: boolean, | ||
description?: string, | ||
defaultValue?: string | number | boolean, | ||
options?: (string | number)[], | ||
min?: number, | ||
max?: number | ||
) { | ||
this.id = id | ||
this.name = name | ||
this.type = type | ||
this.description = description | ||
this.defaultValue = defaultValue | ||
this.required = required | ||
this.options = options | ||
this.min = min | ||
this.max = max | ||
} | ||
} | ||
|
||
const cockpitActionParametersInfo: Record<string, CockpitActionParameter> = {} | ||
export const cockpitActionParametersData: Record<string, string | number | boolean> = {} | ||
|
||
export const getCockpitActionParametersInfo = (id: string): CockpitActionParameter | undefined => { | ||
return cockpitActionParametersInfo[id] | ||
} | ||
|
||
export const getAllCockpitActionParametersInfo = (): Record<string, CockpitActionParameter> => { | ||
return cockpitActionParametersInfo | ||
} | ||
|
||
export const getCockpitActionParameterInfo = (id: string): CockpitActionParameter | undefined => { | ||
return cockpitActionParametersInfo[id] | ||
} | ||
|
||
export const setCockpitActionParameterInfo = (id: string, parameter: CockpitActionParameter): void => { | ||
cockpitActionParametersInfo[id] = parameter | ||
} | ||
|
||
export const getCockpitActionParameterData = (id: string): string | number | boolean | undefined => { | ||
return cockpitActionParametersData[id] | ||
} | ||
|
||
export const setCockpitActionParameterData = (id: string, data: string | number | boolean): void => { | ||
cockpitActionParametersData[id] = data | ||
notifyCockpitActionParameterListeners(id) | ||
} | ||
|
||
export const deleteCockpitActionParameter = (id: string): void => { | ||
delete cockpitActionParametersInfo[id] | ||
delete cockpitActionParametersData[id] | ||
} | ||
|
||
const cockpitActionParametersListeners: Record<string, (() => void)[]> = {} | ||
|
||
export const listenCockpitActionParameter = (id: string, listener: () => void): void => { | ||
if (!cockpitActionParametersListeners[id]) { | ||
cockpitActionParametersListeners[id] = [] | ||
} | ||
cockpitActionParametersListeners[id].push(listener) | ||
} | ||
|
||
export const unlistenCockpitActionParameter = (id: string): void => { | ||
delete cockpitActionParametersListeners[id] | ||
} | ||
|
||
export const notifyCockpitActionParameterListeners = (id: string): void => { | ||
if (cockpitActionParametersListeners[id]) { | ||
cockpitActionParametersListeners[id].forEach((listener) => listener()) | ||
} | ||
} | ||
|
||
export const getCockpitActionParametersListeners = (id: string): (() => void)[] => { | ||
return cockpitActionParametersListeners[id] ?? [] | ||
} |