This repository was archived by the owner on Sep 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_menu.js
More file actions
232 lines (219 loc) · 6.44 KB
/
account_menu.js
File metadata and controls
232 lines (219 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*globals Ext, SWorks */
/*jslint glovar: true, undef: true, nomen: true */
SWorks.CurrentUser = function() {
this.permissions = {};
this.loggedIn = false;
this.addEvents({
'loggedIn': true,
'loggedOut': true,
'permissionsChanged': true
});
this.on('loggedIn', this.load, this);
this.on('loggedOut', function() { this.setPermissions(null); }, this);
};
Ext.extend(SWorks.CurrentUser, Ext.util.Observable, {
setUrls: function(hash) {
this.permissionsUrl = hash.permissions;
this.logoutUrl = hash.logout;
},
isLoggedIn: function() {
return this.loggedIn;
},
logout: function(cb, scope) {
Ext.Ajax.request({
url: this.logoutUrl,
callback: function(options, success, response) {
if(success) {
SWorks.CurrentUser.fireEvent('loggedOut');
} else {
Ext.MessageBox.alert("Logout failed","There was an error when logging out. Please try again.");
}
if(cb) {
cb.call(scope || this, success, response);
}
},
scope: this
});
},
username: function() {
return this.permissions.username;
},
setPermissions: function(newdata) {
if(newdata) {
this.loggedIn = true;
} else {
this.loggedIn = false;
newdata = {};
}
this.permissions = newdata;
this.fireEvent('permissionsChanged', this.permissions);
},
load: function() {
Ext.Ajax.request({
url: this.permissionsUrl,
success: function(response) {
try {
var result = Ext.decode(response.responseText);
this.setPermissions(result);
} catch(err) {}
},
scope: this
});
},
has: function(perm) {
return (typeof perm == 'undefined' || this.permissions[perm] === true);
},
// The reason there is no straight permissions check is because
// they can change with time and you have to be ready for that.
onPermission: function(perm, callback, scope) {
var permCheck = function(p) {
if (typeof perm == 'undefined' || p[perm]) {
callback.call(scope, perm, true);
} else {
callback.call(scope, perm, false);
}
};
this.on('permissionsChanged', permCheck);
permCheck(this.permissions);
}
});
SWorks.CurrentUser = new SWorks.CurrentUser();
SWorks.AccountMenu = Ext.extend(Ext.Panel, {
initComponent: function() {
SWorks.AccountMenu.superclass.initComponent.call(this);
//This doesn't render yet, so it should be pretty quick
this.loginWindow = this.createLoginWindow();
if(this.loginRequired) {
SWorks.CurrentUser.on('loggedOut', function() {
this.loginWindow.show();
}, this);
}
},
afterRender : function(ct, position){
SWorks.AccountMenu.superclass.afterRender.call(this, ct, position);
//Set the panel to reload its content on login or logout
var mgr = this.getUpdater();
mgr.setDefaultUrl(this.menuUrl);
SWorks.CurrentUser.on('loggedIn', mgr.refresh, mgr);
SWorks.CurrentUser.on('loggedOut', mgr.refresh, mgr);
// Rewire the content when it gets updated
mgr.on('update', this.hookContent, this);
// Manually wire the content the first time
this.hookContent();
},
hookContent: function(){
// Wire the login link
var signinLink = Ext.get('signin-link');
if(signinLink) {
signinLink.on('click', function(e,t){
this.loginWindow.show(t.dom);
}, this);
}
// A panel should listen for the update on our updater and wire to the
// account-status element if it is there.
// Wire the logout link
var logoutLink = Ext.get('logout-link');
if(logoutLink) {
logoutLink.on('click', function(){
if(!this.loginWindow.rendered) {
this.loginWindow.render(Ext.getBody());
}
this.loginWindow.showMask();
SWorks.CurrentUser.logout(function(success, response) {
if (!success) {
this.loginWindow.hideMask();
}
}, this);
}, this);
}
},
createLoginWindow: function() {
var form = null, url = this.loginUrl;
var panel;
var win = new Ext.Window({
layout: 'fit',
width: 270,
height: 150,
modal: true,
closable: false,
resizable: false,
draggable: true,
collapsible: false,
title: 'Sign in',
autoCreate: true,
items: panel = new Ext.form.FormPanel({
labelAlign: 'right',
labelWidth: 68,
buttonAlign: 'right',
bodyStyle: 'padding:5px',
items: [
new Ext.form.TextField({
id: 'login-user-name-field',
fieldLabel: 'Username',
name: 'login',
allowBlank: false,
width: 140
}),
new Ext.form.TextField({
id: 'password-field',
fieldLabel: 'Password',
name: 'password',
inputType: 'password',
allowBlank: false,
width: 140
})
],
login: function() {
if(win.submitLock) {
return;
}
win.submitLock = true;
win.keyMap.disable();
panel.form.submit({
url: url,
waitMsg: 'Please wait...',
success: function(form, action) {
win.submitLock = false;
win.hide();
SWorks.CurrentUser.fireEvent("loggedIn");
},
failure: function(form, action) {
SWorks.ErrorHandling.serverError(action.response, action.result);
// TODO the field focus isn't working
win.submitLock = false;
win.keyMap.enable();
panel.form.reset();
panel.form.items.item(0).focus();
}
});
}
}),
buttons: [{
id: 'signin-submit-btn',
text: "Sign in",
handler: function() {
panel.login();
}
}, {
text: "Cancel",
disabled: this.loginRequired,
handler: function() {
win.hide();
}
}],
keys: [
{ key: 27, fn: (this.loginRequired ? Ext.emptyFn : function() { win.hide(); }) },
{ key: Ext.EventObject.ENTER, fn: function() { panel.login(); }}
]
});
// TODO all dialog focus attempts in the whole app are broken
win.on('show', function() {
panel.form.items.item(0).on('blur', function(field){
var test = 0;
});
panel.form.reset();
panel.form.items.item(0).focus();
});
return win;
}
});