6983748cf0089a7b84bda2fd3a601776b0ae85ec
[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                                                                         plugins: [ Ext.ux.FieldLabeler ],
36                                                                 });
37                                                         }
38                                                         var change_password_window = application.create_window({
39                                                                 layout: 'fit',
40                                                                 resizable: true,
41                                                                 title: 'Change password',
42                                                                 iconCls: 'icon-key--pencil',
43                                                                 width: 360,
44                                                                 height: 100,
45                                                                 items: change_password_form = new Ext.FormPanel(Ext.applyIf({
46                                                                         layout: {
47                                                                                 type: 'vbox',
48                                                                                 align: 'stretch',
49                                                                         },
50                                                                         baseCls: 'x-plain',
51                                                                         bodyStyle: 'padding: 5px;',
52                                                                         frame: true,
53                                                                         buttons: [{
54                                                                                 text: 'Change password',
55                                                                                 iconCls: 'icon-key--pencil',
56                                                                                 handler: function(button, event) {
57                                                                                         change_password_form.getForm().submit({
58                                                                                                 success: function(form, action) {
59                                                                                                         Ext.MessageBox.alert('Password changed', 'Your password has been changed.');
60                                                                                                 },
61                                                                                         });
62                                                                                 },
63                                                                         }],
64                                                                         api: {
65                                                                                 submit: Gilbert.api.plugins.auth.save_passwd_form,
66                                                                         },
67                                                                 }, formspec))
68                                                         });
69                                                         change_password_window.doLayout();
70                                                         change_password_window.show(button.el);
71                                                 });
72                                                 
73                                         },
74                                 },{
75                                         text: 'Log out',
76                                         iconCls: 'icon-door-open-out',
77                                         handler: function(button, event) {
78                                                 Gilbert.api.plugins.auth.logout(function(success) {
79                                                         if (success) {
80                                                                 window.onbeforeunload = undefined;
81                                                                 document.location.reload();
82                                                         } else {
83                                                                 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.');
84                                                         }
85                                                 })
86                                         }
87                                 }],
88                         });
89                         application.do_layout();
90                 });
91         },
92         
93         build_theme_menu: function () {
94                 var application = this.application;
95                 
96                 var theme_switcher = function (menuitem) {
97                         var theme_name = menuitem.theme_name;
98                         Gilbert.api.plugins.auth.set_preference('gilbert.theme', theme_name, function () {
99                                 application._set_theme(theme_name);
100                                 application.do_layout();
101                                 application.windows.each(function (win) {
102                                         win.doLayout();
103                                 });
104                         });
105                 };
106                 
107                 var menu = [];
108                 
109                 Ext.each(document.getElementsByClassName('gilbert.theme'), function (theme_element) {
110                         var theme_id = theme_element.id;
111                         var theme_name = theme_id.match(/gilbert.theme.(.*)/)[1];
112                         var current_theme = false;
113                         if (!theme_element.disabled) {
114                                 current_theme = true;
115                         }
116                         
117                         
118                         menu.push({
119                                 text: theme_name.capfirst(),
120                                 checked: current_theme,
121                                 group: 'theme',
122                                 theme_name: theme_name,
123                                 handler: theme_switcher,
124                         });
125                 });
126                 
127                 return menu;
128         },
129         
130 });
131
132
133 Gilbert.on('ready', function (application) {
134         application.register_plugin('auth', new Gilbert.lib.plugins.auth.Plugin());
135 });