jam-cloud/web/app/assets/javascripts/accounts_profile.js

535 lines
16 KiB
JavaScript
Raw Permalink Normal View History

(function (context, $) {
2015-03-04 04:16:13 +00:00
"use strict";
context.JK = context.JK || {};
context.JK.AccountProfileScreen = function (app) {
2015-03-04 04:16:13 +00:00
var $document = $(document);
var logger = context.JK.logger;
var EVENTS = context.JK.EVENTS;
2016-05-01 03:12:25 +00:00
var NAMED_MESSAGES = context.JK.NAMED_MESSAGES;
2015-03-04 04:16:13 +00:00
var api = context.JK.Rest();
var userId;
var user = {};
var selectLocation = null;
var recentUserDetail = null;
var loadingCitiesData = false;
var loadingRegionsData = false;
var loadingCountriesData = false;
var nilOptionStr = '<option value=""></option>';
var nilOptionText = 'n/a';
2015-03-04 07:02:48 +00:00
var $screen = $('#account-profile-basics');
var $avatar = $screen.find('#avatar');
var $country = $screen.find('#country');
var $region = $screen.find('#region');
var $city = $screen.find('#city');
var $firstName = $screen.find('#first-name');
var $lastName = $screen.find('#last-name');
var $gender = $screen.find('#gender');
var $biography = $screen.find('#biography');
var $subscribe = $screen.find('#subscribe');
var $btnCancel = $screen.find('.account-edit-profile-cancel');
var $btnSubmit = $screen.find('.account-edit-profile-submit');
2015-03-04 04:16:13 +00:00
function beforeShow(data) {
userId = data.id;
2015-03-04 04:16:13 +00:00
}
2015-03-04 04:16:13 +00:00
function afterShow(data) {
if (window.ProfileStore.solo) {
$btnSubmit.text('SAVE & RETURN TO PROFILE');
}
else {
$btnSubmit.text('SAVE & NEXT');
}
resetForm();
renderAccountProfile();
2015-03-04 04:16:13 +00:00
}
2015-03-04 04:16:13 +00:00
function resetForm() {
// remove all display errors
$('#account-profile-content-scroller form .error-text').remove()
$('#account-profile-content-scroller form .error').removeClass("error")
}
2015-03-04 07:02:48 +00:00
function populateAccountProfile(userDetail) {
$avatar.attr('src', context.JK.resolveAvatarUrl(userDetail.photo_url));
$country.val(userDetail.country);
$region.val(userDetail.state);
$city.val(userDetail.city);
$firstName.val(userDetail.first_name);
$lastName.val(userDetail.last_name);
$gender.val(userDetail.gender);
$biography.val(userDetail.biography);
2015-03-04 04:16:13 +00:00
if (userDetail.subscribe_email) {
$subscribe.attr('checked', 'checked');
}
2015-03-04 07:02:48 +00:00
var content_root = $('#account-profile-content-scroller');
2015-03-04 04:16:13 +00:00
// set birth_date
if (userDetail.birth_date) {
2015-03-04 04:16:13 +00:00
var birthDateFields = userDetail.birth_date.split('-')
var birthDateYear = birthDateFields[0];
var birthDateMonth = birthDateFields[1];
var birthDateDay = birthDateFields[2];
$('select#user_birth_date_1i', content_root).val(parseInt(birthDateYear));
$('select#user_birth_date_2i', content_root).val(parseInt(birthDateMonth));
$('select#user_birth_date_3i', content_root).val(parseInt(birthDateDay));
}
}
2015-03-04 04:16:13 +00:00
function populateAccountProfileLocation(userDetail, regions, cities) {
populateRegions(regions, userDetail.state);
populateCities(cities, userDetail.city);
2015-03-04 04:16:13 +00:00
}
2015-03-04 04:16:13 +00:00
function populateCountries(countries, userCountry) {
2015-03-04 04:16:13 +00:00
// countries has the format ["US", ...]
var foundCountry = false;
var countrySelect = getCountryElement();
countrySelect.children().remove();
2015-03-04 04:16:13 +00:00
var nilOption = $(nilOptionStr);
nilOption.text(nilOptionText);
countrySelect.append(nilOption);
$.each(countries, function (index, country) {
if (!country) return;
2015-03-04 04:16:13 +00:00
var option = $(nilOptionStr);
option.text(country);
option.attr("value", country);
if (country == userCountry) {
2015-03-04 04:16:13 +00:00
foundCountry = true;
}
2015-03-04 04:16:13 +00:00
countrySelect.append(option);
});
if (!foundCountry) {
2015-03-04 04:16:13 +00:00
// in this case, the user has a country that is not in the database
// this can happen in a development/test scenario, but let's assume it can
// happen in production too.
var option = $(nilOptionStr);
option.text(userCountry);
option.attr("value", userCountry);
countrySelect.append(option);
}
2015-03-04 04:16:13 +00:00
countrySelect.val(userCountry);
countrySelect.attr("disabled", null)
}
2015-03-04 04:16:13 +00:00
function populateCountriesx(countriesx, userCountry) {
2015-03-04 04:16:13 +00:00
// countriesx has the format [{countrycode: "US", countryname: "United States"}, ...]
2015-03-04 04:16:13 +00:00
var foundCountry = false;
var countrySelect = getCountryElement();
countrySelect.children().remove();
2015-03-04 04:16:13 +00:00
var nilOption = $(nilOptionStr);
nilOption.text(nilOptionText);
countrySelect.append(nilOption);
$.each(countriesx, function (index, countryx) {
2015-03-04 04:16:13 +00:00
if (!countryx.countrycode) return;
2015-03-04 04:16:13 +00:00
var option = $(nilOptionStr);
option.text(countryx.countryname);
option.attr("value", countryx.countrycode);
if (countryx.countrycode == userCountry) {
2015-03-04 04:16:13 +00:00
foundCountry = true;
}
2015-03-04 04:16:13 +00:00
countrySelect.append(option);
});
if (!foundCountry) {
2015-03-04 04:16:13 +00:00
// in this case, the user has a country that is not in the database
// this can happen in a development/test scenario, but let's assume it can
// happen in production too.
var option = $(nilOptionStr);
option.text(userCountry);
option.attr("value", userCountry);
countrySelect.append(option);
}
2015-03-04 04:16:13 +00:00
countrySelect.val(userCountry);
countrySelect.attr("disabled", null);
}
2015-03-04 04:16:13 +00:00
function populateRegions(regions, userRegion) {
var regionSelect = getRegionElement()
regionSelect.children().remove()
2015-03-04 04:16:13 +00:00
var nilOption = $(nilOptionStr);
nilOption.text(nilOptionText);
regionSelect.append(nilOption);
$.each(regions, function (index, region) {
if (!region) return;
2015-03-04 04:16:13 +00:00
var option = $(nilOptionStr);
option.text(region['name']);
option.attr("value", region['region']);
2015-03-04 04:16:13 +00:00
regionSelect.append(option);
})
2015-03-04 04:16:13 +00:00
regionSelect.val(userRegion)
regionSelect.attr("disabled", null)
}
2015-03-04 04:16:13 +00:00
function populateCities(cities, userCity) {
var citySelect = getCityElement();
citySelect.children().remove();
2015-03-04 04:16:13 +00:00
var nilOption = $(nilOptionStr);
nilOption.text(nilOptionText);
citySelect.append(nilOption);
$.each(cities, function (index, city) {
if (!city) return;
2015-03-04 04:16:13 +00:00
var option = $(nilOptionStr);
option.text(city);
option.attr("value", city);
2015-03-04 04:16:13 +00:00
citySelect.append(option);
});
2013-05-23 13:53:37 +00:00
2015-03-04 04:16:13 +00:00
citySelect.val(userCity);
citySelect.attr("disabled", null);
}
2014-07-31 12:49:13 +00:00
2015-03-04 04:16:13 +00:00
/****************** MAIN PORTION OF SCREEN *****************/
// events for main screen
function events() {
$btnCancel.click(function (evt) {
evt.stopPropagation();
2016-01-13 02:37:00 +00:00
window.ProfileActions.cancelProfileEdit()
return false;
});
$('#account-profile-content-scroller').on('click', '#account-change-avatar', function (evt) {
evt.stopPropagation();
navToAvatar();
return false;
});
enableSubmits();
2015-03-04 04:16:13 +00:00
}
2016-05-01 03:12:25 +00:00
function teacherGuidance() {
if(recentUserDetail && recentUserDetail.is_a_teacher) {
setTimeout(function() {
var $header = $('#account-edit-profile-form h2')
context.JK.HelpBubbleHelper.teacherMusicianProfile($header, $screen);
}, 2000)
}
}
2015-03-04 04:16:13 +00:00
function renderAccountProfile() {
2015-03-13 01:34:20 +00:00
$.when(api.getUserProfile())
.done(function (userDetail) {
2015-03-04 07:02:48 +00:00
recentUserDetail = userDetail;
populateAccountProfile(userDetail);
2016-05-01 03:12:25 +00:00
teacherGuidance();
2015-03-04 04:16:13 +00:00
selectLocation = new context.JK.SelectLocation(getCountryElement(), getRegionElement(), getCityElement(), app);
selectLocation.load(userDetail.country, userDetail.state, userDetail.city)
});
}
2015-03-04 04:16:13 +00:00
function navToAccount() {
resetForm();
window.location = '/client#/profile/' + context.JK.currentUserId;
2015-03-04 04:16:13 +00:00
}
2015-03-04 04:16:13 +00:00
function navToAvatar() {
resetForm();
window.location = '/client#/account/profile/avatar';
}
2015-03-04 04:16:13 +00:00
function handleUpdateProfile() {
disableSubmits();
2015-03-04 04:16:13 +00:00
resetForm();
var country = getCountryElement().val();
var region = getRegionElement().val();
var city = getCityElement().val();
var firstName = getFirstNameElement().val();
var lastName = getLastNameElement().val();
var gender = getGenderElement().val();
var subscribeEmail = getSubscribeEmail().is(':checked');
var birthDate = getBirthDate();
2015-03-04 07:02:48 +00:00
var biography = $biography.val();
2015-03-04 04:16:13 +00:00
api.updateUser({
country: country,
state: region,
city: city,
first_name: firstName,
last_name: lastName,
gender: gender,
birth_date: birthDate,
2015-03-04 07:02:48 +00:00
biography: biography,
2015-03-04 04:16:13 +00:00
subscribe_email: subscribeEmail
})
.done(postUpdateProfileSuccess)
.fail(postUpdateProfileFailure)
.always(enableSubmits)
}
function enableSubmits() {
$btnSubmit.click(function (evt) {
evt.stopPropagation();
handleUpdateProfile();
return false;
});
$btnSubmit.removeClass("disabled");
$('#account-profile-content-scroller').on('submit', '#account-edit-email-form', function (evt) {
evt.stopPropagation();
handleUpdateProfile();
return false;
});
$("#account-edit-email-form").removeClass("disabled");
}
function disableSubmits() {
$("#account-edit-email-form").addClass("disabled")
$("#account-edit-email-form").off("click")
$btnSubmit.addClass("disabled")
$btnSubmit.off("click")
2015-03-04 04:16:13 +00:00
}
2015-03-04 04:16:13 +00:00
function postUpdateProfileSuccess(response) {
$document.triggerHandler(EVENTS.USER_UPDATED, response);
2016-01-13 02:37:00 +00:00
window.ProfileActions.editProfileNext('experience');
2015-03-04 04:16:13 +00:00
}
2015-03-04 04:16:13 +00:00
function postUpdateProfileFailure(xhr, textStatus, errorMessage) {
2015-03-04 04:16:13 +00:00
var errors = JSON.parse(xhr.responseText)
if (xhr.status == 422) {
2015-03-04 04:16:13 +00:00
var first_name = context.JK.format_errors("first_name", errors);
var last_name = context.JK.format_errors("last_name", errors);
var country = context.JK.format_errors("country", errors);
var state = context.JK.format_errors("state", errors);
var city = context.JK.format_errors("city", errors);
var birth_date = context.JK.format_errors("birth_date", errors);
var gender = context.JK.format_errors("birth_date", errors);
var subscribeEmail = context.JK.format_errors("subscribe_email", errors);
2015-03-04 07:02:48 +00:00
var biography = context.JK.format_errors("biography", errors);
if (first_name != null) {
2015-03-04 04:16:13 +00:00
getFirstNameElement().closest('div.field').addClass('error').end().after(first_name);
}
if (last_name != null) {
2015-03-04 04:16:13 +00:00
getLastNameElement().closest('div.field').addClass('error').end().after(last_name);
}
if (country != null) {
2015-03-04 04:16:13 +00:00
getCountryElement().closest('div.field').addClass('error').end().after(country);
}
if (state != null) {
2015-03-04 04:16:13 +00:00
getRegionElement().closest('div.field').addClass('error').end().after(state);
}
if (city != null) {
2015-03-04 04:16:13 +00:00
getCityElement().closest('div.field').addClass('error').end().after(city);
}
if (birth_date != null) {
2015-03-04 04:16:13 +00:00
getYearElement().closest('div.field').addClass('error').end().after(birth_date);
}
if (subscribeEmail != null) {
2015-03-04 04:16:13 +00:00
getSubscribeEmail().closest('div.field').addClass('error').end().after(subscribeEmail);
}
if (gender != null) {
2015-03-04 04:16:13 +00:00
getGenderElement().closest('div.field').addClass('error').end().after(gender);
}
2016-01-15 18:35:04 +00:00
if (biography != null) {
getBiographyElement().closest('div.field').addClass('error').end().after(biography);
}
2015-03-04 04:16:13 +00:00
}
else {
app.ajaxError(xhr, textStatus, errorMessage)
}
2015-03-04 04:16:13 +00:00
}
2015-03-04 04:16:13 +00:00
function handleCountryChanged() {
var selectedCountry = getCountryElement().val()
var selectedRegion = getRegionElement().val()
var cityElement = getCityElement();
2015-03-04 04:16:13 +00:00
updateRegionList(selectedCountry, getRegionElement());
updateCityList(selectedCountry, null, cityElement);
}
2015-03-04 04:16:13 +00:00
function updateRegionList(selectedCountry, regionElement) {
// only update region
if (selectedCountry) {
// set city disabled while updating
regionElement.attr('disabled', true);
loadingRegionsData = true;
regionElement.children().remove()
regionElement.append($(nilOptionStr).text('loading...'))
api.getRegions({country: selectedCountry})
2015-03-04 04:16:13 +00:00
.done(getRegionsDone)
.error(function (err) {
2015-03-04 04:16:13 +00:00
regionElement.children().remove()
regionElement.append($(nilOptionStr).text(nilOptionText))
})
.always(function () {
loadingRegionsData = false;
});
}
else {
regionElement.children().remove()
regionElement.append($(nilOptionStr).text(nilOptionText))
}
}
2015-03-04 04:16:13 +00:00
function updateCityList(selectedCountry, selectedRegion, cityElement) {
logger.debug("updating city list: selectedCountry %o, selectedRegion %o", selectedCountry, selectedRegion);
// only update cities
if (selectedCountry && selectedRegion) {
// set city disabled while updating
cityElement.attr('disabled', true);
loadingCitiesData = true;
cityElement.children().remove();
cityElement.append($(nilOptionStr).text('loading...'));
api.getCities({country: selectedCountry, region: selectedRegion})
2015-03-04 04:16:13 +00:00
.done(getCitiesDone)
.error(function (err) {
cityElement.children().remove();
cityElement.append($(nilOptionStr).text(nilOptionText));
2015-03-04 04:16:13 +00:00
})
.always(function () {
loadingCitiesData = false;
2015-03-04 04:16:13 +00:00
});
}
else {
cityElement.children().remove();
cityElement.append($(nilOptionStr).text(nilOptionText));
}
}
2015-03-04 04:16:13 +00:00
function handleRegionChanged() {
var selectedCountry = getCountryElement().val()
var selectedRegion = getRegionElement().val()
var cityElement = getCityElement();
2015-03-04 04:16:13 +00:00
updateCityList(selectedCountry, selectedRegion, cityElement);
}
2015-03-04 04:16:13 +00:00
function getCitiesDone(data) {
populateCities(data['cities'], recentUserDetail.city);
}
2015-03-04 04:16:13 +00:00
function getRegionsDone(data) {
populateRegions(data['regions'], recentUserDetail.state);
updateCityList(getCountryElement().val(), getRegionElement().val(), getCityElement());
}
2015-03-04 04:16:13 +00:00
function getCountryElement() {
return $('#account-profile-content-scroller select[name=country]');
}
2015-03-04 04:16:13 +00:00
function getRegionElement() {
return $('#account-profile-content-scroller select[name=region]');
}
2015-03-04 04:16:13 +00:00
function getCityElement() {
return $('#account-profile-content-scroller select[name=city]');
}
2015-03-04 04:16:13 +00:00
function getFirstNameElement() {
return $('#account-profile-content-scroller input[name=first_name]');
}
2015-03-04 04:16:13 +00:00
function getLastNameElement() {
return $('#account-profile-content-scroller input[name=last_name]');
}
2015-03-04 04:16:13 +00:00
function getGenderElement() {
return $('#account-profile-content-scroller select[name=gender]');
}
2015-03-04 04:16:13 +00:00
function getMonthElement() {
return $('#account-profile-content-scroller select#user_birth_date_2i');
}
2015-03-04 04:16:13 +00:00
function getDayElement() {
return $('#account-profile-content-scroller select#user_birth_date_3i');
}
2015-03-04 04:16:13 +00:00
function getYearElement() {
return $('#account-profile-content-scroller select#user_birth_date_1i');
}
2015-03-04 04:16:13 +00:00
function getSubscribeEmail() {
return $('#account-profile-content-scroller input[name=subscribe_email]');
}
2016-01-15 18:35:04 +00:00
function getBiographyElement() {
return $('#account-profile-content-scroller textarea[name=biography]');
}
2015-03-04 04:16:13 +00:00
function getBirthDate() {
2018-01-10 02:56:06 +00:00
var monthElement = getMonthElement()
if(!monthElement)
{
return null;
}
2015-03-04 04:16:13 +00:00
var month = getMonthElement().val()
var day = getDayElement().val()
var year = getYearElement().val()
if (month != null && month.length > 0 && day != null && day.length > 0 && year != null && year.length > 0) {
2015-03-04 04:16:13 +00:00
return month + "-" + day + "-" + year;
}
else {
return null;
}
}
2015-03-04 04:16:13 +00:00
function initialize() {
var screenBindings = {
'beforeShow': beforeShow,
'afterShow': afterShow
};
app.bindScreen('account/profile', screenBindings);
events();
}
2015-03-04 04:16:13 +00:00
this.initialize = initialize;
this.beforeShow = beforeShow;
this.afterShow = afterShow;
return this;
};
})(window, jQuery);