e31d6fd9210c8068db088f55a89c271a07e60655
[philo.git] / contrib / gilbert / media / gilbert / plugins / auth.js
1 Ext.ns('Gilbert.lib.plugins.auth');
2
3
4 Gilbert.lib.plugins.auth.PreferencesWindow = Ext.extend(Ext.Window, {
5         constructor: function (config, application) {
6                 Gilbert.lib.plugins.auth.PreferencesWindow.superclass.constructor.call(this, Ext.applyIf(config||{},{
7                         width: 320,
8                         height: 200,
9                         title: 'Preferences',
10                 }));
11         }
12 });
13
14
15 Gilbert.lib.plugins.auth.Plugin = Ext.extend(Gilbert.lib.plugins.Plugin, {
16         
17         init: function (application) {
18                 Gilbert.lib.plugins.auth.Plugin.superclass.init.call(this, application);
19                 
20                 var preferences_window = new Gilbert.lib.plugins.auth.PreferencesWindow({}, application);
21                 
22                 Gilbert.api.plugins.auth.whoami(function (whoami) {
23                         application.mainmenu.add({
24                                 xtype: 'tbfill',
25                         },{
26                                 xtype: 'tbseparator',
27                         },{
28                                 xtype: 'button',
29                                 iconCls: 'icon-user-silhouette',
30                                 text: '<span style="font-weight: bolder;">' + whoami + '</span>',
31                                 menu: [{
32                                                 text: 'Preferences...',
33                                                 iconCls: 'icon-switch',
34                                                 handler: function (button, event) {
35                                                         preferences_window.show();
36                                                 },
37                                         },{
38                                                 xtype: 'menuseparator',
39                                         },{
40                                         text: 'Change password',
41                                         iconCls: 'icon-key--pencil',
42                                         handler: function(button, event) {
43                                                 Gilbert.api.plugins.auth.get_passwd_form(function(formspec) {
44                                                         var formspec = formspec;
45                                                         for (var item_index in formspec.items) {
46                                                                 var item = formspec.items[item_index];
47                                                                 Ext.apply(item, {
48                                                                         plugins: [ Ext.ux.FieldLabeler ],
49                                                                 });
50                                                         }
51                                                         var change_password_window = application.create_window({
52                                                                 layout: 'fit',
53                                                                 resizable: true,
54                                                                 title: 'Change password',
55                                                                 iconCls: 'icon-key--pencil',
56                                                                 width: 360,
57                                                                 height: 100,
58                                                                 items: change_password_form = new Ext.FormPanel(Ext.applyIf({
59                                                                         layout: {
60                                                                                 type: 'vbox',
61                                                                                 align: 'stretch',
62                                                                         },
63                                                                         baseCls: 'x-plain',
64                                                                         bodyStyle: 'padding: 5px;',
65                                                                         frame: true,
66                                                                         buttons: [{
67                                                                                 text: 'Change password',
68                                                                                 iconCls: 'icon-key--pencil',
69                                                                                 handler: function(button, event) {
70                                                                                         change_password_form.getForm().submit({
71                                                                                                 success: function(form, action) {
72                                                                                                         Ext.MessageBox.alert('Password changed', 'Your password has been changed.');
73                                                                                                 },
74                                                                                         });
75                                                                                 },
76                                                                         }],
77                                                                         api: {
78                                                                                 submit: Gilbert.api.plugins.auth.save_passwd_form,
79                                                                         },
80                                                                 }, formspec))
81                                                         });
82                                                         change_password_window.doLayout();
83                                                         change_password_window.show(button.el);
84                                                 });
85                                                 
86                                         },
87                                 },{
88                                         text: 'Log out',
89                                         iconCls: 'icon-door-open-out',
90                                         handler: function(button, event) {
91                                                 Gilbert.api.plugins.auth.logout(function(success) {
92                                                         if (success) {
93                                                                 window.onbeforeunload = undefined;
94                                                                 document.location.reload();
95                                                         } else {
96                                                                 Ext.MessageBox.alert('Log out failed', 'You have <strong>not</strong> been logged out. This could mean that your connection with the server has been severed. Please try again.');
97                                                         }
98                                                 })
99                                         }
100                                 }],
101                         });
102                         application.do_layout();
103                 });
104         },
105
106 });
107
108
109 Gilbert.on('ready', function (application) {
110         application.register_plugin('auth', new Gilbert.lib.plugins.auth.Plugin());
111 });