Guest User

Untitled

a guest
May 8th, 2018
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.04 KB | None | 0 0
  1. /**
  2. * @class app.account
  3. */
  4. (function(app, $) {
  5. var $cache,
  6. settings;
  7.  
  8. /**
  9. * @private
  10. * @function
  11. * @description Save my account data after validation
  12. **/
  13. function accountSaveForm(data, url, el, msg, clear, modal) {
  14. $.ajax({
  15. method: "POST",
  16. url: url,
  17. data: data,
  18. beforeSend: function() {
  19. app.modal.openDisabled('modalProcessLoading');
  20. },
  21. success: function(data) {
  22. app.modal.closeDisabled('modalProcessLoading');
  23. app.modal.close('modalAccountPassword');
  24.  
  25. if(data.success == false) {
  26. if (data.error) {
  27. app.modal.open('modalWarning', null, null, data.error);
  28. }
  29. if (data.validationErrors) {
  30. el.validate().showErrors(data.validationErrors);
  31. if (modal) {
  32. app.modal.open(modal);
  33. }
  34. }
  35. } else {
  36. app.modal.open('modalSuccess', null, null, msg);
  37. if (clear) {
  38. $.fn.clearForm(el);
  39. }
  40. }
  41. },
  42. error: function (xhr, thrownError) {
  43. app.modal.closeDisabled('modalProcessLoading');
  44. app.modal.close('modalAccountPassword');
  45. app.modal.open('modalProcessError');
  46. }
  47. });
  48. }
  49.  
  50. /**
  51. * @private
  52. * @function
  53. * @description Save my account username
  54. **/
  55. function accountSetUsername() {
  56. var showUsername = $cache.formProfile.find("#accountShowName input[name='accountDisplayName']:checked").val(),
  57. userName = '';
  58.  
  59. app.user.firstName = $cache.formProfile.find('#accountFirstName').val();
  60. app.user.lastName = $cache.formProfile.find('#accountLastName').val();
  61.  
  62. switch(showUsername) {
  63. case 'showFirstName' :
  64. userName = app.user.firstName;
  65. break;
  66. case 'showLastName' :
  67. userName = app.user.lastName;
  68. break;
  69. case 'showFirstLastName' :
  70. userName = app.user.firstName + ' ' + app.user.lastName;
  71. break;
  72. }
  73.  
  74. $('.js-account-username').each(function(){
  75. $(this).text(userName);
  76. });
  77. }
  78.  
  79. /**
  80. * @private
  81. * @function
  82. * @description Get value for account services
  83. **/
  84. function getAccountServicesValue() {
  85. if ($('#publishDesignsHidden').prop('checked')) {
  86. return true
  87. } else {
  88. return false
  89. }
  90. }
  91.  
  92. /**
  93. * @private
  94. * @function
  95. * @description Show backend erorrs validation
  96. **/
  97. function showCustomValidationErrors(errors) {
  98. if (errors["account.email"]) {
  99. $('#accountDataEmailError').text(errors["account.email"]);
  100. }
  101. }
  102.  
  103. /**
  104. * @private
  105. * @function
  106. * @description Send account form data
  107. **/
  108. function sendAccountFormData(data) {
  109. $.ajax({
  110. method: "POST",
  111. dataType : "json",
  112. url: settings.accountDataSave,
  113. data: JSON.stringify(data),
  114. contentType: "application/json",
  115. beforeSend: function() {
  116. app.modal.openDisabled('modalProcessLoading');
  117. },
  118. success: function(data) {
  119. app.modal.closeDisabled('modalProcessLoading');
  120. if(data.success == false) {
  121. if (data.error) {
  122. app.modal.open('modalWarning', null, null, data.error)
  123. } else if (data.validationErrors) {
  124. showCustomValidationErrors(data.validationErrors);
  125. app.modal.open('modalWarning', null, null, 'Please check form validation errors');
  126. } else {
  127. app.modal.open('modalWarning');
  128. }
  129. } else {
  130. app.modal.open('modalProcessSuccess', null, null, 'Account changes saved');
  131. }
  132. },
  133. error: function (xhr, thrownError) {
  134. app.modal.closeDisabled('modalProcessLoading');
  135. app.modal.open('modalWarning');
  136. }
  137. });
  138. }
  139.  
  140. /**
  141. * @private
  142. * @function
  143. * @description Collect account for sending
  144. **/
  145. function collectAccountFormData() {
  146. var data = {
  147. "account": {},
  148. "accountingContact": {},
  149. "service": {},
  150. "paypal": {}
  151. };
  152.  
  153. if (app.account.userdata.account.enabled) {
  154. data.account["email"] = $('#accountDataEmail').val();
  155. data.account["firstName"] = $('#accountFirstName').val();
  156. data.account["lastName"] = $('#accountLastName').val();
  157. }
  158.  
  159. if (app.account.userdata.accountingContact.enabled) {
  160. data.accountingContact["firstName"] = $('#paymentsFirstName').val();
  161. data.accountingContact["lastName"] = $('#paymentsLastName').val();
  162. data.accountingContact["email"] = $('#paymentsEmail').val();
  163. data.accountingContact["phone"] = $('#paymentsPhone').val();
  164. }
  165.  
  166. if (app.account.userdata.paypal.enabled) {
  167. data.paypal["account"] = $('#accountPaypal').val();
  168. }
  169.  
  170. if (app.account.userdata.service.enabled) {
  171. data.service["publishHidden"] = getAccountServicesValue();
  172. }
  173. console.log(data);
  174. sendAccountFormData(data);
  175. }
  176.  
  177. /**
  178. * @private
  179. * @function
  180. * @description Save my account form
  181. **/
  182. function submitAccountForm() {
  183. // Validate multiple forms
  184. if (app.account.userdata.account.enabled) {
  185. $('#accountSettingsEmail').submit();
  186. }
  187.  
  188. if (app.account.userdata.accountingContact.enabled) {
  189. $('#accountSettingsAccounting').submit();
  190. }
  191.  
  192. // payment validation error
  193. if ((!app.payment.available || app.payment.available == "false") && app.account.userdata.creditCards.enabled) {
  194. $('#paymentValidationError').removeClass('hidden');
  195. } else {
  196. $('#paymentValidationError').addClass('hidden');
  197. }
  198.  
  199. var validationLoginDataSuccess = app.account.loginDataValid || !app.account.userdata.account.enabled,
  200. validationAccountingSuccess = app.account.accountingDataValid || !app.account.userdata.accountingContact.enabled;
  201. validationPaymentSuccess = (app.payment.available && app.payment.available !== "false") || !app.account.userdata.creditCards.enabled;
  202.  
  203. // successfull validation
  204. if (validationLoginDataSuccess && validationAccountingSuccess && validationPaymentSuccess) {
  205. collectAccountFormData();
  206. } else {
  207. app.modal.open('modalAccountValidationError');
  208. }
  209. }
  210.  
  211. /**
  212. * @private
  213. * @function
  214. * @description Clear account fields
  215. **/
  216. function clearAccountData() {
  217. // my login
  218. $cache.account.email.val('');
  219. $cache.account.firstname.val('');
  220. $cache.account.lastname.val('');
  221.  
  222. // accounting contact
  223. $cache.accounting.firstname.val('');
  224. $cache.accounting.lastname.val('');
  225. $cache.accounting.email.val('');
  226. $cache.accounting.phone.val('');
  227.  
  228. // paypal
  229. $cache.paypal.account.val('');
  230.  
  231. // services
  232. $cache.service.publishHidden.prop('checked', true);
  233.  
  234. //remove validation
  235. $cache.formEmail.validate().resetForm();
  236.  
  237. if ($('#accountSettingsAccounting').length) {
  238. $cache.formAccounting.validate().resetForm();
  239. }
  240. }
  241.  
  242. /**
  243. * @private
  244. * @function
  245. * @description Reset account data
  246. **/
  247. function resetAccountData() {
  248. clearAccountData();
  249. getAccountData();
  250.  
  251. app.modal.close('modalAccountReset', true);
  252. }
  253.  
  254. /**
  255. * @private
  256. * @function
  257. * @description Toggle forms depends on data values
  258. **/
  259. function triggerAccountForms(data) {
  260. if (!data.account.enabled) {
  261. $cache.formEmail.addClass('hidden');
  262. $('.js-tabs-selector[data-tab="accountsettings"]').addClass('hidden');
  263. }
  264.  
  265. if (!data.paypal.enabled) {
  266. $cache.formPaypal.addClass('hidden');
  267. }
  268.  
  269. if (!data.creditCards.enabled) {
  270. $cache.formPayments.addClass('hidden');
  271. $('.js-tabs-selector[data-tab="payments"]').addClass('hidden');
  272. }
  273.  
  274. if (!data.accountingContact.enabled) {
  275. $cache.wrapperAccounting.addClass('hidden');
  276. $('.js-tabs-selector[data-tab="accounting"]').addClass('hidden');
  277. }
  278.  
  279. if (!data.service.enabled) {
  280. $cache.wrapperService.addClass('hidden');
  281. $('.js-tabs-selector[data-tab="services"]').addClass('hidden');
  282. }
  283. }
  284.  
  285. /**
  286. * @private
  287. * @function
  288. * @description Set input values from json
  289. **/
  290. function setInputValues(data) {
  291. // my login
  292. $cache.account.email.val(data.account.email);
  293. $cache.account.firstname.val(data.account.firstName);
  294. $cache.account.lastname.val(data.account.lastName);
  295.  
  296. // accounting contact
  297. $cache.accounting.firstname.val(data.accountingContact.firstName);
  298. $cache.accounting.lastname.val(data.accountingContact.lastName);
  299. $cache.accounting.email.val(data.accountingContact.email);
  300. $cache.accounting.phone.val(data.accountingContact.phone);
  301.  
  302. //paypal
  303. $cache.paypal.account.val(data.paypal.account);
  304.  
  305. // services
  306. if (data.service.publishHidden) {
  307. $cache.service.publishHidden.prop('checked', true);
  308. } else {
  309. $cache.service.publishVisible.prop('checked', true);
  310. }
  311. }
  312.  
  313. function initPayments() {
  314. if (app.account.userdata.creditCards.enabled && !app.payment.initialized) {
  315. app.payment.init();
  316. }
  317. }
  318.  
  319. /**
  320. * @private
  321. * @function
  322. * @description Set account data from JSON
  323. **/
  324. function setAccountData(data) {
  325. initPayments();
  326. triggerAccountForms(data);
  327. setInputValues(data);
  328. }
  329.  
  330. /**
  331. * @private
  332. * @function
  333. * @description Get account data from JSON
  334. **/
  335. function getAccountData(){
  336. $.ajax({
  337. type: 'GET',
  338. url: settings.accountDataGet,
  339. dataType: 'json',
  340. success: function(result) {
  341. app.account.userdata = result;
  342. setAccountData(result);
  343. },
  344. error: function(result) {
  345. app.modal.open('modalError');
  346. }
  347. });
  348. }
  349.  
  350. /**
  351. * @private
  352. * @function
  353. * @description Clean reset password form
  354. **/
  355. function clearAccountPass() {
  356. $cache.formPassword.find('input').val('');
  357. }
  358.  
  359. /**
  360. * @private
  361. * @function
  362. * @description Change account password
  363. **/
  364. function changeAccountPass(modal) {
  365. $cache.formPassword.validate().resetForm();
  366. $cache.formPassword.find('input').removeClass('error');
  367. app.modal.open(modal);
  368. }
  369.  
  370. /**
  371. * @private
  372. * @function
  373. * @description Initialize my account cache
  374. **/
  375. function initializeCache() {
  376. $cache = {
  377. formEmail: $('#accountSettingsEmail'),
  378. formPassword: $('#accountSettingsPassword'),
  379. formPaypal: $('#accountSettingsPaypal'),
  380. formPayments: $('#payments'),
  381. formProfile: $('#accountSettingsProfile'),
  382. formService: $('#serviceSettings'),
  383. formAccounting: $('#accountSettingsAccounting'),
  384. wrapperService: $('#services'),
  385. wrapperAccounting: $('#accounting'),
  386. account: {
  387. email: $('#accountDataEmail'),
  388. firstname: $('#accountFirstName'),
  389. lastname: $('#accountLastName')
  390. },
  391. accounting: {
  392. firstname: $('#paymentsFirstName'),
  393. lastname: $('#paymentsLastName'),
  394. email: $('#paymentsEmail'),
  395. phone: $('#paymentsPhone')
  396. },
  397. paypal: {
  398. account: $('#accountPaypal')
  399. },
  400. service: {
  401. publishHidden: $('#publishDesignsHidden'),
  402. publishVisible: $('#publishDesignsVisible')
  403. }
  404. }
  405. }
  406.  
  407. /**
  408. * @private
  409. * @function
  410. * @description Initialize my account settings
  411. **/
  412. function initializeSettings() {
  413. settings = {
  414. emailSaveLink: $cache.formEmail.attr('action'),
  415. passSaveLink: $cache.formPassword.attr('action'),
  416. paypalSaveLink: $cache.formPaypal.attr('action'),
  417. profileSaveLink: $cache.formProfile.attr('action'),
  418. serviceSaveLink: $cache.formService.attr('action'),
  419. paymentsSaveLink: $cache.formAccounting.attr('action'),
  420. accountDataSave: $('#js-json-accountdata-save').text(),
  421. accountDataGet: $('#js-json-accountdata-get').text()
  422. }
  423. }
  424.  
  425. /**
  426. * @private
  427. * @function
  428. * @description Initialize my account DOM
  429. **/
  430. function initializeDOM() {
  431. getAccountData();
  432. app.utils.initScrollTo();
  433. }
  434.  
  435. /**
  436. * @private
  437. * @function
  438. * @description Initialize my account Events
  439. **/
  440. function initializeEvents() {
  441. $('.js-account-saveform').on('click', function(){
  442. submitAccountForm();
  443. });
  444.  
  445. $('.js-account-resetform').on('click', function(){
  446. resetAccountData();
  447. });
  448.  
  449. $('.js-account-changepass').on('click', function(){
  450. clearAccountPass();
  451. changeAccountPass($(this).attr('data-modal'));
  452. });
  453. }
  454.  
  455. app.account = {
  456. init: function(){
  457. initializeCache();
  458. initializeEvents();
  459. initializeSettings();
  460. initializeDOM();
  461. },
  462. userdata: {},
  463. loginDataValid: false,
  464. accountingDataValid: false,
  465. savePaypal: function() {
  466. accountSaveForm(
  467. $cache.formPaypal.serialize(),
  468. settings.paypalSaveLink,
  469. $cache.formPaypal,
  470. app.properties.account.paypalSaved
  471. )
  472. },
  473. savePassword: function() {
  474. accountSaveForm(
  475. $cache.formPassword.serialize(),
  476. settings.passSaveLink,
  477. $cache.formPassword,
  478. app.properties.account.passSaved,
  479. true,
  480. 'modalAccountPassword'
  481. );
  482. },
  483. setUsername: function() {
  484. accountSetUsername();
  485. },
  486. saveProfile: function() {
  487. accountSaveForm(
  488. $cache.formProfile.serialize(),
  489. settings.profileSaveLink,
  490. $cache.formProfile,
  491. app.properties.account.profileSaved
  492. );
  493. }
  494. }
  495.  
  496. }(window.app = window.app || {}, jQuery));
Add Comment
Please, Sign In to add comment