Files
NodeBB/public/src/app.js

421 lines
10 KiB
JavaScript
Raw Normal View History

2013-04-22 19:13:39 +00:00
var socket,
config,
app = {},
2013-07-15 01:40:48 -04:00
API_URL = null;
2013-04-22 19:13:39 +00:00
(function() {
var showWelcomeMessage = false;
2013-07-11 15:51:26 -04:00
function loadConfig() {
2013-07-11 15:50:19 -04:00
$.ajax({
2013-08-13 11:25:10 -04:00
url: RELATIVE_PATH + '/api/config',
2013-07-11 15:50:19 -04:00
success: function(data) {
API_URL = data.api_url;
2013-07-11 15:50:19 -04:00
config = data;
socket = io.connect(config.socket.address);
2013-07-11 15:50:19 -04:00
var reconnecting = false;
var reconnectTries = 0;
2013-07-11 15:50:19 -04:00
socket.on('event:connect', function(data) {
console.log('connected to nodebb socket: ', data);
2013-07-25 16:20:18 -04:00
app.username = data.username;
2013-07-25 18:06:20 -04:00
app.showLoginMessage();
2013-07-11 15:50:19 -04:00
});
2013-07-25 16:20:18 -04:00
2013-07-11 15:50:19 -04:00
socket.on('event:alert', function(data) {
app.alert(data);
});
2013-07-11 15:50:19 -04:00
socket.on('connect', function(data){
if(reconnecting) {
setTimeout(function(){
app.alert({
alert_id: 'connection_alert',
title: 'Connected',
message: 'Connection successful.',
type: 'success',
timeout: 5000
});
}, 1000);
reconnecting = false;
reconnectTries = 0;
2013-07-11 15:50:19 -04:00
socket.emit('api:updateHeader', { fields: ['username', 'picture', 'userslug'] });
}
2013-05-23 11:49:56 -04:00
});
2013-07-11 15:50:19 -04:00
socket.on('reconnecting', function(data) {
function showDisconnectModal() {
$('#disconnect-modal').modal({
backdrop:'static',
show:true
});
$('#reload-button').on('click',function(){
$('#disconnect-modal').modal('hide');
window.location.reload();
});
}
2013-07-11 15:50:19 -04:00
reconnecting = true;
reconnectTries++;
2013-07-11 15:50:19 -04:00
if(reconnectTries > 4) {
showDisconnectModal();
return;
}
2013-07-11 15:50:19 -04:00
app.alert({
alert_id: 'connection_alert',
title: 'Reconnecting',
message: 'You have disconnected from NodeBB, we will try to reconnect you. <br/><i class="icon-refresh icon-spin"></i>',
type: 'warning',
2013-07-11 15:50:19 -04:00
timeout: 5000
});
});
2013-07-11 15:50:19 -04:00
socket.on('api:user.get_online_users', function(users) {
jQuery('a.username-field').each(function() {
if (this.processed === true)
return;
2013-07-11 15:50:19 -04:00
var el = jQuery(this),
uid = el.parents('li').attr('data-uid');
2013-07-11 15:50:19 -04:00
if (uid && jQuery.inArray(uid, users) !== -1) {
2013-07-17 12:57:57 -04:00
el.find('i').remove();
el.prepend('<i class="icon-circle"></i>');
2013-07-11 15:50:19 -04:00
} else {
2013-07-17 12:57:57 -04:00
el.find('i').remove();
el.prepend('<i class="icon-circle-blank"></i>');
2013-07-11 15:50:19 -04:00
}
el.processed = true;
});
jQuery('button .username-field').each(function() {
//DRY FAIL
if (this.processed === true)
return;
var el = jQuery(this),
uid = el.parents('li').attr('data-uid');
if (uid && jQuery.inArray(uid, users) !== -1) {
el.parent().addClass('btn-success');
} else {
el.parent().addClass('btn-danger');
}
2013-07-11 15:50:19 -04:00
el.processed = true;
});
});
app.enter_room('global');
2013-07-11 15:50:19 -04:00
},
async: false
});
}
// 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-06-20 16:48:17 -04:00
// Willingly stolen from: http://phpjs.org/functions/strip_tags/
2013-05-22 17:14:06 -04:00
app.strip_tags = function(input, allowed) {
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>)
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
}
// 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());
var alert = $('#'+alert_id);
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);
}
if(alert.length > 0) {
alert.find('strong').html(params.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);
}
else {
var div = document.createElement('div'),
button = document.createElement('button'),
strong = document.createElement('strong'),
p = document.createElement('p');
p.innerHTML = params.message;
strong.innerHTML = params.title;
div.className = "alert toaster-alert " + "alert-" + params.type;
div.setAttribute('id', alert_id);
div.appendChild(button);
div.appendChild(strong);
div.appendChild(p);
button.className = 'close';
button.innerHTML = '&times;';
button.onclick = function(ev) {
div.parentNode.removeChild(div);
}
if (params.location == null)
params.location = 'alert_window';
jQuery('#'+params.location).prepend(jQuery(div).fadeIn('100'));
if (params.timeout) {
2013-06-21 17:48:12 -04:00
startTimeout(div, params.timeout);
}
if (params.clickfn) {
div.onclick = function() {
params.clickfn();
jQuery(div).fadeOut(500, function() {
this.remove();
});
}
}
}
}
app.alertSuccess = function(message, timeout) {
if(!timeout)
timeout = 2000;
app.alert({
title: 'Success',
message: message,
type: 'success',
timeout: timeout
});
}
app.alertError = function(message, timeout) {
if(!timeout)
timeout = 2000;
app.alert({
title: 'Error',
message: message,
type: 'danger',
timeout: timeout
});
}
2013-06-21 17:48:12 -04:00
app.current_room = null;
app.enter_room = function(room) {
2013-07-14 21:58:11 -04:00
if(socket) {
if (app.current_room === 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.current_room
});
2013-07-14 21:58:11 -04:00
app.current_room = room;
}
};
2013-07-17 12:57:57 -04:00
app.populate_online_users = function() {
var uids = [];
2013-07-17 12:57:57 -04:00
jQuery('.post-row').each(function() {
uids.push(this.getAttribute('data-uid'));
});
2013-07-17 12:57:57 -04:00
socket.emit('api:user.get_online_users', uids);
}
2013-07-17 12:57:57 -04:00
app.process_page = function() {
// here is where all modules' onNavigate should be called, I think.
require(['mobileMenu'], function(mobileMenu) {
mobileMenu.onNavigate();
});
2013-07-17 12:57:57 -04:00
app.populate_online_users();
2013-07-22 18:29:09 -04:00
var url = window.location.href,
parts = url.split('/'),
active = parts[parts.length-1];
2013-07-24 12:33:37 -04:00
jQuery('#main-nav li').removeClass('active');
2013-07-22 18:29:09 -04:00
if(active) {
2013-07-24 12:33:37 -04:00
jQuery('#main-nav li a').each(function() {
2013-07-22 18:29:09 -04:00
var href = this.getAttribute('href');
if(active.match(/^users/))
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;
}
});
}
setTimeout(function() {
window.scrollTo(0, 1); // rehide address bar on mobile after page load completes.
}, 100);
}
2013-07-25 16:20:18 -04: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
});
}
if(showWelcomeMessage) {
showWelcomeMessage = false;
if(document.readyState !== 'complete') {
$(document).ready(showAlert);
} else {
showAlert();
}
}
2013-07-25 16:20:18 -04:00
}
2013-08-01 16:11:00 -04:00
app.addCommasToNumbers = function() {
$('.formatted-number').each(function(index, element) {
$(element).html(app.addCommas($(element).html()));
});
}
2013-08-30 14:25:59 -04:00
app.openChat = function(username, touid) {
require(['chat'], function(chat) {
var chatModal;
if(!chat.modalExists(touid)) {
chatModal = chat.createModal(username, touid);
} else {
chatModal = chat.getModal(touid);
}
chat.load(chatModal.attr('UUID'));
});
}
2013-08-01 16:11:00 -04:00
app.createNewPosts = function(data) {
data.posts[0].display_moderator_tools = 'none';
var html = templates.prepare(templates['topic'].blocks['posts']).parse(data),
uniqueid = new Date().getTime(),
tempContainer = jQuery('<div id="' + uniqueid + '"></div>')
.appendTo("#post-container")
.hide()
.append(html)
.fadeIn('slow');
for(var x=0,numPosts=data.posts.length;x<numPosts;x++) {
socket.emit('api:post.privileges', data.posts[x].pid);
}
tempContainer.replaceWith(tempContainer.contents());
infiniteLoaderActive = false;
app.populate_online_users();
app.addCommasToNumbers();
}
app.infiniteLoaderActive = false;
app.loadMorePosts = function(tid, callback) {
if(app.infiniteLoaderActive)
return;
2013-08-01 17:27:37 -04:00
app.infiniteLoaderActive = true;
socket.emit('api:topic.loadMore', {
tid: tid,
after: document.querySelectorAll('#post-container li[data-pid]').length
2013-08-01 16:11:00 -04:00
}, function(data) {
app.infiniteLoaderActive = false;
if(data.posts.length) {
app.createNewPosts(data);
}
2013-08-23 13:45:57 -04:00
if(callback)
callback(data.posts);
2013-08-01 16:11:00 -04:00
});
}
app.scrollToPost = function(pid) {
2013-08-01 16:11:00 -04:00
if(!pid)
return;
2013-08-21 19:21:49 -04:00
2013-08-01 16:11:00 -04:00
var container = $(document.body),
scrollTo = $('#post_anchor_' + pid),
tid = $('#post-container').attr('data-tid');
2013-08-01 16:11:00 -04:00
function animateScroll() {
2013-08-21 19:21:49 -04:00
$('body,html').animate({
scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop() - $('#header-menu').height()
});
}
if(!scrollTo.length && tid) {
2013-08-23 13:45:57 -04:00
var intervalID = setInterval(function() {
app.loadMorePosts(tid, function(posts) {
scrollTo = $('#post_anchor_' + pid);
2013-08-23 13:45:57 -04:00
if(tid && scrollTo.length) {
2013-08-23 13:45:57 -04:00
animateScroll();
}
2013-08-23 13:45:57 -04:00
if(!posts.length || scrollTo.length)
clearInterval(intervalID);
});
}, 100);
2013-08-23 13:45:57 -04:00
} else if(tid) {
animateScroll();
}
2013-08-01 16:11:00 -04:00
}
jQuery('document').ready(function() {
$('#search-form').on('submit', function() {
var input = $(this).find('input');
ajaxify.go("search/"+input.val(), null, "search");
input.val('');
return false;
})
2013-05-17 12:42:20 -04:00
});
showWelcomeMessage = location.href.indexOf('loggedin') !== -1;
2013-07-15 01:40:48 -04:00
loadConfig();
2013-04-22 19:13:39 +00:00
}());