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

357 lines
13 KiB
JavaScript
Raw Normal View History

2011-05-25 19:42:45 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=js
pandora.ui.accountDialog = function(action) {
2011-06-19 17:49:25 +00:00
var that = Ox.Dialog($.extend({
2011-05-25 19:42:45 +00:00
height: 256,
id: 'accountDialog',
minHeight: 256,
minWidth: 384,
width: 384
}, pandora.ui.accountDialogOptions(action)))
.bindEvent({
resize: function(event, data) {
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 = {
login: ['register', 'reset'],
register: ['login'],
reset: ['login'],
resetAndLogin: []
},
buttonTitle = {
login: 'Login',
register: 'Register',
reset: 'Reset Password',
resetAndLogin: 'Reset Password and Login'
},
dialogText = {
login: 'To login to your account, please enter your username and password.',
register: 'To create a new account, please choose a username and password, and enter your e-mail address.',
reset: 'To reset your password, please enter either your username or your e-mail address.',
resetAndLogin: 'To login to your account, please choose a new password, and enter the code that we have just e-mailed to you.'
},
dialogTitle = {
login: 'Login',
register: 'Register',
reset: 'Reset Password',
resetAndLogin: 'Reset Password'
};
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-06-06 15:48:11 +00:00
pandora.$ui.accountDialog.options(ui.accountDialogOptions(type));
2011-05-25 19:42:45 +00:00
});
}
}
return {
buttons: [
$.map(buttons[action], function(type) {
return button(type);
}),
[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-06-19 17:49:25 +00:00
Ox.Element()
2011-05-25 19:42:45 +00:00
.addClass('OxText')
.html(dialogText[action] + '<br/><br/>')
)
.append(
2011-06-06 15:48:11 +00:00
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 = {
'login': ['username', 'password'],
'register': ['newUsername', 'password', 'email'],
'reset': ['usernameOrEmail'],
'resetAndLogin': ['oldUsername', 'newPassword', 'code']
},
$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 == 'login') {
pandora.api.signin(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: 'password', message: 'Incorrect password'}]);
}
});
} else if (action == 'register') {
pandora.api.signup(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);
pandora.ui.accountWelcomeDialog().open();
} else {
callback([{id: 'password', message: result.data.errors.toString()}]); // fixme
}
});
} else if (action == 'reset') {
var usernameOrEmail = data.usernameOrEmail,
key = usernameOrEmail[0].id;
data = {};
data[key] = usernameOrEmail[1];
pandora.api.requestToken(data, function(result) {
if (!result.data.errors) {
2011-06-06 15:48:11 +00:00
pandora.$ui.accountDialog.options(ui.accountDialogOptions('resetAndLogin', 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 == 'resetAndLogin') {
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({
submit: function(event, data) {
},
validate: function(event, data) {
//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-05-25 19:42:45 +00:00
autovalidate: autovalidateCode,
id: 'code',
label: 'Code',
labelWidth: 120,
validate: function(value, callback) {
callback({
message: 'Missing code',
valid: !!value.length
});
},
width: 352
});
} else if (type == 'email') {
2011-06-19 17:49:25 +00:00
return Ox.Input({
2011-05-25 19:42:45 +00:00
autovalidate: autovalidateEmail,
id: 'email',
label: 'E-Mail Address',
labelWidth: 120,
type: 'email',
validate: pandora.validateUser('email'),
width: 352
});
} 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
});
},
width: 352
});
} 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'),
width: 352
});
} 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,
width: 352
});
} 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
});
},
width: 352
});
} 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),
width: 352
});
} 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',
width: 120
})
.bindEvent({
change: function(event, data) {
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),
width: 232
})
],
separators: [
{title: '', width: 0}
]
});
}
}
return that;
};
pandora.ui.accountLogoutDialog = 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();
2011-06-06 15:48:11 +00:00
pandora.$ui.mainMenu.getItem('loginlogout').toggleTitle();
2011-05-25 19:42:45 +00:00
}),
2011-06-19 17:49:25 +00:00
Ox.Button({
2011-05-25 19:42:45 +00:00
id: 'logout',
title: 'Logout'
}).bindEvent('click', function() {
that.close();
pandora.api.signout({}, function(result) {
pandora.logout(result.data);
});
})
],
2011-06-19 17:49:25 +00:00
content: Ox.Element().html('Are you sure you want to logout?'),
2011-05-25 19:42:45 +00:00
height: 160,
keys: {enter: 'logout', escape: 'cancel'},
title: 'Logout',
width: 300
});
return that;
};
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-06-19 17:49:25 +00:00
Ox.Button({
2011-05-25 19:42:45 +00:00
id: 'preferences',
title: 'Preferences...'
}).bindEvent('click', function() {
that.close();
})
],
[
2011-06-19 17:49:25 +00:00
Ox.Button({
2011-05-25 19:42:45 +00:00
id: 'close',
title: 'Close'
}).bindEvent('click', function() {
that.close();
})
]
],
2011-06-19 17:49:25 +00:00
content: Ox.Element().html('Welcome, ' + pandora.user.username + '!<br/><br/>Your account has been created.'),
2011-05-25 19:42:45 +00:00
height: 160,
keys: {enter: 'close', escape: 'close'},
2011-06-06 15:48:11 +00:00
title: 'Welcome to ' + pandora.site.site.name,
2011-05-25 19:42:45 +00:00
width: 300
});
return that;
};