ea089ce564ae783856da77cfb968c8a54b0b4896
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / shortcutmenu.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
14
15 /**
16 * class to handle the shortcut menu
17 */
18 var ShortcutMenu = Class.create({
19
20 /**
21 * registers for resize event listener and executes on DOM ready
22 */
23 initialize: function() {
24 Ext.onReady(function() {
25 this.toolbarItemIcon = $$('#shortcut-menu .dropdown-toggle span.t3-icon')[0];
26 this.initControls();
27 }, this);
28 },
29
30 /**
31 * initializes the controls to follow, edit, and delete shortcuts
32 *
33 */
34 initControls: function() {
35
36 $$('.shortcut-label a').each(function(element) {
37 var shortcutId = element.up('tr.shortcut').identify().slice(9);
38
39 // map InPlaceEditor to edit icons
40 var edit = new Ajax.InPlaceEditor('shortcut-label-' + shortcutId, TYPO3.settings.ajaxUrls['ShortcutMenu::saveShortcut'], {
41 externalControl : 'shortcut-edit-' + shortcutId,
42 externalControlOnly : true,
43 highlightcolor : 'transparent',
44 highlightendcolor : 'transparent',
45 onFormCustomization : this.addGroupSelect,
46 onComplete : this.reRenderMenu.bind(this),
47 callback : function(form, nameInputFieldValue) {
48 var params = form.serialize(true);
49 params.shortcutId = shortcutId;
50 return params;
51 },
52 textBetweenControls : ' ',
53 cancelControl : 'button',
54 clickToEditText : '',
55 htmlResponse : true
56 });
57
58 // follow/execute shortcuts
59 element.observe('click', function(event) {
60 this.toggleMenu();
61 }.bind(this));
62
63 }.bind(this));
64
65 // activate delete icon
66 $$('.shortcut-delete img').each(function(element) {
67 element.observe('click', function(event) {
68 if (confirm('Do you really want to remove this bookmark?')) {
69 var deleteControl = event.element();
70 var shortcutId = deleteControl.up('tr.shortcut').identify().slice(9);
71
72 var del = new Ajax.Request(TYPO3.settings.ajaxUrls['ShortcutMenu::delete'], {
73 parameters : '&shortcutId=' + shortcutId,
74 onComplete : this.reRenderMenu.bind(this)
75 });
76 }
77 }.bind(this));
78 }.bind(this));
79
80 },
81
82 /**
83 * toggles the visibility of the menu and places it under the toolbar icon
84 */
85 toggleMenu: function(event) {},
86
87 /**
88 * adds a select field for the groups
89 */
90 addGroupSelect: function(inPlaceEditor, inPlaceEditorForm) {
91 var selectField = $(document.createElement('select'));
92
93 // determine the shortcut id
94 var shortcutId = inPlaceEditorForm.identify().slice(9, -14);
95
96 // now determine the shortcut's group id
97 var shortcut = $('shortcut-' + shortcutId).up('tr.shortcut');
98 var firstInGroup = null;
99 var shortcutGroupId = 0;
100
101 if (shortcut.hasClassName('first-row')) {
102 firstInGroup = shortcut;
103 } else {
104 firstInGroup = shortcut.previous('.first-row');
105 }
106
107 if (undefined !== firstInGroup) {
108 shortcutGroupId = firstInGroup.previous().identify().slice(15);
109 }
110
111 selectField.name = 'shortcut-group';
112 selectField.id = 'shortcut-group-select-' + shortcutId;
113 selectField.size = 1;
114 selectField.setStyle({marginBottom: '5px'});
115
116 // create options
117 var option;
118 // first create an option for "no group"
119 option = document.createElement('option');
120 option.value = 0;
121 option.selected = (shortcutGroupId === 0 ? true : false);
122 option.appendChild(document.createTextNode('No Group'));
123 selectField.appendChild(option);
124
125 // get the groups
126 var getGroups = new Ajax.Request(TYPO3.settings.ajaxUrls['ShortcutMenu::getGroups'], {
127 method: 'get',
128 asynchronous: false, // needs to be synchronous to build the options before adding the selectfield
129 requestHeaders: {Accept: 'application/json'},
130 onSuccess: function(transport, json) {
131 var shortcutGroups = transport.responseText.evalJSON(true);
132
133 // explicitly make the object a Hash
134 shortcutGroups = $H(json.shortcutGroups);
135 shortcutGroups.each(function(group) {
136 option = document.createElement('option');
137 option.value = group.key
138 option.selected = (shortcutGroupId === group.key ? true : false);
139 option.appendChild(document.createTextNode(group.value));
140 selectField.appendChild(option);
141 });
142
143 }
144 });
145
146 inPlaceEditor._form.appendChild(document.createElement('br'));
147 inPlaceEditor._form.appendChild(selectField);
148 inPlaceEditor._form.appendChild(document.createElement('br'));
149 },
150
151 /**
152 * gets called when the update was succesfull, fetches the complete menu to
153 * honor changes in group assignments
154 */
155 reRenderMenu: function(transport, element, backPath) {
156 var container = $$('#shortcut-menu .dropdown-menu')[0];
157 if (!backPath) {
158 var backPath = '';
159 }
160
161 container.setStyle({
162 height: container.getHeight() + 'px'
163 });
164 container.update('LOADING');
165
166 var render = new Ajax.Updater(
167 container,
168 backPath + TYPO3.settings.ajaxUrls['ShortcutMenu::render'],
169 {
170 asynchronous : false
171 }
172 );
173
174 container.setStyle({
175 height: 'auto'
176 });
177
178 this.initControls();
179 },
180
181 /**
182 * makes a call to the backend class to create a new shortcut,
183 * when finished it reloads the menu
184 */
185 createShortcut: function(backPath, moduleName, url) {
186 var toolbarItemIcon = $$('#shortcut-menu .dropdown-toggle span.t3-icon')[0];
187
188 var parent = Element.up(toolbarItemIcon);
189 var spinner = new Element('span').addClassName('spinner');
190 var oldIcon = toolbarItemIcon.replace(spinner);
191
192 // synchrous call to wait for it to complete and call the render
193 // method with backpath _afterwards_
194 var call = new Ajax.Request(backPath + TYPO3.settings.ajaxUrls['ShortcutMenu::create'], {
195 parameters : 'module=' + moduleName + '&url=' + url,
196 asynchronous : false
197 });
198
199 this.reRenderMenu(null, null, backPath);
200 spinner.replace(oldIcon);
201 }
202
203 });
204
205 var TYPO3BackendShortcutMenu = new ShortcutMenu();