From 1d1e4cc8cc9149bf9bf002eeced1cccf4c6d3ac6 Mon Sep 17 00:00:00 2001 From: Andrew Stegmaier Date: Tue, 1 Aug 2017 13:05:15 -0700 Subject: [PATCH] [Handsontable] Add support for Hooks interface (#18358) * Add all types for 'Hooks' API * Fix linting errors. * Replace Function type with a function signature. --- types/handsontable/index.d.ts | 98 ++++++++++++++++++++++++++++++- types/handsontable/test/global.ts | 27 ++++++++- types/handsontable/test/module.ts | 25 ++++++++ 3 files changed, 147 insertions(+), 3 deletions(-) diff --git a/types/handsontable/index.d.ts b/types/handsontable/index.d.ts index 165d872e8f..6d86a085f1 100644 --- a/types/handsontable/index.d.ts +++ b/types/handsontable/index.d.ts @@ -1,6 +1,8 @@ // Type definitions for Handsontable 0.31 // Project: https://handsontable.com/ -// Definitions by: Handsoncode sp. z o.o. , Ryan Riley +// Definitions by: Handsoncode sp. z o.o. +// Ryan Riley +// Andrew Stegmaier // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped\ declare namespace __Handsontable { @@ -401,8 +403,100 @@ declare namespace Handsontable { } interface Hooks { + /** + * Adds a listener (globally or locally) to a specified hook name. + * + * @param key Hook name. + * @param callback Callback function or an array of functions. + * @param context (optional) The context for the hook callback to be added - a Handsontable instance or leave empty. + */ + add(key: string, callback: ((...params: any[]) => any | void) | Array<(...params: any[]) => any | void>, context?: Handsontable): Object; + + /** + * Returns a new object with empty handlers related to every registered hook name. + */ + createEmptyBucket(): Object; + + /** + * Deregisters a hook name (removes it from the list of known hook names). + * + * @param key Hook name. + */ + deregister(key: string): void; + + /** + * Destroy all listeners connected to the context. If no context is provided, the global listeners will be destroyed. + * + * @param context (optional) A Handsontable instance. + */ + destroy(context?: Handsontable): void; + + /** + * Get hook bucket based on the context of the object or if argument is undefined, get the global hook bucket. + * + * @param context (optional) A Handsontable instance. + */ + getBucket(context?: Handsontable): Object; + + /** + * Returns an array of registered hooks. + */ + getRegistered(): string[]; + + /** + * Checks whether there are any registered listeners for the provided hook name. + * + * @param key Hook name. + * @param context (optional) A Handsontable instance. + */ + has(key: string, context?: Handsontable): boolean; + + /** + * Returns a boolean depending on if a hook by such name has been registered. + * + * @param key Hook name. + */ + isRegistered(key: string): boolean; + + /** + * Adds a listener to a specified hook. After the hook runs this listener will be automatically removed from the bucket. + * + * @param key Hook/Event name. + * @param callback Callback function. + * @param context (optional) A Handsontable instance. + */ + once(key: string, callback: ((...params: any[]) => any | void) | Array<(...params: any[]) => any | void>, context?: Handsontable): void; + + /** + * Registers a hook name (adds it to the list of the known hook names). Used by plugins. + * + * @param key The hook name. + */ register(key: string): void; - run(instace: Core, hookName: string, key?: any, value?: any): any; + + /** + * Removes a listener from a hook with a given name. + * + * @param key Hook/Event name. + * @param callback Callback function. + * @param context (optional) A Handsontable instance. + */ + remove(key: string, callback: (...params: any[]) => any | void, context?: Handsontable): boolean; + + /** + * Runs all local and global callbacks assigned to the hook identified by the key parameter. + * It returns either a return value from the last called callback or the first parameter (p1) passed to the run function. + * + * @param context Handsontable instance. + * @param key Hook/Event name. + * @param p1 (optional) Parameter to be passed as an argument to the callback function. + * @param p2 (optional) Parameter to be passed as an argument to the callback function. + * @param p3 (optional) Parameter to be passed as an argument to the callback function. + * @param p4 (optional) Parameter to be passed as an argument to the callback function. + * @param p5 (optional) Parameter to be passed as an argument to the callback function. + * @param p6 (optional) Parameter to be passed as an argument to the callback function. + */ + run(context: Handsontable, key: string, p1?: any, p2?: any, p3?: any, p4?: any, p5?: any, p6?: any): any; } interface Dom { diff --git a/types/handsontable/test/global.ts b/types/handsontable/test/global.ts index 331f62759d..3760fb1f95 100644 --- a/types/handsontable/test/global.ts +++ b/types/handsontable/test/global.ts @@ -293,7 +293,32 @@ function test_HandsontableMethods() { Handsontable.renderers.TextRenderer(hot, new HTMLTableDataCellElement(), 0, 0, "prop", 1.235, {}); Handsontable.Dom.addEvent(new HTMLElement(), "eventName", () => { return; }); Handsontable.Dom.empty(new HTMLElement()); -} + + let bucket: {}; + let registeredHooks: string[]; + let hasHook: boolean; + let isRegistered: boolean; + bucket = Handsontable.hooks.add('beforeInit', () => { return; }); + bucket = Handsontable.hooks.add('beforeInit', () => { return; }, hot); + Handsontable.hooks.createEmptyBucket(); + Handsontable.hooks.deregister('myHook'); + Handsontable.hooks.destroy(); + Handsontable.hooks.destroy(hot); + bucket = Handsontable.hooks.getBucket(); + bucket = Handsontable.hooks.getBucket(hot); + registeredHooks = Handsontable.hooks.getRegistered(); + hasHook = Handsontable.hooks.has("myHook"); + hasHook = Handsontable.hooks.has("myHook", hot); + isRegistered = Handsontable.hooks.isRegistered("myHook"); + Handsontable.hooks.once('beforeInit', () => { return; }); + Handsontable.hooks.once('beforeInit', [() => { return; }, () => { return; }]); + Handsontable.hooks.once('beforeInit', () => { return; }, hot); + Handsontable.hooks.register('myHook'); + Handsontable.hooks.remove('beforeInit', () => { return; }); + Handsontable.hooks.remove('beforeInit', () => { return; }, hot); + Handsontable.hooks.run(hot, 'beforeInit'); + Handsontable.hooks.run(hot, 'beforeInit', 'param1', 'param2', 'param3', 'param4', 'param5', 'param6'); + } class MyCustomHotPlugin extends Handsontable.plugins.BasePlugin { isEnabled(): boolean { diff --git a/types/handsontable/test/module.ts b/types/handsontable/test/module.ts index a67ab6942d..0bdffed3f5 100644 --- a/types/handsontable/test/module.ts +++ b/types/handsontable/test/module.ts @@ -295,6 +295,31 @@ function test_HandsontableMethods() { Handsontable.renderers.TextRenderer(hot, new HTMLTableDataCellElement(), 0, 0, "prop", 1.235, {}); Handsontable.Dom.addEvent(new HTMLElement(), "eventName", () => { return; }); Handsontable.Dom.empty(new HTMLElement()); + + let bucket: {}; + let registeredHooks: string[]; + let hasHook: boolean; + let isRegistered: boolean; + bucket = Handsontable.hooks.add('beforeInit', () => { return; }); + bucket = Handsontable.hooks.add('beforeInit', () => { return; }, hot); + Handsontable.hooks.createEmptyBucket(); + Handsontable.hooks.deregister('myHook'); + Handsontable.hooks.destroy(); + Handsontable.hooks.destroy(hot); + bucket = Handsontable.hooks.getBucket(); + bucket = Handsontable.hooks.getBucket(hot); + registeredHooks = Handsontable.hooks.getRegistered(); + hasHook = Handsontable.hooks.has("myHook"); + hasHook = Handsontable.hooks.has("myHook", hot); + isRegistered = Handsontable.hooks.isRegistered("myHook"); + Handsontable.hooks.once('beforeInit', () => { return; }); + Handsontable.hooks.once('beforeInit', [() => { return; }, () => { return; }]); + Handsontable.hooks.once('beforeInit', () => { return; }, hot); + Handsontable.hooks.register('myHook'); + Handsontable.hooks.remove('beforeInit', () => { return; }); + Handsontable.hooks.remove('beforeInit', () => { return; }, hot); + Handsontable.hooks.run(hot, 'beforeInit'); + Handsontable.hooks.run(hot, 'beforeInit', 'param1', 'param2', 'param3', 'param4', 'param5', 'param6'); } class MyCustomHotPlugin extends Handsontable.plugins.BasePlugin {