| 1 |
(function($) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
function AysSurveyPopupsPlugin(element, options){ |
| 5 |
this.el = element; |
| 6 |
this.$el = $(element); |
| 7 |
this.htmlClassPrefix = 'ays-survey-'; |
| 8 |
this.ajaxAction = 'ays_survey_ajax'; |
| 9 |
this.dbOptions = undefined; |
| 10 |
this.popupId = undefined; |
| 11 |
this.confirmBeforeUnload = false; |
| 12 |
|
| 13 |
this.init(); |
| 14 |
|
| 15 |
return this; |
| 16 |
} |
| 17 |
|
| 18 |
AysSurveyPopupsPlugin.prototype.init = function() { |
| 19 |
var _this = this; |
| 20 |
|
| 21 |
_this.popupId = _this.$el.data('id'); |
| 22 |
|
| 23 |
if( typeof window.aysSurveyPopupsOptions != 'undefined' ){ |
| 24 |
_this.dbOptions = JSON.parse( atob( window.aysSurveyPopupsOptions[ _this.popupId ] ) ); |
| 25 |
} |
| 26 |
|
| 27 |
_this.setEvents(); |
| 28 |
}; |
| 29 |
|
| 30 |
AysSurveyPopupsPlugin.prototype.setEvents = function(e){ |
| 31 |
var _this = this; |
| 32 |
|
| 33 |
_this.popupSelectorClick( _this.$el, _this); |
| 34 |
|
| 35 |
var closeByEsc = typeof _this.dbOptions.popupEnableCloseByEsc != 'undefined' && _this.dbOptions.popupEnableCloseByEsc == 'on' ? true : false; |
| 36 |
var closePopupOverlayOutsideClick = typeof _this.dbOptions.closePopupOverlayOutsideClick != 'undefined' && _this.dbOptions.closePopupOverlayOutsideClick == 'on' ? true : false; |
| 37 |
|
| 38 |
_this.$el.on('click', '.' + _this.htmlClassPrefix + 'popup-btn-close', function(e){ |
| 39 |
_this.$el.css('display', 'none'); |
| 40 |
|
| 41 |
var hidePopupAfterClick = typeof _this.dbOptions.hidePopupAfterClose != 'undefined' && _this.dbOptions.hidePopupAfterClose == 'on' ? true : false; |
| 42 |
if(hidePopupAfterClick){ |
| 43 |
_this.setPopupCookie("ays_survey_popup_hide_after_click_close_"+_this.popupId+"", 1, 365); |
| 44 |
} |
| 45 |
|
| 46 |
if (document.fullscreenElement) { |
| 47 |
_this.aysSurveyFullScreenWindowDeactivator(); |
| 48 |
} |
| 49 |
}); |
| 50 |
|
| 51 |
_this.$el.on('click', 'input.' + _this.htmlClassPrefix + 'finish-button', function(){ |
| 52 |
var hidePopupAfterSubmit = typeof _this.dbOptions.hidePopup != 'undefined' && _this.dbOptions.hidePopup == 'on' ? true : false; |
| 53 |
var closePopupAfterFinish = typeof _this.dbOptions.enable_popup_close_after_finish != 'undefined' && _this.dbOptions.enable_popup_close_after_finish ? true : false; |
| 54 |
|
| 55 |
if(closePopupAfterFinish){ |
| 56 |
var closePopupAfterFinishDelay = typeof _this.dbOptions.popup_close_after_finish_delay != 'undefined' && _this.dbOptions.popup_close_after_finish_delay != '' ? _this.dbOptions.popup_close_after_finish_delay : 0; |
| 57 |
_this.$el.attr('data-close-popup' , true); |
| 58 |
_this.$el.attr('data-close-popup-delay' , closePopupAfterFinishDelay); |
| 59 |
} |
| 60 |
|
| 61 |
if( hidePopupAfterSubmit ){ |
| 62 |
var aysHasError = $(this).parents('.ays-survey-sections').find('.ays-has-error'); |
| 63 |
if(aysHasError.length == 0){ |
| 64 |
$.ajax({ |
| 65 |
url: aysSurveyMakerPopupsAjaxPublic.ajaxUrl, |
| 66 |
method: 'post', |
| 67 |
dataType: 'json', |
| 68 |
data: { |
| 69 |
action: _this.ajaxAction, |
| 70 |
function: 'ays_survey_popup_set_cookie', |
| 71 |
id: _this.popupId, |
| 72 |
} |
| 73 |
}); |
| 74 |
} |
| 75 |
} |
| 76 |
}); |
| 77 |
|
| 78 |
_this.$el.find('.ays-survey-popup-close-full-screen, .ays-survey-popup-open-full-screen').on('click', function() { |
| 79 |
_this.toggleFullscreen(_this.el); |
| 80 |
}); |
| 81 |
|
| 82 |
if(closeByEsc){ |
| 83 |
$(document).on('keydown', function(event) { |
| 84 |
if (event.keyCode == 27) { |
| 85 |
_this.$el.find('.' + _this.htmlClassPrefix + 'popup-btn-close').trigger('click'); |
| 86 |
} |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
if(closePopupOverlayOutsideClick){ |
| 91 |
var return_false = 0; |
| 92 |
$(document).on('click', function(e) { |
| 93 |
if(return_false == 0){ |
| 94 |
var target = e.target; |
| 95 |
if(!$(target).hasClass(_this.htmlClassPrefix + 'popup-survey-window') && $(target).parents('.' + _this.htmlClassPrefix + 'popup-survey-window').length == 0){ |
| 96 |
_this.$el.find('.' + _this.htmlClassPrefix + 'popup-btn-close').trigger('click'); |
| 97 |
return_false = 1; |
| 98 |
} |
| 99 |
} |
| 100 |
}); |
| 101 |
} |
| 102 |
|
| 103 |
_this.aysSurveyFullScreenDeactivateAll(); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
AysSurveyPopupsPlugin.prototype.toggleFullscreen = function (elem) { |
| 108 |
var _this = this; |
| 109 |
elem = elem || document.documentElement; |
| 110 |
if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) { |
| 111 |
_this.aysSurveyFullScreenActivate( elem ); |
| 112 |
_this.aysSurveyFullScreenWindowActivator( elem ); |
| 113 |
}else{ |
| 114 |
_this.aysSurveyFullScreenDeactivate( elem ); |
| 115 |
_this.aysSurveyFullScreenWindowDeactivator(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
AysSurveyPopupsPlugin.prototype.aysSurveyFullScreenActivate = function (elem) { |
| 120 |
$(elem).find('.ays-survey-popup-full-screen-mode .ays-survey-popup-close-full-screen').css({'display':'inline'}); |
| 121 |
$(elem).find('.ays-survey-popup-full-screen-mode .ays-survey-popup-open-full-screen').css('display','none'); |
| 122 |
$(elem).find('.ays-survey-popup-full-screen-mode').css({'padding-right':'25px'}); |
| 123 |
$(elem).find('.ays-survey-popup-btn-close').css({'top':'0','right' : '0'}); |
| 124 |
$(elem).css({'overflow':'auto'}); |
| 125 |
} |
| 126 |
|
| 127 |
AysSurveyPopupsPlugin.prototype.aysSurveyFullScreenDeactivate = function (elem) { |
| 128 |
$(elem).find('.ays-survey-popup-full-screen-mode .ays-survey-popup-open-full-screen').css({'display':'inline'}); |
| 129 |
$(elem).find('.ays-survey-popup-full-screen-mode .ays-survey-popup-close-full-screen').css('display','none'); |
| 130 |
$(elem).find('.ays-survey-popup-full-screen-mode').css({'padding-right':'0'}); |
| 131 |
$(elem).find('.ays-survey-popup-btn-close').css({'top':'-16px','right' : '-16px'}); |
| 132 |
$(elem).css({'overflow':'initial'}); |
| 133 |
} |
| 134 |
|
| 135 |
AysSurveyPopupsPlugin.prototype.aysSurveyFullScreenDeactivateAll = function (elem) { |
| 136 |
var _this = this; |
| 137 |
document.addEventListener('fullscreenchange', function(event) { |
| 138 |
if (!document.fullscreenElement) { |
| 139 |
var eventTarget = event.target; |
| 140 |
if( $( eventTarget ).hasClass('ays-survey-popup-survey-window') ){ |
| 141 |
_this.aysSurveyFullScreenDeactivate( eventTarget ); |
| 142 |
} |
| 143 |
} |
| 144 |
}, false); |
| 145 |
} |
| 146 |
|
| 147 |
AysSurveyPopupsPlugin.prototype.aysSurveyFullScreenWindowActivator = function (elem) { |
| 148 |
if (elem.requestFullscreen) { |
| 149 |
elem.requestFullscreen(); |
| 150 |
}else if (elem.msRequestFullscreen) { |
| 151 |
elem.msRequestFullscreen(); |
| 152 |
}else if (elem.mozRequestFullScreen) { |
| 153 |
elem.mozRequestFullScreen(); |
| 154 |
}else if (elem.webkitRequestFullscreen) { |
| 155 |
elem.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
AysSurveyPopupsPlugin.prototype.aysSurveyFullScreenWindowDeactivator = function () { |
| 160 |
if(document.exitFullscreen) { |
| 161 |
document.exitFullscreen(); |
| 162 |
}else if (document.msExitFullscreen) { |
| 163 |
document.msExitFullscreen(); |
| 164 |
}else if (document.mozCancelFullScreen) { |
| 165 |
document.mozCancelFullScreen(); |
| 166 |
}else if (document.webkitExitFullscreen) { |
| 167 |
document.webkitExitFullscreen(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
AysSurveyPopupsPlugin.prototype.popupSelectorClick = function (elem, _this) { |
| 172 |
var popupTriggerType = _this.dbOptions.popup_trigger; |
| 173 |
var popupSelector = _this.dbOptions.popup_selector; |
| 174 |
|
| 175 |
if(popupTriggerType == 'on_click'){ |
| 176 |
|
| 177 |
$(document).find(popupSelector).on('click', function(){ |
| 178 |
|
| 179 |
elem.css('display','block'); |
| 180 |
|
| 181 |
}); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
AysSurveyPopupsPlugin.prototype.setPopupCookie = function (name, value, daysToExpire) { |
| 186 |
var date = new Date(); |
| 187 |
date.setTime(date.getTime() + (daysToExpire * 24 * 60 * 60 * 1000)); |
| 188 |
var expires = "expires=" + date.toUTCString(); |
| 189 |
document.cookie = name + "=" + value + "; " + expires + "; path=/"; |
| 190 |
} |
| 191 |
|
| 192 |
$.fn.AysSurveyMakerPopups = function(options) { |
| 193 |
return this.each(function() { |
| 194 |
if (!$.data(this, 'AysSurveyMakerPopups')) { |
| 195 |
$.data(this, 'AysSurveyMakerPopups', new AysSurveyPopupsPlugin(this, options)); |
| 196 |
} else { |
| 197 |
try { |
| 198 |
$(this).data('AysSurveyMakerPopups').init(); |
| 199 |
} catch (err) { |
| 200 |
console.error('AysSurveyMakerPopups has not initiated properly'); |
| 201 |
} |
| 202 |
} |
| 203 |
}); |
| 204 |
}; |
| 205 |
|
| 206 |
$(document).find('.ays-survey-popup-survey-window').AysSurveyMakerPopups(); |
| 207 |
})(jQuery); |
| 208 |
|