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