Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @class app.account
- */
- (function(app, $) {
- var $cache,
- settings;
- /**
- * @private
- * @function
- * @description Save my account data after validation
- **/
- function accountSaveForm(data, url, el, msg, clear, modal) {
- $.ajax({
- method: "POST",
- url: url,
- data: data,
- beforeSend: function() {
- app.modal.openDisabled('modalProcessLoading');
- },
- success: function(data) {
- app.modal.closeDisabled('modalProcessLoading');
- app.modal.close('modalAccountPassword');
- if(data.success == false) {
- if (data.error) {
- app.modal.open('modalWarning', null, null, data.error);
- }
- if (data.validationErrors) {
- el.validate().showErrors(data.validationErrors);
- if (modal) {
- app.modal.open(modal);
- }
- }
- } else {
- app.modal.open('modalSuccess', null, null, msg);
- if (clear) {
- $.fn.clearForm(el);
- }
- }
- },
- error: function (xhr, thrownError) {
- app.modal.closeDisabled('modalProcessLoading');
- app.modal.close('modalAccountPassword');
- app.modal.open('modalProcessError');
- }
- });
- }
- /**
- * @private
- * @function
- * @description Save my account username
- **/
- function accountSetUsername() {
- var showUsername = $cache.formProfile.find("#accountShowName input[name='accountDisplayName']:checked").val(),
- userName = '';
- app.user.firstName = $cache.formProfile.find('#accountFirstName').val();
- app.user.lastName = $cache.formProfile.find('#accountLastName').val();
- switch(showUsername) {
- case 'showFirstName' :
- userName = app.user.firstName;
- break;
- case 'showLastName' :
- userName = app.user.lastName;
- break;
- case 'showFirstLastName' :
- userName = app.user.firstName + ' ' + app.user.lastName;
- break;
- }
- $('.js-account-username').each(function(){
- $(this).text(userName);
- });
- }
- /**
- * @private
- * @function
- * @description Get value for account services
- **/
- function getAccountServicesValue() {
- if ($('#publishDesignsHidden').prop('checked')) {
- return true
- } else {
- return false
- }
- }
- /**
- * @private
- * @function
- * @description Show backend erorrs validation
- **/
- function showCustomValidationErrors(errors) {
- if (errors["account.email"]) {
- $('#accountDataEmailError').text(errors["account.email"]);
- }
- }
- /**
- * @private
- * @function
- * @description Send account form data
- **/
- function sendAccountFormData(data) {
- $.ajax({
- method: "POST",
- dataType : "json",
- url: settings.accountDataSave,
- data: JSON.stringify(data),
- contentType: "application/json",
- beforeSend: function() {
- app.modal.openDisabled('modalProcessLoading');
- },
- success: function(data) {
- app.modal.closeDisabled('modalProcessLoading');
- if(data.success == false) {
- if (data.error) {
- app.modal.open('modalWarning', null, null, data.error)
- } else if (data.validationErrors) {
- showCustomValidationErrors(data.validationErrors);
- app.modal.open('modalWarning', null, null, 'Please check form validation errors');
- } else {
- app.modal.open('modalWarning');
- }
- } else {
- app.modal.open('modalProcessSuccess', null, null, 'Account changes saved');
- }
- },
- error: function (xhr, thrownError) {
- app.modal.closeDisabled('modalProcessLoading');
- app.modal.open('modalWarning');
- }
- });
- }
- /**
- * @private
- * @function
- * @description Collect account for sending
- **/
- function collectAccountFormData() {
- var data = {
- "account": {},
- "accountingContact": {},
- "service": {},
- "paypal": {}
- };
- if (app.account.userdata.account.enabled) {
- data.account["email"] = $('#accountDataEmail').val();
- data.account["firstName"] = $('#accountFirstName').val();
- data.account["lastName"] = $('#accountLastName').val();
- }
- if (app.account.userdata.accountingContact.enabled) {
- data.accountingContact["firstName"] = $('#paymentsFirstName').val();
- data.accountingContact["lastName"] = $('#paymentsLastName').val();
- data.accountingContact["email"] = $('#paymentsEmail').val();
- data.accountingContact["phone"] = $('#paymentsPhone').val();
- }
- if (app.account.userdata.paypal.enabled) {
- data.paypal["account"] = $('#accountPaypal').val();
- }
- if (app.account.userdata.service.enabled) {
- data.service["publishHidden"] = getAccountServicesValue();
- }
- console.log(data);
- sendAccountFormData(data);
- }
- /**
- * @private
- * @function
- * @description Save my account form
- **/
- function submitAccountForm() {
- // Validate multiple forms
- if (app.account.userdata.account.enabled) {
- $('#accountSettingsEmail').submit();
- }
- if (app.account.userdata.accountingContact.enabled) {
- $('#accountSettingsAccounting').submit();
- }
- // payment validation error
- if ((!app.payment.available || app.payment.available == "false") && app.account.userdata.creditCards.enabled) {
- $('#paymentValidationError').removeClass('hidden');
- } else {
- $('#paymentValidationError').addClass('hidden');
- }
- var validationLoginDataSuccess = app.account.loginDataValid || !app.account.userdata.account.enabled,
- validationAccountingSuccess = app.account.accountingDataValid || !app.account.userdata.accountingContact.enabled;
- validationPaymentSuccess = (app.payment.available && app.payment.available !== "false") || !app.account.userdata.creditCards.enabled;
- // successfull validation
- if (validationLoginDataSuccess && validationAccountingSuccess && validationPaymentSuccess) {
- collectAccountFormData();
- } else {
- app.modal.open('modalAccountValidationError');
- }
- }
- /**
- * @private
- * @function
- * @description Clear account fields
- **/
- function clearAccountData() {
- // my login
- $cache.account.email.val('');
- $cache.account.firstname.val('');
- $cache.account.lastname.val('');
- // accounting contact
- $cache.accounting.firstname.val('');
- $cache.accounting.lastname.val('');
- $cache.accounting.email.val('');
- $cache.accounting.phone.val('');
- // paypal
- $cache.paypal.account.val('');
- // services
- $cache.service.publishHidden.prop('checked', true);
- //remove validation
- $cache.formEmail.validate().resetForm();
- if ($('#accountSettingsAccounting').length) {
- $cache.formAccounting.validate().resetForm();
- }
- }
- /**
- * @private
- * @function
- * @description Reset account data
- **/
- function resetAccountData() {
- clearAccountData();
- getAccountData();
- app.modal.close('modalAccountReset', true);
- }
- /**
- * @private
- * @function
- * @description Toggle forms depends on data values
- **/
- function triggerAccountForms(data) {
- if (!data.account.enabled) {
- $cache.formEmail.addClass('hidden');
- $('.js-tabs-selector[data-tab="accountsettings"]').addClass('hidden');
- }
- if (!data.paypal.enabled) {
- $cache.formPaypal.addClass('hidden');
- }
- if (!data.creditCards.enabled) {
- $cache.formPayments.addClass('hidden');
- $('.js-tabs-selector[data-tab="payments"]').addClass('hidden');
- }
- if (!data.accountingContact.enabled) {
- $cache.wrapperAccounting.addClass('hidden');
- $('.js-tabs-selector[data-tab="accounting"]').addClass('hidden');
- }
- if (!data.service.enabled) {
- $cache.wrapperService.addClass('hidden');
- $('.js-tabs-selector[data-tab="services"]').addClass('hidden');
- }
- }
- /**
- * @private
- * @function
- * @description Set input values from json
- **/
- function setInputValues(data) {
- // my login
- $cache.account.email.val(data.account.email);
- $cache.account.firstname.val(data.account.firstName);
- $cache.account.lastname.val(data.account.lastName);
- // accounting contact
- $cache.accounting.firstname.val(data.accountingContact.firstName);
- $cache.accounting.lastname.val(data.accountingContact.lastName);
- $cache.accounting.email.val(data.accountingContact.email);
- $cache.accounting.phone.val(data.accountingContact.phone);
- //paypal
- $cache.paypal.account.val(data.paypal.account);
- // services
- if (data.service.publishHidden) {
- $cache.service.publishHidden.prop('checked', true);
- } else {
- $cache.service.publishVisible.prop('checked', true);
- }
- }
- function initPayments() {
- if (app.account.userdata.creditCards.enabled && !app.payment.initialized) {
- app.payment.init();
- }
- }
- /**
- * @private
- * @function
- * @description Set account data from JSON
- **/
- function setAccountData(data) {
- initPayments();
- triggerAccountForms(data);
- setInputValues(data);
- }
- /**
- * @private
- * @function
- * @description Get account data from JSON
- **/
- function getAccountData(){
- $.ajax({
- type: 'GET',
- url: settings.accountDataGet,
- dataType: 'json',
- success: function(result) {
- app.account.userdata = result;
- setAccountData(result);
- },
- error: function(result) {
- app.modal.open('modalError');
- }
- });
- }
- /**
- * @private
- * @function
- * @description Clean reset password form
- **/
- function clearAccountPass() {
- $cache.formPassword.find('input').val('');
- }
- /**
- * @private
- * @function
- * @description Change account password
- **/
- function changeAccountPass(modal) {
- $cache.formPassword.validate().resetForm();
- $cache.formPassword.find('input').removeClass('error');
- app.modal.open(modal);
- }
- /**
- * @private
- * @function
- * @description Initialize my account cache
- **/
- function initializeCache() {
- $cache = {
- formEmail: $('#accountSettingsEmail'),
- formPassword: $('#accountSettingsPassword'),
- formPaypal: $('#accountSettingsPaypal'),
- formPayments: $('#payments'),
- formProfile: $('#accountSettingsProfile'),
- formService: $('#serviceSettings'),
- formAccounting: $('#accountSettingsAccounting'),
- wrapperService: $('#services'),
- wrapperAccounting: $('#accounting'),
- account: {
- email: $('#accountDataEmail'),
- firstname: $('#accountFirstName'),
- lastname: $('#accountLastName')
- },
- accounting: {
- firstname: $('#paymentsFirstName'),
- lastname: $('#paymentsLastName'),
- email: $('#paymentsEmail'),
- phone: $('#paymentsPhone')
- },
- paypal: {
- account: $('#accountPaypal')
- },
- service: {
- publishHidden: $('#publishDesignsHidden'),
- publishVisible: $('#publishDesignsVisible')
- }
- }
- }
- /**
- * @private
- * @function
- * @description Initialize my account settings
- **/
- function initializeSettings() {
- settings = {
- emailSaveLink: $cache.formEmail.attr('action'),
- passSaveLink: $cache.formPassword.attr('action'),
- paypalSaveLink: $cache.formPaypal.attr('action'),
- profileSaveLink: $cache.formProfile.attr('action'),
- serviceSaveLink: $cache.formService.attr('action'),
- paymentsSaveLink: $cache.formAccounting.attr('action'),
- accountDataSave: $('#js-json-accountdata-save').text(),
- accountDataGet: $('#js-json-accountdata-get').text()
- }
- }
- /**
- * @private
- * @function
- * @description Initialize my account DOM
- **/
- function initializeDOM() {
- getAccountData();
- app.utils.initScrollTo();
- }
- /**
- * @private
- * @function
- * @description Initialize my account Events
- **/
- function initializeEvents() {
- $('.js-account-saveform').on('click', function(){
- submitAccountForm();
- });
- $('.js-account-resetform').on('click', function(){
- resetAccountData();
- });
- $('.js-account-changepass').on('click', function(){
- clearAccountPass();
- changeAccountPass($(this).attr('data-modal'));
- });
- }
- app.account = {
- init: function(){
- initializeCache();
- initializeEvents();
- initializeSettings();
- initializeDOM();
- },
- userdata: {},
- loginDataValid: false,
- accountingDataValid: false,
- savePaypal: function() {
- accountSaveForm(
- $cache.formPaypal.serialize(),
- settings.paypalSaveLink,
- $cache.formPaypal,
- app.properties.account.paypalSaved
- )
- },
- savePassword: function() {
- accountSaveForm(
- $cache.formPassword.serialize(),
- settings.passSaveLink,
- $cache.formPassword,
- app.properties.account.passSaved,
- true,
- 'modalAccountPassword'
- );
- },
- setUsername: function() {
- accountSetUsername();
- },
- saveProfile: function() {
- accountSaveForm(
- $cache.formProfile.serialize(),
- settings.profileSaveLink,
- $cache.formProfile,
- app.properties.account.profileSaved
- );
- }
- }
- }(window.app = window.app || {}, jQuery));
Add Comment
Please, Sign In to add comment