pandora/static/js/pandora/ui/account.js

384 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
pandora.ui.accountDialog = function(action) {
2011-06-19 17:49:25 +00:00
var that = Ox.Dialog($.extend({
2011-08-17 11:39:55 +00:00
fixedSize: true,
height: 192,
2011-05-25 19:42:45 +00:00
id: 'accountDialog',
2011-08-17 11:39:55 +00:00
width: 432
2011-05-25 19:42:45 +00:00
}, pandora.ui.accountDialogOptions(action)))
.bindEvent({
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) {
//Ox.print('ACTION', action)
2011-06-06 15:48:11 +00:00
pandora.$ui.accountForm && pandora.$ui.accountForm.removeElement();
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',
resetAndSignin: 'Reset Password and 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() {
2011-06-06 15:48:11 +00:00
pandora.$ui.accountDialog.close();
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() {
//Ox.print('CLICK EVENT', type)
2011-08-17 11:39:55 +00:00
pandora.$ui.accountDialog.options(pandora.ui.accountDialogOptions(type));
2011-05-25 19:42:45 +00:00
});
}
}
return {
2011-08-17 11:39:55 +00:00
buttons: Ox.merge($.map(buttons[action], function(type) {
2011-05-25 19:42:45 +00:00
return button(type);
2011-08-17 11:39:55 +00:00
}), [{}, 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/icon64.png'})
.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') {
//Ox.print('REMOVING')
//Ox.Event.unbind('usernameOrEmailSelect')
//Ox.Event.unbind('usernameOrEmailSelectMenu')
//Ox.Event.unbind('usernameOrEmailInput')
}
//Ox.print('REMOVING ITEM', item.options('id'));
item.removeElement();
});
}
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 = $.map(items[action], function(v) {
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) {
if (action == 'signin') {
2011-05-25 19:42:45 +00:00
pandora.api.signin(data, function(result) {
if (!result.data.errors) {
2011-06-06 15:48:11 +00:00
pandora.$ui.accountDialog.close();
pandora.signin(result.data);
2011-05-25 19:42:45 +00:00
} else {
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) {
2011-06-06 15:48:11 +00:00
pandora.$ui.accountDialog.close();
pandora.signin(result.data);
2011-05-25 19:42:45 +00:00
pandora.ui.accountWelcomeDialog().open();
} else {
callback([{id: 'password', message: result.data.errors.toString()}]); // fixme
}
});
} else if (action == 'reset') {
Ox.print('DATA???', data)
2011-05-25 19:42:45 +00:00
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));
2011-05-25 19:42:45 +00:00
} else {
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) {
2011-06-06 15:48:11 +00:00
pandora.$ui.accountDialog.close();
2011-05-25 19:42:45 +00:00
pandora.login(result.data);
} else {
callback([{id: 'code', message: 'Incorrect code'}]);
}
})
}
}
}).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-05-25 19:42:45 +00:00
//Ox.print('FORM VALIDATE', data)
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: function(value, callback) {
callback({
message: 'Missing code',
valid: !!value.length
});
},
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,
type: 'email',
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',
validate: function(value, callback) {
callback({
message: 'Missing password',
valid: value.length > 0
});
},
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: function(value, callback) {
callback({
message: 'Missing Password',
valid: value.length > 0
});
},
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-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-05-25 19:42:45 +00:00
var selected = data.selected[0].id;
2011-06-06 15:48:11 +00:00
pandora.$ui.usernameOrEmailInput.options({
2011-05-25 19:42:45 +00:00
autovalidate: selected == 'username' ? pandora.autovalidateUsername : autovalidateEmail,
validate: validateUser(selected, true),
value: ''
}).focus();
}
}),
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}
]
});
}
}
return that;
};
pandora.ui.accountSignoutDialog = function() {
2011-06-19 17:49:25 +00:00
var that = Ox.Dialog({
2011-05-25 19:42:45 +00:00
buttons: [
2011-06-19 17:49:25 +00:00
Ox.Button({
2011-05-25 19:42:45 +00:00
id: 'cancel',
title: 'Cancel'
}).bindEvent('click', function() {
that.close();
pandora.$ui.mainMenu.getItem('signinsignout').toggleTitle();
2011-05-25 19:42:45 +00:00
}),
2011-06-19 17:49:25 +00:00
Ox.Button({
id: 'signout',
title: 'Sign Out'
2011-05-25 19:42:45 +00:00
}).bindEvent('click', function() {
that.close();
pandora.api.signout({}, function(result) {
pandora.signout(result.data);
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/icon64.png'})
.css({position: 'absolute', left: '16px', top: '16px', width: '64px', height: '64px'})
)
.append(
2011-09-01 04:46:44 +00:00
$('<div>')
2011-08-17 11:39:55 +00:00
.css({position: 'absolute', left: '96px', top: '16px', width: '192px'})
.html('Are you sure you want to sign out?')
2011-08-17 11:39:55 +00:00
),
fixedSize: true,
height: 128,
keys: {enter: 'signout', escape: 'cancel'},
title: 'Sign Out',
2011-08-17 11:39:55 +00:00
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-05-25 19:42:45 +00:00
],
2011-08-17 11:39:55 +00:00
content: Ox.Element()
.append(
$('<img>')
.attr({src: '/static/png/icon64.png'})
.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, ' + pandora.user.username + '!<br/><br/>Your account has been created.')
),
fixedSize: true,
height: 128,
2011-05-25 19:42:45 +00:00
keys: {enter: 'close', escape: 'close'},
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;
};