0011198: Setup logout destroy credential cache
[tine20] / tine20 / Tinebase / js / MainMenu.js
1 /*
2  * Tine 2.0
3  * 
4  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
5  * @author      Cornelius Weiss <c.weiss@metaways.de>
6  * @copyright   Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
7  */
8 Ext.ns('Tine.Tinebase');
9
10 /**
11  * Tine 2.0 jsclient main menu
12  * 
13  * @namespace   Tine.Tinebase
14  * @class       Tine.Tinebase.MainMenu
15  * @extends     Ext.Toolbar
16  * @author      Cornelius Weiss <c.weiss@metaways.de>
17  */
18 Tine.Tinebase.MainMenu = Ext.extend(Ext.Toolbar, {
19     /**
20      * @cfg {Boolean} showMainMenu
21      */
22     showMainMenu: false,
23     style: {'padding': '0px 2px'},
24     cls: 'tbar-mainmenu',
25     
26     /**
27      * @type Array
28      * @property mainActions
29      */
30     mainActions: null,
31     
32     initComponent: function() {
33         this.initActions();
34         this.onlineStatus = new Ext.ux.ConnectionStatus({
35             showIcon: false
36         });
37         
38         this.items = this.getItems();
39         
40         var buttonTpl = new Ext.Template(
41             '<table id="{4}" cellspacing="0" class="x-btn {3}"><tbody class="{1}">',
42             '<tr><td class="x-btn-ml"><i>&#160;</i></td><td class="x-btn-mc"><em class="{2}" unselectable="on"><button type="{0}"></button></em></td><td class="x-btn-mr"><i>&#160;</i></td></tr>',
43             '</tbody></table>'
44         ).compile();
45         
46         Ext.each(this.items, function(item) {
47             item.template = buttonTpl;
48         }, this);
49         
50         this.supr().initComponent.call(this);
51     },
52     
53     getItems: function() {
54         return [{
55             text: Tine.title,
56             hidden: !this.showMainMenu,
57             menu: {
58                 id: 'Tinebase_System_Menu', 
59                 items: this.getMainActions()
60         }},
61         '->',
62         this.actionLearnMore,
63         // TODO add a bigger spacer here?
64         { xtype: 'spacer' },
65         {
66             text: String.format(_('User: {0}'), Tine.Tinebase.registry.get('currentAccount').accountDisplayName),
67             menu: this.getUserActions(),
68             menuAlign: 'tr-br',
69             iconCls: Tine.Tinebase.registry.get('userAccountChanged') ? 'renderer_accountUserChangedIcon' : 'renderer_accountUserIcon'
70         },
71         this.onlineStatus, 
72         this.action_logout];
73     },
74     
75     /**
76      * returns all main actions
77      * 
78      * @return {Array}
79      */
80     getMainActions: function() {
81         if (! this.mainActions) {
82             this.mainActions = [
83                 this.action_aboutTine,
84                 this.action_loadTutorial,
85                 '-',
86                 this.getUserActions(),
87                 '-',
88                 this.action_logout
89             ];
90             
91             if (Tine.Tinebase.registry.get("version").buildType.match(/(DEVELOPMENT|DEBUG)/)) {
92                 this.mainActions.splice(2, 0, '-', this.action_showDebugConsole);
93             }
94         }
95         
96         return this.mainActions;
97     },
98     
99     getUserActions: function() {
100         if (! this.userActions) {
101             this.userActions = [
102                 this.action_editProfile,
103                 this.action_showPreferencesDialog,
104                 this.action_changePassword,
105                 this.action_notificationPermissions
106             ];
107             
108             if (Tine.Tinebase.registry.get('userAccountChanged')) {
109                 this.action_returnToOriginalUser = new Tine.widgets.account.ChangeAccountAction({
110                     returnToOriginalUser: true,
111                     text: _('Return to original user account')
112                 });
113                 this.userActions = this.userActions.concat(this.action_returnToOriginalUser);
114                 
115             } else if (Tine.Tinebase.registry.get("config") 
116                 && Tine.Tinebase.registry.get("config").roleChangeAllowed 
117                 && Tine.Tinebase.registry.get("config").roleChangeAllowed.value) 
118             {
119                 this.action_changeUserAccount = new Tine.widgets.account.ChangeAccountAction({});
120                 
121                 var roleChangeAllowed = Tine.Tinebase.registry.get("config").roleChangeAllowed.value,
122                     currentAccountName = Tine.Tinebase.registry.get('currentAccount').accountLoginName;
123                 if (roleChangeAllowed[currentAccountName]) {
124                     this.userActions = this.userActions.concat(this.action_changeUserAccount);
125                 }
126             }
127         }
128         return this.userActions;
129     },
130     
131     /**
132      * initialize actions
133      * @private
134      */
135     initActions: function() {
136         this.action_aboutTine = new Ext.Action({
137             text: String.format(_('About {0}'), Tine.title),
138             handler: this.onAboutTine20,
139             iconCls: 'action_about'
140         });
141         
142         this.action_loadTutorial = new Ext.Action({
143             text: String.format(_('Help')),
144             iconCls: 'action_loadTutorial',
145             handler: this.onLoadTutorial,
146             scope: this
147         });
148
149         this.action_showDebugConsole = new Ext.Action({
150             text: _('Debug Console (Ctrl + F11)'),
151             handler: Tine.Tinebase.common.showDebugConsole,
152             iconCls: 'tinebase-action-debug-console'
153         });
154         
155         this.action_showPreferencesDialog = new Ext.Action({
156             text: _('Preferences'),
157             disabled: false,
158             handler: this.onEditPreferences,
159             iconCls: 'action_adminMode'
160         });
161
162         this.action_editProfile = new Ext.Action({
163             text: _('Edit Profile'),
164             disabled: false,
165             handler: this.onEditProfile,
166             iconCls: 'tinebase-accounttype-user'
167         });
168         
169         this.action_changePassword = new Ext.Action({
170             text: _('Change password'),
171             handler: this.onChangePassword,
172             disabled: (! Tine.Tinebase.configManager.get('changepw')),
173             iconCls: 'action_password'
174         });
175         
176         this.action_logout = new Ext.Action({
177             text: _('Logout'),
178             tooltip:  String.format(_('Logout from {0}'), Tine.title),
179             iconCls: 'action_logOut',
180             handler: this.onLogout,
181             scope: this
182         });
183         
184         this.actionLearnMore = new Ext.Action({
185             text: String.format(_('Learn more about {0}'), Tine.title),
186             tooltip: Tine.weburl,
187             iconCls: 'tine-favicon',
188             handler: function() {
189                 window.open(Tine.weburl, '_blank');
190             },
191             scope: this
192         });
193         
194         this.action_notificationPermissions = new Ext.Action({
195             text: _('Allow desktop notifications'),
196             tooltip:  _('Request permissions for webkit desktop notifications.'),
197             iconCls: 'action_edit',
198             disabled: ! (window.webkitNotifications && window.webkitNotifications.checkPermission() != 0),
199             handler: function() {
200                 window.webkitNotifications.requestPermission(Ext.emptyFn);
201             },
202             scope: this
203         });
204     },
205     
206     /**
207      * open new window/tab to show help and tutorial
208      */
209     onLoadTutorial: function() {
210         window.open(Tine.helpUrl,'_blank');
211     },
212     
213     /**
214      * @private
215      */
216     onAboutTine20: function() {
217         var aboutDialog = new Tine.Tinebase.AboutDialog();
218         aboutDialog.show();
219     },
220     
221     /**
222      * @private
223      */
224     onChangePassword: function() {
225         var passwordDialog = new Tine.Tinebase.PasswordChangeDialog();
226         passwordDialog.show();
227     },
228     
229     /**
230      * @private
231      */
232     onEditPreferences: function() {
233         Tine.widgets.dialog.Preferences.openWindow({});
234     },
235
236     /**
237      * @private
238      */
239     onEditProfile: function() {
240         Tine.widgets.dialog.Preferences.openWindow({
241             initialCardName: 'Tinebase.UserProfile'
242         });
243     },
244     
245     /**
246      * the logout button handler function
247      * @private
248      */
249     onLogout: function() {
250         if (Tine.Tinebase.registry.get('confirmLogout') != '0') {
251             Ext.MessageBox.confirm(_('Confirm'), _('Are you sure you want to logout?'), function(btn, text) {
252                 if (btn == 'yes') {
253                     this._doLogout();
254                 }
255             }, this);
256         } else {
257             this._doLogout();
258         }
259     },
260     
261     /**
262      * logout user & redirect
263      */
264     _doLogout: function() {
265         Ext.MessageBox.wait(_('Logging you out...'), _('Please wait!'));
266         Ext.Ajax.request( {
267             params : {
268                 method : Ext.isObject(Tine.Setup) ? 'Setup.logout' : 'Tinebase.logout'
269             },
270             callback : function(options, Success, response) {
271                 // clear the authenticated mod_ssl session
272                 if (document.all == null) {
273                     if (window.crypto && Ext.isFunction(window.crypto.logout)) {
274                         window.crypto.logout();
275                     }
276                 } else {
277                     document.execCommand('ClearAuthenticationCache');
278                 }
279                 // remove the event handler
280                 // the reload() trigers the unload event
281                 var redirect = (Tine.Tinebase.registry.get('redirectUrl'));
282                 if (redirect && redirect != '') {
283                     window.location = Tine.Tinebase.registry.get('redirectUrl');
284                 } else {
285                     window.location.reload();
286                 }
287             }
288         });
289     }
290 });