Fixed the password change form on the client side, and fixed form handling in the...
[philo.git] / contrib / gilbert / media / gilbert / plugins / auth.js
1 Ext.ns('Gilbert.lib.plugins.auth');
2
3
4 Gilbert.lib.plugins.auth.Plugin = Ext.extend(Gilbert.lib.plugins.Plugin, {
5         
6         init: function (application) {
7                 Gilbert.lib.plugins.auth.Plugin.superclass.init.call(this, application);
8                 
9                 var outer = this;
10                 
11                 Gilbert.api.plugins.auth.whoami(function (whoami) {
12                         application.mainmenu.add({
13                                 xtype: 'tbfill',
14                         },{
15                                 xtype: 'tbseparator',
16                         },{
17                                 xtype: 'button',
18                                 iconCls: 'icon-user-silhouette',
19                                 text: '<span style="font-weight: bolder;">' + whoami + '</span>',
20                                 menu: [{
21                                                 text: 'Theme',
22                                                 iconCls: 'icon-mask',
23                                                 menu: outer.build_theme_menu(),
24                                         },{
25                                                 xtype: 'menuseparator',
26                                         },{
27                                         text: 'Change password',
28                                         iconCls: 'icon-key--pencil',
29                                         handler: function(button, event) {
30                                                 Gilbert.api.plugins.auth.get_passwd_form(function(formspec) {
31                                                         var formspec = formspec;
32                                                         for (var item_index in formspec.items) {
33                                                                 var item = formspec.items[item_index];
34                                                                 Ext.apply(item, {
35                                                                         anchor: '100%',
36                                                                 });
37                                                         }
38                                                         var change_password_form = new Gilbert.lib.ui.DjangoForm(Ext.applyIf({
39                                                                 title: 'Change password',
40                                                                 header: false,
41                                                                 iconCls: 'icon-key--pencil',
42                                                                 bodyStyle: 'padding: 10px;',
43                                                                 baseCls: 'x-plain',
44                                                                 autoScroll: true,
45                                                                 api: {
46                                                                         submit: Gilbert.api.plugins.auth.save_passwd_form,
47                                                                 },
48                                                         }, formspec));
49                                                         var change_password_window = application.create_window({
50                                                                 layout: 'fit',
51                                                                 title: change_password_form.title,
52                                                                 iconCls: change_password_form.iconCls,
53                                                                 bodyStyle: 'padding: 5px; background: solid;',
54                                                                 width: 360,
55                                                                 height: 240,
56                                                                 maximizable: false,
57                                                                 items: [change_password_form],
58                                                                 bbar: [
59                                                                         '->',
60                                                                         {
61                                                                                 text: 'Change password',
62                                                                                 iconCls: 'icon-key--pencil',
63                                                                                 handler: function(button, event) {
64                                                                                         change_password_form.getForm().submit({
65                                                                                                 success: function(form, action) {
66                                                                                                         Ext.MessageBox.alert('Password changed', 'Your password has been changed.');
67                                                                                                         change_password_window.close();
68                                                                                                 },
69                                                                                         });
70                                                                                 },
71                                                                         }
72                                                                 ],
73                                                         });
74                                                         change_password_window.doLayout();
75                                                         change_password_window.show(button.el);
76                                                 });
77                                                 
78                                         },
79                                 },{
80                                         text: 'Log out',
81                                         iconCls: 'icon-door-open-out',
82                                         handler: function(button, event) {
83                                                 Gilbert.api.plugins.auth.logout(function(success) {
84                                                         if (success) {
85                                                                 window.onbeforeunload = undefined;
86                                                                 document.location.reload();
87                                                         } else {
88                                                                 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.');
89                                                         }
90                                                 })
91                                         }
92                                 }],
93                         });
94                         application.do_layout();
95                 });
96         },
97         
98         build_theme_menu: function () {
99                 var application = this.application;
100                 
101                 var theme_switcher = function (menuitem) {
102                         var theme_name = menuitem.theme_name;
103                         Gilbert.api.plugins.auth.set_preference('gilbert.theme', theme_name, function () {
104                                 application._set_theme(theme_name);
105                                 application.do_layout();
106                                 application.windows.each(function (win) {
107                                         win.doLayout();
108                                 });
109                         });
110                 };
111                 
112                 var menu = [];
113                 
114                 Ext.each(document.getElementsByClassName('gilbert.theme'), function (theme_element) {
115                         var theme_id = theme_element.id;
116                         var theme_name = theme_id.match(/gilbert.theme.(.*)/)[1];
117                         var current_theme = false;
118                         if (!theme_element.disabled) {
119                                 current_theme = true;
120                         }
121                         
122                         
123                         menu.push({
124                                 text: theme_name.capfirst(),
125                                 checked: current_theme,
126                                 group: 'theme',
127                                 theme_name: theme_name,
128                                 handler: theme_switcher,
129                         });
130                 });
131                 
132                 return menu;
133         },
134         
135 });
136
137
138 Gilbert.on('ready', function (application) {
139         application.register_plugin('auth', new Gilbert.lib.plugins.auth.Plugin());
140 });