6befb12007833b10e9079cd497eb6a2724cd8830
[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 $(SystemInformationMenu.identifier.toolbarIconSelector, SystemInformationMenu.identifier.containerSelector).replaceWith($existingIcon);
78
79 SystemInformationMenu.initialize();
80 }
81 }).done(function(){
82 // reload error data every five minutes
83 SystemInformationMenu.timer = setTimeout(
84 SystemInformationMenu.updateMenu,
85 1000 * 300
86 );
87 });
88 };
89
90 /**
91 * Updates the counter
92 */
93 SystemInformationMenu.updateCounter = function() {
94 var $container = $(SystemInformationMenu.identifier.containerSelector).find(SystemInformationMenu.identifier.menuContainerSelector).find('.t3js-systeminformation-container'),
95 $counter = $(SystemInformationMenu.identifier.counter),
96 count = $container.data('count'),
97 badgeClass = $container.data('severityclass');
98
99 $counter.text(count).toggle(parseInt(count) > 0);
100
101 // ensure all default classes are available and previous
102 // (at this time in processing unknown) class is removed
103 $counter.removeClass();
104 $counter.addClass('t3js-systeminformation-counter toolbar-item-badge badge');
105 // badgeClass e.g. could be 'badge-info', 'badge-danger', ...
106 if (badgeClass !== '') {
107 $counter.addClass(badgeClass);
108 }
109 };
110
111 /**
112 * Updates the UC and opens the linked module
113 *
114 * @param {Event} e
115 */
116 SystemInformationMenu.openModule = function(e) {
117 e.preventDefault();
118 e.stopPropagation();
119
120 var storedSystemInformationSettings = {},
121 moduleStorageObject = {},
122 requestedModule = $(e.currentTarget).data('modulename'),
123 timestamp = Math.floor((new Date()).getTime() / 1000);
124
125 if (Storage.Persistent.isset('systeminformation')) {
126 storedSystemInformationSettings = JSON.parse(Storage.Persistent.get('systeminformation'));
127 }
128
129 moduleStorageObject[requestedModule] = {lastAccess: timestamp};
130 $.extend(true, storedSystemInformationSettings, moduleStorageObject);
131 var $ajax = Storage.Persistent.set('systeminformation', JSON.stringify(storedSystemInformationSettings));
132 $ajax.done(function() {
133 // finally, open the module now
134 TYPO3.ModuleMenu.App.showModule(requestedModule);
135 Viewport.Topbar.refresh();
136 });
137 };
138
139 Viewport.Topbar.Toolbar.registerEvent(SystemInformationMenu.updateMenu);
140
141 return SystemInformationMenu;
142 });