dcd61ae31f0d2c67f729dd910f1f747ae311b2a1
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / AjaxDataHandler.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 * AjaxDataHandler - Javascript functions to work with AJAX and interacting with tce_db.php
16 */
17 define('TYPO3/CMS/Backend/AjaxDataHandler', ['jquery', 'TYPO3/CMS/Backend/FlashMessages'], function ($) {
18 var AjaxDataHandler = {};
19
20 AjaxDataHandler.initialize = function() {
21
22 // click events for all action icons to hide/unhide
23 $(document).on('click', '.t3js-record-hide', function(evt) {
24 evt.preventDefault();
25 var $anchorElement = $(this);
26 var $iconElement = $anchorElement.find('span');
27 var $rowElement = $anchorElement.closest('tr[data-uid]');
28 var table = $anchorElement.closest('table[data-table]').data('table');
29 var hasVisibleState = $anchorElement.data('state') === 'visible';
30 var params = $anchorElement.data('params');
31
32 var removeClass = hasVisibleState ? 'fa-toggle-on' : 'fa-toggle-off';
33 var addClass = hasVisibleState ? 'fa-toggle-off' : 'fa-toggle-on';
34 var nextState = hasVisibleState ? 'hidden' : 'visible';
35 var nextParams = hasVisibleState ? params.replace('=1', '=0') : params.replace('=0', '=1');
36
37 // add a spinner
38 $iconElement.removeClass(removeClass);
39 AjaxDataHandler._showSpinnerIcon($iconElement);
40
41 // make the AJAX call to toggle the visibility
42 AjaxDataHandler._call(params).done(function(result) {
43 AjaxDataHandler._hideSpinnerIcon($iconElement);
44 // print messages on errors
45 if (result.hasErrors) {
46 $.each(result.messages, function(position, message) {
47 top.TYPO3.Flashmessage.display(message.severity, message.title, message.message);
48 });
49 // revert to the old class
50 $iconElement.addClass(removeClass);
51 } else {
52 $anchorElement.data('state', nextState).data('params', nextParams);
53 $iconElement.removeClass(removeClass).addClass(addClass);
54 if (nextState === 'hidden') {
55 // add overlay icon
56 $rowElement.find('td.col-icon span.t3-icon').append('<span class="t3-icon t3-icon-status t3-icon-status-overlay t3-icon-overlay-hidden t3-icon-overlay">&nbsp;</span>');
57 } else {
58 // remove overlay icon
59 $rowElement.find('td.col-icon span.t3-icon span.t3-icon').remove();
60 }
61 $rowElement.fadeTo('fast', 0.4, function() {
62 $rowElement.fadeTo('fast', 1);
63 });
64
65 if (table === 'pages' && top.TYPO3 && top.TYPO3.Backend && top.TYPO3.Backend.NavigationContainer && top.TYPO3.Backend.NavigationContainer.PageTree) {
66 top.TYPO3.Backend.NavigationContainer.PageTree.refreshTree();
67 }
68 }
69
70 });
71 });
72 };
73
74 /**
75 * AJAX call to tce_db.php
76 * returns a jQuery Promise to work with
77 * @private
78 */
79 AjaxDataHandler._call = function(params) {
80 return $.getJSON(TYPO3.settings.ajaxUrls['DataHandler::process'], params);
81 };
82
83 /**
84 * Replace the given icon with a spinner icon
85 * @private
86 */
87 AjaxDataHandler._showSpinnerIcon = function($iconElement) {
88 $iconElement.addClass('fa-spin fa-circle-o-notch');
89 };
90
91 /**
92 * Removes the spinner icon classes
93 * @private
94 */
95 AjaxDataHandler._hideSpinnerIcon = function($iconElement) {
96 $iconElement.removeClass('fa-spin fa-circle-o-notch');
97 };
98
99 /**
100 * initialize and return the object
101 */
102 return function() {
103 AjaxDataHandler.initialize();
104
105 // return the object in the global space
106 return AjaxDataHandler;
107 }();
108 });