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

382 lines
14 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({
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-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 = {
'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-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({
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),
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.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-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('Are you sure you want to logout?')
),
fixedSize: true,
height: 128,
2011-05-25 19:42:45 +00:00
keys: {enter: 'logout', escape: 'cancel'},
title: 'Logout',
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();
}),
{},
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;
};