Merge branch '2013.10' into 2014.09
[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                 this.action_installChromeWebApp
107             ];
108             
109             if (Tine.Tinebase.registry.get('userAccountChanged')) {
110                 this.action_returnToOriginalUser = new Tine.widgets.account.ChangeAccountAction({
111                     returnToOriginalUser: true,
112                     text: _('Return to original user account')
113                 });
114                 this.userActions = this.userActions.concat(this.action_returnToOriginalUser);
115                 
116             } else if (Tine.Tinebase.registry.get("config") 
117                 && Tine.Tinebase.registry.get("config").roleChangeAllowed 
118                 && Tine.Tinebase.registry.get("config").roleChangeAllowed.value) 
119             {
120                 this.action_changeUserAccount = new Tine.widgets.account.ChangeAccountAction({});
121                 
122                 var roleChangeAllowed = Tine.Tinebase.registry.get("config").roleChangeAllowed.value,
123                     currentAccountName = Tine.Tinebase.registry.get('currentAccount').accountLoginName;
124                 if (roleChangeAllowed[currentAccountName]) {
125                     this.userActions = this.userActions.concat(this.action_changeUserAccount);
126                 }
127             }
128         }
129         return this.userActions;
130     },
131     
132     /**
133      * initialize actions
134      * @private
135      */
136     initActions: function() {
137         this.action_aboutTine = new Ext.Action({
138             text: String.format(_('About {0}'), Tine.title),
139             handler: this.onAboutTine20,
140             iconCls: 'action_about'
141         });
142         
143         this.action_loadTutorial = new Ext.Action({
144             text: String.format(_('Help')),
145             iconCls: 'action_loadTutorial',
146             handler: this.onLoadTutorial,
147             scope: this
148         });
149
150         this.action_showDebugConsole = new Ext.Action({
151             text: _('Debug Console (Ctrl + F11)'),
152             handler: Tine.Tinebase.common.showDebugConsole,
153             iconCls: 'tinebase-action-debug-console'
154         });
155         
156         this.action_showPreferencesDialog = new Ext.Action({
157             text: _('Preferences'),
158             disabled: false,
159             handler: this.onEditPreferences,
160             iconCls: 'action_adminMode'
161         });
162
163         this.action_editProfile = new Ext.Action({
164             text: _('Edit Profile'),
165             disabled: false,
166             handler: this.onEditProfile,
167             iconCls: 'tinebase-accounttype-user'
168         });
169         
170         this.action_changePassword = new Ext.Action({
171             text: _('Change password'),
172             handler: this.onChangePassword,
173             disabled: (! Tine.Tinebase.configManager.get('changepw')),
174             iconCls: 'action_password'
175         });
176         
177         this.action_logout = new Ext.Action({
178             text: _('Logout'),
179             tooltip:  String.format(_('Logout from {0}'), Tine.title),
180             iconCls: 'action_logOut',
181             handler: this.onLogout,
182             scope: this
183         });
184         
185         this.actionLearnMore = new Ext.Action({
186             text: String.format(_('Learn more about {0}'), Tine.title),
187             tooltip: Tine.weburl,
188             iconCls: 'tine-favicon',
189             handler: function() {
190                 window.open(Tine.weburl, '_blank');
191             },
192             scope: this
193         });
194         
195         this.action_notificationPermissions = new Ext.Action({
196             text: _('Allow desktop notifications'),
197             tooltip:  _('Request permissions for webkit desktop notifications.'),
198             iconCls: 'action_edit',
199             disabled: ! (window.webkitNotifications && window.webkitNotifications.checkPermission() != 0),
200             handler: function() {
201                 window.webkitNotifications.requestPermission(Ext.emptyFn);
202             },
203             scope: this
204         });
205         
206         this.action_installChromeWebApp = new Ext.Action({
207             text: _('Install web app'),
208             tooltip:  _('Install Tine 2.0 as web app in your browser.'),
209             iconCls: 'action_edit',
210             disabled: ! (Ext.isChrome && chrome.app && !chrome.app.isInstalled),
211             handler: function() {
212                 chrome.app.install();
213             },
214             scope: this
215         });
216     },
217     
218     /**
219      * open new window/tab to show help and tutorial
220      */
221     onLoadTutorial: function() {
222         window.open(Tine.helpUrl,'_blank');
223     },
224     
225     /**
226      * @private
227      */
228     onAboutTine20: function() {
229         var aboutDialog = new Tine.Tinebase.AboutDialog();
230         aboutDialog.show();
231     },
232     
233     /**
234      * @private
235      */
236     onChangePassword: function() {
237         var passwordDialog = new Tine.Tinebase.PasswordChangeDialog();
238         passwordDialog.show();
239     },
240     
241     /**
242      * @private
243      */
244     onEditPreferences: function() {
245         Tine.widgets.dialog.Preferences.openWindow({});
246     },
247
248     /**
249      * @private
250      */
251     onEditProfile: function() {
252         Tine.widgets.dialog.Preferences.openWindow({
253             initialCardName: 'Tinebase.UserProfile'
254         });
255     },
256     
257     /**
258      * the logout button handler function
259      * @private
260      */
261     onLogout: function() {
262         if (Tine.Tinebase.registry.get('confirmLogout') != '0') {
263             Ext.MessageBox.confirm(_('Confirm'), _('Are you sure you want to logout?'), function(btn, text) {
264                 if (btn == 'yes') {
265                     this._doLogout();
266                 }
267             }, this);
268         } else {
269             this._doLogout();
270         }
271     },
272     
273     /**
274      * logout user & redirect
275      */
276     _doLogout: function() {
277         Ext.MessageBox.wait(_('Logging you out...'), _('Please wait!'));
278         Ext.Ajax.request( {
279             params : {
280                 method : 'Tinebase.logout'
281             },
282             callback : function(options, Success, response) {
283                 // clear the authenticated mod_ssl session
284                 if (document.all == null) {
285                     if (window.crypto && Ext.isFunction(window.crypto.logout)) {
286                         window.crypto.logout();
287                     }
288                 } else {
289                     document.execCommand('ClearAuthenticationCache');
290                 }
291                 // remove the event handler
292                 // the reload() trigers the unload event
293                 var redirect = (Tine.Tinebase.registry.get('redirectUrl'));
294                 if (redirect && redirect != '') {
295                     window.location = Tine.Tinebase.registry.get('redirectUrl');
296                 } else {
297                     window.location.reload();
298                 }
299             }
300         });
301     }
302 });