source: deluge/ui/web/js/Deluge.Login.js@ e837493

2.0.x develop extjs4-port
Last change on this file since e837493 was e837493, checked in by Damien Churchill <damoc@gmail.com>, 16 years ago

add basic session support

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2Script: deluge-login.js
3 Contains all objects and functions related to the login system.
4
5Copyright:
6 (C) Damien Churchill 2009 <damoxc@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, write to:
19 The Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor
21 Boston, MA 02110-1301, USA.
22*/
23
24(function(){
25 Ext.deluge.LoginWindow = Ext.extend(Ext.Window, {
26
27 firstShow: true,
28
29 constructor: function(config) {
30 config = Ext.apply({
31 layout: 'fit',
32 width: 300,
33 height: 120,
34 bodyStyle: 'padding: 10px 5px;',
35 buttonAlign: 'center',
36 closeAction: 'hide',
37 closable: false,
38 modal: true,
39 plain: true,
40 resizable: false,
41 title: _('Login'),
42 iconCls: 'x-deluge-login-window-icon'
43 }, config);
44 Ext.deluge.LoginWindow.superclass.constructor.call(this, config);
45 },
46
47 initComponent: function() {
48 Ext.deluge.LoginWindow.superclass.initComponent.call(this);
49 Deluge.Events.on('logout', this.onLogout, this);
50 this.on('show', this.onShow, this);
51 this.on('beforeshow', this.onBeforeShow, this);
52
53 this.addButton({
54 text: _('Login'),
55 handler: this.onLogin,
56 scope: this
57 });
58
59 this.loginForm = this.add({
60 xtype: 'form',
61 defaultType: 'textfield',
62 id: 'loginForm',
63 baseCls: 'x-plain',
64 labelWidth: 55,
65 items: [{
66 fieldLabel: _('Password'),
67 id: 'password',
68 name: 'password',
69 inputType: 'password',
70 anchor: '100%',
71 listeners: {
72 'specialkey': {
73 fn: this.onKey,
74 scope: this
75 }
76 }
77 }]
78 })
79 },
80
81 onKey: function(field, e) {
82 if (e.getKey() == 13) this.onLogin();
83 },
84
85 onLogin: function() {
86 var passwordField = this.loginForm.items.get('password');
87 Deluge.Client.web.login(passwordField.getValue(), {
88 success: function(result) {
89 if (result) {
90 Deluge.Events.fire('login');
91 this.hide();
92 passwordField.setRawValue('');
93 Deluge.UI.cookies.set("session", result);
94 } else {
95 Ext.MessageBox.show({
96 title: _('Login Failed'),
97 msg: _('You entered an incorrect password'),
98 buttons: Ext.MessageBox.OK,
99 modal: false,
100 fn: function() {
101 passwordField.focus();
102 },
103 icon: Ext.MessageBox.WARNING,
104 iconCls: 'x-deluge-icon-warning'
105 });
106 }
107 },
108 scope: this
109 });
110 },
111
112 onLogout: function() {
113 var session = Deluge.UI.cookies.get("session", false);
114 if (session) {
115 Deluge.Client.web.delete_session(session, {
116 success: function(result) {
117 Deluge.UI.cookies.set("session", false);
118 this.show();
119 },
120 scope: this
121 });
122 }
123 },
124
125 onBeforeShow: function() {
126 var session = Deluge.UI.cookies.get("session", false);
127 if (session) {
128 Deluge.Client.web.check_session(session, {
129 success: function(result) {
130 if (result) {
131 Deluge.Events.fire('login');
132 this.loginForm.items.get('password').setRawValue('');
133 this.hide();
134 } else {
135 Deluge.UI.cookies.set("session", false);
136 this.show();
137 }
138 },
139 failure: function(result) {
140 Deluge.UI.cookies.set("session", false);
141 this.show();
142 },
143 scope: this
144 });
145 return false;
146 }
147 },
148
149 onShow: function() {
150 var passwordField = this.loginForm.items.get('password');
151 passwordField.focus(false, 150);
152 }
153 });
154
155 Deluge.Login = new Ext.deluge.LoginWindow();
156})();
Note: See TracBrowser for help on using the repository browser.