source: other-projects/nz-flag-design/trunk/design-2d/Original editor.method.ac/method-draw/lib/contextmenu.js@ 29468

Last change on this file since 29468 was 29468, checked in by sjs49, 9 years ago

Initial commit for editor.method.ac for flag design

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