contextmenu.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*globals $, svgEditor*/
  2. /*jslint vars: true, eqeq: true*/
  3. /**
  4. * Package: svgedit.contextmenu
  5. *
  6. * Licensed under the Apache License, Version 2
  7. *
  8. * Author: Adam Bender
  9. */
  10. // Dependencies:
  11. // 1) jQuery (for dom injection of context menus)
  12. var svgedit = svgedit || {};
  13. (function() {
  14. var self = this;
  15. if (!svgedit.contextmenu) {
  16. svgedit.contextmenu = {};
  17. }
  18. self.contextMenuExtensions = {};
  19. var menuItemIsValid = function(menuItem) {
  20. return menuItem && menuItem.id && menuItem.label && menuItem.action && typeof menuItem.action == 'function';
  21. };
  22. var addContextMenuItem = function(menuItem) {
  23. // menuItem: {id, label, shortcut, action}
  24. if (!menuItemIsValid(menuItem)) {
  25. console.error("Menu items must be defined and have at least properties: id, label, action, where action must be a function");
  26. return;
  27. }
  28. if (menuItem.id in self.contextMenuExtensions) {
  29. console.error('Cannot add extension "' + menuItem.id + '", an extension by that name already exists"');
  30. return;
  31. }
  32. // Register menuItem action, see below for deferred menu dom injection
  33. console.log("Registed contextmenu item: {id:"+ menuItem.id+", label:"+menuItem.label+"}");
  34. self.contextMenuExtensions[menuItem.id] = menuItem;
  35. //TODO: Need to consider how to handle custom enable/disable behavior
  36. };
  37. var hasCustomHandler = function(handlerKey) {
  38. return self.contextMenuExtensions[handlerKey] && true;
  39. };
  40. var getCustomHandler = function(handlerKey) {
  41. return self.contextMenuExtensions[handlerKey].action;
  42. };
  43. var injectExtendedContextMenuItemIntoDom = function(menuItem) {
  44. if (Object.keys(self.contextMenuExtensions).length === 0) {
  45. // all menuItems appear at the bottom of the menu in their own container.
  46. // if this is the first extension menu we need to add the separator.
  47. $("#cmenu_canvas").append("<li class='separator'>");
  48. }
  49. var shortcut = menuItem.shortcut || "";
  50. $("#cmenu_canvas").append("<li class='disabled'><a href='#" + menuItem.id + "'>"
  51. + menuItem.label + "<span class='shortcut'>"
  52. + shortcut + "</span></a></li>");
  53. };
  54. // Defer injection to wait out initial menu processing. This probably goes away once all context
  55. // menu behavior is brought here.
  56. svgEditor.ready(function() {
  57. var menuItem;
  58. for (menuItem in contextMenuExtensions) {
  59. injectExtendedContextMenuItemIntoDom(contextMenuExtensions[menuItem]);
  60. }
  61. });
  62. svgedit.contextmenu.resetCustomMenus = function(){self.contextMenuExtensions = {};};
  63. svgedit.contextmenu.add = addContextMenuItem;
  64. svgedit.contextmenu.hasCustomHandler = hasCustomHandler;
  65. svgedit.contextmenu.getCustomHandler = getCustomHandler;
  66. }());