pandora/static/js/pandora/account.js

398 lines
15 KiB
JavaScript
Raw Normal View History

2011-07-29 18:37:11 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-05-25 19:42:45 +00:00
2011-11-05 17:04:10 +00:00
'use strict';
2011-05-25 19:42:45 +00:00
pandora.ui.accountDialog = function(action) {
var that = Ox.Dialog(Ox.extend({
2011-08-17 11:39:55 +00:00
fixedSize: true,
height: 192,
2011-05-25 19:42:45 +00:00
id: 'accountDialog',
removeOnClose: true,
2011-08-17 11:39:55 +00:00
width: 432
2011-05-25 19:42:45 +00:00
}, pandora.ui.accountDialogOptions(action)))
.bindEvent({
2011-10-29 10:35:39 +00:00
open: function() {
pandora.$ui.accountForm.find('input')[0].focus();
},
2011-09-17 17:40:15 +00:00
resize: function(data) {
2011-05-25 19:42:45 +00:00
var width = data.width - 32;
2011-06-06 15:48:11 +00:00
pandora.$ui.accountForm.items.forEach(function(item) {
2011-05-25 19:42:45 +00:00
item.options({width: width});
});
}
});
return that;
};
pandora.ui.accountDialogOptions = function(action, value) {
2011-11-04 15:54:42 +00:00
//Ox.Log('', 'ACTION', action)
pandora.$ui.accountForm && pandora.$ui.accountForm.remove();
2011-05-25 19:42:45 +00:00
var buttons = {
signin: ['signup', 'reset'],
signup: ['signin'],
reset: ['signin'],
resetAndSignin: []
2011-05-25 19:42:45 +00:00
},
buttonTitle = {
signin: 'Sign In',
signup: 'Sign Up',
2011-05-25 19:42:45 +00:00
reset: 'Reset Password',
2011-10-22 15:31:12 +00:00
resetAndSignin: 'Sign In'
2011-05-25 19:42:45 +00:00
},
dialogText = {
signin: 'To sign in to your account, please enter your username and password.',
signup: 'To sign up for an account, please choose a username and password, and enter your e-mail address.',
2011-05-25 19:42:45 +00:00
reset: 'To reset your password, please enter either your username or your e-mail address.',
resetAndSignin: 'To sign in to your account, please choose a new password, and enter the code that we have just e-mailed to you.'
2011-05-25 19:42:45 +00:00
},
dialogTitle = {
signin: 'Sign In',
signup: 'Sign Up',
2011-05-25 19:42:45 +00:00
reset: 'Reset Password',
resetAndSignin: 'Reset Password'
2011-05-25 19:42:45 +00:00
};
function button(type) {
if (type == 'cancel') {
2011-06-19 17:49:25 +00:00
return Ox.Button({
2011-05-25 19:42:45 +00:00
id: 'cancel' + Ox.toTitleCase(action),
title: 'Cancel'
}).bindEvent('click', function() {
pandora.$ui.accountDialog.close();
2011-11-09 22:32:54 +00:00
pandora.UI.set({page: ''});
2011-05-25 19:42:45 +00:00
});
} else if (type == 'submit') {
2011-06-19 17:49:25 +00:00
return Ox.Button({
2011-05-25 19:42:45 +00:00
disabled: true,
id: 'submit' + Ox.toTitleCase(action),
title: buttonTitle[action]
}).bindEvent('click', function() {
2011-06-06 15:48:11 +00:00
pandora.$ui.accountForm.submit();
2011-05-25 19:42:45 +00:00
});
} else {
2011-06-19 17:49:25 +00:00
return Ox.Button({
2011-05-25 19:42:45 +00:00
id: type,
title: buttonTitle[type] + '...'
}).bindEvent('click', function() {
2011-12-22 15:48:48 +00:00
if (['signin', 'signup'].indexOf(type) > -1 && type != pandora.user.ui.page) {
2011-11-09 22:32:54 +00:00
pandora.UI.set({page: type});
} else {
pandora.$ui.accountDialog.options(pandora.ui.accountDialogOptions(type));
}
pandora.$ui.accountForm.find('input.OxInput')[0].focus();
2011-05-25 19:42:45 +00:00
});
}
}
2011-10-07 17:56:39 +00:00
2011-05-25 19:42:45 +00:00
return {
2011-10-22 15:31:12 +00:00
buttons: Ox.merge(
buttons[action].map(function(type) {
2011-05-25 19:42:45 +00:00
return button(type);
2011-10-22 15:31:12 +00:00
}),
buttons[action].length ? [{}] : [],
[button('cancel'), button('submit')]
),
2011-06-19 17:49:25 +00:00
content: Ox.Element()
2011-05-25 19:42:45 +00:00
.append(
2011-08-17 11:39:55 +00:00
$('<img>')
.attr({src: '/static/png/icon.png'})
2011-08-17 11:39:55 +00:00
.css({position: 'absolute', left: '16px', top: '16px', width: '64px', height: '64px'})
2011-05-25 19:42:45 +00:00
)
.append(
2011-08-17 11:39:55 +00:00
Ox.Element()
.css({position: 'absolute', left: '96px', top: '16px', width: '320px'})
.append(
Ox.Element()
.addClass('OxText')
.html(dialogText[action] + '<br/><br/>')
)
.append(
pandora.$ui.accountForm = pandora.ui.accountForm(action, value)
)
2011-05-25 19:42:45 +00:00
),
keys: {
enter: 'submit' + Ox.toTitleCase(action),
escape: 'cancel' + Ox.toTitleCase(action)
},
title: dialogTitle[action]
};
};
pandora.ui.accountForm = function(action, value) {
2011-06-06 15:48:11 +00:00
if (pandora.$ui.accountForm) {
pandora.$ui.accountForm.items.forEach(function(item) {
2011-05-25 19:42:45 +00:00
if (item.options('id') == 'usernameOrEmail') {
2011-11-04 15:54:42 +00:00
//Ox.Log('', 'REMOVING')
2011-05-25 19:42:45 +00:00
//Ox.Event.unbind('usernameOrEmailSelect')
//Ox.Event.unbind('usernameOrEmailSelectMenu')
//Ox.Event.unbind('usernameOrEmailInput')
}
2011-11-04 15:54:42 +00:00
//Ox.Log('', 'REMOVING ITEM', item.options('id'));
item.remove();
2011-05-25 19:42:45 +00:00
});
}
var items = {
'signin': ['username', 'password'],
'signup': ['newUsername', 'password', 'email'],
2011-05-25 19:42:45 +00:00
'reset': ['usernameOrEmail'],
'resetAndSignin': ['oldUsername', 'newPassword', 'code']
2011-05-25 19:42:45 +00:00
},
$items = items[action].map(function(v) {
2011-05-25 19:42:45 +00:00
return item(v, value);
}),
2011-06-19 17:49:25 +00:00
that = Ox.Form({
2011-05-25 19:42:45 +00:00
id: 'accountForm' + Ox.toTitleCase(action),
items: $items,
submit: function(data, callback) {
2011-10-29 10:35:39 +00:00
pandora.$ui.accountDialog.disableButtons();
if (action == 'signin') {
2011-05-25 19:42:45 +00:00
pandora.api.signin(data, function(result) {
if (!result.data.errors) {
pandora.$ui.accountDialog.close();
pandora.signin(result.data);
2011-05-25 19:42:45 +00:00
} else {
2011-10-29 10:35:39 +00:00
pandora.$ui.accountDialog.enableButtons();
2011-05-25 19:42:45 +00:00
callback([{id: 'password', message: 'Incorrect password'}]);
}
});
} else if (action == 'signup') {
2011-05-25 19:42:45 +00:00
pandora.api.signup(data, function(result) {
if (!result.data.errors) {
pandora.$ui.accountDialog.close();
pandora.signin(result.data);
2011-05-25 19:42:45 +00:00
pandora.ui.accountWelcomeDialog().open();
} else {
2011-10-29 10:35:39 +00:00
pandora.$ui.accountDialog.enableButtons();
2011-05-25 19:42:45 +00:00
callback([{id: 'password', message: result.data.errors.toString()}]); // fixme
}
});
} else if (action == 'reset') {
var usernameOrEmail = data.usernameOrEmail,
key = usernameOrEmail[0];
2011-05-25 19:42:45 +00:00
data = {};
data[key] = usernameOrEmail[1];
pandora.api.requestToken(data, function(result) {
if (!result.data.errors) {
pandora.$ui.accountDialog.options(pandora.ui.accountDialogOptions('resetAndSignin', result.data.username));
pandora.$ui.accountDialog.enableButtons();
2011-05-25 19:42:45 +00:00
} else {
2011-10-29 10:35:39 +00:00
pandora.$ui.accountDialog.enableButtons();
2011-05-25 19:42:45 +00:00
callback([{id: 'usernameOrEmail', message: 'Unknown ' + (key == 'username' ? 'username' : 'e-mail address')}])
}
});
} else if (action == 'resetAndSignin') {
2011-05-25 19:42:45 +00:00
pandora.api.resetPassword(data, function(result) {
if (!result.data.errors) {
pandora.$ui.accountDialog.close();
2011-10-07 17:56:39 +00:00
pandora.signin(result.data);
2011-05-25 19:42:45 +00:00
} else {
2011-10-29 10:35:39 +00:00
pandora.$ui.accountDialog.enableButtons();
2011-05-25 19:42:45 +00:00
callback([{id: 'code', message: 'Incorrect code'}]);
}
2011-10-07 17:56:39 +00:00
});
2011-05-25 19:42:45 +00:00
}
}
}).bindEvent({
2011-09-17 17:40:15 +00:00
submit: function(data) {
2011-05-25 19:42:45 +00:00
},
2011-09-17 17:40:15 +00:00
validate: function(data) {
2011-11-04 15:54:42 +00:00
Ox.Log('', 'FORM VALIDATE', data, action, 'submit' + Ox.toTitleCase(action));
2011-06-06 15:48:11 +00:00
pandora.$ui.accountDialog[
2011-05-25 19:42:45 +00:00
(data.valid ? 'enable' : 'disable') + 'Button'
]('submit' + Ox.toTitleCase(action));
}
});
that.items = $items;
function item(type, value) {
if (type == 'code') {
2011-06-19 17:49:25 +00:00
return Ox.Input({
2011-08-17 11:39:55 +00:00
autovalidate: pandora.autovalidateCode,
2011-05-25 19:42:45 +00:00
id: 'code',
label: 'Code',
labelWidth: 120,
validate: pandora.validateCode,
2011-08-17 11:39:55 +00:00
width: 320
2011-05-25 19:42:45 +00:00
});
} else if (type == 'email') {
2011-06-19 17:49:25 +00:00
return Ox.Input({
2011-08-17 11:39:55 +00:00
autovalidate: pandora.autovalidateEmail,
2011-05-25 19:42:45 +00:00
id: 'email',
label: 'E-Mail Address',
labelWidth: 120,
2011-11-02 17:26:08 +00:00
type: 'email', // fixme: ??
2011-05-25 19:42:45 +00:00
validate: pandora.validateUser('email'),
2011-08-17 11:39:55 +00:00
width: 320
2011-05-25 19:42:45 +00:00
});
} else if (type == 'newPassword') {
2011-06-19 17:49:25 +00:00
return Ox.Input({
2011-05-25 19:42:45 +00:00
autovalidate: /.+/,
id: 'password',
label: 'New Password',
labelWidth: 120,
type: 'password',
2011-10-22 14:50:53 +00:00
validate: pandora.validateNewPassword,
2011-08-17 11:39:55 +00:00
width: 320
2011-05-25 19:42:45 +00:00
});
} else if (type == 'newUsername') {
2011-06-19 17:49:25 +00:00
return Ox.Input({
2011-05-25 19:42:45 +00:00
autovalidate: pandora.autovalidateUsername,
id: 'username',
label: 'Username',
labelWidth: 120,
validate: pandora.validateUser('username'),
2011-08-17 11:39:55 +00:00
width: 320
2011-05-25 19:42:45 +00:00
});
} else if (type == 'oldUsername') {
2011-06-19 17:49:25 +00:00
return Ox.Input({
2011-05-25 19:42:45 +00:00
disabled: true,
id: 'username',
label: 'Username',
labelWidth: 120,
value: value,
2011-08-17 11:39:55 +00:00
width: 320
2011-05-25 19:42:45 +00:00
});
} else if (type == 'password') {
2011-06-19 17:49:25 +00:00
return Ox.Input({
2011-05-25 19:42:45 +00:00
autovalidate: /.+/,
id: 'password',
label: 'Password',
labelWidth: 120,
type: 'password',
validate: pandora.validatePassword,
2011-08-17 11:39:55 +00:00
width: 320
2011-05-25 19:42:45 +00:00
});
} else if (type == 'username') {
2011-06-19 17:49:25 +00:00
return Ox.Input({
2011-05-25 19:42:45 +00:00
autovalidate: pandora.autovalidateUsername,
id: 'username',
label: 'Username',
labelWidth: 120,
validate: pandora.validateUser('username', true),
2011-08-17 11:39:55 +00:00
width: 320
2011-10-29 17:46:46 +00:00
});
2011-05-25 19:42:45 +00:00
} else if (type == 'usernameOrEmail') {
2011-06-19 17:49:25 +00:00
return Ox.FormElementGroup({
2011-05-25 19:42:45 +00:00
id: 'usernameOrEmail',
elements: [
2011-06-19 17:49:25 +00:00
pandora.$ui.usernameOrEmailSelect = Ox.Select({
2011-05-25 19:42:45 +00:00
id: 'usernameOrEmailSelect',
items: [
{id: 'username', title: 'Username'},
{id: 'email', title: 'E-Mail Address'},
],
overlap: 'right',
2011-08-17 11:39:55 +00:00
width: 128
2011-05-25 19:42:45 +00:00
})
.bindEvent({
2011-09-17 17:40:15 +00:00
change: function(data) {
2011-06-06 15:48:11 +00:00
pandora.$ui.usernameOrEmailInput.options({
2011-12-21 15:34:28 +00:00
autovalidate: data.value == 'username'
2011-10-22 14:50:53 +00:00
? pandora.autovalidateUsername : pandora.autovalidateEmail,
2011-12-21 15:34:28 +00:00
validate: pandora.validateUser(data.value, true),
2011-05-25 19:42:45 +00:00
value: ''
2011-12-18 09:44:31 +00:00
}).focusInput(true);
that.$element.find('.OxFormMessage:visible').html('').hide();
2011-10-22 14:50:53 +00:00
pandora.$ui.accountDialog.disableButton('submitReset');
2011-05-25 19:42:45 +00:00
}
}),
2011-06-19 17:49:25 +00:00
pandora.$ui.usernameOrEmailInput = Ox.Input({
2011-05-25 19:42:45 +00:00
autovalidate: pandora.autovalidateUsername,
id: 'usernameOrEmailInput',
validate: pandora.validateUser('username', true),
2011-08-17 11:39:55 +00:00
width: 192
2011-05-25 19:42:45 +00:00
})
],
separators: [
{title: '', width: 0}
]
});
}
}
2011-10-29 17:46:46 +00:00
return that;
2011-05-25 19:42:45 +00:00
};
pandora.ui.accountSignoutDialog = function() {
2011-06-19 17:49:25 +00:00
var that = Ox.Dialog({
2011-10-08 15:16:22 +00:00
buttons: [
Ox.Button({
id: 'stay',
title: 'Stay Signed In'
2011-10-08 15:16:22 +00:00
}).bindEvent('click', function() {
that.close();
2011-11-10 08:42:05 +00:00
pandora.UI.set({page: ''});
2011-10-08 15:16:22 +00:00
}),
Ox.Button({
id: 'signout',
title: 'Sign Out'
}).bindEvent('click', function() {
that.close();
pandora.UI.set({page: ''});
2011-10-08 15:16:22 +00:00
pandora.api.signout({}, function(result) {
pandora.signout(result.data);
});
})
],
content: Ox.Element()
.append(
$('<img>')
.attr({src: '/static/png/icon.png'})
2011-10-08 15:16:22 +00:00
.css({position: 'absolute', left: '16px', top: '16px', width: '64px', height: '64px'})
)
.append(
$('<div>')
.css({position: 'absolute', left: '96px', top: '16px', width: '192px'})
.html('Are you sure you want to sign out?')
),
fixedSize: true,
height: 128,
keys: {enter: 'signout', escape: 'stay'},
removeOnClose: true,
2011-10-08 15:16:22 +00:00
title: 'Sign Out',
width: 304
});
2011-05-25 19:42:45 +00:00
return that;
2011-08-17 11:39:55 +00:00
};
2011-05-25 19:42:45 +00:00
pandora.ui.accountWelcomeDialog = function() {
2011-06-19 17:49:25 +00:00
var that = Ox.Dialog({
2011-05-25 19:42:45 +00:00
buttons: [
2011-08-17 11:39:55 +00:00
Ox.Button({
id: 'preferences',
title: 'Preferences...'
}).bindEvent('click', function() {
that.close();
pandora.$ui.preferencesDialog = pandora.ui.preferencesDialog().open();
2011-08-17 11:39:55 +00:00
}),
{},
Ox.Button({
id: 'close',
title: 'Close'
}).bindEvent('click', function() {
that.close();
2011-08-17 11:39:55 +00:00
})
2011-05-25 19:42:45 +00:00
],
2011-08-17 11:39:55 +00:00
content: Ox.Element()
.append(
$('<img>')
.attr({src: '/static/png/icon.png'})
2011-08-17 11:39:55 +00:00
.css({position: 'absolute', left: '16px', top: '16px', width: '64px', height: '64px'})
)
.append(
Ox.Element()
.css({position: 'absolute', left: '96px', top: '16px', width: '192px'})
.html(
'Welcome, ' + Ox.encodeHTMLEntities(pandora.user.username)
+ '!<br/><br/>Your account has been created.'
)
2011-08-17 11:39:55 +00:00
),
fixedSize: true,
height: 128,
2011-05-25 19:42:45 +00:00
keys: {enter: 'close', escape: 'close'},
removeOnClose: true,
2011-06-06 15:48:11 +00:00
title: 'Welcome to ' + pandora.site.site.name,
2011-08-17 11:39:55 +00:00
width: 304
2011-05-25 19:42:45 +00:00
});
return that;
};