| 1 |
jQuery(document).ready(function ($) { |
| 2 |
if (parseInt(wpforo_widgets.is_live_notifications_on) && typeof wpforo_check_notifications === 'function') { |
| 3 |
setTimeout(wpforo_check_notifications, parseInt(wpforo_widgets.live_notifications_start), parseInt(wpforo_widgets.live_notifications_interval)); |
| 4 |
} |
| 5 |
|
| 6 |
$(document).on('keydown', function (e) { |
| 7 |
if (e.code === 'Escape') $('.wpf-notifications').slideUp(250, 'linear'); |
| 8 |
}); |
| 9 |
|
| 10 |
$(document).on('click', '.wpf-alerts:not(.wpf-processing)', function () { |
| 11 |
var notifications = $('.wpforo-subtop').find('.wpf-notifications'); |
| 12 |
$('.wpf-notifications').not(notifications).slideUp(250, 'linear'); |
| 13 |
if (notifications.is(':visible')) { |
| 14 |
notifications.slideUp(250, 'linear'); |
| 15 |
} else { |
| 16 |
wpforo_load_notifications($(this)); |
| 17 |
notifications.slideDown(250, 'linear'); |
| 18 |
} |
| 19 |
}); |
| 20 |
|
| 21 |
$(document).on('click', '.wpf-widget-alerts:not(.wpf-processing)', function () { |
| 22 |
var notifications = $('.wpf-widget-alerts').parents('.wpf-prof-wrap').find('.wpf-notifications'); |
| 23 |
$('.wpf-notifications').not(notifications).slideUp(250, 'linear'); |
| 24 |
if (notifications.is(':visible')) { |
| 25 |
notifications.slideUp(250, 'linear'); |
| 26 |
} else { |
| 27 |
wpforo_load_notifications($(this)); |
| 28 |
notifications.slideDown(250, 'linear'); |
| 29 |
} |
| 30 |
}); |
| 31 |
|
| 32 |
$(document).on('click', '.wpf-action.wpf-notification-action-clear-all', function () { |
| 33 |
var foro_n = $(this).data('foro_n'); |
| 34 |
if (foro_n) { |
| 35 |
$('.wpf-notifications').slideUp(250, 'linear'); |
| 36 |
$.ajax({ |
| 37 |
type: 'POST', |
| 38 |
url: wpforo_widgets.ajax_url, |
| 39 |
data: { |
| 40 |
foro_n: foro_n, |
| 41 |
action: 'wpforo_clear_all_notifications', |
| 42 |
}, |
| 43 |
}).done(function (r) { |
| 44 |
if (r) { |
| 45 |
$('.wpf-notifications .wpf-notification-actions').hide(); |
| 46 |
$('.wpf-notifications .wpf-notification-content').html(r); |
| 47 |
wpforo_bell(0); |
| 48 |
} |
| 49 |
wpforo_trigger_custom_event(document, 'wpforo_clear_all_notifications', { r }); |
| 50 |
}); |
| 51 |
} |
| 52 |
}); |
| 53 |
|
| 54 |
function do_wpforo_ajax_widget (elem, is_on_load) { |
| 55 |
var j = elem.data('json'); |
| 56 |
if (j) { |
| 57 |
if (typeof j !== 'object') j = JSON.parse(j); |
| 58 |
if (j['instance'] !== undefined && typeof j['instance'] !== 'object') j['instance'] = JSON.parse(j['instance']); |
| 59 |
if (j['topic_args'] !== undefined && typeof j['topic_args'] !== 'object') j['topic_args'] = JSON.parse(j['topic_args']); |
| 60 |
if (j['post_args'] !== undefined && typeof j['post_args'] !== 'object') j['post_args'] = JSON.parse(j['post_args']); |
| 61 |
|
| 62 |
if (j['boardid'] !== undefined) { |
| 63 |
var ajax_url = wpforo_widgets.ajax_url.replace(/[&?]wpforo_boardid=\d*/i, ''); |
| 64 |
ajax_url += (/\?/.test(ajax_url) ? '&' : '?') + 'wpforo_boardid=' + j['boardid']; |
| 65 |
|
| 66 |
if (j['instance'] !== undefined && j['instance']['refresh_interval'] !== undefined) { |
| 67 |
var interval = parseInt(j['instance']['refresh_interval']); |
| 68 |
} |
| 69 |
|
| 70 |
if (j['instance'] !== undefined) j['instance'] = JSON.stringify(j['instance']); |
| 71 |
if (j['topic_args'] !== undefined) j['topic_args'] = JSON.stringify(j['topic_args']); |
| 72 |
if (j['post_args'] !== undefined) j['post_args'] = JSON.stringify(j['post_args']); |
| 73 |
|
| 74 |
var do_ajax = true; |
| 75 |
if (is_on_load && elem.hasClass('wpforo-ajax-widget-onload-false')) do_ajax = false; |
| 76 |
|
| 77 |
if (do_ajax) { |
| 78 |
$.ajax({ |
| 79 |
type: 'POST', |
| 80 |
url: ajax_url, |
| 81 |
data: j, |
| 82 |
}).done(function (r) { |
| 83 |
if (r.success) elem.html(r.data['html']); |
| 84 |
|
| 85 |
if (!isNaN(interval) && interval > 0) { |
| 86 |
setTimeout(function () { |
| 87 |
do_wpforo_ajax_widget(elem); |
| 88 |
}, interval * 1000); |
| 89 |
} |
| 90 |
}); |
| 91 |
} else { |
| 92 |
if (!isNaN(interval) && interval > 0) { |
| 93 |
setTimeout(function () { |
| 94 |
do_wpforo_ajax_widget(elem); |
| 95 |
}, interval * 1000); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
function do_wpforo_ajax_widgets () { |
| 103 |
var wdgts = $('.wpforo-widget-wrap .wpforo-ajax-widget[data-json]'); |
| 104 |
if (wdgts.length) { |
| 105 |
wdgts.each(function (k, v) { |
| 106 |
do_wpforo_ajax_widget($(v), true); |
| 107 |
}); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
do_wpforo_ajax_widgets(); |
| 112 |
}); |
| 113 |
|
| 114 |
function wpforo_bell (wpf_alerts) { |
| 115 |
wpf_alerts = parseInt(wpf_alerts); |
| 116 |
if (wpf_alerts > 0) { |
| 117 |
var wpforo_bell = ''; |
| 118 |
var wpf_tooltip = ''; |
| 119 |
if (typeof window.wpforo_phrase === 'function') { |
| 120 |
var wpforo_notification_phrase = wpforo_phrase('You have a new notification'); |
| 121 |
if (wpf_alerts > 1) wpforo_notification_phrase = wpforo_phrase('You have new notifications'); |
| 122 |
wpf_tooltip = 'wpf-tooltip="' + wpforo_notification_phrase + '" wpf-tooltip-size="middle"'; |
| 123 |
} |
| 124 |
wpforo_bell = '<div class="wpf-bell" ' + wpf_tooltip + '><svg height="24" width="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="currentColor" d="M224 0c-17.7 0-32 14.3-32 32V51.2C119 66 64 130.6 64 208v18.8c0 47-17.3 92.4-48.5 127.6l-7.4 8.3c-8.4 9.4-10.4 22.9-5.3 34.4S19.4 416 32 416H416c12.6 0 24-7.4 29.2-18.9s3.1-25-5.3-34.4l-7.4-8.3C401.3 319.2 384 273.9 384 226.8V208c0-77.4-55-142-128-156.8V32c0-17.7-14.3-32-32-32zm45.3 493.3c12-12 18.7-28.3 18.7-45.3H224 160c0 17 6.7 33.3 18.7 45.3s28.3 18.7 45.3 18.7s33.3-6.7 45.3-18.7z"/></svg> <span class="wpf-alerts-count">' + wpf_alerts + '</span></div>'; |
| 125 |
jQuery('.wpf-alerts').addClass('wpf-new'); |
| 126 |
jQuery('.wpf-widget-alerts').addClass('wpf-new'); |
| 127 |
} else { |
| 128 |
wpforo_bell = '<div class="wpf-bell"><svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path fill="currentColor" d="M224 0c-17.7 0-32 14.3-32 32V51.2C119 66 64 130.6 64 208v25.4c0 45.4-15.5 89.5-43.8 124.9L5.3 377c-5.8 7.2-6.9 17.1-2.9 25.4S14.8 416 24 416H424c9.2 0 17.6-5.3 21.6-13.6s2.9-18.2-2.9-25.4l-14.9-18.6C399.5 322.9 384 278.8 384 233.4V208c0-77.4-55-142-128-156.8V32c0-17.7-14.3-32-32-32zm0 96c61.9 0 112 50.1 112 112v25.4c0 47.9 13.9 94.6 39.7 134.6H72.3C98.1 328 112 281.3 112 233.4V208c0-61.9 50.1-112 112-112zm64 352H224 160c0 17 6.7 33.3 18.7 45.3s28.3 18.7 45.3 18.7s33.3-6.7 45.3-18.7s18.7-28.3 18.7-45.3z"/></svg></div>'; |
| 129 |
jQuery('.wpf-alerts').removeClass('wpf-new'); |
| 130 |
jQuery('.wpf-widget-alerts').removeClass('wpf-new'); |
| 131 |
} |
| 132 |
jQuery('.wpf-alerts').html(wpforo_bell); |
| 133 |
jQuery('.wpf-widget-alerts').html(wpforo_bell); |
| 134 |
} |
| 135 |
|
| 136 |
var wpforo_check_notifications_timeout; |
| 137 |
var wpforo_last_alert_count = -1; |
| 138 |
var wpforo_idle_polls = 0; |
| 139 |
var wpforo_max_interval = 300000; // 5 minutes max backoff |
| 140 |
|
| 141 |
function wpforo_check_notifications (wpforo_check_interval) { |
| 142 |
wpforo_check_interval = parseInt(wpforo_check_interval); |
| 143 |
if (isNaN(wpforo_check_interval)) wpforo_check_interval = 60000; |
| 144 |
|
| 145 |
// Skip poll when page is not visible (browser tab in background) |
| 146 |
if (document.hidden) { |
| 147 |
wpforo_check_notifications_timeout = setTimeout(wpforo_check_notifications, wpforo_check_interval, wpforo_check_interval); |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
var getdata = jQuery('.wpf-notifications').is(':visible'); |
| 152 |
jQuery.ajax({ |
| 153 |
type: 'POST', |
| 154 |
url: wpforo_widgets.ajax_url, |
| 155 |
data: { |
| 156 |
getdata: getdata, |
| 157 |
action: 'wpforo_notifications', |
| 158 |
}, |
| 159 |
success: function (response) { |
| 160 |
wpforo_notifications_ui_update(response); |
| 161 |
var current_alerts = parseInt(response.data.alerts); |
| 162 |
if (current_alerts !== wpforo_last_alert_count) { |
| 163 |
// Alert count changed — reset to base interval |
| 164 |
wpforo_idle_polls = 0; |
| 165 |
wpforo_last_alert_count = current_alerts; |
| 166 |
} else { |
| 167 |
// No change — increase backoff |
| 168 |
wpforo_idle_polls++; |
| 169 |
} |
| 170 |
}, |
| 171 |
complete: function () { |
| 172 |
// Exponential backoff: double interval every 3 idle polls, cap at 5 min |
| 173 |
var backoff = Math.min(wpforo_check_interval * Math.pow(2, Math.floor(wpforo_idle_polls / 3)), wpforo_max_interval); |
| 174 |
wpforo_check_notifications_timeout = setTimeout(wpforo_check_notifications, backoff, wpforo_check_interval); |
| 175 |
}, |
| 176 |
error: function () { |
| 177 |
clearTimeout(wpforo_check_notifications_timeout); |
| 178 |
}, |
| 179 |
}); |
| 180 |
} |
| 181 |
|
| 182 |
function wpforo_load_notifications ($this) { |
| 183 |
$this.addClass('wpf-processing'); |
| 184 |
jQuery('.wpf-notifications .wpf-notification-content').html('<div class="wpf-nspin"><svg width="24" height="24" viewBox="0 0 24 24">\n' + |
| 185 |
' <g stroke="currentColor">\n' + |
| 186 |
' <circle cx="12" cy="12" r="9.5" fill="none" stroke-linecap="round" stroke-width="3">\n' + |
| 187 |
' <animate attributeName="stroke-dasharray" calcMode="spline" dur="1.5s" keySplines="0.42,0,0.58,1;0.42,0,0.58,1;0.42,0,0.58,1" keyTimes="0;0.475;0.95;1"\n' + |
| 188 |
' repeatCount="indefinite" values="0 150;42 150;42 150;42 150"></animate>\n' + |
| 189 |
' <animate attributeName="stroke-dashoffset" calcMode="spline" dur="1.5s" keySplines="0.42,0,0.58,1;0.42,0,0.58,1;0.42,0,0.58,1" keyTimes="0;0.475;0.95;1"\n' + |
| 190 |
' repeatCount="indefinite" values="0;-16;-59;-59"></animate>\n' + |
| 191 |
' </circle>\n' + |
| 192 |
' <animateTransform attributeName="transform" dur="2s" repeatCount="indefinite" type="rotate" values="0 12 12;360 12 12"></animateTransform>\n' + |
| 193 |
' </g>\n' + |
| 194 |
' </svg></div>'); |
| 195 |
jQuery.ajax({ |
| 196 |
type: 'POST', |
| 197 |
url: wpforo_widgets.ajax_url, |
| 198 |
data: { |
| 199 |
getdata: 1, |
| 200 |
action: 'wpforo_notifications', |
| 201 |
}, |
| 202 |
success: wpforo_notifications_ui_update, |
| 203 |
error: function () { |
| 204 |
clearTimeout(wpforo_check_notifications_timeout); |
| 205 |
}, |
| 206 |
}).always(function () { |
| 207 |
$this.removeClass('wpf-processing'); |
| 208 |
}); |
| 209 |
} |
| 210 |
|
| 211 |
function wpforo_notifications_ui_update (response) { |
| 212 |
var wpf_alerts = parseInt(response.data.alerts); |
| 213 |
if (wpf_alerts > 0) { |
| 214 |
jQuery('.wpf-notifications .wpf-notification-actions').show(); |
| 215 |
} else { |
| 216 |
jQuery('.wpf-notifications .wpf-notification-actions').hide(); |
| 217 |
} |
| 218 |
if (response.data.notifications) jQuery('.wpf-notifications .wpf-notification-content').html(response.data.notifications); |
| 219 |
wpforo_bell(wpf_alerts); |
| 220 |
|
| 221 |
wpforo_trigger_custom_event(document, 'wpforo_notifications_ui_update', { response }); |
| 222 |
} |
| 223 |
|
| 224 |
if (typeof wpforo_trigger_custom_event !== 'function') { |
| 225 |
/** |
| 226 |
* Trigger a custom event. |
| 227 |
* |
| 228 |
* @param {Element|Document} target HTML element to dispatch the event on. |
| 229 |
* @param {string} name Event name. |
| 230 |
* @param [detail = null] Event addintional data information. |
| 231 |
*/ |
| 232 |
function wpforo_trigger_custom_event (target, name, detail) { |
| 233 |
if (typeof detail === 'undefined') detail = null; |
| 234 |
var event; |
| 235 |
if (typeof CustomEvent === 'function') { |
| 236 |
event = new CustomEvent(name, { bubbles: true, cancelable: true, detail: detail }); |
| 237 |
} else { |
| 238 |
event = document.createEvent('Event'); |
| 239 |
event.initEvent(name, true, true); |
| 240 |
event.detail = detail; |
| 241 |
} |
| 242 |
|
| 243 |
target.dispatchEvent(event); |
| 244 |
window['wpforo_break_after_custom_event_'.name] = false; |
| 245 |
} |
| 246 |
} |
| 247 |
|