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 |
|
---|
11 | var 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 | })();
|
---|