9c21c5b7ca02f8b919bd89bc6f014daf6baa0975
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Resources / Public / JavaScript / Toolbar / WorkspacesMenu.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 * toolbar menu for the workspaces functionality to switch between the workspaces
15 * and jump to the workspaces module
16 */
17 define('TYPO3/CMS/Workspaces/Toolbar/WorkspacesMenu', ['jquery'], function($) {
18
19 var WorkspacesMenu = {
20 options: {
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'
28 }
29 };
30
31 /**
32 * registers event listeners
33 */
34 WorkspacesMenu.initializeEvents = function() {
35
36 // link to the module
37 $(WorkspacesMenu.options.containerSelector).on('click', WorkspacesMenu.options.workspaceModuleLinkSelector, function(evt) {
38 evt.preventDefault();
39 top.goToModule($(this).data('module'));
40 });
41
42 // observe all clicks on workspace links in the menu
43 $(WorkspacesMenu.options.containerSelector).on('click', WorkspacesMenu.options.menuItemSelector, function(evt) {
44 evt.preventDefault();
45 WorkspacesMenu.switchWorkspace($(this).data('workspaceid'));
46 });
47 };
48
49 /**
50 * switches the workspace via AJAX (which returns the new data, as JSON),
51 * then reloads the module menu, and the content frame
52 */
53 WorkspacesMenu.switchWorkspace = function(workspaceId) {
54 $.ajax({
55 url: TYPO3.settings.ajaxUrls['Workspaces::setWorkspace'],
56 type: 'post',
57 data: {
58 workspaceId: workspaceId,
59 pageId: fsMod.recentIds['web']
60 },
61 success: function(response) {
62 if (!response.workspaceId) {
63 response.workspaceId = 0;
64 }
65
66 WorkspacesMenu.performWorkspaceSwitch(response.workspaceId, response.title);
67
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);
74
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();
79
80 } else if (TYPO3.configuration.pageModule) {
81 TYPO3.ModuleMenu.App.showModule(TYPO3.configuration.pageModule);
82 }
83
84 // reload the module menu
85 TYPO3.ModuleMenu.App.refreshMenu();
86 }
87 });
88 };
89
90 /**
91 * changes the data in the module menu and the updates the backend context
92 *
93 * @param id the workspace ID
94 * @param title the workspace title
95 */
96 WorkspacesMenu.performWorkspaceSwitch = function(id, title) {
97 top.TYPO3.Workspaces.workspaceTitle = title;
98 top.TYPO3.configuration.inWorkspace = id !== 0;
99
100 WorkspacesMenu.updateBackendContext(title);
101
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';
105
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');
109
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');
114 };
115
116 /**
117 * checks if the TYPO3 backend is within a backend context and adds a class
118 * also updates the workspaces title
119 */
120 WorkspacesMenu.updateBackendContext = function(title) {
121
122 if (TYPO3.configuration.inWorkspace) {
123 $('body').addClass(WorkspacesMenu.options.workspaceBodyClass);
124 WorkspacesMenu.updateTopBar(title || TYPO3.lang['Workspaces.workspaceTitle']);
125 } else {
126 $('body').removeClass(WorkspacesMenu.options.workspaceBodyClass);
127 WorkspacesMenu.updateTopBar();
128 }
129 };
130
131 /**
132 * adds the workspace title to the toolbar next to the username
133 *
134 * @param workspaceTitle
135 */
136 WorkspacesMenu.updateTopBar = function(workspaceTitle) {
137 $('.' + WorkspacesMenu.options.workspacesTitleInToolbarClass, WorkspacesMenu.options.containerSelector).remove();
138
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);
144 }
145 };
146
147 /**
148 * initialize and return the WorkspacesMenu object
149 */
150 return function() {
151 $(document).ready(function() {
152 WorkspacesMenu.initializeEvents();
153 WorkspacesMenu.updateBackendContext();
154 });
155
156 TYPO3.WorkspacesMenu = WorkspacesMenu;
157 return WorkspacesMenu;
158 }();
159 });