123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- var svgedit = svgedit || {};
- (function() {
- var self = this;
- if (!svgedit.contextmenu) {
- svgedit.contextmenu = {};
- }
- self.contextMenuExtensions = {};
- var menuItemIsValid = function(menuItem) {
- return menuItem && menuItem.id && menuItem.label && menuItem.action && typeof menuItem.action == 'function';
- };
- var addContextMenuItem = function(menuItem) {
-
- 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;
- }
-
- console.log("Registed contextmenu item: {id:"+ menuItem.id+", label:"+menuItem.label+"}");
- self.contextMenuExtensions[menuItem.id] = menuItem;
-
- };
- var hasCustomHandler = function(handlerKey) {
- return self.contextMenuExtensions[handlerKey] && true;
- };
- var getCustomHandler = function(handlerKey) {
- return self.contextMenuExtensions[handlerKey].action;
- };
- var injectExtendedContextMenuItemIntoDom = function(menuItem) {
- if (Object.keys(self.contextMenuExtensions).length === 0) {
-
-
- $("#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>");
- };
-
-
- svgEditor.ready(function() {
- var menuItem;
- for (menuItem in contextMenuExtensions) {
- injectExtendedContextMenuItemIntoDom(contextMenuExtensions[menuItem]);
- }
- });
- svgedit.contextmenu.resetCustomMenus = function(){self.contextMenuExtensions = {};};
- svgedit.contextmenu.add = addContextMenuItem;
- svgedit.contextmenu.hasCustomHandler = hasCustomHandler;
- svgedit.contextmenu.getCustomHandler = getCustomHandler;
- }());
|