[BUGFIX] Make bookmarks to single records working
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / Toolbar / 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 * shortcut menu logic to add new shortcut, remove a shortcut
16 * and edit a shortcut
17 */
18 define('TYPO3/CMS/Backend/Toolbar/ShortcutMenu', ['jquery'], function($) {
19
20 var ShortcutMenu = {
21 $spinnerElement: $('<span>', {
22 class: 't3-icon fa fa-circle-o-notch fa-spin'
23 }),
24 options: {
25 containerSelector: '#typo3-cms-backend-backend-toolbaritems-shortcuttoolbaritem',
26 toolbarIconSelector: '.dropdown-toggle span.t3-icon',
27 toolbarMenuSelector: '.dropdown-menu',
28 shortcutItemSelector: '.dropdown-menu .shortcut',
29 shortcutDeleteSelector: '.shortcut-delete',
30 shortcutEditSelector: '.shortcut-edit',
31 shortcutFormTitleSelector: 'input[name="shortcut-title"]',
32 shortcutFormGroupSelector: 'select[name="shortcut-group"]',
33 shortcutFormSaveSelector: '.shortcut-form-save',
34 shortcutFormCancelSelector: '.shortcut-form-cancel'
35 }
36 };
37
38 /**
39 * build the in-place-editor for a shortcut
40 */
41 ShortcutMenu.editShortcut = function($shortcutRecord) {
42 // load the form
43 $.ajax({
44 url: TYPO3.settings.ajaxUrls['ShortcutMenu::getShortcutEditForm'],
45 data: {
46 shortcutId: $shortcutRecord.data('shortcutid'),
47 shortcutGroup: $shortcutRecord.data('shortcutgroup')
48 },
49 cache: false
50 }).done(function(data) {
51 $shortcutRecord.html(data);
52 });
53 };
54
55 /**
56 * save the data from the in-place-editor for a shortcut
57 */
58 ShortcutMenu.saveShortcutForm = function($shortcutRecord) {
59 $.ajax({
60 url: TYPO3.settings.ajaxUrls['ShortcutMenu::saveShortcut'],
61 data: {
62 shortcutId: $shortcutRecord.data('shortcutid'),
63 shortcutTitle: $shortcutRecord.find(ShortcutMenu.options.shortcutFormTitleSelector).val(),
64 shortcutGroup: $shortcutRecord.find(ShortcutMenu.options.shortcutFormGroupSelector).val()
65 },
66 type: 'post',
67 cache: false
68 }).done(function(data) {
69 // @todo: we can evaluate here, but what to do? a message?
70 ShortcutMenu.refreshMenu();
71 });
72 };
73
74 /**
75 * removes an existing short by sending an AJAX call
76 */
77 ShortcutMenu.deleteShortcut = function($shortcutRecord) {
78 // @todo: translations
79 top.TYPO3.Modal.confirm('Delete bookmark', 'Do you really want to remove this bookmark?')
80 .on('confirm.button.ok', function() {
81 $.ajax({
82 url: TYPO3.settings.ajaxUrls['ShortcutMenu::delete'],
83 data: {
84 shortcutId: $shortcutRecord.data('shortcutid')
85 },
86 type: 'post',
87 cache: false
88 }).done(function() {
89 // a reload is used in order to restore the original behaviour
90 // e.g. remove groups that are now empty because the last one in the group
91 // was removed
92 ShortcutMenu.refreshMenu();
93 });
94 $(this).trigger('modal-dismiss');
95 })
96 .on('confirm.button.cancel', function() {
97 $(this).trigger('modal-dismiss');
98 });
99 };
100
101 /**
102 * makes a call to the backend class to create a new shortcut,
103 * when finished it reloads the menu
104 */
105 ShortcutMenu.createShortcut = function(moduleName, url, confirmationText, motherModule) {
106 var shouldCreateShortcut = true;
107 if (typeof confirmationText !== 'undefined') {
108 shouldCreateShortcut = window.confirm(confirmationText);
109 }
110
111 if (shouldCreateShortcut) {
112 var $toolbarItemIcon = $(ShortcutMenu.options.toolbarIconSelector, ShortcutMenu.options.containerSelector);
113 var $spinner = ShortcutMenu.$spinnerElement.clone();
114 var $existingItem = $toolbarItemIcon.replaceWith($spinner);
115
116 $.ajax({
117 url: TYPO3.settings.ajaxUrls['ShortcutMenu::create'],
118 type: 'post',
119 data: {
120 module: moduleName,
121 url: url,
122 motherModName: motherModule
123 },
124 cache: false
125 }).done(function() {
126 ShortcutMenu.refreshMenu();
127 $spinner.replaceWith($existingItem);
128 });
129 }
130 };
131
132 /**
133 * reloads the menu after an update
134 */
135 ShortcutMenu.refreshMenu = function() {
136 $.ajax({
137 url: TYPO3.settings.ajaxUrls['ShortcutMenu::render'],
138 type: 'get',
139 cache: false
140 }).done(function(data) {
141 $(ShortcutMenu.options.toolbarMenuSelector, ShortcutMenu.options.containerSelector).html(data);
142 });
143 };
144
145 /**
146 * Registers listeners
147 */
148 ShortcutMenu.initializeEvents = function() {
149 $(ShortcutMenu.options.containerSelector).on('click', ShortcutMenu.options.shortcutDeleteSelector, function(evt) {
150 evt.preventDefault();
151 evt.stopImmediatePropagation();
152 ShortcutMenu.deleteShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
153 }).on('click', ShortcutMenu.options.shortcutEditSelector, function(evt) {
154 evt.preventDefault();
155 evt.stopImmediatePropagation();
156 ShortcutMenu.editShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
157 }).on('click', ShortcutMenu.options.shortcutFormSaveSelector, function(evt) {
158 ShortcutMenu.saveShortcutForm($(this).closest(ShortcutMenu.options.shortcutItemSelector));
159 }).on('click', ShortcutMenu.options.shortcutFormCancelSelector, function() {
160 // re-render the menu on canceling the update of a shortcut
161 ShortcutMenu.refreshMenu();
162 });
163 };
164
165 /**
166 * initialize and return the ShortcutMenu object
167 */
168 return function() {
169 $(document).ready(function() {
170 ShortcutMenu.initializeEvents();
171 });
172
173 TYPO3.ShortcutMenu = ShortcutMenu;
174 return ShortcutMenu;
175 }();
176 });