| 1 |
/** |
| 2 |
* MLS Import — deactivation exit survey. |
| 3 |
* |
| 4 |
* Intercepts the "Deactivate" link for the MLS Import plugin on the Plugins |
| 5 |
* screen, shows a short survey modal, sends the chosen reason to the server, |
| 6 |
* then proceeds with the original deactivation. Submitting or skipping both |
| 7 |
* complete the deactivation — the user is never trapped. |
| 8 |
*/ |
| 9 |
( function ( $ ) { |
| 10 |
'use strict'; |
| 11 |
|
| 12 |
var cfg = window.mlsimport_deact_survey || {}; |
| 13 |
|
| 14 |
$( function () { |
| 15 |
|
| 16 |
// The Deactivate link inside the MLS Import plugin's row. |
| 17 |
var $link = $( 'tr[data-plugin="' + cfg.plugin_basename + '"] span.deactivate a' ); |
| 18 |
if ( ! $link.length ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
var deactivateUrl = ''; |
| 23 |
|
| 24 |
// --- build the modal once ------------------------------------------ |
| 25 |
var optionsHtml = ''; |
| 26 |
$.each( cfg.options || {}, function ( value, label ) { |
| 27 |
optionsHtml += |
| 28 |
'<label class="mlsimport-es-option">' + |
| 29 |
'<input type="radio" name="mlsimport_exit_reason" value="' + value + '"> ' + |
| 30 |
'<span>' + label + '</span>' + |
| 31 |
'</label>'; |
| 32 |
} ); |
| 33 |
|
| 34 |
var $modal = $( |
| 35 |
'<div id="mlsimport-exit-survey-modal" class="mlsimport-es-hidden">' + |
| 36 |
'<div class="mlsimport-es-backdrop"></div>' + |
| 37 |
'<div class="mlsimport-es-card" role="dialog" aria-modal="true">' + |
| 38 |
'<h2 class="mlsimport-es-title">' + ( cfg.i18n.title || '' ) + '</h2>' + |
| 39 |
'<p class="mlsimport-es-intro">' + ( cfg.i18n.intro || '' ) + '</p>' + |
| 40 |
'<div class="mlsimport-es-options">' + optionsHtml + '</div>' + |
| 41 |
'<textarea class="mlsimport-es-other mlsimport-es-hidden" rows="3" ' + |
| 42 |
'placeholder="' + ( cfg.i18n.other_placeholder || '' ) + '"></textarea>' + |
| 43 |
'<div class="mlsimport-es-actions">' + |
| 44 |
'<button type="button" class="button button-primary mlsimport-es-submit" disabled>' + |
| 45 |
( cfg.i18n.submit || '' ) + |
| 46 |
'</button>' + |
| 47 |
'<a href="#" class="mlsimport-es-skip">' + ( cfg.i18n.skip || '' ) + '</a>' + |
| 48 |
'</div>' + |
| 49 |
'</div>' + |
| 50 |
'</div>' |
| 51 |
); |
| 52 |
$( 'body' ).append( $modal ); |
| 53 |
|
| 54 |
var $submit = $modal.find( '.mlsimport-es-submit' ); |
| 55 |
var $other = $modal.find( '.mlsimport-es-other' ); |
| 56 |
|
| 57 |
// --- intercept the Deactivate click -------------------------------- |
| 58 |
$link.on( 'click', function ( e ) { |
| 59 |
e.preventDefault(); |
| 60 |
deactivateUrl = $( this ).attr( 'href' ); |
| 61 |
$modal.removeClass( 'mlsimport-es-hidden' ); |
| 62 |
} ); |
| 63 |
|
| 64 |
// Selecting a reason enables Submit; "other" reveals the text field. |
| 65 |
$modal.on( 'change', 'input[name="mlsimport_exit_reason"]', function () { |
| 66 |
$submit.prop( 'disabled', false ); |
| 67 |
$other.toggleClass( 'mlsimport-es-hidden', this.value !== 'other' ); |
| 68 |
} ); |
| 69 |
|
| 70 |
function goDeactivate() { |
| 71 |
if ( deactivateUrl ) { |
| 72 |
window.location.href = deactivateUrl; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// Submit: send the answer, then deactivate regardless of the result. |
| 77 |
$submit.on( 'click', function () { |
| 78 |
$submit.prop( 'disabled', true ); |
| 79 |
$.post( cfg.ajax_url, { |
| 80 |
action: 'mlsimport_exit_survey_submit', |
| 81 |
security: cfg.nonce, |
| 82 |
reason: $modal.find( 'input[name="mlsimport_exit_reason"]:checked' ).val() || '', |
| 83 |
details: $other.val() || '' |
| 84 |
} ).always( goDeactivate ); |
| 85 |
} ); |
| 86 |
|
| 87 |
// Skip: no data sent, deactivate immediately. |
| 88 |
$modal.on( 'click', '.mlsimport-es-skip', function ( e ) { |
| 89 |
e.preventDefault(); |
| 90 |
goDeactivate(); |
| 91 |
} ); |
| 92 |
|
| 93 |
} ); |
| 94 |
|
| 95 |
}( jQuery ) ); |
| 96 |
|