2 * This file is part of the TYPO3 CMS project.
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.
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
11 * The TYPO3 project - inspiring people to share!
14 * toolbar menu for the workspaces functionality to switch between the workspaces
15 * and jump to the workspaces module
17 define('TYPO3/CMS/Workspaces/Toolbar/WorkspacesMenu', ['jquery'], function($) {
19 var WorkspacesMenu
= {
21 containerSelector
: '#workspace-selector-menu',
22 menuItemSelector
: '.dropdown-menu li a.tx-workspaces-switchlink',
23 activeMenuItemSelector
: '.dropdown-menu .selected',
24 toolbarItemSelector
: '.dropdown-toggle',
25 workspaceBodyClass
: 'typo3-in-workspace', // attached to <body> when in a workspace
26 workspacesTitleInToolbarClass
: 'topbar-workspaces-title',
27 workspaceModuleLinkSelector
: '.tx-workspaces-modulelink'
32 * registers event listeners
34 WorkspacesMenu
.initializeEvents = function() {
37 $(WorkspacesMenu
.options
.containerSelector
).on('click', WorkspacesMenu
.options
.workspaceModuleLinkSelector
, function(evt
) {
39 top
.goToModule($(this).data('module'));
42 // observe all clicks on workspace links in the menu
43 $(WorkspacesMenu
.options
.containerSelector
).on('click', WorkspacesMenu
.options
.menuItemSelector
, function(evt
) {
45 WorkspacesMenu
.switchWorkspace($(this).data('workspaceid'));
50 * switches the workspace via AJAX (which returns the new data, as JSON),
51 * then reloads the module menu, and the content frame
53 WorkspacesMenu
.switchWorkspace = function(workspaceId
) {
55 url
: TYPO3
.settings
.ajaxUrls
['Workspaces::setWorkspace'],
58 workspaceId
: workspaceId
,
59 pageId
: fsMod
.recentIds
['web']
61 success: function(response
) {
62 if (!response
.workspaceId
) {
63 response
.workspaceId
= 0;
66 WorkspacesMenu
.performWorkspaceSwitch(response
.workspaceId
, response
.title
);
68 // append the returned page ID to the current module URL
69 if (response
.pageId
) {
70 fsMod
.recentIds
['web'] = response
.pageId
;
71 var url
= TYPO3
.Backend
.ContentContainer
.getUrl();
72 url
+= (url
.indexOf('?') == -1 ? '?' : '&') + '&id=' + response
.pageId
;
73 TYPO3
.Backend
.ContentContainer
.setUrl(url
);
75 // when in web module reload, otherwise send the user to the web module
76 } else if (currentModuleLoaded
.startsWith('web_')) {
77 TYPO3
.Backend
.NavigationContainer
.PageTree
.refreshTree();
78 TYPO3
.ModuleMenu
.App
.reloadFrames();
80 } else if (TYPO3
.configuration
.pageModule
) {
81 TYPO3
.ModuleMenu
.App
.showModule(TYPO3
.configuration
.pageModule
);
84 // reload the module menu
85 TYPO3
.ModuleMenu
.App
.refreshMenu();
91 * changes the data in the module menu and the updates the backend context
93 * @param id the workspace ID
94 * @param title the workspace title
96 WorkspacesMenu
.performWorkspaceSwitch = function(id
, title
) {
97 top
.TYPO3
.Workspaces
.workspaceTitle
= title
;
98 top
.TYPO3
.configuration
.inWorkspace
= id
!== 0;
100 WorkspacesMenu
.updateBackendContext(title
);
102 // first remove all checks, then set the check in front of the selected workspace
103 var stateActiveClass
= 't3-icon t3-icon-status t3-icon-status-status t3-icon-status-checked';
104 var stateInactiveClass
= 't3-icon t3-icon-empty t3-icon-empty-empty t3-icon-empty';
106 // remove "selected" class and checkmark
107 $(WorkspacesMenu
.options
.activeMenuItemSelector
+ ' span', WorkspacesMenu
.options
.containerSelector
).removeClass(stateActiveClass
).addClass(stateInactiveClass
);
108 $(WorkspacesMenu
.options
.activeMenuItemSelector
, WorkspacesMenu
.options
.containerSelector
).removeClass('selected');
110 // add "selected" class and checkmark
111 var $activeElement
= $(WorkspacesMenu
.options
.menuItemSelector
+ '[data-workspaceid=' + id
+ ']', WorkspacesMenu
.options
.containerSelector
);
112 $activeElement
.find('span').removeClass(stateInactiveClass
).addClass(stateActiveClass
);
113 $activeElement
.parent().addClass('selected');
117 * checks if the TYPO3 backend is within a backend context and adds a class
118 * also updates the workspaces title
120 WorkspacesMenu
.updateBackendContext = function(title
) {
122 if (TYPO3
.configuration
.inWorkspace
) {
123 $('body').addClass(WorkspacesMenu
.options
.workspaceBodyClass
);
124 WorkspacesMenu
.updateTopBar(title
|| TYPO3
.lang
['Workspaces.workspaceTitle']);
126 $('body').removeClass(WorkspacesMenu
.options
.workspaceBodyClass
);
127 WorkspacesMenu
.updateTopBar();
132 * adds the workspace title to the toolbar next to the username
134 * @param workspaceTitle
136 WorkspacesMenu
.updateTopBar = function(workspaceTitle
) {
137 $('.' + WorkspacesMenu
.options
.workspacesTitleInToolbarClass
, WorkspacesMenu
.options
.containerSelector
).remove();
139 if (workspaceTitle
&& workspaceTitle
.length
) {
140 var title
= $('<span>', {
141 'class': WorkspacesMenu
.options
.workspacesTitleInToolbarClass
142 }).text(workspaceTitle
);
143 $(WorkspacesMenu
.options
.toolbarItemSelector
, WorkspacesMenu
.options
.containerSelector
).append(title
);
148 * initialize and return the WorkspacesMenu object
151 $(document
).ready(function() {
152 WorkspacesMenu
.initializeEvents();
153 WorkspacesMenu
.updateBackendContext();
156 TYPO3
.WorkspacesMenu
= WorkspacesMenu
;
157 return WorkspacesMenu
;