Adding fugue-icons submodule and adding django-staticmedia to the list of prerequisites.
[philo.git] / contrib / gilbert / templates / gilbert / api.js
1 {% load staticmedia %}
2
3 Ext.Direct.addProvider({
4         'namespace': 'Gilbert.api',
5         'url': '{% url gilbert:router %}',
6         'type': 'remoting',
7         'actions': {{% for gilbert_class in gilbert.core_api.gilbert_plugin_classes.values %}
8                 '{{ gilbert_class.gilbert_class_name }}': [{% for method in gilbert_class.gilbert_class_methods.values %}{
9                         'name': '{{ method.gilbert_method_name }}',
10                         'len': {{ method.gilbert_method_argc }}
11                 },{% endfor %}],{% endfor %}
12         }
13 });
14
15 {% if not logged_in %}
16
17 Ext.onReady(function() {
18         var login_form = new Ext.FormPanel({
19                 frame: true,
20                 bodyStyle: 'padding: 5px 5px 0',
21                 items: [
22                         {
23                                 fieldLabel: 'Username',
24                                 name: 'username',
25                                 xtype: 'textfield',
26                         },
27                         {
28                                 fieldLabel: 'Password',
29                                 name: 'password',
30                                 xtype: 'textfield',
31                                 inputType: 'password',
32                         }
33                 ],
34                 buttons: [
35                         {
36                                 text: 'Login',
37                                 handler: function(sender) {
38                                         // document.location.reload();
39                                         var the_form = login_form.getForm().el.dom;
40                                         var username = the_form[0].value;
41                                         var password = the_form[1].value;
42                                         Gilbert.api.auth.login(username, password, function(result) {
43                                                 if (result) {
44                                                         document.location.reload();
45                                                 } else {
46                                                         Ext.MessageBox.alert('Login failed', 'Unable to authenticate.', function() {
47                                                                 login_form.getForm().reset();
48                                                         });
49                                                 }
50                                         });
51                                 }
52                         }
53                 ],
54         });
55         var login_window = new Ext.Window({
56                 title: 'Login',
57                 closable: false,
58                 width: 266,
59                 height: 130,
60                 layout: 'fit',
61                 items: login_form,
62         });
63         login_window.show();
64 });
65
66
67 {% else %}
68
69 Ext.ns('Gilbert', 'Gilbert.ui', 'Gilbert.models', 'Gilbert.plugins');
70
71 {% for app_label, models in gilbert.model_registry.items %}Ext.Direct.addProvider({
72         'namespace': 'Gilbert.models.{{ app_label }}',
73         'url': '{% url gilbert:models app_label %}',
74         'type': 'remoting',
75         'actions': {{% for model_name, admin in models.items %}
76                 '{{ model_name }}': [{% for method in admin.gilbert_class_methods.values %}{
77                                 'name': '{{ method.gilbert_method_name }}',
78                                 'len': {{ method.gilbert_method_argc }}
79                         },{% endfor %}],{% endfor %}
80         }
81 });{% endfor %}
82 {% for plugin in gilbert.plugin_registry.values %}Ext.Direct.addProvider({
83         'namespace': 'Gilbert.plugins.{{ plugin.gilbert_plugin_name }}',
84         'url': '{% url gilbert:plugins plugin.gilbert_plugin_name %}',
85         'type': 'remoting',
86         'actions': {{% for gilbert_class in plugin.gilbert_plugin_classes %}
87                 '{{ gilbert_class.gilbert_class_name }}': [{% for method in gilbert_class.gilbert_class_methods.values %}{}
88                         'name': '{{ method.gilbert_method_name }}',
89                         'len': {{ method.gilbert_method_argc }}
90                 },{% endfor %}],{% endfor %}
91         }
92 });{% endfor %}
93
94 Gilbert.ui.Application = function(cfg) {
95         Ext.apply(this, cfg, {
96                 title: '{{ gilbert.title }}',
97         });
98         this.addEvents({
99                 'ready': true,
100                 'beforeunload': true,
101         });
102         Ext.onReady(this.initApplication, this);
103 };
104
105 Ext.extend(Gilbert.ui.Application, Ext.util.Observable, {
106         initApplication: function() {
107                 
108                 Ext.QuickTips.init();
109                 
110                 this.desktop = new Ext.Panel({
111                         region: 'center',
112                         border: false,
113                         padding: '5',
114                         bodyStyle: 'background: none;',
115                 });
116                 var desktop = this.desktop;
117                 
118                 this.toolbar = new Ext.Toolbar({
119                         region: 'north',
120                         autoHeight: true,
121                         items: [
122                         {
123                                 xtype: 'tbtext',
124                                 text: this.title,
125                                 style: 'font-weight: bolder; font-size: larger; text-transform: uppercase;',
126                         },
127                         {
128                                 xtype: 'tbseparator',
129                         }
130                         ]
131                 });
132                 var toolbar = this.toolbar;
133                 
134                 this.viewport = new Ext.Viewport({
135                         renderTo: Ext.getBody(),
136                         layout: 'border',
137                         items: [
138                         toolbar,
139                         desktop,
140                         ],
141                 });
142                 var viewport = this.viewport;
143                 
144                 var windows = new Ext.WindowGroup();
145                 
146                 this.createWindow = function(config, cls) {
147                         var win = new(cls || Ext.Window)(Ext.applyIf(config || {},
148                         {
149                                 renderTo: desktop.el,
150                                 manager: windows,
151                                 constrainHeader: true,
152                                 maximizable: true,
153                         }));
154                         win.render(desktop.el);
155                         return win;
156                 };
157                 var createWindow = this.createWindow;
158                 
159                 if (this.plugins) {
160                         for (var pluginNum = 0; pluginNum < this.plugins.length; pluginNum++) {
161                                 this.plugins[pluginNum].initWithApp(this);
162                         };
163                 };
164                 
165                 if (this.user) {
166                         var user = this.user;
167                         toolbar.add({ xtype: 'tbfill' });
168                         toolbar.add({ xtype: 'tbseparator' });
169                         toolbar.add({
170                                 xtype: 'button',
171                                 text: '<b>' + user + '</b>',
172                                 style: 'font-weight: bolder !important; font-size: smaller !important; text-transform: uppercase !important;',
173                                 menu: [
174                                 {
175                                         text: 'Change password',
176                                         handler: function(button, event) {
177                                                 var edit_window = createWindow({
178                                                         layout: 'fit',
179                                                         title: 'Change password',
180                                                         width: 266,
181                                                         height: 170,
182                                                         layout: 'fit',
183                                                         items: _change_password_form = new Ext.FormPanel({
184                                                                 frame: true,
185                                                                 bodyStyle: 'padding: 5px 5px 0',
186                                                                 items: [
187                                                                         {
188                                                                                 fieldLabel: 'Current password',
189                                                                                 name: 'current_password',
190                                                                                 xtype: 'textfield',
191                                                                                 inputType: 'password',
192                                                                         },
193                                                                         {
194                                                                                 fieldLabel: 'New password',
195                                                                                 name: 'new_password',
196                                                                                 xtype: 'textfield',
197                                                                                 inputType: 'password',
198                                                                         },
199                                                                         {
200                                                                                 fieldLabel: 'New password (confirm)',
201                                                                                 name: 'new_password_confirm',
202                                                                                 xtype: 'textfield',
203                                                                                 inputType: 'password',
204                                                                         }
205                                                                 ],
206                                                                 buttons: [
207                                                                         {
208                                                                                 text: 'Change password',
209                                                                                 handler: function(sender) {
210                                                                                         // document.location.reload();
211                                                                                         var the_form = _change_password_form.getForm().el.dom;
212                                                                                         var current_password = the_form[0].value;
213                                                                                         var new_password = the_form[1].value;
214                                                                                         var new_password_confirm = the_form[2].value;
215                                                                                         Gilbert.api.auth.passwd(current_password, new_password, new_password_confirm, function(result) {
216                                                                                                 if (result) {
217                                                                                                         Ext.MessageBox.alert('Password changed', 'Your password has been changed.');
218                                                                                                 } else {
219                                                                                                         Ext.MessageBox.alert('Password unchanged', 'Unable to change your password.', function() {
220                                                                                                                 _change_password_form.getForm().reset();
221                                                                                                         });
222                                                                                                 }
223                                                                                         });
224                                                                                 }
225                                                                         }
226                                                                 ],
227                                                         }),
228                                                 });
229                                                 edit_window.show(this);
230                                         },
231                                 },
232                                 {
233                                         text: 'Log out',
234                                         handler: function(button, event) {
235                                                 Gilbert.api.auth.logout(function(result) {
236                                                         if (result) {
237                                                                 Ext.MessageBox.alert('Logout successful', 'You have been logged out.', function() {
238                                                                         document.location.reload();
239                                                                 });
240                                                         } else {
241                                                                 Ext.MessageBox.alert('Logout failed', 'A bit odd, you might say.');
242                                                         }
243                                                 });
244                                         },
245                                 },
246                                 ],
247                         });
248                 };
249                 
250                 toolbar.doLayout();
251                 viewport.doLayout();
252         },
253 });
254
255 Ext.BLANK_IMAGE_URL = '{% mediaurl "gilbert/extjs/resources/images/default/s.gif" %}';
256
257 Ext.onReady(function(){
258         Gilbert.Application = new Gilbert.ui.Application({
259                 user: '{% filter force_escape %}{% firstof user.get_full_name user.username %}{% endfilter %}',
260                 plugins: [{% for plugin in gilbert.plugin_registry.values %}{% if plugin.gilbert_plugin_javascript %}
261                         {{ plugin.gilbert_plugin_javascript|safe }},
262                 {% endif %}{% endfor %}],
263         });
264 });
265 {% endif %}