8098c9fe45e864f161b678988102a82727644b3e
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / Toolbar / SystemInformationMenu.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/SystemInformationMenu
16 * System information menu handler
17 */
18 define([
19 'jquery',
20 'TYPO3/CMS/Backend/Icons',
21 'TYPO3/CMS/Backend/Storage',
22 'TYPO3/CMS/Backend/Viewport'
23 ], function($, Icons, Storage, Viewport) {
24 'use strict';
25
26 /**
27 *
28 * @type {{identifier: {containerSelector: string, toolbarIconSelector: string, menuContainerSelector: string, moduleLinks: string}, elements: {$counter: (*|jQuery|HTMLElement)}}}
29 * @exports TYPO3/CMS/Backend/Toolbar/SystemInformationMenu
30 */
31 var SystemInformationMenu = {
32 identifier: {
33 containerSelector: '#typo3-cms-backend-backend-toolbaritems-systeminformationtoolbaritem',
34 toolbarIconSelector: '.toolbar-item-icon .t3js-icon',
35 menuContainerSelector: '.dropdown-menu',
36 moduleLinks: '.t3js-systeminformation-module',
37 counter: '.t3js-systeminformation-counter'
38 }
39 };
40
41 /**
42 * Initialize the events
43 */
44 SystemInformationMenu.initialize = function() {
45 $(SystemInformationMenu.identifier.moduleLinks).on('click', SystemInformationMenu.openModule);
46 };
47
48 /**
49 * Timer for auto reloading the SystemInformation
50 */
51 SystemInformationMenu.timer = null;
52
53 /**
54 * Updates the menu
55 */
56 SystemInformationMenu.updateMenu = function() {
57 var $toolbarItemIcon = $(SystemInformationMenu.identifier.toolbarIconSelector, SystemInformationMenu.identifier.containerSelector),
58 $existingIcon = $toolbarItemIcon.clone(),
59 $menuContainer = $(SystemInformationMenu.identifier.containerSelector).find(SystemInformationMenu.identifier.menuContainerSelector);
60
61 if (SystemInformationMenu.timer !== null) {
62 clearTimeout(SystemInformationMenu.timer);
63 SystemInformationMenu.timer = null;
64 }
65
66 Icons.getIcon('spinner-circle-light', Icons.sizes.small).done(function(spinner) {
67 $toolbarItemIcon.replaceWith(spinner);
68 });
69
70 $.ajax({
71 url: TYPO3.settings.ajaxUrls['systeminformation_render'],
72 type: 'post',
73 cache: false,
74 success: function(data) {
75 $menuContainer.html(data);
76 SystemInformationMenu.updateCounter();
77
78 SystemInformationMenu.initialize();
79 },
80 complete: function() {
81 $(SystemInformationMenu.identifier.toolbarIconSelector, SystemInformationMenu.identifier.containerSelector).replaceWith($existingIcon);
82 }
83 }).done(function(){
84 // reload error data every five minutes
85 SystemInformationMenu.timer = setTimeout(
86 SystemInformationMenu.updateMenu,
87 1000 * 300
88 );
89 });
90 };
91
92 /**
93 * Updates the counter
94 */
95 SystemInformationMenu.updateCounter = function() {
96 var $container = $(SystemInformationMenu.identifier.containerSelector).find(SystemInformationMenu.identifier.menuContainerSelector).find('.t3js-systeminformation-container'),
97 $counter = $(SystemInformationMenu.identifier.counter),
98 count = $container.data('count'),
99 badgeClass = $container.data('severityclass');
100
101 $counter.text(count).toggle(parseInt(count) > 0);
102
103 // ensure all default classes are available and previous
104 // (at this time in processing unknown) class is removed
105 $counter.removeClass();
106 $counter.addClass('t3js-systeminformation-counter toolbar-item-badge badge');
107 // badgeClass e.g. could be 'badge-info', 'badge-danger', ...
108 if (badgeClass !== '') {
109 $counter.addClass(badgeClass);
110 }
111 };
112
113 /**
114 * Updates the UC and opens the linked module
115 *
116 * @param {Event} e
117 */
118 SystemInformationMenu.openModule = function(e) {
119 e.preventDefault();
120 e.stopPropagation();
121
122 var storedSystemInformationSettings = {},
123 moduleStorageObject = {},
124 requestedModule = $(e.currentTarget).data('modulename'),
125 timestamp = Math.floor((new Date()).getTime() / 1000);
126
127 if (Storage.Persistent.isset('systeminformation')) {
128 storedSystemInformationSettings = JSON.parse(Storage.Persistent.get('systeminformation'));
129 }
130
131 moduleStorageObject[requestedModule] = {lastAccess: timestamp};
132 $.extend(true, storedSystemInformationSettings, moduleStorageObject);
133 var $ajax = Storage.Persistent.set('systeminformation', JSON.stringify(storedSystemInformationSettings));
134 $ajax.done(function() {
135 // finally, open the module now
136 TYPO3.ModuleMenu.App.showModule(requestedModule);
137 Viewport.Topbar.refresh();
138 });
139 };
140
141 Viewport.Topbar.Toolbar.registerEvent(SystemInformationMenu.updateMenu);
142
143 return SystemInformationMenu;
144 });