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