From fbed9b12ccd59b4a41a6f7b1d921c476c32e8773 Mon Sep 17 00:00:00 2001 From: rolux Date: Sat, 22 Oct 2011 14:50:53 +0000 Subject: [PATCH] update account and preferences dialogs --- static/js/pandora/UI.js | 7 ++ static/js/pandora/autovalidate.js | 78 ++++++++++++++--------- static/js/pandora/ui/account.js | 13 ++-- static/js/pandora/ui/preferencesDialog.js | 66 +++++++++++++++---- 4 files changed, 114 insertions(+), 50 deletions(-) diff --git a/static/js/pandora/UI.js b/static/js/pandora/UI.js index 2ac8afd1a..c176c4523 100644 --- a/static/js/pandora/UI.js +++ b/static/js/pandora/UI.js @@ -14,6 +14,13 @@ pandora.UI = (function() { return !key ? self.previousUI : self.previousUI[key]; }; + that.reset = function() { + pandora.user.ui = pandora.site.user.ui; + pandora.user.ui._list = pandora.getListsState(pandora.user.ui.find); + pandora.user.ui._groupsState = pandora.getGroupsState(pandora.user.ui.find); + pandora.user.ui._findState = pandora.getFindState(pandora.user.ui.find); + }; + // sets pandora.user.ui.key to val // key foo.bar.baz sets pandora.user.ui.foo.bar.baz // val null removes a key diff --git a/static/js/pandora/autovalidate.js b/static/js/pandora/autovalidate.js index 2a8ec29dc..3d0dd3f02 100644 --- a/static/js/pandora/autovalidate.js +++ b/static/js/pandora/autovalidate.js @@ -1,51 +1,69 @@ // vim: et:ts=4:sw=4:sts=4:ft=javascript + pandora.autovalidateCode = function(value, blur, callback) { - value = value.toUpperCase().split('').map(function(v) { - return /[0-9A-Z]/.test(v) ? v : null; - }).join(''); - callback(value); + value = value.split('').map(function(v) { + return /[A-Z]/.test(v) ? v : null; + }).join('').substr(0, 16); + callback({valid: value.length == 16, value: value}); }; pandora.autovalidateEmail = function(value, blur, callback) { value = value.toLowerCase().split('').map(function(v, i) { return /[0-9a-z\.\+\-_@]/.test(v) ? v : null; - }).join(''); - callback(value); + }).join('').substr(0, 255); + callback({valid: Ox.isValidEmail(value), value: value}); }; pandora.autovalidateListname = function(value, blur, callback) { + // A valid listname consists of 1 to 255 unicode characters, + // without leading, trailing or consecutive spaces var length = value.length; - value = value.split('').map(function(v, i) { - if (new RegExp('[0-9' + Ox.regexp.letters + '\\(\\)' + ((i == 0 || (i == length - 1 && blur)) ? '' : ' \-') + ']', 'i').test(v)) { - return v; - } else { - return null; - } + value = value.toLowerCase().split('').map(function(v, i) { + return /\s/.test(v) && (i == 0 || (i == length - 1 && blur)) ? null : v; }).join(''); - [' ', ' -', '- ', '--', '\\(\\(', '\\)\\(', '\\)\\)'].forEach(function(v) { - //Ox.print(v, v[0], v[0] == '\\') - while (value.indexOf(v) > -1) { - value = value.replace(new RegExp(v, 'g'), v[0] + (v[0] == '\\' ? v[1] : '')); - } - }); - callback(value); + value = value.replace(/\s+/g, ' ').substr(0, 255); + callback({valid: !!value.length, value: value}); }; pandora.autovalidateUsername = function(value, blur, callback) { + // A valid username consists of 1 to 255 unicode characters, + // without leading, trailing or consecutive spaces var length = value.length; value = value.toLowerCase().split('').map(function(v, i) { - if (new RegExp('[0-9a-z' + ((i == 0 || (i == length - 1 && blur)) ? '' : '\-_') + ']').test(v)) { - return v; - } else { - return null; - } + return /\s/.test(v) && (i == 0 || (i == length - 1 && blur)) ? null : v; }).join(''); - ['--', '-_', '_-', '__'].forEach(function(v) { - while (value.indexOf(v) > -1) { - value = value.replace(new RegExp(v, 'g'), v[0]); - } + value = value.replace(/\s+/g, ' ').substr(0, 255); + callback({valid: !!value.length, value: value}); +}; + +pandora.validateNewEmail = function(value, callback) { + value == pandora.user.email ? callback({ + message: '', + valid: true, + value: value + }) : Ox.isValidEmail(value) ? pandora.api.findUser({ + key: 'email', + value: value, + operator: '==' + }, function(result) { + callback({ + message: 'E-mail address already exists', + valid: !result.data.users.length, + value: value + }); + }) : callback({ + message: (!value.length ? 'Missing' : 'Invalid') + ' e-mail address', + valid: false, + value: value + }); +}; + +pandora.validateNewPassword = function(value, callback) { + callback({ + message: 'Missing password', + valid: value.length > 0, + value: value }); - callback(value); }; pandora.validateUser = function(key, existing) { @@ -56,7 +74,7 @@ pandora.validateUser = function(key, existing) { valid ? pandora.api.findUser({ key: key, value: value, - operator: '=' + operator: '==' }, function(result) { var valid = existing == !!result.data.users.length; //Ox.print(existing, result.data.users) diff --git a/static/js/pandora/ui/account.js b/static/js/pandora/ui/account.js index ac403e801..bd7b4d0ee 100644 --- a/static/js/pandora/ui/account.js +++ b/static/js/pandora/ui/account.js @@ -215,12 +215,7 @@ pandora.ui.accountForm = function(action, value) { label: 'New Password', labelWidth: 120, type: 'password', - validate: function(value, callback) { - callback({ - message: 'Missing password', - valid: value.length > 0 - }); - }, + validate: pandora.validateNewPassword, width: 320 }); } else if (type == 'newUsername') { @@ -282,10 +277,12 @@ pandora.ui.accountForm = function(action, value) { change: function(data) { var selected = data.selected[0].id; pandora.$ui.usernameOrEmailInput.options({ - autovalidate: selected == 'username' ? pandora.autovalidateUsername : autovalidateEmail, - validate: validateUser(selected, true), + autovalidate: selected == 'username' + ? pandora.autovalidateUsername : pandora.autovalidateEmail, + validate: pandora.validateUser(selected, true), value: '' }).focus(); + pandora.$ui.accountDialog.disableButton('submitReset'); } }), pandora.$ui.usernameOrEmailInput = Ox.Input({ diff --git a/static/js/pandora/ui/preferencesDialog.js b/static/js/pandora/ui/preferencesDialog.js index 2b2e76a8b..23dfcffba 100644 --- a/static/js/pandora/ui/preferencesDialog.js +++ b/static/js/pandora/ui/preferencesDialog.js @@ -26,24 +26,66 @@ pandora.ui.preferencesDialog = function() { width: 320 }), Ox.Input({ - id: 'password', - label: 'New Passowrd', - labelWidth: 120, - type: 'password', - width: 320 - }), + autovalidate: /.+/, + id: 'password', + label: 'New Password', + labelWidth: 120, + type: 'password', + validate: pandora.validateNewPassword, + width: 320 + }) + .bindEvent({ + validate: function(data) { + data.valid && pandora.api.editPreferences({password: data.value}); + } + }), Ox.Input({ - id: 'email', - label: 'E-Mail Address', - labelWidth: 120, - value: pandora.user.email, - width: 320 - }) + autovalidate: pandora.autovalidateEmail, + id: 'email', + label: 'E-Mail Address', + labelWidth: 120, + validate: pandora.validateNewEmail, + value: pandora.user.email, + width: 320 + }) + .bindEvent({ + validate: function(data) { + if (data.valid && data.value != pandora.user.email) { + pandora.user.email = data.value; + pandora.api.editPreferences({email: data.value}); + } + } + }) ] }) .css({position: 'absolute', left: '96px', top: '16px'}) + .bindEvent({ + change: function(data) { + return; + var preferences = {}; + preferences[data.id] = data.data.value; + pandora.api.editPreferences(preferences, function(result) { + if (data.id == 'email') { + pandora.user.email = data.data.value; + } + }); + } + }) ); } else { + $content.append( + Ox.Button({ + title: 'Reset UI Settings', + width: 150 + }) + .bindEvent({ + click: function() { + pandora.UI.reset(); + pandora.$ui.appPanel.reload(); + } + }) + .css({position: 'absolute', left: '96px', top: '16px'}) + ); /* content.append(Ox.FormElementGroup({ elements: [