| 1 |
import * as Survey from 'survey-jquery' |
| 2 |
|
| 3 |
( function ( $ ) { |
| 4 |
$( document ).on( 'ready', function () { |
| 5 |
let type = 'ai_survey'; |
| 6 |
let surveyWrapper = $( '.hts-survey-wrapper' ); |
| 7 |
|
| 8 |
if ( surveyWrapper.hasClass( 'hts-woocommerce-csat' ) ) { |
| 9 |
type = 'woo_survey'; |
| 10 |
} |
| 11 |
|
| 12 |
if ( surveyWrapper.hasClass( 'hts-ai-onboarding-csat' ) ) { |
| 13 |
type = 'ai_onboarding_survey'; |
| 14 |
} |
| 15 |
|
| 16 |
if ( surveyWrapper.hasClass( 'hts-affiliate-csat' ) ) { |
| 17 |
type = 'affiliate_plugin_survey'; |
| 18 |
} |
| 19 |
|
| 20 |
// Check if the cookie already exists |
| 21 |
function checkCookie() { |
| 22 |
var match = document.cookie.match(/(?:^|;)\s*HtsSkipSurvey\s*=\s*([^;]*)/); |
| 23 |
var cookieValue = match ? match[1] : ""; |
| 24 |
return cookieValue !== undefined && cookieValue !== ""; |
| 25 |
} |
| 26 |
|
| 27 |
$( ".hts-survey-wrapper .close-btn" ).click( function () { |
| 28 |
var expirationTime = new Date(); |
| 29 |
expirationTime.setTime( expirationTime.getTime() + ( 86400 * 30 ) ) // 30 Days; |
| 30 |
document.cookie = "HtsSkipSurvey=1; expires=" + expirationTime.toUTCString() + "; path=/"; |
| 31 |
|
| 32 |
$.ajax( { |
| 33 |
url: ajaxurl, |
| 34 |
method: 'POST', |
| 35 |
data: { |
| 36 |
action: 'hostinger_hide_survey', |
| 37 |
nonce: hostingerContainer.nonce, |
| 38 |
}, |
| 39 |
dataType: 'json', |
| 40 |
success: function ( data ) { |
| 41 |
surveyWrapper.hide(); |
| 42 |
}, |
| 43 |
error: function ( xhr, status, error ) { |
| 44 |
console.log( 'AJAX request failed: ' + error ); |
| 45 |
} |
| 46 |
} ); |
| 47 |
} ); |
| 48 |
|
| 49 |
if ( surveyWrapper.length && ! checkCookie() ) { |
| 50 |
$.ajax( { |
| 51 |
url: ajaxurl, |
| 52 |
method: 'POST', |
| 53 |
data: { |
| 54 |
action: 'hostinger_get_survey', |
| 55 |
nonce: hostingerContainer.nonce, |
| 56 |
type: type |
| 57 |
}, |
| 58 |
dataType: 'json', |
| 59 |
success: function ( data ) { |
| 60 |
|
| 61 |
if ( data.success === false) return; |
| 62 |
|
| 63 |
let questionsCount = $( '#hts-questionsLeft' ); |
| 64 |
surveyWrapper.show(); |
| 65 |
const survey = new Survey.Model( data ); |
| 66 |
survey.focusFirstQuestionAutomatic = false; |
| 67 |
survey.render( "hostinger-feedback-survey" ); |
| 68 |
survey.onComplete.add( onSurveyComplete ); |
| 69 |
survey.onCurrentPageChanged.add( onPageChanged ); // Add this line |
| 70 |
survey.render( "surveyElement" ); |
| 71 |
|
| 72 |
let answeredQuestions = 0; |
| 73 |
let totalQuestions = survey.getAllQuestions().length; |
| 74 |
if ( totalQuestions >= 2 ) { |
| 75 |
questionsCount.show() |
| 76 |
$( "#hts-allQuestions" ).html( totalQuestions ); |
| 77 |
} |
| 78 |
|
| 79 |
function updateQuestionsLeft () { |
| 80 |
var remaining = answeredQuestions + 1; |
| 81 |
document.getElementById( "hts-currentQuestion" ).textContent = remaining; |
| 82 |
} |
| 83 |
|
| 84 |
function onPageChanged ( sender, options ) { |
| 85 |
if ( options.isNextPage || options.isPrevPage ) { |
| 86 |
answeredQuestions = survey.currentPageNo; |
| 87 |
updateQuestionsLeft(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
function onSurveyComplete ( sender ) { |
| 92 |
const results = JSON.stringify( sender.data ); |
| 93 |
$( '#hts-questionsLeft' ).remove(); |
| 94 |
hostinger_submit_survey( results, type ); |
| 95 |
} |
| 96 |
|
| 97 |
updateQuestionsLeft(); |
| 98 |
}, |
| 99 |
error: function ( xhr, status, error ) { |
| 100 |
console.log( 'AJAX request failed: ' + error ); |
| 101 |
} |
| 102 |
} ); |
| 103 |
} |
| 104 |
|
| 105 |
function hostinger_submit_survey ( survey_answers, type ) { |
| 106 |
$.ajax( { |
| 107 |
url: ajaxurl, |
| 108 |
method: 'POST', |
| 109 |
data: { |
| 110 |
action: 'hostinger_submit_survey', |
| 111 |
nonce: hostingerContainer.nonce, |
| 112 |
survey_results: survey_answers, |
| 113 |
type: type |
| 114 |
}, |
| 115 |
dataType: 'json', |
| 116 |
success: function ( data ) { |
| 117 |
setTimeout(function ( ) { |
| 118 |
var expirationTime = new Date(); |
| 119 |
expirationTime.setTime( expirationTime.getTime() + ( 86400 ) ); // 24Hours; |
| 120 |
document.cookie = "HtsSkipSurvey=1; expires=" + expirationTime.toUTCString() + "; path=/"; |
| 121 |
surveyWrapper.hide('300'); |
| 122 |
},1000); |
| 123 |
}, |
| 124 |
error: function ( xhr, status, error ) { |
| 125 |
console.log( 'AJAX request failed: ' + error ); |
| 126 |
} |
| 127 |
} ); |
| 128 |
} |
| 129 |
|
| 130 |
} ); |
| 131 |
|
| 132 |
} )( jQuery ); |
| 133 |
|