3eca32db27c92ad4b97855e7c3227e7c85873178
[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 * Module: TYPO3/CMS/Backend/Toolbar/ShortcutMenu
16 * shortcut menu logic to add new shortcut, remove a shortcut
17 * and edit a shortcut
18 */
19 define(['jquery',
20 'TYPO3/CMS/Backend/Modal',
21 'TYPO3/CMS/Backend/Icons',
22 'TYPO3/CMS/Backend/Notification',
23 'TYPO3/CMS/Backend/Viewport'
24 ], function($, Modal, Icons, Notification, Viewport) {
25 'use strict';
26
27 /**
28 *
29 * @type {{options: {containerSelector: string, toolbarIconSelector: string, toolbarMenuSelector: string, shortcutItemSelector: string, shortcutDeleteSelector: string, shortcutEditSelector: string, shortcutFormTitleSelector: string, shortcutFormGroupSelector: string, shortcutFormSaveSelector: string, shortcutFormCancelSelector: string}}}
30 * @exports TYPO3/CMS/Backend/Toolbar/ShortcutMenu
31 */
32 var ShortcutMenu = {
33 options: {
34 containerSelector: '#typo3-cms-backend-backend-toolbaritems-shortcuttoolbaritem',
35 toolbarIconSelector: '.dropdown-toggle span.icon',
36 toolbarMenuSelector: '.dropdown-menu',
37
38 shortcutItemSelector: '.t3js-topbar-shortcut',
39 shortcutDeleteSelector: '.t3js-shortcut-delete',
40 shortcutEditSelector: '.t3js-shortcut-edit',
41
42 shortcutFormTitleSelector: 'input[name="shortcut-title"]',
43 shortcutFormGroupSelector: 'select[name="shortcut-group"]',
44 shortcutFormSaveSelector: '.shortcut-form-save',
45 shortcutFormCancelSelector: '.shortcut-form-cancel',
46 shortcutFormSelector: '.shortcut-form'
47 }
48 };
49
50 /**
51 * build the in-place-editor for a shortcut
52 *
53 * @param {Object} $shortcutRecord
54 */
55 ShortcutMenu.editShortcut = function($shortcutRecord) {
56 // load the form
57 $.ajax({
58 url: TYPO3.settings.ajaxUrls['shortcut_editform'],
59 data: {
60 shortcutId: $shortcutRecord.data('shortcutid'),
61 shortcutGroup: $shortcutRecord.data('shortcutgroup')
62 },
63 cache: false
64 }).done(function(data) {
65 $(ShortcutMenu.options.containerSelector).find(ShortcutMenu.options.toolbarMenuSelector).html(data);
66 });
67 };
68
69 /**
70 * Save the data from the in-place-editor for a shortcut
71 *
72 * @param {Object} $shortcutForm
73 */
74 ShortcutMenu.saveShortcutForm = function($shortcutForm) {
75 $.ajax({
76 url: TYPO3.settings.ajaxUrls['shortcut_saveform'],
77 data: {
78 shortcutId: $shortcutForm.data('shortcutid'),
79 shortcutTitle: $shortcutForm.find(ShortcutMenu.options.shortcutFormTitleSelector).val(),
80 shortcutGroup: $shortcutForm.find(ShortcutMenu.options.shortcutFormGroupSelector).val()
81 },
82 type: 'post',
83 cache: false
84 }).done(function(data) {
85 Notification.success(TYPO3.lang['bookmark.savedTitle'], TYPO3.lang['bookmark.savedMessage']);
86 ShortcutMenu.refreshMenu();
87 });
88 };
89
90 /**
91 * removes an existing short by sending an AJAX call
92 *
93 * @param {Object} $shortcutRecord
94 */
95 ShortcutMenu.deleteShortcut = function($shortcutRecord) {
96 Modal.confirm(TYPO3.lang['bookmark.delete'], TYPO3.lang['bookmark.confirmDelete'])
97 .on('confirm.button.ok', function() {
98 $.ajax({
99 url: TYPO3.settings.ajaxUrls['shortcut_remove'],
100 data: {
101 shortcutId: $shortcutRecord.data('shortcutid')
102 },
103 type: 'post',
104 cache: false
105 }).done(function() {
106 // a reload is used in order to restore the original behaviour
107 // e.g. remove groups that are now empty because the last one in the group
108 // was removed
109 ShortcutMenu.refreshMenu();
110 });
111 $(this).trigger('modal-dismiss');
112 })
113 .on('confirm.button.cancel', function() {
114 $(this).trigger('modal-dismiss');
115 });
116 };
117
118 /**
119 * makes a call to the backend class to create a new shortcut,
120 * when finished it reloads the menu
121 *
122 * @param {String} moduleName
123 * @param {String} url
124 * @param {String} confirmationText
125 * @param {String} motherModule
126 * @param {Object} shortcutButton
127 * @param {String} displayName
128 */
129 ShortcutMenu.createShortcut = function(moduleName, url, confirmationText, motherModule, shortcutButton, displayName) {
130 if (typeof confirmationText !== 'undefined') {
131 Modal.confirm(TYPO3.lang['bookmark.create'], confirmationText)
132 .on('confirm.button.ok', function() {
133 var $toolbarItemIcon = $(ShortcutMenu.options.toolbarIconSelector, ShortcutMenu.options.containerSelector),
134 $existingIcon = $toolbarItemIcon.clone();
135
136 Icons.getIcon('spinner-circle-light', Icons.sizes.small).done(function(spinner) {
137 $toolbarItemIcon.replaceWith(spinner);
138 });
139
140 $.ajax({
141 url: TYPO3.settings.ajaxUrls['shortcut_create'],
142 type: 'post',
143 data: {
144 module: moduleName,
145 url: url,
146 motherModName: motherModule,
147 displayName: displayName
148 },
149 cache: false
150 }).done(function() {
151 ShortcutMenu.refreshMenu();
152 $(ShortcutMenu.options.toolbarIconSelector, ShortcutMenu.options.containerSelector).replaceWith($existingIcon);
153 if (typeof shortcutButton === 'object') {
154 Icons.getIcon('actions-system-shortcut-active', Icons.sizes.small).done(function(icons) {
155 $(shortcutButton).html(icons['actions-system-shortcut-active']);
156 });
157 $(shortcutButton).addClass('active');
158 $(shortcutButton).attr('title', null);
159 $(shortcutButton).attr('onclick', null);
160 }
161 });
162 $(this).trigger('modal-dismiss');
163 })
164 .on('confirm.button.cancel', function() {
165 $(this).trigger('modal-dismiss');
166 });
167 }
168
169 };
170
171 /**
172 * reloads the menu after an update
173 */
174 ShortcutMenu.refreshMenu = function() {
175 $.ajax({
176 url: TYPO3.settings.ajaxUrls['shortcut_list'],
177 type: 'get',
178 cache: false
179 }).done(function(data) {
180 $(ShortcutMenu.options.toolbarMenuSelector, ShortcutMenu.options.containerSelector).html(data);
181 });
182 };
183
184 /**
185 * Registers listeners
186 */
187 ShortcutMenu.initializeEvents = function() {
188 $(ShortcutMenu.options.containerSelector).on('click', ShortcutMenu.options.shortcutDeleteSelector, function(evt) {
189 evt.preventDefault();
190 evt.stopImmediatePropagation();
191 ShortcutMenu.deleteShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
192 }).on('click', ShortcutMenu.options.shortcutFormGroupSelector, function(evt) {
193 evt.preventDefault();
194 evt.stopImmediatePropagation();
195 }).on('click', ShortcutMenu.options.shortcutEditSelector, function(evt) {
196 evt.preventDefault();
197 evt.stopImmediatePropagation();
198 ShortcutMenu.editShortcut($(this).closest(ShortcutMenu.options.shortcutItemSelector));
199 }).on('click', ShortcutMenu.options.shortcutFormSaveSelector, function(evt) {
200 evt.preventDefault();
201 evt.stopImmediatePropagation();
202 ShortcutMenu.saveShortcutForm($(this).closest(ShortcutMenu.options.shortcutFormSelector));
203 }).on('submit', ShortcutMenu.options.shortcutFormSelector, function(evt) {
204 evt.preventDefault();
205 evt.stopImmediatePropagation();
206 ShortcutMenu.saveShortcutForm($(this).closest(ShortcutMenu.options.shortcutFormSelector));
207 }).on('click', ShortcutMenu.options.shortcutFormCancelSelector, function(evt) {
208 evt.preventDefault();
209 evt.stopImmediatePropagation();
210 ShortcutMenu.refreshMenu();
211 });
212 };
213
214 Viewport.Topbar.Toolbar.registerEvent(ShortcutMenu.initializeEvents);
215
216 // expose as global object
217 TYPO3.ShortcutMenu = ShortcutMenu;
218
219 return ShortcutMenu;
220 });