forked from iVis-at-Bilkent/cytoscape.js-context-menus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cytoscape-context-menus.js
493 lines (409 loc) · 15.7 KB
/
cytoscape-context-menus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
;(function(){ 'use strict';
var $ = typeof jQuery === typeof undefined ? null : jQuery;
var register = function( cytoscape, $ ){
if( !cytoscape ){ return; } // can't register if cytoscape unspecified
var defaults = {
// List of initial menu items
menuItems: [
/*
{
id: 'remove',
content: 'remove',
tooltipText: 'remove',
selector: 'node, edge',
onClickFunction: function () {
console.log('remove element');
},
hasTrailingDivider: true
},
{
id: 'hide',
content: 'hide',
tooltipText: 'remove',
selector: 'node, edge',
onClickFunction: function () {
console.log('hide element');
},
disabled: true
}*/
],
// css classes that menu items will have
menuItemClasses: [
// add class names to this list
],
// css classes that context menu will have
contextMenuClasses: [
// add class names to this list
]
};
var eventCyTapStart; // The event to be binded on tap start
// To initialize with options.
cytoscape('core', 'contextMenus', function (opts) {
var cy = this;
// Initilize scratch pad
if (!cy.scratch('cycontextmenus')) {
cy.scratch('cycontextmenus', {});
}
var options = getScratchProp('options');
var $cxtMenu = getScratchProp('cxtMenu');
var menuItemCSSClass = 'cy-context-menus-cxt-menuitem';
var dividerCSSClass = 'cy-context-menus-divider';
// Merge default options with the ones coming from parameter
function extend(defaults, options) {
var obj = {};
for (var i in defaults) {
obj[i] = defaults[i];
}
for (var i in options) {
obj[i] = options[i];
}
return obj;
};
function getScratchProp(propname) {
return cy.scratch('cycontextmenus')[propname];
};
function setScratchProp(propname, value) {
cy.scratch('cycontextmenus')[propname] = value;
};
function preventDefaultContextTap() {
$(".cy-context-menus-cxt-menu").contextmenu( function() {
return false;
});
}
// Get string representation of css classes
function getMenuItemClassStr(classes, itemClases, hasTrailingDivider) {
var str = classes.join(' ');
if (itemClases != null) {
str += itemClases.join(' ');
}
str += ' ' + menuItemCSSClass;
if(hasTrailingDivider) {
str += ' ' + dividerCSSClass;
}
return str;
}
function displayComponent($component) {
$component.css('display', 'block');
}
function hideComponent($component) {
$component.css('display', 'none');
}
function hideMenuItemComponents() {
$cxtMenu.children().css('display', 'none');
}
function bindOnClickFunction($component, onClickFcn) {
var callOnClickFcn;
$component.on('click', callOnClickFcn = function() {
onClickFcn(getScratchProp('currentCyEvent'));
});
$component.data('call-on-click-function', callOnClickFcn);
}
function bindCyCxttap($component, selector, coreAsWell) {
function _cxtfcn(event) {
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')) {
displayComponent($cxtMenu);
}
// anyVisibleChild indicates if there is any visible child of context menu if not do not show the context menu
setScratchProp('anyVisibleChild', true);// there is visible child
displayComponent($component); // display the component
}
// If there is no visible element hide the context menu as well(If it is visible)
if (!getScratchProp('anyVisibleChild') && $cxtMenu.is(':visible')) {
hideComponent($cxtMenu);
}
}
var cxtfcn;
var cxtCoreFcn;
if(coreAsWell) {
cy.on('cxttap', cxtCoreFcn = function(event) {
var target = event.target || event.cyTarget;
if( target != cy ) {
return;
}
_cxtfcn(event);
});
}
if(selector) {
cy.on('cxttap', selector, cxtfcn = function(event) {
_cxtfcn(event);
});
}
// Bind the event to menu item to be able to remove it back
$component.data('cy-context-menus-cxtfcn', cxtfcn);
$component.data('cy-context-menus-cxtcorefcn', cxtCoreFcn);
}
function bindCyEvents() {
cy.on('tapstart', eventCyTapStart = function(){
hideComponent($cxtMenu);
setScratchProp('cxtMenuPosition', undefined);
setScratchProp('currentCyEvent', undefined);
});
}
function performBindings($component, onClickFcn, selector, coreAsWell) {
bindOnClickFunction($component, onClickFcn);
bindCyCxttap($component, selector, coreAsWell);
}
// Adjusts context menu if necessary
function adjustCxtMenu(event) {
var currentCxtMenuPosition = getScratchProp('cxtMenuPosition');
var cyPos = event.position || event.cyPosition;
if( currentCxtMenuPosition != cyPos ) {
hideMenuItemComponents();
setScratchProp('anyVisibleChild', false);// we hide all children there is no visible child remaining
setScratchProp('cxtMenuPosition', cyPos);
var containerPos = $(cy.container()).offset();
var renderedPos = event.renderedPosition || event.cyRenderedPosition;
var left = containerPos.left + renderedPos.x;
var top = containerPos.top + renderedPos.y;
$cxtMenu.css('left', left);
$cxtMenu.css('top', top);
}
}
function createAndAppendMenuItemComponents(menuItems) {
for (var i = 0; i < menuItems.length; i++) {
createAndAppendMenuItemComponent(menuItems[i]);
}
}
function createAndAppendMenuItemComponent(menuItem) {
// Create and append menu item
var $menuItemComponent = createMenuItemComponent(menuItem);
appendComponentToCxtMenu($menuItemComponent);
performBindings($menuItemComponent, menuItem.onClickFunction, 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);
}
// create cxtMenu and append it to body
function createAndAppendCxtMenuComponent() {
var classes = options.contextMenuClasses.join(' ');
// classes += ' cy-context-menus-cxt-menu';
$cxtMenu = $('<div class=' + classes + '></div>');
$cxtMenu.addClass('cy-context-menus-cxt-menu');
setScratchProp('cxtMenu', $cxtMenu);
$('body').append($cxtMenu);
return $cxtMenu;
}
// Creates a menu item as an html component
function createMenuItemComponent(item) {
var classStr = getMenuItemClassStr(options.menuItemClasses, item.menuItemClasses, item.hasTrailingDivider);
var itemStr = '<button id="' + item.id + '" class="' + classStr + '"';
if(item.tooltipText) {
itemStr += ' title="' + item.tooltipText + '"';
}
if(item.disabled) {
itemStr += ' disabled';
}
if (!item.image){
itemStr += '>' + item.content + '</button>';
}
else{
itemStr += '>' + '<img src="' + item.image.src + '" width="' + item.image.width + 'px"; height="'
+ item.image.height + 'px"; style="position:absolute; top: ' + item.image.y + 'px; left: '
+ item.image.x + 'px;">' + item.content + '</button>';
};
var $menuItemComponent = $(itemStr);
$menuItemComponent.data('selector', item.selector);
$menuItemComponent.data('on-click-function', item.onClickFunction);
$menuItemComponent.data('show', (typeof(item.show) === 'undefined' || item.show));
return $menuItemComponent;
}
// Appends the given component to cxtMenu
function appendComponentToCxtMenu(component) {
$cxtMenu.append(component);
bindMenuItemClickFunction(component);
}
// Insert the given component to cxtMenu just before the existing item with given ID
function insertComponentBeforeExistingItem(component, existingItemID) {
var $existingItem = $('#' + existingItemID);
component.insertBefore($existingItem);
}
function destroyCxtMenu() {
if(!getScratchProp('active')) {
return;
}
removeAndUnbindMenuItems();
cy.off('tapstart', eventCyTapStart);
$cxtMenu.remove();
$cxtMenu = undefined;
setScratchProp($cxtMenu, undefined);
setScratchProp('active', false);
setScratchProp('anyVisibleChild', false);
}
function removeAndUnbindMenuItems() {
var children = $cxtMenu.children();
$(children).each(function() {
removeAndUnbindMenuItem($(this));
});
}
function removeAndUnbindMenuItem(itemID) {
var $component = typeof itemID === 'string' ? $('#' + itemID) : itemID;
var cxtfcn = $component.data('cy-context-menus-cxtfcn');
var selector = $component.data('selector');
var callOnClickFcn = $component.data('call-on-click-function');
var cxtCoreFcn = $component.data('cy-context-menus-cxtcorefcn');
if(cxtfcn) {
cy.off('cxttap', selector, cxtfcn);
}
if(cxtCoreFcn) {
cy.off('cxttap', cxtCoreFcn);
}
if(callOnClickFcn) {
$component.off('click', callOnClickFcn);
}
$component.remove();
}
function moveBeforeOtherMenuItemComponent(componentID, existingComponentID) {
if( componentID === existingComponentID ) {
return;
}
var $component = $('#' + componentID).detach();
var $existingComponent = $('#' + existingComponentID);
$component.insertBefore($existingComponent);
}
function bindMenuItemClickFunction(component) {
component.click( function() {
hideComponent($cxtMenu);
setScratchProp('cxtMenuPosition', undefined);
});
}
function disableComponent(componentID) {
$('#' + componentID).attr('disabled', true);
}
function enableComponent(componentID) {
$('#' + componentID).attr('disabled', false);
}
function setTrailingDivider(componentID, status) {
var $component = $('#' + componentID);
if(status) {
$component.addClass(dividerCSSClass);
}
else {
$component.removeClass(dividerCSSClass);
}
}
// Get an extension instance to enable users to access extension methods
function getInstance(cy) {
var instance = {
// Returns whether the extension is active
isActive: function() {
return getScratchProp('active');
},
// Appends given menu item to the menu items list.
appendMenuItem: function(item) {
createAndAppendMenuItemComponent(item);
return cy;
},
// Appends menu items in the given list to the menu items list.
appendMenuItems: function(items) {
createAndAppendMenuItemComponents(items);
return cy;
},
// Removes the menu item with given ID.
removeMenuItem: function(itemID) {
removeAndUnbindMenuItem(itemID);
return cy;
},
// Sets whether the menuItem with given ID will have a following divider.
setTrailingDivider: function(itemID, status) {
setTrailingDivider(itemID, status);
return cy;
},
// Inserts given item before the existingitem.
insertBeforeMenuItem: function(item, existingItemID) {
createAndInsertMenuItemComponentBeforeExistingComponent(item, existingItemID);
return cy;
},
// Moves the item with given ID before the existingitem.
moveBeforeOtherMenuItem: function(itemID, existingItemID) {
moveBeforeOtherMenuItemComponent(itemID, existingItemID);
return cy;
},
// Disables the menu item with given ID.
disableMenuItem: function(itemID) {
disableComponent(itemID);
return cy;
},
// Enables the menu item with given ID.
enableMenuItem: function(itemID) {
enableComponent(itemID);
return cy;
},
// Disables the menu item with given ID.
hideMenuItem: function(itemID) {
$('#'+itemID).data('show', false);
hideComponent($('#'+itemID));
return cy;
},
// Enables the menu item with given ID.
showMenuItem: function(itemID) {
$('#'+itemID).data('show', true);
displayComponent($('#'+itemID));
return cy;
},
// Destroys the extension instance
destroy: function() {
destroyCxtMenu();
return cy;
},
// Get menu items
getMenuItems: function() {
return $('.cy-context-menus-cxt-menu');
},
// Hide menu items according to given class name
hideMenuItemsByGivenClass: function(className) {
menuItems = $('.'+className);
menuItems.each(function (index, menuItem) {
$(menuItem).data('show', false);
hideComponent($(menuItem));
});
return cy;
},
// Show menu items according to given class name
showMenuItemsByGivenClass: function(className) {
menuItems = $('.'+className);
menuItems.each(function (index, menuItem) {
$(menuItem).data('show', true);
displayComponent($(menuItem));
});
return cy;
}
};
return instance;
}
if ( opts !== 'get' ) {
// merge the options with default ones
options = extend(defaults, opts);
setScratchProp('options', options);
// Clear old context menu if needed
if(getScratchProp('active')) {
destroyCxtMenu();
}
setScratchProp('active', true);
$cxtMenu = createAndAppendCxtMenuComponent();
var menuItems = options.menuItems;
createAndAppendMenuItemComponents(menuItems);
bindCyEvents();
preventDefaultContextTap();
}
return getInstance(this);
});
};
if( typeof module !== 'undefined' && module.exports ){ // expose as a commonjs module
module.exports = register;
}
if( typeof define !== 'undefined' && define.amd ){ // expose as an amd/requirejs module
define('cytoscape-context-menus', function(){
return register;
});
}
if( typeof cytoscape !== 'undefined' && $ ){ // expose to global cytoscape (i.e. window.cytoscape)
register( cytoscape, $ );
}
})();