latepoint
/
lib
/
kit
/
bsf-analytics
/
modules
/
deactivation-survey
/
assets
/
js
Last commit date
feedback.js
5 months ago
feedback.min.js
5 months ago
feedback.js
240 lines
| 1 | ( function ( $ ) { |
| 2 | const UserDeactivationPopup = { |
| 3 | slug: '', |
| 4 | skipButton: '', |
| 5 | formWrapper: '', |
| 6 | radioButton: '', |
| 7 | closeButton: '', |
| 8 | buttonAction: '', |
| 9 | feedbackForm: '', |
| 10 | feedbackInput: '', |
| 11 | deactivateUrl: '', |
| 12 | buttonTrigger: '', |
| 13 | deactivateButton: '', |
| 14 | submitDeactivate: '', |
| 15 | |
| 16 | /** |
| 17 | * Caches elements for later use. |
| 18 | */ |
| 19 | _cacheElements() { |
| 20 | this.slug = udsVars?._plugin_slug || ''; |
| 21 | this.skipButton = $( '.uds-feedback-skip' ); |
| 22 | this.submitDeactivate = $( '.uds-feedback-submit' ); |
| 23 | this.deactivateButton = $( '#the-list' ).find( |
| 24 | `.row-actions span.deactivate a` |
| 25 | ); |
| 26 | this.feedbackForm = $( '.uds-feedback-form' ); // Feedback Form. |
| 27 | this.feedbackInput = $( '.uds-options-feedback' ); // Feedback Textarea. |
| 28 | this.formWrapper = $( '.uds-feedback-form--wrapper' ); |
| 29 | this.closeButton = $( '.uds-feedback-form--wrapper .uds-close' ); |
| 30 | this.radioButton = $( '.uds-reason-input' ); |
| 31 | }, |
| 32 | |
| 33 | /** |
| 34 | * Shows the feedback popup by adding the 'show' class to the form wrapper. |
| 35 | * |
| 36 | * @param {string} slug - The slug of the plugin. |
| 37 | */ |
| 38 | _showPopup( slug ) { |
| 39 | $( `#deactivation-survey-${ slug }` ).addClass( 'show_popup' ); |
| 40 | }, |
| 41 | |
| 42 | /** |
| 43 | * Hides the feedback popup by removing the 'show' class from the form wrapper. |
| 44 | */ |
| 45 | _hidePopup() { |
| 46 | this.formWrapper.removeClass( 'show_popup' ); |
| 47 | }, |
| 48 | |
| 49 | /** |
| 50 | * Redirects to the deactivate URL if it exists, otherwise reloads the current page. |
| 51 | */ |
| 52 | _redirectOrReload() { |
| 53 | if ( this.deactivateUrl ) { |
| 54 | window.location.href = this.deactivateUrl; |
| 55 | } else { |
| 56 | location.reload(); |
| 57 | } |
| 58 | }, |
| 59 | |
| 60 | /** |
| 61 | * Toggles the visibility of the feedback form and CTA based on the event target's attributes. |
| 62 | * |
| 63 | * @param {Event} event - The event that triggered this function. |
| 64 | */ |
| 65 | _hideShowFeedbackAndCTA( event ) { |
| 66 | const acceptFeedback = |
| 67 | $( event.target ).attr( 'data-accept_feedback' ) === 'true'; |
| 68 | const showCta = |
| 69 | $( event.target ).attr( 'data-show_cta' ) === 'true'; |
| 70 | |
| 71 | $( event.target ) |
| 72 | .closest( this.formWrapper ) |
| 73 | .find( '.uds-options-feedback' ) |
| 74 | .removeClass( 'hide' ) |
| 75 | .addClass( acceptFeedback ? 'show' : 'hide' ); |
| 76 | $( event.target ) |
| 77 | .closest( this.formWrapper ) |
| 78 | .find( '.uds-option-feedback-cta' ) |
| 79 | .removeClass( 'hide' ) |
| 80 | .addClass( showCta ? 'show' : 'hide' ); |
| 81 | }, |
| 82 | |
| 83 | /** |
| 84 | * Changes the placeholder text of the feedback input based on the event target's attribute. |
| 85 | * |
| 86 | * @param {Event} event - The event that triggered this function. |
| 87 | */ |
| 88 | _changePlaceholderText( event ) { |
| 89 | const radioButtonPlaceholder = |
| 90 | event.target.getAttribute( 'data-placeholder' ); |
| 91 | |
| 92 | $( event.target ) |
| 93 | .closest( this.formWrapper ) |
| 94 | .find( this.feedbackInput ) |
| 95 | .attr( 'placeholder', radioButtonPlaceholder || '' ); |
| 96 | |
| 97 | this._hideShowFeedbackAndCTA( event ); |
| 98 | }, |
| 99 | |
| 100 | /** |
| 101 | * Submits the feedback form and handles the response. |
| 102 | * |
| 103 | * @param {Event} event - The event that triggered this function. |
| 104 | * @param {Object} self - A reference to the current object. |
| 105 | */ |
| 106 | _submitFeedback( event, self ) { |
| 107 | event.preventDefault(); |
| 108 | |
| 109 | const currentForm = $( event.target ); |
| 110 | const closestForm = currentForm.closest( this.feedbackForm ); // Cache the closest form |
| 111 | |
| 112 | // Gather form data. |
| 113 | const formData = { |
| 114 | action: 'uds_plugin_deactivate_feedback', |
| 115 | security: udsVars?._ajax_nonce || '', |
| 116 | reason: |
| 117 | closestForm |
| 118 | .find( this.radioButton.filter( ':checked' ) ) |
| 119 | .val() || '', // Get the selected radio button value from the current form. |
| 120 | source: closestForm.find( 'input[name="source"]' ).val() || '', |
| 121 | referer: |
| 122 | closestForm.find( 'input[name="referer"]' ).val() || '', |
| 123 | version: |
| 124 | closestForm.find( 'input[name="version"]' ).val() || '', |
| 125 | feedback: closestForm.find( this.feedbackInput ).val() || '', // Get the feedback input value from the current form. |
| 126 | }; |
| 127 | |
| 128 | currentForm |
| 129 | .find( '.uds-feedback-' + this.buttonAction ) |
| 130 | .text( 'Deactivating.' ) |
| 131 | .addClass( 'processing' ); |
| 132 | |
| 133 | // Prepare AJAX call. |
| 134 | $.ajax( { |
| 135 | url: udsVars?.ajaxurl, // URL to send the request to. |
| 136 | type: 'POST', // HTTP method. |
| 137 | data: formData, // Data to be sent. |
| 138 | success( response ) { |
| 139 | if ( response.success ) { |
| 140 | self._redirectOrReload(); |
| 141 | } |
| 142 | self._hidePopup(); |
| 143 | }, |
| 144 | /* eslint-disable */ |
| 145 | error( xhr, status, error ) { |
| 146 | /* eslint-disable */ |
| 147 | self._redirectOrReload(); |
| 148 | }, |
| 149 | } ); |
| 150 | }, |
| 151 | |
| 152 | _handleClick( e ) { |
| 153 | // Close feedback form or show/hide popup if clicked outside and add a click on a Activate button of Theme. |
| 154 | if ( |
| 155 | e.target.classList.contains( 'show_popup' ) && |
| 156 | e.target.closest( '.uds-feedback-form--wrapper' ) |
| 157 | ) { |
| 158 | this._hidePopup(); |
| 159 | } else if ( e.target.classList.contains( 'activate' ) ) { |
| 160 | this.deactivateUrl = e.target.href; |
| 161 | |
| 162 | // Don't show for Child Themes if parent theme is active & Parent Theme if child theme is active. |
| 163 | if ( |
| 164 | -1 !== |
| 165 | this.deactivateUrl.indexOf( |
| 166 | `stylesheet=${ udsVars?._current_theme }-child` |
| 167 | ) || |
| 168 | -1 !== |
| 169 | this.deactivateUrl.indexOf(`stylesheet=${udsVars?._current_theme}&`) |
| 170 | ) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | e.preventDefault(); |
| 175 | this._showPopup( udsVars?._current_theme ); |
| 176 | } |
| 177 | }, |
| 178 | |
| 179 | /** |
| 180 | * Initializes the feedback popup by caching elements and binding events. |
| 181 | */ |
| 182 | _init() { |
| 183 | this._cacheElements(); |
| 184 | this._bind(); |
| 185 | }, |
| 186 | |
| 187 | /** |
| 188 | * Binds event listeners to various elements to handle user interactions. |
| 189 | */ |
| 190 | _bind() { |
| 191 | const self = this; // Store reference to the current object. |
| 192 | |
| 193 | // Open the popup when clicked on the deactivate button. |
| 194 | this.deactivateButton.on( 'click', function ( event ) { |
| 195 | let closestTr = $( event.target ).closest( 'tr' ); |
| 196 | let slug = closestTr.data( 'slug' ); |
| 197 | |
| 198 | if ( self.slug.includes( slug ) ) { |
| 199 | event.preventDefault(); |
| 200 | // Set the deactivation URL. |
| 201 | self.deactivateUrl = $( event.target ).attr( 'href' ); |
| 202 | self._showPopup( slug ); |
| 203 | } |
| 204 | } ); |
| 205 | |
| 206 | // Close the popup on a click of Close button. |
| 207 | this.closeButton.on( 'click', function ( event ) { |
| 208 | event.preventDefault(); |
| 209 | self._hidePopup(); // Use self to refer to the UserDeactivationPopup instance. |
| 210 | } ); |
| 211 | |
| 212 | // Click event on radio button to change the placeholder of textarea. |
| 213 | this.radioButton.on( 'click', function ( event ) { |
| 214 | self._changePlaceholderText( event ); |
| 215 | } ); |
| 216 | |
| 217 | // Combined submit and skip button actions. |
| 218 | this.submitDeactivate |
| 219 | .add( this.skipButton ) |
| 220 | .on( 'click', function ( event ) { |
| 221 | event.preventDefault(); // Prevent default button action. |
| 222 | self.buttonAction = $( event.target ).attr( 'data-action' ); |
| 223 | $( event.target ).closest( self.feedbackForm ).submit(); |
| 224 | } ); |
| 225 | |
| 226 | this.feedbackForm.on( 'submit', function ( event ) { |
| 227 | self._submitFeedback( event, self ); |
| 228 | } ); |
| 229 | |
| 230 | document.addEventListener( 'click', function ( e ) { |
| 231 | self._handleClick( e ); |
| 232 | } ); |
| 233 | }, |
| 234 | }; |
| 235 | |
| 236 | $( function () { |
| 237 | UserDeactivationPopup._init(); |
| 238 | } ); |
| 239 | } )( jQuery ); |
| 240 |