24b25df7b1d55be237f21f77fc23c508f596e7f4
[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) {
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 },
123 cache: false
124 }).done(function() {
125 ShortcutMenu.refreshMenu();
126 $spinner.replaceWith($existingItem);
127 });
128 }
129 };
130
131 /**
132 * reloads the menu after an update
133 */
134 ShortcutMenu.refreshMenu = function() {
135 $.ajax({
136 url: TYPO3.settings.ajaxUrls['ShortcutMenu::render'],
137 type: 'get',
138 cache: false
139 }).done(function(data) {
140 $(ShortcutMenu.options.toolbarMenuSelector, ShortcutMenu.options.containerSelector).html(data);
141 });
142 };
143
144 /**
145 * Registers listeners
146 */
147 ShortcutMenu.initializeEvents = function() {
148 $(ShortcutMenu.options.containerSelector).on('click', ShortcutMenu.options.shortcutDeleteSelector, function(evt) {
149 evt.preventDefault();
150 evt.stopImmediatePropagation();
151 ShortcutMenu.deleteShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
152 }).on('click', ShortcutMenu.options.shortcutEditSelector, function(evt) {
153 evt.preventDefault();
154 evt.stopImmediatePropagation();
155 ShortcutMenu.editShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
156 }).on('click', ShortcutMenu.options.shortcutFormSaveSelector, function(evt) {
157 ShortcutMenu.saveShortcutForm($(this).closest(ShortcutMenu.options.shortcutItemSelector));
158 }).on('click', ShortcutMenu.options.shortcutFormCancelSelector, function() {
159 // re-render the menu on canceling the update of a shortcut
160 ShortcutMenu.refreshMenu();
161 });
162 };
163
164 /**
165 * initialize and return the ShortcutMenu object
166 */
167 return function() {
168 $(document).ready(function() {
169 ShortcutMenu.initializeEvents();
170 });
171
172 TYPO3.ShortcutMenu = ShortcutMenu;
173 return ShortcutMenu;
174 }();
175 });