UNPKG

console-gui-tools

Version:

A simple library to draw option menu, text popup or other widgets and layout on a Node.js console.

144 lines 5.55 kB
/// <reference types="node" /> import { EventEmitter } from "events"; import { ConsoleManager, KeyListenerArgs } from "../../ConsoleGui.js"; import { PhisicalValues } from "../Utils.js"; /** * @description The configuration for the OptionPopup class. * @typedef {Object} OptionPopupConfig * * @prop {string} id - The id of the popup. * @prop {string} title - The title of the popup. * @prop {Array<string | number>} options - The options of the popup. * @prop {string | number} selected - The selected option of the popup. * @prop {boolean} [visible] - If the popup is visible. * * @export * @interface OptionPopupConfig */ export interface OptionPopupConfig { id: string; title: string; options: Array<string | number>; selected: string | number; visible?: boolean; } /** * @class OptionPopup * @extends EventEmitter * @description This class is used to create a popup with a list of selectable options. * * ![OptionPopup](https://user-images.githubusercontent.com/14907987/165752387-2eac4936-1b5d-462e-9353-562d04f1b4fe.gif) * * Emits the following events: * - "confirm" when the user confirm the option * - "cancel" when the user cancel the option * - "exit" when the user exit the option * @param {string} id - The id of the popup. * @param {string} title - The title of the popup. * @param {Array<string | number>} options - The options of the popup. * @param {string | number} selected - The selected option. * @param {boolean} visible - If the popup is visible. Default is false (make it appears using show()). * * @example ```ts * const popup = new OptionPopup({ * id:"popup1", * title: "Choose the option", * options, * selected * }).show().on("confirm", (option) => { console.log(option) }) // show the popup and wait for the user to confirm * ``` */ export declare class OptionPopup extends EventEmitter { readonly CM: ConsoleManager; readonly id: string; title: string; private options; private selected; private visible; private marginTop; private startIndex; private parsingMouseFrame; /** @var {number} x - The x offset of the popup to be drown. If 0 it will be placed on the center */ private offsetX; /** @var {number} y - The y offset of the popup to be drown. If 0 it will be placed on the center */ private offsetY; private absoluteValues; private dragging; private dragStart; private focused; constructor(config: OptionPopupConfig); private adaptOptions; /** * @description This function is used to make the ConsoleManager handle the key events when the popup is showed. * Inside this function are defined all the keys that can be pressed and the actions to do when they are pressed. * @param {string} str - The string of the input. * @param {Object} key - The key object. * @memberof OptionPopup */ keyListener(_str: string, key: KeyListenerArgs): void; /** * @description This function is used to get the selected option. * @returns {string | number} The selected value of the popup. * @memberof OptionPopup */ getSelected(): string | number; /** * @description This function is used to change the selection of the popup. It also refresh the ConsoleManager. * @param {string | number} selected - The new value of the selection. * @memberof OptionPopup * @returns {OptionPopup} The instance of the OptionPopup. */ setSelected(selected: string | number): OptionPopup; /** * @description This function is used to show the popup. It also register the key events and refresh the ConsoleManager. * @returns {OptionPopup} The instance of the OptionPopup. * @memberof OptionPopup */ show(): OptionPopup; /** * @description This function is used to hide the popup. It also unregister the key events and refresh the ConsoleManager. * @returns {OptionPopup} The instance of the OptionPopup. * @memberof OptionPopup */ hide(): OptionPopup; /** * @description This function is used to get the visibility of the popup. * @returns {boolean} The visibility of the popup. * @memberof OptionPopup */ isVisible(): boolean; /** * @description This function is used to return the PhisicalValues of the popup (x, y, width, height). * @memberof OptionPopup * @private * @returns {OptionPopup} The instance of the OptionPopup. * @memberof OptionPopup */ getPosition(): PhisicalValues; /** * @description This function is used to add the OptionPopup key listener callback to te ConsoleManager. * @returns {OptionPopup} The instance of the OptionPopup. * @memberof OptionPopup */ private manageInput; /** * @description This function is used to remove the OptionPopup key listener callback to te ConsoleManager. * @returns {OptionPopup} The instance of the OptionPopup. * @memberof OptionPopup */ private unManageInput; /** * @description This function is used to manage the mouse events on the OptionPopup. * @param {MouseEvent} event - The string of the input. * @memberof OptionPopup */ private mouseListener; /** * @description This function is used to draw the OptionPopup to the screen in the middle. * @returns {OptionPopup} The instance of the OptionPopup. * @memberof OptionPopup */ draw(): OptionPopup; } export default OptionPopup; //# sourceMappingURL=OptionPopup.d.ts.map