Skip to content

Commit

Permalink
dropped support for deprecated method signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
nkappler committed Oct 3, 2024
1 parent 577c821 commit 1c8e789
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 164 deletions.
53 changes: 9 additions & 44 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ type CTXMItem = CTXMAnchor | CTXMAction | CTXMHeading | CTXMDivider | CTXMSubMen
* This is a Menu Definition. In fact, it's just an array of Context Menu Items
*/
type CTXMenu = CTXMItem[];
/**
* A function that is called before the context menu is opened.
* It is passed the menu definition and the MouseEvent.
* Can be used to manipulate the menu based on the Event. (e.g. Cursor Position)
* Needs to return a menu definition.
*/
type BeforeRenderFN = (menu: CTXMenu, e?: MouseEvent) => CTXMenu;
interface CTXConfig {
onBeforeShow?: BeforeRenderFN;
/**
* Callback that is called before the context menu is opened.
* Can be used to manipulate the menu based on the Event. (e.g. to appear at the Cursor Position)
* @param menu - the original menu definition
* @param event - mouse event, when openend from context menu event
* @returns Needs to return a menu definition.
*/
onBeforeShow?: (menu: CTXMenu, event?: MouseEvent) => CTXMenu;
onShow?: Function;
onBeforeHide?: Function;
onHide?: Function;
Expand All @@ -87,22 +87,6 @@ interface CTXMenuSingleton {
* @param config A config object, See `CTXConfig`.
*/
attach(target: string, ctxMenu: CTXMenu, config?: CTXConfig): void;
/**
* The attach method is used to bind a context menu to any DOM Node and takes the following arguments:
* @param target A selector string to define the target node (eg `'body'`, or `'#someID'`)
* @param ctxMenu An array of objects defining the menu layout.
* @param beforeRender An optional callback function that is called before the context menu is opened.
* It is passed two arguments:
* `menu` - the menu definition,
* `event` - the MouseEvent.
* `beforeRender` needs to return a new menu definition which will be used.
*
* @deprecated as of version 1.7.
* Method Signature changed. Third parameter should be a config option now.
* You can pass the beforeRender callback like this: `attach("#target", [...], { onBeforeShow: beforeRender })`
* Calling this signature won't work in a future update
*/
attach(target: string, ctxMenu: CTXMenu, beforeRender?: BeforeRenderFN): void;
/**
* The update method is used to update an existing context menu.
* You can update each the menu definition or beforeRender function only by passing undefined for the other argument.
Expand All @@ -112,25 +96,6 @@ interface CTXMenuSingleton {
* @param config A config object, See `CTXConfig`. Only defined members will be updated.
*/
update(target: string, ctxMenu?: CTXMenu, config?: CTXConfig): void;
/**
* The update method is used to update an existing context menu.
* You can update each the menu definition or beforeRender function only by passing undefined for the other argument.
* If you try to update a menu which does not exist, it will silently be attached instead.
* @param target A selector string to define the target node (eg `'body'`, or `'#someID'`)
* @param ctxMenu An array of objects defining the updated menu layout. _(can be undefined when only updating beforeRender)_
* @param beforeRender The updated callback function that is called before the context menu is opened.
* It is passed two arguments:
* `menu` - the menu definition,
* `event` - the MouseEvent.
* `beforeRender` needs to return a new menu definition which will be used.
*
* @deprecated as of version 1.7.
* Method Signature changed. Third parameter should be a config option now.
* You can pass the beforeRender callback like this: `update("#target", [...], { onBeforeShow: beforeRender })`
* Calling this signature won't work in a future update
*/
update(target: string, ctxMenu?: CTXMenu, beforeRender?: BeforeRenderFN): void;
update(target: string, ctxMenu?: CTXMenu, config?: CTXConfig): void;
/**
* The delete method is used to delete a context menu
* @param target A selector string to define the target node (eg `'body'`, or `'#someID'`)
Expand All @@ -152,4 +117,4 @@ interface CTXMenuSingleton {

declare const ctxmenu: CTXMenuSingleton;

export { BeforeRenderFN, CTXConfig, CTXMAction, CTXMAnchor, CTXMDivider, CTXMHeading, CTXMInteractive, CTXMItem, CTXMItemEventListener, CTXMItemEventRegistry, CTXMSubMenu, CTXMenu, CTXMenuSingleton, ValueOrFunction, ctxmenu };
export { CTXConfig, CTXMAction, CTXMAnchor, CTXMDivider, CTXMHeading, CTXMInteractive, CTXMItem, CTXMItemEventListener, CTXMItemEventRegistry, CTXMSubMenu, CTXMenu, CTXMenuSingleton, ValueOrFunction, ctxmenu };
19 changes: 11 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function generateMenuItem(item) {
return item.icon;
}, "icon", true ], [ itemIsHeading, "heading", false ], [ itemIsSubMenu, "submenu", true ], [ isDisabled, "disabled", false ], [ itemIsInteractive, "interactive", true ] ], item, li);
if (itemIsDivider(item)) return li;
Object.entries((_a = getProp(item.attributes)) !== null && _a !== void 0 ? _a : {}).forEach((function(_a) {
var attr = _a[0], val = _a[1];
return li.setAttribute(attr, val);
}));
[ makeInnerHTML, makeAttributes, makeIcon, addEventHandlers, makeAnchor ].forEach((function(step) {
return step.call(null, item, li);
}));
Expand Down Expand Up @@ -293,9 +297,6 @@ var styles = 'html{min-height:100%}.ctxmenu{position:fixed;border:1px solid #999
ContextMenu.prototype.attach = function(target, ctxMenu, _config) {
var _this = this;
if (_config === void 0) _config = {};
if (typeof _config === "function") return this.attach(target, ctxMenu, {
onBeforeShow: _config
});
var config = this.getConfig(_config);
var t = document.querySelector(target);
if (this.cache[target] !== void 0) {
Expand All @@ -318,9 +319,6 @@ var styles = 'html{min-height:100%}.ctxmenu{position:fixed;border:1px solid #999
};
ContextMenu.prototype.update = function(target, ctxMenu, _config) {
if (_config === void 0) _config = {};
if (typeof _config === "function") return this.update(target, ctxMenu, {
onBeforeShow: _config
});
var o = this.cache[target];
var config = Object.assign({}, o === null || o === void 0 ? void 0 : o.config, _config);
var t = document.querySelector(target);
Expand Down Expand Up @@ -348,7 +346,7 @@ var styles = 'html{min-height:100%}.ctxmenu{position:fixed;border:1px solid #999
this.onHide = config.onHide;
this.onBeforeHide = config.onBeforeHide;
var newMenu = (_b = (_a = config.onBeforeShow) === null || _a === void 0 ? void 0 : _a.call(config, ctxMenu.slice(), eventOrElement instanceof MouseEvent ? eventOrElement : void 0)) !== null && _b !== void 0 ? _b : ctxMenu;
this.menu = this.generateDOM(newMenu, eventOrElement);
this.menu = this.generateDOM(newMenu, eventOrElement, config.attributes);
document.body.appendChild(this.menu);
(_c = config.onShow) === null || _c === void 0 ? void 0 : _c.call(config, this.menu);
this.menu.addEventListener("wheel", (function() {
Expand Down Expand Up @@ -382,6 +380,7 @@ var styles = 'html{min-height:100%}.ctxmenu{position:fixed;border:1px solid #999
};
ContextMenu.prototype.generateDOM = function(ctxMenu, parentOrEvent, attributes) {
var _this = this;
if (attributes === void 0) attributes = {};
var container = generateMenu(ctxMenu);
setPosition(container, parentOrEvent);
ctxMenu.forEach((function(item, i) {
Expand All @@ -395,9 +394,13 @@ var styles = 'html{min-height:100%}.ctxmenu{position:fixed;border:1px solid #999
if (!itemIsSubMenu(item)) return;
onHoverDebounced(li, (function() {
if (li.querySelector("ul")) return;
li.appendChild(_this.generateDOM(getProp(item.subMenu), li));
li.appendChild(_this.generateDOM(getProp(item.subMenu), li, getProp(item.subMenuAttributes)));
}));
}));
Object.entries(attributes).forEach((function(_a) {
var attr = _a[0], val = _a[1];
return container.setAttribute(attr, val);
}));
return container;
};
ContextMenu.addStylesToDom = function() {
Expand Down
4 changes: 2 additions & 2 deletions index.min.js

Large diffs are not rendered by default.

14 changes: 3 additions & 11 deletions src/ctxmenu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*! ctxMenu v1.7.0 | (c) Nikolaj Kappler | https://github.com/nkappler/ctxmenu/blob/master/LICENSE !*/

import { generateMenu, onHoverDebounced } from "./elementFactory";
import type { BeforeRenderFN, CTXConfig, CTXMenu, CTXMenuSingleton } from "./interfaces";
import type { CTXConfig, CTXMenu, CTXMenuSingleton } from "./interfaces";
import { resetDirections, setPosition } from "./position";
//@ts-ignore file will only be present after first run of npm run build
import { styles } from "./styles";
Expand Down Expand Up @@ -65,11 +65,7 @@ class ContextMenu implements CTXMenuSingleton {
};
}

/** @deprecated */
public attach(target: string, ctxMenu: CTXMenu, beforeRender?: BeforeRenderFN): void;
public attach(target: string, ctxMenu: CTXMenu, config?: CTXConfig): void;
public attach(target: string, ctxMenu: CTXMenu, _config: CTXConfig | BeforeRenderFN = {}): void {
if (typeof _config === "function") { return this.attach(target, ctxMenu, { onBeforeShow: _config }) }
public attach(target: string, ctxMenu: CTXMenu, _config: CTXConfig = {}): void {
const config = this.getConfig(_config);
const t = document.querySelector<HTMLElement>(target);
if (this.cache[target] !== undefined) {
Expand All @@ -92,11 +88,7 @@ class ContextMenu implements CTXMenuSingleton {
t.addEventListener("contextmenu", handler);
}

/** @deprecated */
public update(target: string, ctxMenu?: CTXMenu, beforeRender?: BeforeRenderFN): void;
public update(target: string, ctxMenu?: CTXMenu, config?: CTXConfig): void;
public update(target: string, ctxMenu?: CTXMenu, _config: CTXConfig | BeforeRenderFN = {}) {
if (typeof _config === "function") { return this.update(target, ctxMenu, { onBeforeShow: _config }); }
public update(target: string, ctxMenu?: CTXMenu, _config: CTXConfig = {}) {
const o = this.cache[target];
const config = Object.assign({}, o?.config, _config);
const t = document.querySelector<HTMLElement>(target);
Expand Down
52 changes: 8 additions & 44 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,15 @@ export type CTXMItem = CTXMAnchor | CTXMAction | CTXMHeading | CTXMDivider | CTX
*/
export type CTXMenu = CTXMItem[];

/**
* A function that is called before the context menu is opened.
* It is passed the menu definition and the MouseEvent.
* Can be used to manipulate the menu based on the Event. (e.g. Cursor Position)
* Needs to return a menu definition.
*/
export type BeforeRenderFN = (menu: CTXMenu, e?: MouseEvent) => CTXMenu;

export interface CTXConfig {
onBeforeShow?: BeforeRenderFN;
/**
* Callback that is called before the context menu is opened.
* Can be used to manipulate the menu based on the Event. (e.g. to appear at the Cursor Position)
* @param menu - the original menu definition
* @param event - mouse event, when openend from context menu event
* @returns Needs to return a menu definition.
*/
onBeforeShow?: (menu: CTXMenu, event?: MouseEvent) => CTXMenu;
onShow?: Function;
onBeforeHide?: Function;
onHide?: Function;
Expand All @@ -101,22 +100,6 @@ export interface CTXMenuSingleton {
* @param config A config object, See `CTXConfig`.
*/
attach(target: string, ctxMenu: CTXMenu, config?: CTXConfig): void;
/**
* The attach method is used to bind a context menu to any DOM Node and takes the following arguments:
* @param target A selector string to define the target node (eg `'body'`, or `'#someID'`)
* @param ctxMenu An array of objects defining the menu layout.
* @param beforeRender An optional callback function that is called before the context menu is opened.
* It is passed two arguments:
* `menu` - the menu definition,
* `event` - the MouseEvent.
* `beforeRender` needs to return a new menu definition which will be used.
*
* @deprecated as of version 1.7.
* Method Signature changed. Third parameter should be a config option now.
* You can pass the beforeRender callback like this: `attach("#target", [...], { onBeforeShow: beforeRender })`
* Calling this signature won't work in a future update
*/
attach(target: string, ctxMenu: CTXMenu, beforeRender?: BeforeRenderFN): void;
/**
* The update method is used to update an existing context menu.
* You can update each the menu definition or beforeRender function only by passing undefined for the other argument.
Expand All @@ -126,25 +109,6 @@ export interface CTXMenuSingleton {
* @param config A config object, See `CTXConfig`. Only defined members will be updated.
*/
update(target: string, ctxMenu?: CTXMenu, config?: CTXConfig): void;
/**
* The update method is used to update an existing context menu.
* You can update each the menu definition or beforeRender function only by passing undefined for the other argument.
* If you try to update a menu which does not exist, it will silently be attached instead.
* @param target A selector string to define the target node (eg `'body'`, or `'#someID'`)
* @param ctxMenu An array of objects defining the updated menu layout. _(can be undefined when only updating beforeRender)_
* @param beforeRender The updated callback function that is called before the context menu is opened.
* It is passed two arguments:
* `menu` - the menu definition,
* `event` - the MouseEvent.
* `beforeRender` needs to return a new menu definition which will be used.
*
* @deprecated as of version 1.7.
* Method Signature changed. Third parameter should be a config option now.
* You can pass the beforeRender callback like this: `update("#target", [...], { onBeforeShow: beforeRender })`
* Calling this signature won't work in a future update
*/
update(target: string, ctxMenu?: CTXMenu, beforeRender?: BeforeRenderFN): void;
update(target: string, ctxMenu?: CTXMenu, config?: CTXConfig): void;
/**
* The delete method is used to delete a context menu
* @param target A selector string to define the target node (eg `'body'`, or `'#someID'`)
Expand Down
53 changes: 9 additions & 44 deletions standalone/ctxmenu.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ type CTXMItem = CTXMAnchor | CTXMAction | CTXMHeading | CTXMDivider | CTXMSubMen
* This is a Menu Definition. In fact, it's just an array of Context Menu Items
*/
type CTXMenu = CTXMItem[];
/**
* A function that is called before the context menu is opened.
* It is passed the menu definition and the MouseEvent.
* Can be used to manipulate the menu based on the Event. (e.g. Cursor Position)
* Needs to return a menu definition.
*/
type BeforeRenderFN = (menu: CTXMenu, e?: MouseEvent) => CTXMenu;
interface CTXConfig {
onBeforeShow?: BeforeRenderFN;
/**
* Callback that is called before the context menu is opened.
* Can be used to manipulate the menu based on the Event. (e.g. to appear at the Cursor Position)
* @param menu - the original menu definition
* @param event - mouse event, when openend from context menu event
* @returns Needs to return a menu definition.
*/
onBeforeShow?: (menu: CTXMenu, event?: MouseEvent) => CTXMenu;
onShow?: Function;
onBeforeHide?: Function;
onHide?: Function;
Expand All @@ -87,22 +87,6 @@ interface CTXMenuSingleton {
* @param config A config object, See `CTXConfig`.
*/
attach(target: string, ctxMenu: CTXMenu, config?: CTXConfig): void;
/**
* The attach method is used to bind a context menu to any DOM Node and takes the following arguments:
* @param target A selector string to define the target node (eg `'body'`, or `'#someID'`)
* @param ctxMenu An array of objects defining the menu layout.
* @param beforeRender An optional callback function that is called before the context menu is opened.
* It is passed two arguments:
* `menu` - the menu definition,
* `event` - the MouseEvent.
* `beforeRender` needs to return a new menu definition which will be used.
*
* @deprecated as of version 1.7.
* Method Signature changed. Third parameter should be a config option now.
* You can pass the beforeRender callback like this: `attach("#target", [...], { onBeforeShow: beforeRender })`
* Calling this signature won't work in a future update
*/
attach(target: string, ctxMenu: CTXMenu, beforeRender?: BeforeRenderFN): void;
/**
* The update method is used to update an existing context menu.
* You can update each the menu definition or beforeRender function only by passing undefined for the other argument.
Expand All @@ -112,25 +96,6 @@ interface CTXMenuSingleton {
* @param config A config object, See `CTXConfig`. Only defined members will be updated.
*/
update(target: string, ctxMenu?: CTXMenu, config?: CTXConfig): void;
/**
* The update method is used to update an existing context menu.
* You can update each the menu definition or beforeRender function only by passing undefined for the other argument.
* If you try to update a menu which does not exist, it will silently be attached instead.
* @param target A selector string to define the target node (eg `'body'`, or `'#someID'`)
* @param ctxMenu An array of objects defining the updated menu layout. _(can be undefined when only updating beforeRender)_
* @param beforeRender The updated callback function that is called before the context menu is opened.
* It is passed two arguments:
* `menu` - the menu definition,
* `event` - the MouseEvent.
* `beforeRender` needs to return a new menu definition which will be used.
*
* @deprecated as of version 1.7.
* Method Signature changed. Third parameter should be a config option now.
* You can pass the beforeRender callback like this: `update("#target", [...], { onBeforeShow: beforeRender })`
* Calling this signature won't work in a future update
*/
update(target: string, ctxMenu?: CTXMenu, beforeRender?: BeforeRenderFN): void;
update(target: string, ctxMenu?: CTXMenu, config?: CTXConfig): void;
/**
* The delete method is used to delete a context menu
* @param target A selector string to define the target node (eg `'body'`, or `'#someID'`)
Expand All @@ -154,4 +119,4 @@ declare global {
}
}

export { BeforeRenderFN, CTXConfig, CTXMAction, CTXMAnchor, CTXMDivider, CTXMHeading, CTXMInteractive, CTXMItem, CTXMItemEventListener, CTXMItemEventRegistry, CTXMSubMenu, CTXMenu, CTXMenuSingleton, ValueOrFunction };
export { CTXConfig, CTXMAction, CTXMAnchor, CTXMDivider, CTXMHeading, CTXMInteractive, CTXMItem, CTXMItemEventListener, CTXMItemEventRegistry, CTXMSubMenu, CTXMenu, CTXMenuSingleton, ValueOrFunction };
Loading

0 comments on commit 1c8e789

Please sign in to comment.