[BUGFIX] Async broken dialog message for adding new shortcut entry
[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 if (typeof confirmationText !== 'undefined') {
107 // @todo: translations
108 top.TYPO3.Modal.confirm('Create bookmark', confirmationText)
109 .on('confirm.button.ok', function() {
110 var $toolbarItemIcon = $(ShortcutMenu.options.toolbarIconSelector, ShortcutMenu.options.containerSelector);
111 var $spinner = ShortcutMenu.$spinnerElement.clone();
112 var $existingItem = $toolbarItemIcon.replaceWith($spinner);
113
114 $.ajax({
115 url: TYPO3.settings.ajaxUrls['ShortcutMenu::create'],
116 type: 'post',
117 data: {
118 module: moduleName,
119 url: url,
120 motherModName: motherModule
121 },
122 cache: false
123 }).done(function() {
124 ShortcutMenu.refreshMenu();
125 $spinner.replaceWith($existingItem);
126 });
127 $(this).trigger('modal-dismiss');
128 })
129 .on('confirm.button.cancel', function() {
130 $(this).trigger('modal-dismiss');
131 });
132 }
133
134 };
135
136 /**
137 * reloads the menu after an update
138 */
139 ShortcutMenu.refreshMenu = function() {
140 $.ajax({
141 url: TYPO3.settings.ajaxUrls['ShortcutMenu::render'],
142 type: 'get',
143 cache: false
144 }).done(function(data) {
145 $(ShortcutMenu.options.toolbarMenuSelector, ShortcutMenu.options.containerSelector).html(data);
146 });
147 };
148
149 /**
150 * Registers listeners
151 */
152 ShortcutMenu.initializeEvents = function() {
153 $(ShortcutMenu.options.containerSelector).on('click', ShortcutMenu.options.shortcutDeleteSelector, function(evt) {
154 evt.preventDefault();
155 evt.stopImmediatePropagation();
156 ShortcutMenu.deleteShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
157 }).on('click', ShortcutMenu.options.shortcutEditSelector, function(evt) {
158 evt.preventDefault();
159 evt.stopImmediatePropagation();
160 ShortcutMenu.editShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
161 }).on('click', ShortcutMenu.options.shortcutFormSaveSelector, function(evt) {
162 ShortcutMenu.saveShortcutForm($(this).closest(ShortcutMenu.options.shortcutItemSelector));
163 }).on('click', ShortcutMenu.options.shortcutFormCancelSelector, function() {
164 // re-render the menu on canceling the update of a shortcut
165 ShortcutMenu.refreshMenu();
166 });
167 };
168
169 /**
170 * initialize and return the ShortcutMenu object
171 */
172 return function() {
173 $(document).ready(function() {
174 ShortcutMenu.initializeEvents();
175 });
176
177 TYPO3.ShortcutMenu = ShortcutMenu;
178 return ShortcutMenu;
179 }();
180 });