2014-08-31 23:22:56 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
|
|
|
App.Presenters.RegistrationPresenter = function(
|
2014-09-08 22:02:28 +02:00
|
|
|
_,
|
2014-08-31 23:22:56 +02:00
|
|
|
jQuery,
|
2014-09-02 09:36:42 +02:00
|
|
|
util,
|
2014-09-04 18:06:25 +02:00
|
|
|
promise,
|
2014-09-04 19:06:12 +02:00
|
|
|
api,
|
2014-08-31 23:22:56 +02:00
|
|
|
topNavigationPresenter,
|
2014-09-04 19:06:12 +02:00
|
|
|
messagePresenter) {
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
var $el = jQuery('#content');
|
2014-09-02 09:36:42 +02:00
|
|
|
var template;
|
2014-09-08 22:02:28 +02:00
|
|
|
var $messages;
|
2014-09-02 09:36:42 +02:00
|
|
|
|
2014-09-02 23:07:50 +02:00
|
|
|
function init() {
|
2014-09-03 09:10:26 +02:00
|
|
|
topNavigationPresenter.select('register');
|
2014-09-11 13:59:12 +02:00
|
|
|
topNavigationPresenter.changeTitle('Registration');
|
2014-09-04 18:06:25 +02:00
|
|
|
promise.wait(util.promiseTemplate('registration-form')).then(function(html) {
|
2014-09-03 09:10:26 +02:00
|
|
|
template = _.template(html);
|
|
|
|
render();
|
|
|
|
});
|
2014-09-02 23:07:50 +02:00
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
|
|
|
function render() {
|
|
|
|
$el.html(template());
|
2014-09-02 23:07:50 +02:00
|
|
|
$el.find('form').submit(registrationFormSubmitted);
|
2014-08-31 23:22:56 +02:00
|
|
|
$messages = $el.find('.messages');
|
|
|
|
$messages.width($el.find('form').width());
|
2014-09-02 23:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function registrationFormSubmitted(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
messagePresenter.hideMessages($messages);
|
|
|
|
|
2014-09-08 22:02:28 +02:00
|
|
|
var formData = {
|
2014-09-07 00:33:46 +02:00
|
|
|
userName: $el.find('[name=userName]').val(),
|
|
|
|
password: $el.find('[name=password]').val(),
|
|
|
|
passwordConfirmation: $el.find('[name=passwordConfirmation]').val(),
|
2014-09-02 23:07:50 +02:00
|
|
|
email: $el.find('[name=email]').val(),
|
|
|
|
};
|
|
|
|
|
2014-09-08 22:02:28 +02:00
|
|
|
if (!validateRegistrationFormData(formData)) {
|
2014-09-02 23:07:50 +02:00
|
|
|
return;
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-09-02 23:07:50 +02:00
|
|
|
|
2014-09-07 00:33:46 +02:00
|
|
|
api.post('/users', formData)
|
2014-09-02 23:07:50 +02:00
|
|
|
.then(function(response) {
|
|
|
|
registrationSuccess(response);
|
2014-09-07 00:33:46 +02:00
|
|
|
}).fail(function(response) {
|
2014-09-02 23:07:50 +02:00
|
|
|
registrationFailure(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registrationSuccess(apiResponse) {
|
|
|
|
$el.find('form').slideUp(function() {
|
|
|
|
var message = 'Registration complete! ';
|
2014-09-08 13:06:32 +02:00
|
|
|
if (!apiResponse.json.confirmed) {
|
|
|
|
message += '<br/>Check your inbox for activation e-mail.<br/>If e-mail doesn\'t show up, check your spam folder.';
|
|
|
|
} else {
|
|
|
|
message += '<a href="#/login">Click here</a> to login.';
|
|
|
|
}
|
2014-09-02 23:07:50 +02:00
|
|
|
messagePresenter.showInfo($messages, message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function registrationFailure(apiResponse) {
|
|
|
|
messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse);
|
|
|
|
}
|
2014-08-31 23:22:56 +02:00
|
|
|
|
2014-09-07 00:33:46 +02:00
|
|
|
function validateRegistrationFormData(formData) {
|
2014-09-08 22:02:28 +02:00
|
|
|
if (formData.userName.length === 0) {
|
2014-09-01 19:36:34 +02:00
|
|
|
messagePresenter.showError($messages, 'User name cannot be empty.');
|
2014-09-01 21:54:22 +02:00
|
|
|
return false;
|
2014-09-01 19:36:34 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 22:02:28 +02:00
|
|
|
if (formData.password.length === 0) {
|
2014-09-01 19:36:34 +02:00
|
|
|
messagePresenter.showError($messages, 'Password cannot be empty.');
|
2014-09-01 21:54:22 +02:00
|
|
|
return false;
|
2014-09-01 19:36:34 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 22:02:28 +02:00
|
|
|
if (formData.password !== formData.passwordConfirmation) {
|
2014-09-01 19:36:34 +02:00
|
|
|
messagePresenter.showError($messages, 'Passwords must be the same.');
|
2014-09-01 21:54:22 +02:00
|
|
|
return false;
|
2014-09-01 19:36:34 +02:00
|
|
|
}
|
2014-09-01 21:54:22 +02:00
|
|
|
|
|
|
|
return true;
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-09-01 19:36:34 +02:00
|
|
|
|
2014-08-31 23:22:56 +02:00
|
|
|
return {
|
2014-09-03 09:10:26 +02:00
|
|
|
init: init,
|
2014-08-31 23:22:56 +02:00
|
|
|
render: render,
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-09-11 12:33:44 +02:00
|
|
|
App.DI.register('registrationPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'topNavigationPresenter', 'messagePresenter'], App.Presenters.RegistrationPresenter);
|