2018-05-16 08:32:44 +00:00
|
|
|
/* eslint-disable no-var */
|
2018-05-16 00:53:27 +00:00
|
|
|
/* globals $, svgEditor */
|
2012-02-25 02:13:18 +00:00
|
|
|
/**
|
|
|
|
* Package: svgedit.contextmenu
|
2018-05-16 00:53:27 +00:00
|
|
|
*
|
2012-02-25 02:13:18 +00:00
|
|
|
* Licensed under the Apache License, Version 2
|
2018-05-16 00:53:27 +00:00
|
|
|
*
|
2012-02-25 02:13:18 +00:00
|
|
|
* Author: Adam Bender
|
|
|
|
*/
|
|
|
|
// Dependencies:
|
|
|
|
// 1) jQuery (for dom injection of context menus)
|
2018-05-16 00:53:27 +00:00
|
|
|
var svgedit = svgedit || {}; // eslint-disable-line no-use-before-define
|
|
|
|
(function () {
|
|
|
|
var self = this;
|
|
|
|
if (!svgedit.contextmenu) {
|
2018-05-18 04:02:30 +00:00
|
|
|
svgedit.contextmenu = {};
|
2018-05-16 00:53:27 +00:00
|
|
|
}
|
|
|
|
self.contextMenuExtensions = {};
|
|
|
|
var menuItemIsValid = function (menuItem) {
|
2018-05-18 04:02:30 +00:00
|
|
|
return menuItem && menuItem.id && menuItem.label && menuItem.action && typeof menuItem.action === 'function';
|
2018-05-16 00:53:27 +00:00
|
|
|
};
|
|
|
|
var addContextMenuItem = function (menuItem) {
|
2018-05-18 04:02:30 +00:00
|
|
|
// menuItem: {id, label, shortcut, action}
|
|
|
|
if (!menuItemIsValid(menuItem)) {
|
|
|
|
console.error('Menu items must be defined and have at least properties: id, label, action, where action must be a function');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (menuItem.id in self.contextMenuExtensions) {
|
|
|
|
console.error('Cannot add extension "' + menuItem.id + '", an extension by that name already exists"');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Register menuItem action, see below for deferred menu dom injection
|
|
|
|
console.log('Registed contextmenu item: {id:' + menuItem.id + ', label:' + menuItem.label + '}');
|
|
|
|
self.contextMenuExtensions[menuItem.id] = menuItem;
|
|
|
|
// TODO: Need to consider how to handle custom enable/disable behavior
|
2018-05-16 00:53:27 +00:00
|
|
|
};
|
|
|
|
var hasCustomHandler = function (handlerKey) {
|
2018-05-18 04:02:30 +00:00
|
|
|
return self.contextMenuExtensions[handlerKey] && true;
|
2018-05-16 00:53:27 +00:00
|
|
|
};
|
|
|
|
var getCustomHandler = function (handlerKey) {
|
2018-05-18 04:02:30 +00:00
|
|
|
return self.contextMenuExtensions[handlerKey].action;
|
2018-05-16 00:53:27 +00:00
|
|
|
};
|
|
|
|
var injectExtendedContextMenuItemIntoDom = function (menuItem) {
|
2018-05-18 04:02:30 +00:00
|
|
|
if (Object.keys(self.contextMenuExtensions).length === 0) {
|
|
|
|
// all menuItems appear at the bottom of the menu in their own container.
|
|
|
|
// if this is the first extension menu we need to add the separator.
|
|
|
|
$('#cmenu_canvas').append("<li class='separator'>");
|
|
|
|
}
|
|
|
|
var shortcut = menuItem.shortcut || '';
|
|
|
|
$('#cmenu_canvas').append("<li class='disabled'><a href='#" + menuItem.id + "'>" +
|
|
|
|
menuItem.label + "<span class='shortcut'>" +
|
|
|
|
shortcut + '</span></a></li>');
|
2018-05-16 00:53:27 +00:00
|
|
|
};
|
|
|
|
// Defer injection to wait out initial menu processing. This probably goes away once all context
|
|
|
|
// menu behavior is brought here.
|
|
|
|
svgEditor.ready(function () {
|
2018-05-18 04:02:30 +00:00
|
|
|
var menuItem;
|
|
|
|
for (menuItem in self.contextMenuExtensions) {
|
|
|
|
injectExtendedContextMenuItemIntoDom(self.contextMenuExtensions[menuItem]);
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
});
|
|
|
|
svgedit.contextmenu.resetCustomMenus = function () { self.contextMenuExtensions = {}; };
|
|
|
|
svgedit.contextmenu.add = addContextMenuItem;
|
|
|
|
svgedit.contextmenu.hasCustomHandler = hasCustomHandler;
|
|
|
|
svgedit.contextmenu.getCustomHandler = getCustomHandler;
|
2014-01-31 02:23:40 +00:00
|
|
|
}());
|