"use strict";
$(function () {
var CB_FETCHTIME_MS = 15 * 1000;
var GERNERAL_REFRESHTIME_MS = 60 * 1000;
// AJAX CB Refresh
var refresh = function () {
$.ajax({ url: '/', dataType: 'html',
// testing shows the table rendered here is the same as in the nodelet
data: { node: 'showchatmessages', displaytype: 'raw', ticker: 'yes' } })
.done(function (data) {
var source = $('
').html(data).find('table.cb_table, .cb_quiet');
$('#Chatterbox table.cb_table, #Chatterbox .cb_quiet').replaceWith(source);
$('#Chatterbox table.cb_table + br, #Chatterbox .cb_quiet + br').remove();
$('#Chatterbox .cb_quiet').append(' ');
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.warn("AJAX error: " + textStatus + " / " + jqXHR.status + " " + errorThrown);
});
};
// Refresh "Other Users" (followed by CB refresh!)
var refresh_all = function () {
$.ajax({ url: '/', dataType: 'html',
// testing shows the body rendered here is the same as in the nodelet
data: { node: 'showotherusers', displaytype: 'raw', ticker: 'yes' } })
.done(function (data) {
// sadly, the tag is not preserved, so we need to do some funky parsing here
var inSection = false;
var source = $.grep( $('').html(data).contents().toArray(), function (obj) {
if (inSection) {
if ( $(obj).is('.update-time') ) inSection = false;
return true;
}
else if ( $(obj).is('.other-users-text') )
{ inSection = true; return true; }
return false;
});
$('#Other_Users .nodelet_body').empty();
$('#Other_Users .nodelet_body').append(source);
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.warn("AJAX error: " + textStatus + " / " + jqXHR.status + " " + errorThrown);
})
.always(refresh);
};
// AJAX Message Fetching
var notify_cb = $('', { type: 'checkbox' });
var ajax_cb = $('', { type: 'checkbox', checked: !window["AJAXCB_DEFAULTOFF"] });
var fetch_timer;
var fetchit;
var start_fetch = function () {
if ( ajax_cb[0].checked )
fetch_timer = setTimeout(fetchit, CB_FETCHTIME_MS);
};
var lastid;
fetchit = function () {
if (fetch_timer) clearTimeout(fetch_timer);
fetch_timer = null;
if ( !ajax_cb[0].checked ) return;
var query = { node_id: '207304', xmlstyle: 'modern', ticker: 'yes' };
if ( lastid ) query['fromid'] = lastid;
$.ajax({ url: '/', dataType: 'xml', data: query })
.done(function (data) {
var count = parseInt($("info", data).attr("count"));
if ( count>0 ) {
refresh();
if ( notify_cb[0].checked ) {
var authors = {};
$('message author', data).each(function () { authors[ $(this).text() ] = 1; });
authors = Object.keys(authors).sort().join(", ");
var notification = new Notification('New PerlMonks Chatter',
{ body: count+" new messages by "+authors,
tag: "PerlMonksChatter", icon: "/favicon.ico", badge: "/favicon.ico" });
}
}
var li = $("info", data).attr("lastid");
if ( li ) lastid = li;
})
.fail(function (jqXHR, textStatus, errorThrown) {
console.warn("AJAX error: " + textStatus + " / " + jqXHR.status + " " + errorThrown);
})
.always(start_fetch);
};
var all_refresh_timer;
if ( !window["AJAXCB_DEFAULTOFF"] ) {
start_fetch();
all_refresh_timer = setInterval(refresh_all, GERNERAL_REFRESHTIME_MS);
}
// AJAX Form Submission
var form = $('#Chatterbox form');
form.on('submit', function (evt) {
evt.preventDefault();
// use the frame from the FullPage Chat as it renders faster
var thedata = { op: 'message', node_id: '3193',
displaytype: 'raw', message: $('#talkbox').val() };
$(":input",form).prop("disabled", true);
$.ajax({ url: form.attr("action"), method: 'post', data: thedata })
.done(function (data) {
$('#talkbox').val("");
// want to use fetchit() if possible here to update the lastid
if ( ajax_cb[0].checked ) fetchit(); else refresh();
})
.fail(function ( jqXHR, textStatus, errorThrown ) {
alert("CB submission error: "+textStatus+" / "+jqXHR.status+" "+errorThrown);
})
.always(function () {
$(":input",form).prop("disabled", false);
});
});
// Checkboxes
var cb_div = $('', { style: "font-size: 12px; display: inline-block;" });
cb_div.append( $('', { text: "AJAX Refresh" }).prepend(ajax_cb) );
form.after($(" "), cb_div);
ajax_cb.change(function () {
if ( this.checked ) {
start_fetch();
all_refresh_timer = setInterval(refresh_all, GERNERAL_REFRESHTIME_MS);
refresh_all();
}
else {
if (fetch_timer) clearTimeout(fetch_timer);
fetch_timer = null;
if (all_refresh_timer) clearInterval(all_refresh_timer);
all_refresh_timer = null;
}
});
if ( "Notification" in window ) {
cb_div.append( $('', { text: "Notifications" }).prepend(notify_cb) );
notify_cb.change(function () {
if ( !this.checked ) return;
if ( Notification.permission == "default" ) {
notify_cb.attr("disabled", true);
Notification.requestPermission(function (result) {
notify_cb.removeAttr("disabled");
if ( result != "granted" ) notify_cb[0].checked = false;
});
}
else if ( Notification.permission != "granted" ) {
alert("You've denied this page the ability to send notifications, please change this via your browser's settings.");
this.checked = false;
}
});
}
});