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