Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add onDisplay event #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions cytoscape-context-menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
content: 'remove',
tooltipText: 'remove',
selector: 'node, edge',
onDisplayFunction: function () {
console.log('remove element');
},
onClickFunction: function () {
console.log('remove element');
},
Expand Down Expand Up @@ -136,10 +139,25 @@
$component.data('call-on-click-function', callOnClickFcn);
}

function bindOnDisplayFunction($component, onDisplayFcn) {

$component.data('call-on-display-function', onDisplayFcn);
}

function bindCyCxttap($component, selector, coreAsWell) {

function _cxtfcn(event) {

var onDisplayFcn = $component.data('call-on-display-function') || null;

if( onDisplayFcn) {

onDisplayFcn(event, $component, selector);
}

setScratchProp('currentCyEvent', event);
adjustCxtMenu(event); // adjust the position of context menu

if ($component.data('show')) {
// Now we have a visible element display context menu if it is not visible
if (!$cxtMenu.is(':visible')) {
Expand Down Expand Up @@ -172,6 +190,7 @@

if(selector) {
cy.on('cxttap', selector, cxtfcn = function(event) {

_cxtfcn(event);
});
}
Expand All @@ -189,8 +208,12 @@
});
}

function performBindings($component, onClickFcn, selector, coreAsWell) {
function performBindings($component, onClickFcn, onDisplayFcn, selector, coreAsWell) {

bindOnClickFunction($component, onClickFcn);

bindOnDisplayFunction($component, onDisplayFcn);

bindCyCxttap($component, selector, coreAsWell);
}

Expand Down Expand Up @@ -226,15 +249,15 @@
var $menuItemComponent = createMenuItemComponent(menuItem);
appendComponentToCxtMenu($menuItemComponent);

performBindings($menuItemComponent, menuItem.onClickFunction, menuItem.selector, menuItem.coreAsWell);
performBindings($menuItemComponent, menuItem.onClickFunction, menuItem.onDisplayFunction, menuItem.selector, menuItem.coreAsWell);
}//insertComponentBeforeExistingItem(component, existingItemID)

function createAndInsertMenuItemComponentBeforeExistingComponent(menuItem, existingComponentID) {
// Create and insert menu item
var $menuItemComponent = createMenuItemComponent(menuItem);
insertComponentBeforeExistingItem($menuItemComponent, existingComponentID);

performBindings($menuItemComponent, menuItem.onClickFunction, menuItem.selector, menuItem.coreAsWell);
performBindings($menuItemComponent, menuItem.onClickFunction, menuItem.onDisplayFunction, menuItem.selector, menuItem.coreAsWell);
}

// create cxtMenu and append it to body
Expand All @@ -251,6 +274,7 @@

// Creates a menu item as an html component
function createMenuItemComponent(item) {

var classStr = getMenuItemClassStr(options.menuItemClasses, item.hasTrailingDivider);
var itemStr = '<button id="' + item.id + '" class="' + classStr + '"';

Expand All @@ -270,6 +294,8 @@
+ item.image.x + 'px;">' + item.content + '</button>';
};

console.log(itemStr);

var $menuItemComponent = $(itemStr);

$menuItemComponent.data('selector', item.selector);
Expand Down Expand Up @@ -319,6 +345,7 @@
var cxtfcn = $component.data('cy-context-menus-cxtfcn');
var selector = $component.data('selector');
var callOnClickFcn = $component.data('call-on-click-function');
var callOnDisplayFcn = $component.data('call-on-display-function');
var cxtCoreFcn = $component.data('cy-context-menus-cxtcorefcn');

if(cxtfcn) {
Expand All @@ -333,6 +360,10 @@
$component.off('click', callOnClickFcn);
}

if(callOnDisplayFcn) {
$component.off('show', callOnDisplayFcn);
}

$component.remove();
}

Expand Down