Files
NodeBB/public/src/app.js

458 lines
11 KiB
JavaScript
Raw Normal View History

2013-04-22 19:13:39 +00:00
var socket,
config,
2013-10-07 12:57:40 -04:00
app = {
2013-12-08 10:38:09 -05:00
"username": null,
"uid": null,
"isFocused": true,
"currentRoom": null
2013-10-07 12:57:40 -04:00
};
2013-04-22 19:13:39 +00:00
(function () {
var showWelcomeMessage = false;
2013-09-24 14:56:51 -04:00
app.loadConfig = function() {
2013-07-11 15:50:19 -04:00
$.ajax({
2013-08-13 11:25:10 -04:00
url: RELATIVE_PATH + '/api/config',
success: function (data) {
2013-07-11 15:50:19 -04:00
config = data;
2013-09-24 14:56:51 -04:00
if(socket) {
socket.disconnect();
setTimeout(function() {
socket.socket.connect();
}, 200);
2013-09-24 14:56:51 -04:00
} else {
var max_reconnection_attemps = 5;
var reconnection_delay = 200;
socket = io.connect(RELATIVE_PATH, {
'max reconnection attempts': max_reconnection_attemps,
'reconnection delay': reconnection_delay
});
2013-09-24 14:56:51 -04:00
var reconnecting = false,
reconnectEl, reconnectTimer;
2013-09-24 14:56:51 -04:00
socket.on('event:connect', function (data) {
app.username = data.username;
2013-10-07 12:57:40 -04:00
app.uid = data.uid;
2013-10-07 13:00:57 -04:00
2013-09-24 14:56:51 -04:00
app.showLoginMessage();
socket.emit('api:updateHeader', {
fields: ['username', 'picture', 'userslug']
});
2013-09-24 14:56:51 -04:00
});
2013-07-25 16:20:18 -04:00
2013-09-24 14:56:51 -04:00
socket.on('event:alert', function (data) {
app.alert(data);
});
2013-09-24 14:56:51 -04:00
socket.on('connect', function (data) {
if (reconnecting) {
reconnectEl.tooltip('destroy');
reconnectEl.html('<i class="fa fa-check"></i>');
2013-09-24 14:56:51 -04:00
reconnecting = false;
setTimeout(function() {
reconnectEl.removeClass('active');
}, 3000);
2013-09-24 14:56:51 -04:00
}
socket.emit('api:updateHeader', {
fields: ['username', 'picture', 'userslug']
});
});
socket.on('event:disconnect', function() {
socket.socket.connect();
2013-09-24 14:56:51 -04:00
});
socket.on('reconnecting', function (data, attempt) {
if(attempt == max_reconnection_attemps) {
socket.socket.reconnectionAttempts = 0;
socket.socket.reconnectionDelay = reconnection_delay;
return;
}
if (!reconnectEl) reconnectEl = $('#reconnect');
2013-09-24 14:56:51 -04:00
reconnecting = true;
if (!reconnectEl.hasClass('active')) reconnectEl.html('<i class="fa fa-spinner fa-spin"></i>');
reconnectEl.addClass('active');
reconnectEl.tooltip({
placement: 'bottom'
});
2013-07-11 15:50:19 -04:00
});
2013-09-24 14:56:51 -04:00
socket.on('api:user.get_online_users', function (users) {
jQuery('a.username-field').each(function () {
if (this.processed === true)
return;
2013-09-24 14:56:51 -04:00
var el = jQuery(this),
uid = el.parents('li').attr('data-uid');
2013-09-24 14:56:51 -04:00
if (uid && jQuery.inArray(uid, users) !== -1) {
el.find('i').remove();
2013-11-26 14:25:46 -05:00
el.prepend('<i class="fa fa-circle"></i>');
2013-09-24 14:56:51 -04:00
} else {
el.find('i').remove();
2013-11-26 14:25:46 -05:00
el.prepend('<i class="fa fa-circle-o"></i>');
2013-09-24 14:56:51 -04:00
}
2013-09-24 14:56:51 -04:00
el.processed = true;
});
jQuery('button .username-field').each(function () {
//DRY FAIL
if (this.processed === true)
return;
2013-09-24 14:56:51 -04:00
var el = jQuery(this),
uid = el.parents('li').attr('data-uid');
2013-09-24 14:56:51 -04:00
if (uid && jQuery.inArray(uid, users) !== -1) {
el.parent().addClass('btn-success');
} else {
el.parent().addClass('btn-danger');
}
2013-09-24 14:56:51 -04:00
el.processed = true;
});
2013-07-11 15:50:19 -04:00
});
2013-10-28 14:36:31 -04:00
socket.on('event:banned', function() {
app.alert({
title: 'Banned',
message: 'You are banned you will be logged out!',
type: 'warning',
timeout: 1000
});
setTimeout(app.logout, 1000);
});
app.enterRoom('global');
2013-09-24 14:56:51 -04:00
}
2013-07-11 15:50:19 -04:00
},
async: false
});
2013-12-08 10:38:09 -05:00
};
2013-10-28 14:36:31 -04:00
app.logout = function() {
$.post(RELATIVE_PATH + '/logout', {
_csrf: $('#csrf_token').val()
}, function() {
2013-11-26 15:28:45 -05:00
window.location.href = RELATIVE_PATH + '/';
2013-10-28 14:36:31 -04:00
});
2013-12-08 10:38:09 -05:00
};
2013-10-28 14:36:31 -04:00
// takes a string like 1000 and returns 1,000
app.addCommas = function (text) {
2013-06-20 16:48:17 -04:00
return text.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
2013-12-08 10:38:09 -05:00
};
2013-06-20 16:48:17 -04:00
// Willingly stolen from: http://phpjs.org/functions/strip_tags/
app.strip_tags = function (input, allowed) {
2013-05-22 17:14:06 -04:00
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
2013-09-17 13:03:53 -04:00
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
2013-05-22 17:14:06 -04:00
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
2013-05-22 17:14:06 -04:00
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
2013-12-08 10:38:09 -05:00
};
2013-05-22 17:14:06 -04:00
// use unique alert_id to have multiple alerts visible at a time, use the same alert_id to fade out the current instance
// type : error, success, info, warning/notify
// title = bolded title text
// message = alert message content
// timeout default = permanent
// location : alert_window (default) or content
app.alert = function (params) {
var alert_id = 'alert_button_' + ((params.alert_id) ? params.alert_id : new Date().getTime());
2013-09-17 13:03:53 -04:00
var alert = $('#' + alert_id);
var title = params.title || '';
2013-06-21 17:48:12 -04:00
function startTimeout(div, timeout) {
var timeoutId = setTimeout(function () {
$(div).fadeOut(1000, function () {
$(this).remove();
2013-06-21 17:48:12 -04:00
});
}, timeout);
2013-06-21 17:48:12 -04:00
$(div).attr('timeoutId', timeoutId);
}
2013-09-17 13:03:53 -04:00
if (alert.length > 0) {
alert.find('strong').html(title);
alert.find('p').html(params.message);
alert.attr('class', "alert toaster-alert " + "alert-" + params.type);
2013-06-21 17:48:12 -04:00
clearTimeout(alert.attr('timeoutId'));
startTimeout(alert, params.timeout);
2013-09-17 13:03:53 -04:00
} else {
var div = $('<div id="' + alert_id + '" class="alert toaster-alert alert-' + params.type +'"></div>'),
button = $('<button class="close">&times;</button>'),
strong = $('<strong>' + title + '</strong>'),
p = $('<p>' + params.message + '</p>');
div.append(button)
.append(strong)
.append(p);
button.on('click', function () {
div.remove();
});
if (params.location == null)
params.location = 'alert_window';
$('#' + params.location).prepend(div.fadeIn('100'));
if (params.timeout) {
2013-06-21 17:48:12 -04:00
startTimeout(div, params.timeout);
}
if (params.clickfn) {
div.on('click', function () {
params.clickfn();
div.fadeOut(500, function () {
$(this).remove();
});
});
}
}
2013-12-08 10:38:09 -05:00
};
app.alertSuccess = function (message, timeout) {
2013-09-17 13:03:53 -04:00
if (!timeout)
timeout = 2000;
app.alert({
title: 'Success',
message: message,
type: 'success',
timeout: timeout
});
2013-12-08 10:38:09 -05:00
};
app.alertError = function (message, timeout) {
2013-09-17 13:03:53 -04:00
if (!timeout)
timeout = 2000;
app.alert({
title: 'Error',
message: message,
type: 'danger',
timeout: timeout
});
2013-12-08 10:38:09 -05:00
};
2013-06-21 17:48:12 -04:00
app.enterRoom = function (room) {
2013-09-17 13:03:53 -04:00
if (socket) {
if (app.currentRoom === room) {
2013-07-14 21:58:11 -04:00
return;
}
2013-07-14 21:58:11 -04:00
socket.emit('event:enter_room', {
'enter': room,
'leave': app.currentRoom
2013-07-14 21:58:11 -04:00
});
app.currentRoom = room;
2013-07-14 21:58:11 -04:00
}
};
app.populateOnlineUsers = function () {
2013-07-17 12:57:57 -04:00
var uids = [];
jQuery('.post-row').each(function () {
2013-07-17 12:57:57 -04:00
uids.push(this.getAttribute('data-uid'));
});
2013-07-17 12:57:57 -04:00
socket.emit('api:user.get_online_users', uids);
2013-12-08 10:38:09 -05:00
};
function highlightNavigationLink() {
var path = window.location.pathname,
parts = path.split('/'),
2013-09-17 13:03:53 -04:00
active = parts[parts.length - 1];
2013-07-24 12:33:37 -04:00
jQuery('#main-nav li').removeClass('active');
2013-09-17 13:03:53 -04:00
if (active) {
jQuery('#main-nav li a').each(function () {
2013-07-22 18:29:09 -04:00
var href = this.getAttribute('href');
2013-09-23 13:43:15 -04:00
if (active == "sort-posts" || active == "sort-reputation" || active == "search" || active == "latest" || active == "online")
2013-07-22 18:29:09 -04:00
active = 'users';
2013-07-24 12:33:37 -04:00
if (href && href.match(active)) {
2013-07-22 18:29:09 -04:00
jQuery(this.parentNode).addClass('active');
return false;
}
});
}
2013-12-08 10:38:09 -05:00
};
2013-11-24 22:48:58 -05:00
app.createUserTooltips = function() {
$('img[title].teaser-pic,img[title].user-img').each(function() {
$(this).tooltip({
placement: 'top',
title: $(this).attr('title')
});
});
2013-12-08 10:38:09 -05:00
};
2013-11-24 22:48:58 -05:00
app.makeNumbersHumanReadable = function(elements) {
elements.each(function() {
2013-11-29 14:12:19 -05:00
$(this).html(utils.makeNumberHumanReadable($(this).attr('title')));
2013-11-28 11:16:52 -05:00
});
2013-12-08 10:38:09 -05:00
};
2013-11-28 11:16:52 -05:00
app.processPage = function () {
app.populateOnlineUsers();
highlightNavigationLink();
2013-07-22 18:29:09 -04:00
2013-09-19 15:02:35 -04:00
$('span.timeago').timeago();
2013-10-19 15:58:54 -04:00
$('.post-content img').addClass('img-responsive');
2013-11-28 11:16:52 -05:00
2013-11-29 14:19:39 -05:00
app.makeNumbersHumanReadable($('.human-readable-number'));
2013-09-23 14:40:31 -04:00
2013-11-24 22:48:58 -05:00
app.createUserTooltips();
setTimeout(function () {
window.scrollTo(0, 1); // rehide address bar on mobile after page load completes.
}, 100);
2013-12-08 10:38:09 -05:00
};
app.showLoginMessage = function () {
function showAlert() {
2013-07-25 16:20:18 -04:00
app.alert({
type: 'success',
title: 'Welcome Back ' + app.username + '!',
message: 'You have successfully logged in!',
timeout: 5000
});
}
2013-09-17 13:03:53 -04:00
if (showWelcomeMessage) {
showWelcomeMessage = false;
2013-09-17 13:03:53 -04:00
if (document.readyState !== 'complete') {
$(document).ready(showAlert);
} else {
showAlert();
}
}
2013-12-08 10:38:09 -05:00
};
2013-07-25 16:20:18 -04:00
app.addCommasToNumbers = function () {
$('.formatted-number').each(function (index, element) {
2013-08-01 16:11:00 -04:00
$(element).html(app.addCommas($(element).html()));
});
2013-12-08 10:38:09 -05:00
};
2013-08-01 16:11:00 -04:00
app.openChat = function (username, touid) {
if (username === app.username) {
app.alert({
type: 'warning',
title: 'Invalid Chat',
message: "You can't chat with yourself!",
timeout: 5000
});
return;
}
2013-11-21 20:11:06 -05:00
if (!app.username) {
app.alert({
type: 'danger',
title: 'Not Logged In',
message: 'Please log in to chat with <strong>' + username + '</strong>',
timeout: 5000
});
return;
}
require(['chat'], function (chat) {
2013-08-30 14:25:59 -04:00
var chatModal;
2013-09-17 13:03:53 -04:00
if (!chat.modalExists(touid)) {
2013-08-30 14:25:59 -04:00
chatModal = chat.createModal(username, touid);
} else {
chatModal = chat.getModal(touid);
}
chat.load(chatModal.attr('UUID'));
2013-11-04 12:37:06 -05:00
chat.center(chatModal);
2013-08-30 14:25:59 -04:00
});
2013-12-08 10:38:09 -05:00
};
2013-11-26 14:25:46 -05:00
app.scrollToTop = function () {
$('body,html').animate({
scrollTop: 0
});
};
app.scrollToBottom = function () {
$('body,html').animate({
scrollTop: $('html').height() - 100
});
};
var titleObj = {
active: false,
interval: undefined,
titles: []
};
app.alternatingTitle = function (title) {
if (typeof title !== 'string') return;
if (title.length > 0) {
titleObj.titles[1] = title;
if (titleObj.interval) {
clearInterval(titleObj.interval);
}
titleObj.interval = setInterval(function() {
window.document.title = titleObj.titles[titleObj.titles.indexOf(window.document.title) ^ 1];
}, 2000);
} else {
if (titleObj.interval) {
clearInterval(titleObj.interval);
}
if (titleObj.titles[0]) window.document.title = titleObj.titles[0];
}
};
app.refreshTitle = function(url) {
if (!url) {
var a = document.createElement('a');
a.href = document.location;
url = a.pathname.slice(1);
}
socket.emit('api:meta.buildTitle', url, function(title, numNotifications) {
titleObj.titles[0] = (numNotifications > 0 ? '(' + numNotifications + ') ' : '') + title;
app.alternatingTitle('');
});
};
jQuery('document').ready(function () {
$('#search-form').on('submit', function () {
var input = $(this).find('input');
2013-09-17 13:03:53 -04:00
ajaxify.go("search/" + input.val(), null, "search");
input.val('');
return false;
2013-09-23 14:40:31 -04:00
});
2013-12-08 10:38:09 -05:00
$(window).blur(function(){
app.isFocused = false;
});
$(window).focus(function(){
app.isFocused = true;
});
2013-05-17 12:42:20 -04:00
});
showWelcomeMessage = location.href.indexOf('loggedin') !== -1;
2013-09-24 14:56:51 -04:00
app.loadConfig();
app.alternatingTitle('');
2013-09-17 13:03:53 -04:00
}());