c718eeb2c9900e6781488f3ee6be23b4ca56f6de
[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: 'fa fa-circle-o-notch fa-spin'
23 }),
24 options: {
25 containerSelector: '#typo3-cms-backend-backend-toolbaritems-shortcuttoolbaritem',
26 toolbarIconSelector: '.dropdown-toggle .fa',
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['shortcut_editform'],
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['shortcut_saveform'],
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['shortcut_list'],
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, shortcutButton) {
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['shortcut_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 if (typeof shortcutButton === 'object') {
127 $(shortcutButton).addClass('active');
128 $(shortcutButton).attr('title', null);
129 $(shortcutButton).attr('onclick', null);
130 }
131 });
132 $(this).trigger('modal-dismiss');
133 })
134 .on('confirm.button.cancel', function() {
135 $(this).trigger('modal-dismiss');
136 });
137 }
138
139 };
140
141 /**
142 * reloads the menu after an update
143 */
144 ShortcutMenu.refreshMenu = function() {
145 $.ajax({
146 url: TYPO3.settings.ajaxUrls['shortcut_list'],
147 type: 'get',
148 cache: false
149 }).done(function(data) {
150 $(ShortcutMenu.options.toolbarMenuSelector, ShortcutMenu.options.containerSelector).html(data);
151 });
152 };
153
154 /**
155 * Registers listeners
156 */
157 ShortcutMenu.initializeEvents = function() {
158 $(ShortcutMenu.options.containerSelector).on('click', ShortcutMenu.options.shortcutDeleteSelector, function(evt) {
159 evt.preventDefault();
160 evt.stopImmediatePropagation();
161 ShortcutMenu.deleteShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
162 }).on('click', ShortcutMenu.options.shortcutEditSelector, function(evt) {
163 evt.preventDefault();
164 evt.stopImmediatePropagation();
165 ShortcutMenu.editShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
166 }).on('click', ShortcutMenu.options.shortcutFormSaveSelector, function(evt) {
167 ShortcutMenu.saveShortcutForm($(this).closest(ShortcutMenu.options.shortcutItemSelector));
168 }).on('click', ShortcutMenu.options.shortcutFormCancelSelector, function() {
169 // re-render the menu on canceling the update of a shortcut
170 ShortcutMenu.refreshMenu();
171 });
172 };
173
174 /**
175 * initialize and return the ShortcutMenu object
176 */
177 return function() {
178 $(document).ready(function() {
179 ShortcutMenu.initializeEvents();
180 });
181
182 TYPO3.ShortcutMenu = ShortcutMenu;
183 return ShortcutMenu;
184 }();
185 });