dd14834794fa5667fd95ec8a14aefc02a0b0b0bd
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / extjs / components / pagetree / javascript / contextmenu.js
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13 Ext.namespace('TYPO3.Components.PageTree');
14
15 /**
16 * @class TYPO3.Components.PageTree.ContextMenu
17 *
18 * Context menu implementation
19 *
20 * @namespace TYPO3.Components.PageTree
21 * @extends Ext.menu.Menu
22 */
23 TYPO3.Components.PageTree.ContextMenu = Ext.extend(Ext.menu.Menu, {
24 /**
25 * Context menu node
26 *
27 * @cfg {Ext.tree.TreeNode}
28 */
29 node: null,
30
31 /**
32 * Page Tree
33 *
34 * @cfg {TYPO3.Components.PageTree.Tree}
35 */
36 pageTree: null,
37
38 /**
39 * Component Id
40 *
41 * @type {String}
42 */
43 id: 'typo3-pagetree-contextmenu',
44
45 /**
46 * Parent clicks should be ignored
47 *
48 * @type {Boolean}
49 */
50 ignoreParentClicks: true,
51
52 /**
53 * Listeners
54 *
55 * The itemclick event triggers the configured single click action
56 */
57 listeners: {
58 itemclick: {
59 fn: function (item) {
60 if (this.pageTree.commandProvider[item.callbackAction]) {
61 if (item.parentMenu.pageTree.stateHash) {
62 fsMod.recentIds['web'] = item.parentMenu.node.attributes.nodeData.id;
63 item.parentMenu.pageTree.stateHash['lastSelectedNode'] = item.parentMenu.node.id;
64 }
65
66 this.pageTree.commandProvider[item.callbackAction](
67 item.parentMenu.node,
68 item.parentMenu.pageTree,
69 item
70 );
71 }
72 }
73 }
74 },
75
76 /**
77 * Fills the menu with the actions
78 *
79 * @param {Ext.tree.TreeNode} node
80 * @param {TYPO3.Components.PageTree.Tree} pageTree
81 * @param {Object} contextMenuConfiguration
82 * @return {void}
83 */
84 fill: function(node, pageTree, contextMenuConfiguration) {
85 this.node = node;
86 this.pageTree = pageTree;
87
88 var components = this.preProcessContextMenuConfiguration(contextMenuConfiguration);
89 if (components.length) {
90 for (var component in components) {
91 if (components[component] === '-') {
92 this.addSeparator();
93 } else if (typeof components[component] === 'object') {
94 this.addItem(components[component]);
95 }
96 }
97 }
98 },
99
100 /**
101 * Parses the context menu actions array recursively and generates the
102 * components for the context menu including separators/dividers and sub menus
103 *
104 * @param {Object} contextMenuConfiguration
105 * @param {int} level
106 * @return {Object}
107 */
108 preProcessContextMenuConfiguration: function(contextMenuConfiguration, level) {
109 level = level || 0;
110 if (level > 5) {
111 return [];
112 }
113
114 var components = [];
115 var index = 0;
116
117 var modulesInsideGroup = false;
118 var subMenus = 0;
119 for (var singleAction in contextMenuConfiguration) {
120 if (contextMenuConfiguration[singleAction]['type'] === 'submenu') {
121 var subMenuComponents = this.preProcessContextMenuConfiguration(
122 contextMenuConfiguration[singleAction]['childActions'],
123 level + 1
124 );
125
126 if (subMenuComponents.length) {
127 var subMenu = new TYPO3.Components.PageTree.ContextMenu({
128 id: this.id + '-sub' + ++subMenus,
129 items: subMenuComponents,
130 node: this.node,
131 pageTree: this.pageTree
132 });
133
134 components[index++] = {
135 text: contextMenuConfiguration[singleAction]['label'],
136 cls: 'contextMenu-subMenu',
137 menu: subMenu,
138 icon: contextMenuConfiguration[singleAction]['icon']
139 };
140 }
141 } else if (contextMenuConfiguration[singleAction]['type'] === 'divider') {
142 if (modulesInsideGroup) {
143 components[index++] = '-';
144 modulesInsideGroup = false;
145 }
146 } else {
147 modulesInsideGroup = true;
148
149 if (typeof contextMenuConfiguration[singleAction] === 'object') {
150 var component = {
151 'text': contextMenuConfiguration[singleAction]['label'],
152 'icon': contextMenuConfiguration[singleAction]['icon'],
153 'callbackAction': contextMenuConfiguration[singleAction]['callbackAction'],
154 'customAttributes': contextMenuConfiguration[singleAction]['customAttributes']
155 };
156 component.itemTpl = Ext.menu.Item.prototype.itemTpl = new Ext.XTemplate(
157 '<a id="{id}" class="{cls}" hidefocus="true" unselectable="on" href="{href}">',
158 '<span class="x-menu-item-icon" unselectable="on">{icon}</span>',
159 '<span class="x-menu-item-text">{text}</span>',
160 '</a>'
161 );
162
163 components[index++] = component;
164 }
165 }
166 }
167
168 // remove divider if it's the last item of the context menu
169 if (components[components.length - 1] === '-') {
170 components[components.length - 1] = '';
171 }
172
173 return components;
174 }
175 });
176
177 // XTYPE Registration
178 Ext.reg('TYPO3.Components.PageTree.ContextMenu', TYPO3.Components.PageTree.ContextMenu);