UNPKG

coveo-search-ui-extensions

Version:

Small generic components to extend the functionality of Coveo's Search UI framework.

94 lines (93 loc) 3.62 kB
import { IResultsComponentBindings } from 'coveo-search-ui'; import { ActionButtonOptions, IActionButtonOptionsWithTitle, IActionButtonOptionsWithIcon } from './ActionButton'; /** * Represent a state that can be used by a StatefulActionButton. */ export declare type StatefulActionButtonState = ActionButtonOptions & IStateOptions; export interface IStatefulActionButtonOptionsWithTitle extends IActionButtonOptionsWithTitle, IStateOptions { } export interface IStatefulActionButtonOptionsWithIcon extends IActionButtonOptionsWithIcon, IStateOptions { } export interface IStateOptions { /** * The name of the state. Used by StatefulActionButton for logging. */ name: string; /** * Called when this state is set as the current state. * Called after the onStateExit of the previous state if any. */ onStateEntry?: (this: StatefulActionButton) => void; /** * Called when this state has been set as the current state and another is about to get set. * Called before the onStateEntry of the next state if any. */ onStateExit?: (this: StatefulActionButton) => void; } /** * Represent a transition from one `IStatefulActionButtonState` to another. */ export interface IStatefulActionButtonTransition { from: StatefulActionButtonState; to: StatefulActionButtonState; } /** * The options format for a StatefulActionButton */ export interface IStatefulActionButtonOptions { /** * An array containing all states used by the StatefulActionButton instance. * If `switchTo` is called with an unknown state, a warning will be emitted * and the transition will not occur. */ states: [StatefulActionButtonState, ...StatefulActionButtonState[]]; /** * An array containing all the state transitions allowed on the StatefulActionButton instance. * If `switchTo` is called with an illegal transitions , a warning will be emitted * and the transition will not occur. */ allowedTransitions?: IStatefulActionButtonTransition[]; /** * The initial state to be used by the StatefulActionButton instance. * If nullish or missing from the states array, a warning will be emitted, * no ActionButton will be constructed and the element will be hidden */ initialState: StatefulActionButtonState; } /** * An action button able to handle multiple states and their transitions. */ export declare class StatefulActionButton { element: HTMLElement; options: IStatefulActionButtonOptions; bindings?: IResultsComponentBindings; static ID: string; private currentState; private innerActionButton; constructor(element: HTMLElement, options: IStatefulActionButtonOptions, bindings?: IResultsComponentBindings); /** * Switch the state of the instance if the state and the transition between the current and new state are allowed. * @param state a state to try to switch to */ switchTo(state: StatefulActionButtonState): void; /** * Return the current state of the instance. */ getCurrentState(): StatefulActionButtonState; /** * Check if the options given to the constructor are valid. * If not, it will also display the appropriate warning. */ private checkOptionsValidity; private areTransitionsValid; private generateInvalidTransitionMessage; /** * Check if a transition from the current state to @param state is allowed. * @param state the destination of the transition */ private isTransitionAllowed; /** * Handle user click. */ private handleClick; }