Advertisement
shapoval

account.js

Nov 4th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var isImageMimeType = function (mimeType) {
  2.     return (mimeType || '').trim().toLowerCase().indexOf('image/') === 0;
  3. };
  4.  
  5. // dynamic account completeness value changer
  6. (function () {
  7.     var blocks = document.querySelectorAll('.cards.account-completeness');
  8.     if (!blocks.length) {
  9.         return;
  10.     }
  11.  
  12.     var changeAccountCompleteness = function (params) {
  13.         updateAccountCompletenessSection(
  14.             params.percent,
  15.             params.sections.completed,
  16.             params.sections.total
  17.         );
  18.     };
  19.  
  20.     var sendForm = window.sendForm;
  21.     window.sendForm = function (form, callback, multipart, validationEnable) {
  22.         var middleware = function (response) {
  23.             if (typeof response === 'object' && 'accountCompleteness' in response) {
  24.                 changeAccountCompleteness(response.accountCompleteness);
  25.             }
  26.  
  27.             if (callback) {
  28.                 callback.apply(this, arguments);
  29.             }
  30.         };
  31.  
  32.         return sendForm(form, middleware, multipart, validationEnable);
  33.     }
  34. })();
  35.  
  36. // fill progress bar
  37. var updateAccountCompletenessSection = (function () {
  38.     var sections = document.querySelectorAll('.account-completeness'),
  39.         updateValues = function () {
  40.             sections.forEach(function (node) {
  41.                 var progressBar = node.querySelector('.progress__bar'),
  42.                     percent = node.querySelector('.percent');
  43.  
  44.                 if (!progressBar) {
  45.                     return;
  46.                 }else if (!progressBar.parentElement.offsetWidth) {// if element hidden
  47.                     progressBar.style.width = progressBar.getAttribute('data-percent') + '%';
  48.                 } else {
  49.                     fillProgressBar(progressBar, percent);
  50.                 }
  51.             });
  52.         };
  53.  
  54.     updateValues();
  55.  
  56.     return function (completenessPercent, completedSectionsNumber, totalSectionsNumber) {
  57.         sections.forEach(function (node) {
  58.             var progressBar = node.querySelector('.progress__bar');
  59.  
  60.             progressBar && progressBar.setAttribute('data-percent', completenessPercent);
  61.  
  62.             [
  63.                 [completedSectionsNumber, '.sections-completed'],
  64.                 [totalSectionsNumber, '.sections-total'],
  65.             ].forEach(function (item) {
  66.                 if (item[0] === undefined) {
  67.                     return;
  68.                 }
  69.  
  70.                 var block = node.querySelector(item[1]);
  71.                 if (block) {
  72.                     block.textContent = +item[0];
  73.                 }
  74.             });
  75.         });
  76.  
  77.         updateValues();
  78.     };
  79. })();
  80.  
  81. // agreemnt type checkbox
  82. (function () {
  83.     var form = document.querySelector('.form--agreement');
  84.     if (!form) {
  85.         return;
  86.     }
  87.  
  88.     form.querySelectorAll('input[type="radio"]').forEach(function (el) {
  89.         if (el.checked) {
  90.             el.parentElement.parentElement.classList.add('active');
  91.         }
  92.  
  93.         el.addEventListener('change', function () {
  94.             form.querySelectorAll('.form--agreement__checkbox').forEach(function (el) {
  95.                 el.classList.remove('active');
  96.             });
  97.  
  98.             this.parentElement.parentElement.classList.add('active');
  99.         });
  100.     })
  101. })();
  102.  
  103. // account information
  104. (function () {
  105.     var accountInfo = document.querySelector('.account-information');
  106.     if (!accountInfo) {
  107.         return;
  108.     }
  109.  
  110.     // one listener for all elements
  111.     var showAccountMembers = function () {
  112.         var formOfMember = accountInfo.querySelector('.form-block-of-member');
  113.         if (formOfMember) {
  114.             formOfMember.style.display = 'block';
  115.         }
  116.  
  117.         this.style.display = 'none';
  118.     };
  119.  
  120.     ['.account-information__button', '.edit-information'].forEach(function (selector) {
  121.         var node = accountInfo.querySelector(selector);
  122.         if (!node) {
  123.             return;
  124.         }
  125.  
  126.         node.addEventListener('click', showAccountMembers);
  127.     });
  128.    
  129.     new PhoneNumber();
  130. })();
  131.  
  132. // add couple member
  133. (function () {
  134.     var addMemberLink = document.querySelector('.add-another-member');
  135.     if (!addMemberLink) {
  136.         return;
  137.     }
  138.  
  139.     addMemberLink.addEventListener('click', function (e) {
  140.         e.preventDefault();
  141.  
  142.         var url = '/couples-club/account/add-couple-member';
  143.         var memberNumber = document.querySelectorAll('.member-wrapper').length;
  144.  
  145.         postRequest(url, { 'formNumber': memberNumber }, function (response) {
  146.             if (!response.success) {
  147.                 return;
  148.             }
  149.  
  150.             var newMembersBlock = document.getElementById('new_members_block');
  151.             if (newMembersBlock) {
  152.                 newMembersBlock.innerHTML += response.formHTML;
  153.             }
  154.  
  155.             CustomSelect.init();
  156.         });
  157.     });
  158. })();
  159.  
  160. // bank account
  161. (function () {
  162.     // form
  163.     var setListenersToForms = function (container, callback) {
  164.         (container || document).querySelectorAll('.bank_account_form').forEach(function (el) {
  165.             sendForm(el, function () {
  166.                 if (callback) {
  167.                     callback();
  168.                 }
  169.  
  170.                 location.href = location.href;
  171.             });
  172.         });
  173.     };
  174.  
  175.     setListenersToForms();
  176.  
  177.     if (!('PopupPage' in window)) {
  178.         return;
  179.     }
  180.  
  181.     var popupInstance = PopupPage.getInstanceByName('bank-account-popup');
  182.     if (!popupInstance) {
  183.         return;
  184.     }
  185.  
  186.     var pageData = {
  187.         title: popupInstance.wrapper.querySelector('.popup-content__title').textContent,
  188.         text: popupInstance.wrapper.querySelector('.popup-content__text').outerHTML,
  189.         content: popupInstance.wrapper.querySelectorAll('.tabs-list, .tab-block').map(function (node) {
  190.             return node.outerHTML;
  191.         })
  192.     };
  193.  
  194.     popupInstance.subscribe('shown', function (container) {
  195.         container.querySelector('.popup-content__title').textContent = pageData.title;
  196.         container.querySelector('.popup-content__text').innerHTML = pageData.text;
  197.  
  198.         var form = container.querySelector('.popup-page__content-form');
  199.         if (form) {
  200.             form.parentNode.removeChild(form);
  201.         }
  202.  
  203.         var tabsName = 'bank-account-' + (new Date().getTime() + Math.random());
  204.  
  205.         var content = document.createElement('div');
  206.         content.setAttribute('data-tabs-name', tabsName);
  207.         content.innerHTML = pageData.content.join('\n')
  208.  
  209.         container.querySelector('.popup-page__content-wrapper').appendChild(content);
  210.  
  211.         content.querySelectorAll('.close-popup').forEach(function (node) {
  212.             node.addEventListener('click', function (e) {
  213.                 e.preventDefault();
  214.  
  215.                 popupInstance.hide();
  216.             });
  217.         });
  218.  
  219.         setListenersToForms(container, function () {
  220.             popupInstance.hide();
  221.         });
  222.  
  223.         if ('Tabs' in window) {
  224.             var tabsInstance = Tabs.getInstanceByName(tabsName);
  225.             if (tabsInstance) {
  226.                 tabsInstance.openDefaultTab();
  227.             }
  228.         }
  229.     });
  230. })();
  231.  
  232. (function () {
  233.     // bank data formatters
  234.    
  235.     var accountNumberFormatter = getStringFormatter(function (value) {
  236.             return value.replace(/\D+?/g, '');
  237.         }),
  238.         accountNumberListener = function () {
  239.             this.value = accountNumberFormatter(this.value);
  240.         };
  241.  
  242.     document.querySelectorAll('.bank-account-number').forEach(function (el) {
  243.         el.addEventListener('input', accountNumberListener);
  244.         accountNumberListener.call(el);
  245.     });
  246.  
  247.     var ibanNumberFormatter = getStringFormatter(),
  248.         ibanNumberListener = function () {
  249.             this.value = ibanNumberFormatter(this.value);
  250.         };
  251.        
  252.     document.querySelectorAll('.bank-iban-number').forEach(function (el) {
  253.         el.addEventListener('input', ibanNumberListener);
  254.         ibanNumberListener.call(el);
  255.     });
  256. })();
  257.  
  258. // agreement type
  259. (function () {
  260.     var agreementForm = document.getElementById('agreement-form');
  261.     if (!agreementForm) {
  262.         return;
  263.     }
  264.  
  265.     sendForm(agreementForm, function (response) {
  266.         if (!response.success) {
  267.             return;
  268.         }
  269.  
  270.         agreementForm.remove();
  271.         document.querySelector('div.form-block-of-member').innerHTML += response.formHTML;
  272.     });
  273. })();
  274.  
  275. // profile form
  276. (function () {
  277.     var profile_popup = PopupPage.getInstanceByName('profile-form-popup-page');
  278.     console.log(profile_popup);
  279.     profile_popup.subscribe('shown', function(container) {
  280.         container.querySelectorAll('.profile_form').forEach(function(profile_form) {
  281.             addEvents(profile_form);
  282.         });
  283.     });
  284.    
  285.     var addEvents = function(profileForm) {
  286.         console.log(profileForm);
  287.         document.addEventListener('click', function(e) {
  288.             if (e.target && e.target.name == 'relationship_type[other]') {
  289.                 var repsonalInfo = e.target.closest('.person-info'),
  290.                     relationshipOtherWrapper = repsonalInfo ? repsonalInfo.querySelector('.relationship-other-wrapper') : null;
  291.                    
  292.                 if (relationshipOtherWrapper) {
  293.                     relationshipOtherWrapper.style.display = e.target.checked ? 'block' : 'none';
  294.                 }
  295.             }
  296.         });
  297.  
  298.         var countWords = function(text) {
  299.             text = text.replace(/[\s\v\n]+?/gim, ' ');
  300.  
  301.             var words = text.split(' ').filter(function(word) {
  302.                 return word.length > 2;
  303.             });
  304.  
  305.             return words.length;
  306.         }
  307.  
  308.         document.addEventListener('keyup', function(e) {
  309.             if (e.target && e.target.name == 'story') {
  310.                 var storyWrapper = e.target.closest('.story-wrapper'),
  311.                     minWordsBlock = storyWrapper ? storyWrapper.querySelector('.min-words') : null;
  312.  
  313.                 if (minWordsBlock) {
  314.                     minWordsBlock.textContent = countWords(e.target.value) + '/150 words';
  315.                 }
  316.             }
  317.         })
  318.  
  319.         var submitButton = profileForm.querySelector('.submit-photos__block__button');
  320.         console.log(submitButton);
  321.         if (submitButton) {
  322.             var listener = function () {
  323.                 var requiredFields = [].slice.call(
  324.                         profileForm.querySelectorAll('input[type="text"][name], textarea')
  325.                     ),
  326.                     checkboxOther = profileForm.querySelector('input[name="relationship_type[other]"]'),
  327.                     disableSubmitButton = requiredFields.some(function (field) {
  328.                         if (field.name == 'relationship_other' && !checkboxOther.checked)  {
  329.                             return false;
  330.                         }
  331.  
  332.                         return !field.value.trim().length;
  333.                     });
  334.  
  335.                 if (!disableSubmitButton) {
  336.                     disableSubmitButton = [].every.call(
  337.                         profileForm.querySelectorAll('input[name^="relationship_type"]'),
  338.                         function (checkbox) {
  339.                             return !checkbox.checked;
  340.                         }
  341.                     );
  342.                 }
  343.  
  344.                 submitButton.disabled = disableSubmitButton;
  345.             };
  346.  
  347.             profileForm.addEventListener('input', listener);
  348.             profileForm.addEventListener('change', listener);
  349.             profileForm.addEventListener('change', function() {
  350.                 console.log(123);
  351.             });
  352.             profileForm.addEventListener('click', listener);
  353.  
  354.             listener();
  355.         }
  356.  
  357.         sendForm(profileForm, function (response) {
  358.             if (!response.success) {
  359.                 return;
  360.             }
  361.  
  362.             var popup = document.getElementById('profile-form').popup;
  363.             if(popup) {
  364.                 popup.subscribe('popup-hidden', function() {
  365.                     location.href = location.href;
  366.                 });
  367.                 popup.hide();
  368.             }
  369.         });
  370.     }
  371.  
  372.     document.querySelectorAll('.profile_form').forEach(function(profile_form) {
  373.         addEvents(profile_form);
  374.     });
  375. })();
  376.  
  377. // account completed button
  378. (function () {
  379.     var accCompletedBtn = document.querySelector('.account-completed');
  380.     if (!accCompletedBtn) {
  381.         return;
  382.     }
  383.  
  384.     accCompletedBtn.addEventListener('click', function (e) {
  385.         e.preventDefault();
  386.  
  387.         location.href = '/couples-club/submissions-item/submit';
  388.     });
  389. })();
  390.  
  391. // member IDs and profile photos dropzones
  392. (function () {
  393.     var dropzonePreviewTemplate = document.getElementById('dropzone-preview-template');
  394.  
  395.     var getDropzoneConfig = (function () {
  396.         var dropzoneConfig = {
  397.             uploadMultiple: true,
  398.             addRemoveLinks: !dropzonePreviewTemplate,
  399.             dictRemoveFile: '<img src="/modules/CouplesClub/template/assets/icons/trash-icon.svg">',
  400.             dictCancelUpload: '<img src="/modules/CouplesClub/template/assets/icons/trash-icon.svg">'
  401.         };
  402.  
  403.         if (dropzonePreviewTemplate) {
  404.             dropzoneConfig.previewTemplate = dropzonePreviewTemplate.innerHTML;
  405.         }
  406.  
  407.         return function (el) {
  408.             var config = JSON.parse(JSON.stringify(dropzoneConfig));
  409.  
  410.             config.init = function () {
  411.                 var paramName = el.getAttribute('data-param-name');
  412.                 if (paramName) {
  413.                     this.options.paramName = paramName;
  414.                 }
  415.  
  416.                 var loadPhotosUrl = el.getAttribute('data-load-photos-url');
  417.                 if (!loadPhotosUrl) {
  418.                     return;
  419.                 }
  420.  
  421.                 var instance = this;
  422.                 getRequest(loadPhotosUrl, function (response) {
  423.                     response.forEach(function (value, key) {
  424.                         var mockFile = {
  425.                             name: value.name,
  426.                             size: value.size
  427.                         };
  428.  
  429.                         instance.options.addedfile.call(instance, mockFile);
  430.                         instance.options.thumbnail.call(instance, mockFile, value.image);
  431.  
  432.                         mockFile.previewElement.setAttribute('data-id', value.id);
  433.                     });
  434.                 });
  435.             };
  436.  
  437.             config.removedfile = function (file) {
  438.                 var removeUrl = el.getAttribute('data-remove-url'),
  439.                     fileId = file.previewElement.getAttribute('data-id'),
  440.                     remove = function () {
  441.                         file.previewElement.parentNode.removeChild(file.previewElement);
  442.                     };
  443.  
  444.                 if (!removeUrl || !fileId) {
  445.                     return void remove();
  446.                 };
  447.  
  448.                 postRequest(removeUrl, { 'id': fileId }, remove);
  449.             };
  450.  
  451.             return config;
  452.         };
  453.     })();
  454.  
  455.     var popupsInstances = (function () {
  456.         if (!('PopupPage' in window)) {
  457.             return [];
  458.         }
  459.  
  460.         var instances = ['submit-photos-popup', 'copies-ids-popup'].reduce(function (res, name) {
  461.             var instance = PopupPage.getInstanceByName(name);
  462.             if (instance) {
  463.                 res.push(instance);
  464.             }
  465.  
  466.             return res;
  467.         }, []);
  468.  
  469.         instances.forEach(function(instance) {
  470.             var pageData = {
  471.                 title: instance.wrapper.querySelector('.popup-content__title').textContent,
  472.                 text: instance.wrapper.querySelector('.popup-content__text').outerHTML,
  473.                 content: instance.wrapper.querySelectorAll(
  474.                     '.submit-wrapper, .submit-photos-popup__submit-button-wrapper'
  475.                 ).map(function (node) {
  476.                     return node.outerHTML;
  477.                 })
  478.             };
  479.  
  480.             instance.subscribe('shown', function (container) {
  481.                 container.querySelector('.popup-content__title').textContent = pageData.title;
  482.                 container.querySelector('.popup-content__text').innerHTML = pageData.text;
  483.  
  484.                 var content = document.createElement('div');
  485.                 content.innerHTML = pageData.content.join('\n')
  486.  
  487.                 container.querySelector('.popup-page__content-wrapper').appendChild(content);
  488.             });
  489.         });
  490.  
  491.         return instances;
  492.     })();
  493.  
  494.     // initialize dropzones
  495.     var initializer = function (container) {
  496.         (container || document).querySelectorAll('.dropzone-multiple').forEach(function (el) {
  497.             if (el.__dropZoneInstance !== undefined) {
  498.                 return;
  499.             }
  500.  
  501.             var instance,
  502.                 deferredUploading = !!el.getAttribute('data-deferred-uploading'),
  503.                 dropzoneConfig = getDropzoneConfig(el);
  504.  
  505.             dropzoneConfig.autoProcessQueue = !deferredUploading;
  506.  
  507.             if (!!el.getAttribute('data-upload-only-images')) {
  508.                 dropzoneConfig.addedfile = function () {
  509.                     errorMessage.reset();
  510.  
  511.                     Dropzone.prototype.defaultOptions.addedfile.apply(this, arguments);
  512.                 };
  513.                
  514.                 dropzoneConfig.accept = function (file, done) {
  515.                     if (isImageMimeType(file.type)) {
  516.                         done();
  517.  
  518.                         return;
  519.                     }
  520.  
  521.                     instance.removeFile(file);
  522.  
  523.                     errorMessage(
  524.                         el,
  525.                         "Oops! That's the wrong format. Only JPG or PNG files are accepted."
  526.                     ).classList.add('error-message');
  527.                 };
  528.             }
  529.  
  530.             instance = new Dropzone(el, dropzoneConfig);
  531.  
  532.             el.__dropZoneInstance = instance;
  533.  
  534.             if (dropzonePreviewTemplate) {
  535.                 initRemovingConfirmation(el);
  536.             }
  537.         });
  538.     };
  539.  
  540.     popupsInstances.forEach(function (instance) {
  541.         instance.subscribe('shown', function (container) {
  542.             initializer(container);
  543.  
  544.             container.querySelectorAll('.dropzone').forEach(function (node) {
  545.                 node.addEventListener('click', function () {
  546.                     this.querySelectorAll('.photo-list.pseudo').forEach(function (confirmWindow) {
  547.                         if (confirmWindow.classList.contains('popup')) {
  548.                             return;
  549.                         }
  550.  
  551.                         confirmWindow.classList.add('popup');
  552.                         confirmWindow.querySelector('.delete-confirm').classList.add('popup__change-password');
  553.                     });
  554.                 });
  555.             });
  556.         })
  557.     });
  558.  
  559.     initializer();
  560.  
  561.     var popupInstance = PopupPage.getInstanceByName('submit-photos-popup');
  562.     if (!popupInstance) {
  563.         return;
  564.     }
  565.  
  566.     popupInstance.subscribe('shown', function (container) {
  567.         var wrapper = container.querySelector('.popup-page__content-wrapper'),
  568.             popup = document.querySelector('[data-popup-target="submit-photos"] .popup__account-page__wrapper');
  569.  
  570.         if (!wrapper || !popup) {
  571.             return;
  572.         }
  573.  
  574.         ['validationError', 'successfullyUploaded'].forEach(function (eventName){
  575.             wrapper.addEventListener(eventName, function(e) {
  576.                 popup.dispatchEvent(
  577.                     new Event(eventName, { bubbles: true })
  578.                 );
  579.             });
  580.         });
  581.     });
  582.  
  583. })();
  584.  
  585. // submit photos dropzones events handlers
  586. (function () {
  587.     var popupBlock = document.getElementById('submit-photos');
  588.     if(!popupBlock || !popupBlock.popup) {
  589.         return;
  590.     }
  591.  
  592.     popupBlock.addEventListener('validationError', function () {
  593.         var errorPopup = document.getElementById('profile-photos-error-message').popup;
  594.         if(errorPopup) {
  595.             errorPopup.show();
  596.         }
  597.     });
  598.  
  599.     popupBlock.addEventListener('successfullyUploaded', function () {
  600.         location.href = location.href;
  601.     });
  602. })();
  603.  
  604. // submit copies-ids dropzones events handlers
  605. (function () {
  606.     var popupBlock = document.getElementById('copies-id');
  607.     if (!popupBlock || !popupBlock.popup) {
  608.         return;
  609.     }
  610.  
  611.     var successfullyUploadedListener = function () {
  612.         var section = document.querySelector('.cards.copies-id');
  613.         if (!section) {
  614.             return;
  615.         }
  616.  
  617.         section.classList.add('filled-completely');
  618.     };
  619.  
  620.     popupBlock.addEventListener('successfullyUploaded', function () {
  621.         popupBlock.popup.hide();
  622.  
  623.         successfullyUploadedListener();
  624.     });
  625.  
  626.     var updateSaveButtonState = function (container) {
  627.             var saveButton = container.querySelector('#save-document-data');
  628.             if (!saveButton) {
  629.                 return;
  630.             }
  631.  
  632.             var filesTotal = 0,
  633.                 filesForUploading = 0,
  634.                 allowUpload = true;
  635.  
  636.             getDropzones(container).forEach(function (instance) {
  637.                 var filesToUpload = instance.getQueuedFiles().length,
  638.                     minFiles = +(instance.element.getAttribute('data-min-files') || 0);
  639.  
  640.                 filesTotal += instance.files.length;
  641.                 filesForUploading += filesToUpload;
  642.  
  643.                 if (allowUpload && filesToUpload < minFiles) {
  644.                     allowUpload = false;
  645.                 }
  646.             });
  647.  
  648.             saveButton.disabled = !filesForUploading || !allowUpload;
  649.         },
  650.         getDropzones = function (container) {
  651.             return [].reduce.call(
  652.                 container.querySelectorAll('.dropzone-multiple'),
  653.                 function (result, node) {
  654.                     var instance = node.__dropZoneInstance;
  655.                     if (instance) {
  656.                         result.push(instance);
  657.                     }
  658.  
  659.                     return result;
  660.                 },
  661.                 []
  662.             );
  663.         };
  664.  
  665.     var init = function (container) {
  666.         getDropzones(container).forEach(function (instance) {
  667.             instance.on('addedfile', function (file) {
  668.                 if (!isImageMimeType(file.type)) {
  669.                     file.previewElement.classList.add('dz-image-preview');
  670.                 };
  671.  
  672.                 setTimeout(function () {
  673.                     updateSaveButtonState(container);
  674.                 }, 0);
  675.             });
  676.             instance.on('removedfile', function () {
  677.                 updateSaveButtonState(container);
  678.             });
  679.         });
  680.     };
  681.  
  682.     init(popupBlock);
  683.     updateSaveButtonState(popupBlock);
  684.  
  685.     var popupPageInstance = PopupPage.getInstanceByName('copies-ids-popup');
  686.     if (!popupPageInstance) {
  687.         return;
  688.     }
  689.  
  690.     popupPageInstance.subscribe('shown', function (container) {
  691.         init(container);
  692.  
  693.         container.addEventListener('successfullyUploaded', function () {
  694.             popupPageInstance.hide();
  695.  
  696.             successfullyUploadedListener();
  697.         });
  698.     });
  699.  
  700.     var saveCopiesIds = function() {
  701.             var documentFormCount = document.querySelectorAll('.member-document-form').length,
  702.                 successfullyFormCount = document.querySelectorAll('.member-document-form.successfully-submitted').length;
  703.  
  704.             if (documentFormCount != successfullyFormCount) {
  705.                 return;
  706.             }
  707.  
  708.             dropzoneUploadPhotos(document.querySelector('#save-document-data'));
  709.         };
  710.  
  711.     document.addEventListener('click', function (e) {
  712.         if (e.target.getAttribute('data-action') !== 'save-document-data') {
  713.             return;
  714.         }
  715.  
  716.         document.querySelectorAll('.member-document-form').forEach(function (form) {
  717.             sendForm(form, function (response) {
  718.                 if (response.success) {
  719.                     form.classList.add('successfully-submitted');
  720.                 }
  721.                 saveCopiesIds();
  722.             });
  723.  
  724.             form.dispatchEvent(new Event('submit'));
  725.         });
  726.     });
  727. })();
  728.  
  729. // payout request
  730. (function () {
  731.     var popupWrapper = document.getElementById('payout-request-popup');
  732.     if (!popupWrapper) {
  733.         return;
  734.     }
  735.  
  736.     var payoutRequestPopup = popupWrapper.popup;
  737.  
  738.     document.querySelectorAll('.payment-information__payout').forEach(function (node) {
  739.         var button = node.querySelector('button.btn--submit');
  740.  
  741.         button.addEventListener('click', function (e) {
  742.             e.preventDefault();
  743.  
  744.             getRequest('/couples-club/submissions/payout-request/', function (response) {
  745.                 if (!!response.success && payoutRequestPopup) {
  746.                     payoutRequestPopup.show();
  747.  
  748.                     node.querySelector('.price span').textContent = 0 + '€';
  749.                     button.disabled = true;
  750.                 }
  751.             })
  752.         });
  753.     });
  754.  
  755.     if (!payoutRequestPopup) {
  756.         return;
  757.     }
  758. })();
  759.  
  760. // remove member of couple button
  761. (function () {
  762.     var section = document.querySelector('.cards.account-information'),
  763.         confirmationPopup = document.getElementById('couple-member-removing-confirmation'),
  764.         listener = function () {};
  765.  
  766.     if (!section || !confirmationPopup || !confirmationPopup.popup) {
  767.         return;
  768.     }
  769.  
  770.     confirmationPopup.popup.subscribe('popup-hidden', function () {
  771.         var confirmButton = confirmationPopup.querySelector('[data-confirm]');
  772.         if (confirmButton) {
  773.             setTimeout(function () {
  774.                 confirmButton.removeEventListener('click', listener);
  775.             }, 1);
  776.         }
  777.     });
  778.  
  779.     section.addEventListener('click', function (e) {
  780.         var target = e.target || e.srcElement;
  781.         if (!target || !target.classList.contains('account-member__remove-button')) {
  782.             target = target.closest('.account-member__remove-button');
  783.            
  784.             if (!target) {
  785.                 return;
  786.             }
  787.         }
  788.  
  789.         var memberBlock = target.closest('.member-wrapper'),
  790.             requestUrl = target.getAttribute('data-request-url'),
  791.             confirmButton = confirmationPopup.querySelector('[data-confirm]');
  792.  
  793.         if (!memberBlock || !confirmButton) {
  794.             return;
  795.         }
  796.  
  797.         listener = function () {
  798.             var removeMemberForm = function () {
  799.                 memberBlock.parentNode.removeChild(memberBlock);
  800.             };
  801.  
  802.             if (!requestUrl) {
  803.                 return void removeMemberForm();
  804.             }
  805.  
  806.             getRequest(requestUrl, function (res) {
  807.                 if (!!res.success) {
  808.                     removeMemberForm();
  809.                 }
  810.             });
  811.         };
  812.        
  813.         confirmButton.addEventListener('click', listener);
  814.        
  815.         confirmationPopup.popup.show();
  816.         confirmationPopup.setAttribute('tabindex', 0);
  817.         confirmationPopup.focus();
  818.     });
  819. })();
  820.  
  821. (function() {
  822.     var loadPhotos = document.querySelectorAll('.load-photos');
  823.  
  824.     if (!loadPhotos) {
  825.         return;
  826.     }
  827.    
  828.     loadPhotos.forEach(function(container) {
  829.         var url = container.getAttribute('data-load-photos-url');
  830.  
  831.         if (!url) {
  832.             return;
  833.         }
  834.  
  835.         getRequest(url, function(images) {
  836.             for (var key in images) {
  837.                 var image = images[key];
  838.                 var img = document.createElement('img');
  839.                 img.classList.add('image-item');
  840.                 img.src = image['image'];
  841.                 container.appendChild(img);
  842.             }
  843.         });
  844.     });
  845. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement