0a8d86ea3bb554095108d74178c1edc64f15d35f
[Packages/TYPO3.CMS.git] / typo3 / sysext / pagetree / components / pagetree / javascript / contextmenu.js
1 Ext.namespace('TYPO3.Components.PageTree');
2
3 TYPO3.Components.PageTree.ContextMenu = Ext.extend(Ext.menu.Menu, {
4 node: null,
5 pageTree: null,
6 id: 'typo3-pagetree-contextmenu',
7
8 listeners: {
9 itemclick: {
10 fn: function (item) {
11 this.pageTree.commandProvider[item.callbackAction](
12 item.parentMenu.node,
13 item.parentMenu.pageTree
14 );
15 }
16 }
17 },
18
19 /**
20 * Fill menu with menu items and returns the number of context menu items
21 *
22 * @param node
23 * @param pageTree
24 * @param contextMenuConfiguration
25 * @return int
26 */
27 fillWithMenuItems: function(node, pageTree, contextMenuConfiguration) {
28 this.node = node;
29 this.pageTree = pageTree;
30
31 var components = this.preProcessContextMenuConfiguration(contextMenuConfiguration);
32
33 if (components.length) {
34 for (var component in components) {
35 if (components[component] === '-') {
36 this.addSeparator();
37 } else {
38 if (typeof components[component] === 'object') {
39 this.addItem(components[component]);
40 }
41 }
42 }
43 }
44 return components.length;
45 },
46
47 // Private
48 // recursively parses the context menu actions array and generates the
49 // components for the context menu including separators/dividers and sub menus
50 preProcessContextMenuConfiguration: function(contextMenuConfiguration, level) {
51 level = level || 0;
52 if (level > 5) {
53 return [];
54 }
55
56 var components = [];
57 var index = 0;
58
59 var modulesInsideGroup = false;
60 var subMenus = 0;
61 for (var singleAction in contextMenuConfiguration) {
62 if (contextMenuConfiguration[singleAction]['type'] === 'submenu') {
63 var subMenuComponents = this.preProcessContextMenuConfiguration(
64 contextMenuConfiguration[singleAction]['childActions'],
65 level + 1
66 );
67
68 if (subMenuComponents.length) {
69 var subMenu = new TYPO3.Components.PageTree.ContextMenu({
70 id: this.id + '-sub' + ++subMenus,
71 items: subMenuComponents,
72 node: this.node,
73 pageTree: this.pageTree
74 });
75
76 components[index++] = {
77 text: contextMenuConfiguration[singleAction]['label'],
78 cls: 'contextMenu-subMenu',
79 menu: subMenu
80 };
81 }
82 } else if (contextMenuConfiguration[singleAction]['type'] === 'divider') {
83 if (modulesInsideGroup) {
84 components[index++] = '-';
85 modulesInsideGroup = false;
86 }
87 } else {
88 modulesInsideGroup = true;
89
90 if (typeof contextMenuConfiguration[singleAction] === 'object') {
91 var component = {
92 'text': contextMenuConfiguration[singleAction]['label'],
93 'icon': contextMenuConfiguration[singleAction]['icon'],
94 'iconCls': contextMenuConfiguration[singleAction]['class'],
95 'callbackAction': contextMenuConfiguration[singleAction]['callbackAction']
96 };
97
98 component.itemTpl = Ext.menu.Item.prototype.itemTpl = new Ext.XTemplate(
99 '<a id="{id}" class="{cls}" hidefocus="true" unselectable="on" href="{href}">',
100 '<span class="{hrefTarget}">',
101 '<img src="{icon}" class="x-menu-item-icon {iconCls}" unselectable="on" />',
102 '</span>',
103 '<span class="x-menu-item-text">{text}</span>',
104 '</a>'
105 );
106
107 components[index++] = component;
108 }
109 }
110 }
111
112 // remove divider if it's the last item of the context menu
113 if (components.last() === '-') {
114 components[components.length - 1] = '';
115 }
116
117 return components;
118 }
119 });
120
121 // XTYPE Registration
122 Ext.reg('TYPO3.Components.PageTree.ContextMenu', TYPO3.Components.PageTree.ContextMenu);