From c7635282f441e2fd1667716019766417966d8c0f Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Sat, 25 Feb 2012 02:13:18 +0000 Subject: [PATCH] Fix Issue 907: Allow custom menu options (Apply patch from adambender) git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2055 eee81c28-f429-11dd-99c0-75d572ba1ddd --- Makefile | 1 + editor/contextmenu.js | 67 ++++++++++++++++++++++++++++++ editor/svg-editor.html | 1 + editor/svg-editor.js | 6 ++- test/all_tests.html | 1 + test/contextmenu_test.html | 83 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 editor/contextmenu.js create mode 100644 test/contextmenu_test.html diff --git a/Makefile b/Makefile index 58c385ca..2a8948fd 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,7 @@ JS_FILES=\ path.js \ svgcanvas.js \ svg-editor.js \ + contextmenu.js \ locale/locale.js JS_INPUT_FILES=$(addprefix editor/, $(JS_FILES)) diff --git a/editor/contextmenu.js b/editor/contextmenu.js new file mode 100644 index 00000000..0d5dd343 --- /dev/null +++ b/editor/contextmenu.js @@ -0,0 +1,67 @@ +/** + * Package: svgedit.contextmenu + * + * Licensed under the Apache License, Version 2 + * + * Author: Adam Bender + */ +// Dependencies: +// 1) jQuery (for dom injection of context menus) +var svgedit = svgedit || {}; +(function() { + var self = this; + if (!svgedit.contextmenu) { + svgedit.contextmenu = {}; + } + self.contextMenuExtensions = {} + var addContextMenuItem = function(menuItem) { + // 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 + } + 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) { + // 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("
  • "); + } + var shortcut = menuItem.shortcut || ""; + $("#cmenu_canvas").append("
  • " + + menuItem.label + "" + + shortcut + "
  • "); + } + + var menuItemIsValid = function(menuItem) { + return menuItem && menuItem.id && menuItem.label && menuItem.action && typeof menuItem.action == 'function'; + } + + // Defer injection to wait out initial menu processing. This probably goes away once all context + // menu behavior is brought here. + svgEditor.ready(function() { + 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; +})(); diff --git a/editor/svg-editor.html b/editor/svg-editor.html index 067dd016..5cad70c7 100644 --- a/editor/svg-editor.html +++ b/editor/svg-editor.html @@ -39,6 +39,7 @@ + diff --git a/editor/svg-editor.js b/editor/svg-editor.js index 3e09597e..ba94114b 100644 --- a/editor/svg-editor.js +++ b/editor/svg-editor.js @@ -4302,7 +4302,11 @@ case 'move_back': moveToBottomSelected(); break; - + default: + if(svgedit.contextmenu && svgedit.contextmenu.hasCustomHandler(action)){ + svgedit.contextmenu.getCustomHandler(action).call(); + } + break; } if(svgCanvas.clipBoard.length) { diff --git a/test/all_tests.html b/test/all_tests.html index 0fb28bad..ec2df341 100644 --- a/test/all_tests.html +++ b/test/all_tests.html @@ -7,6 +7,7 @@

    All SVG-edit Tests

    This file frames all SVG-edit test pages. This should only include tests known to work. These tests are known to pass 100% in the following: Firefox 3.6, Chrome 7, IE9 Preview 6 (1.9.8006.6000), Opera 10.63. If a test is broken in this page, it is possible that YOU broke it. Please do not submit code that breaks any of these tests.

    + diff --git a/test/contextmenu_test.html b/test/contextmenu_test.html new file mode 100644 index 00000000..c32f4e8e --- /dev/null +++ b/test/contextmenu_test.html @@ -0,0 +1,83 @@ + + + + + + + + + + + +

    Unit Tests for contextmenu.js

    +

    +

    +
      +
    + + +