PluginProbe
Hostinger Tools / 2.0.6
Hostinger Tools v2.0.6
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / src / js / surveys.js

surveys.js in Hostinger Tools 2.0.6, at src/js/surveys.js

118 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( ! checkCookie() ) {
29 var expirationTime = new Date();
30 expirationTime.setTime( expirationTime.getTime() + ( 86400 ) ) // 24Hours;
31 document.cookie = "HtsSkipSurvey=1; expires=" + expirationTime.toUTCString() + "; path=/";
32 surveyWrapper.hide();
33 }
34 } );
35
36 if ( surveyWrapper.length && ! checkCookie() ) {
37 $.ajax( {
38 url: ajaxurl,
39 method: 'POST',
40 data: {
41 action: 'hostinger_get_survey',
42 nonce: hostingerContainer.nonce,
43 type: type
44 },
45 dataType: 'json',
46 success: function ( data ) {
47 let questionsCount = $( '#hts-questionsLeft' );
48 surveyWrapper.show();
49 const survey = new Survey.Model( data );
50 survey.focusFirstQuestionAutomatic = false;
51 survey.render( "hostinger-feedback-survey" );
52 survey.onComplete.add( onSurveyComplete );
53 survey.onCurrentPageChanged.add( onPageChanged ); // Add this line
54 survey.render( "surveyElement" );
55
56 let answeredQuestions = 0;
57 let totalQuestions = survey.getAllQuestions().length;
58 if ( totalQuestions >= 2 ) {
59 questionsCount.show()
60 $( "#hts-allQuestions" ).html( totalQuestions );
61 }
62
63 function updateQuestionsLeft () {
64 var remaining = answeredQuestions + 1;
65 document.getElementById( "hts-currentQuestion" ).textContent = remaining;
66 }
67
68 function onPageChanged ( sender, options ) {
69 if ( options.isNextPage || options.isPrevPage ) {
70 answeredQuestions = survey.currentPageNo;
71 updateQuestionsLeft();
72 }
73 }
74
75 function onSurveyComplete ( sender ) {
76 const results = JSON.stringify( sender.data );
77 $( '#hts-questionsLeft' ).remove();
78 hostinger_submit_survey( results, type );
79 }
80
81 updateQuestionsLeft();
82
83 },
84 error: function ( xhr, status, error ) {
85 console.log( 'AJAX request failed: ' + error );
86 }
87 } );
88 }
89
90 function hostinger_submit_survey ( survey_answers, type ) {
91 $.ajax( {
92 url: ajaxurl,
93 method: 'POST',
94 data: {
95 action: 'hostinger_submit_survey',
96 nonce: hostingerContainer.nonce,
97 survey_results: survey_answers,
98 type: type
99 },
100 dataType: 'json',
101 success: function ( data ) {
102 setTimeout(function ( ) {
103 var expirationTime = new Date();
104 expirationTime.setTime( expirationTime.getTime() + ( 86400 ) ); // 24Hours;
105 document.cookie = "HtsSkipSurvey=1; expires=" + expirationTime.toUTCString() + "; path=/";
106 surveyWrapper.hide('300');
107 },1000);
108 },
109 error: function ( xhr, status, error ) {
110 console.log( 'AJAX request failed: ' + error );
111 }
112 } );
113 }
114
115 } );
116
117 } )( jQuery );
118