2013-04-22 19:13:39 +00:00
|
|
|
var socket,
|
2013-04-23 19:38:48 +00:00
|
|
|
config,
|
|
|
|
|
app = {};
|
2013-04-22 19:13:39 +00:00
|
|
|
|
2013-04-24 20:40:34 +00:00
|
|
|
// todo: cleanup,etc
|
2013-04-22 19:13:39 +00:00
|
|
|
(function() {
|
|
|
|
|
|
2013-04-22 19:19:46 +00:00
|
|
|
$.ajax({
|
2013-04-23 16:18:43 -04:00
|
|
|
url: '/config.json?v=' + new Date().getTime(),
|
2013-04-22 19:19:46 +00:00
|
|
|
success: function(data) {
|
|
|
|
|
config = data;
|
|
|
|
|
socket = io.connect('http://' + config.socket.address + config.socket.port? ':' + config.socket.port : '');
|
|
|
|
|
|
2013-04-24 21:16:10 +00:00
|
|
|
socket.on('event:connect', function(data) {
|
2013-04-24 21:19:04 +00:00
|
|
|
console.log('connected to nodebb socket: ', data);
|
2013-04-22 19:19:46 +00:00
|
|
|
});
|
2013-04-23 20:17:12 +00:00
|
|
|
|
|
|
|
|
socket.on('event:alert', function(data) {
|
|
|
|
|
app.alert(data);
|
|
|
|
|
});
|
2013-04-22 19:19:46 +00:00
|
|
|
},
|
|
|
|
|
async: false
|
2013-04-22 19:13:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2013-04-23 19:38:48 +00: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
|
|
|
|
|
// timeout default = permanent
|
|
|
|
|
// location : notification_window (default) or content
|
|
|
|
|
app.alert = function(params) {
|
|
|
|
|
var div = document.createElement('div'),
|
|
|
|
|
button = document.createElement('button'),
|
|
|
|
|
strong = document.createElement('strong'),
|
|
|
|
|
p = document.createElement('p');
|
|
|
|
|
|
|
|
|
|
var alert_id = 'alert_button_' + ((alert_id) ? alert_id : new Date().getTime());
|
|
|
|
|
|
|
|
|
|
jQuery('#'+alert_id).fadeOut(500, function() {
|
|
|
|
|
this.remove();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
p.innerHTML = params.message;
|
|
|
|
|
strong.innerHTML = params.title;
|
|
|
|
|
|
2013-04-24 20:40:34 +00:00
|
|
|
div.className = "alert toaster-alert " + ((params.type=='warning') ? '' : "alert-" + params.type);
|
2013-04-23 19:38:48 +00:00
|
|
|
|
|
|
|
|
div.setAttribute('id', alert_id);
|
|
|
|
|
div.appendChild(button);
|
|
|
|
|
div.appendChild(strong);
|
|
|
|
|
div.appendChild(p);
|
|
|
|
|
|
|
|
|
|
button.className = 'close';
|
|
|
|
|
button.innerHTML = '×';
|
|
|
|
|
button.onclick = function(ev) {
|
|
|
|
|
div.parentNode.removeChild(div);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (params.location == null) params.location = 'notification_window';
|
|
|
|
|
|
|
|
|
|
jQuery('#'+params.location).prepend(jQuery(div).fadeIn('100'));
|
|
|
|
|
|
|
|
|
|
if (params.timeout) {
|
|
|
|
|
setTimeout(function() {
|
2013-04-24 20:40:34 +00:00
|
|
|
jQuery(div).fadeOut(1000);
|
2013-04-23 19:38:48 +00:00
|
|
|
}, params.timeout)
|
|
|
|
|
}
|
2013-04-24 20:40:34 +00:00
|
|
|
|
|
|
|
|
if (params.clickfn) {
|
|
|
|
|
div.onclick = function() {
|
|
|
|
|
params.clickfn();
|
|
|
|
|
jQuery(div).fadeOut(500);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-23 19:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-24 20:40:34 +00:00
|
|
|
var post_window = null;
|
|
|
|
|
app.open_post_window = function() {
|
|
|
|
|
post_window = post_window || document.getElementById('post_window');
|
|
|
|
|
jQuery(post_window).slideToggle(250);
|
2013-04-24 21:16:10 +00:00
|
|
|
document.getElementById('post_title').focus();
|
2013-04-24 20:40:34 +00:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
app.post_topic = function() {
|
2013-04-24 20:47:41 +00:00
|
|
|
var title = document.getElementById('post_title').value,
|
|
|
|
|
content = document.getElementById('post_content').value;
|
2013-04-24 20:40:34 +00:00
|
|
|
|
|
|
|
|
if (title.length < 5 || content.length < 5) {
|
|
|
|
|
app.alert({
|
|
|
|
|
title: 'Topic Post Failure',
|
|
|
|
|
message: 'You need to write more dude.',
|
|
|
|
|
type: 'error',
|
|
|
|
|
timeout: 2000,
|
|
|
|
|
clickfn: function() {
|
|
|
|
|
ajaxify.go('register');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-24 21:16:10 +00:00
|
|
|
socket.emit('api:topics.post', {
|
2013-04-24 20:40:34 +00:00
|
|
|
'title' : title,
|
|
|
|
|
'content' : content
|
|
|
|
|
});
|
|
|
|
|
jQuery(post_window).slideToggle(250);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
jQuery('document').ready(function() {
|
2013-04-25 12:59:31 -04:00
|
|
|
// On menu click, change "active" state
|
|
|
|
|
var menuEl = document.querySelector('.nav'),
|
|
|
|
|
liEls = menuEl.querySelectorAll('li'),
|
|
|
|
|
logoutEl = document.getElementById('logout'),
|
|
|
|
|
parentEl;
|
|
|
|
|
|
|
|
|
|
menuEl.addEventListener('click', function(e) {
|
|
|
|
|
parentEl = e.target.parentNode;
|
|
|
|
|
if (parentEl.nodeName === 'LI') {
|
|
|
|
|
for(var x=0,numLis=liEls.length;x<numLis;x++) {
|
|
|
|
|
if (liEls[x] !== parentEl) liEls[x].className = '';
|
|
|
|
|
else parentEl.className = 'active';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
|
|
// Posting
|
2013-04-24 20:40:34 +00:00
|
|
|
jQuery('#post_window').slideToggle(0);
|
2013-04-25 12:59:31 -04:00
|
|
|
|
|
|
|
|
// Logout
|
|
|
|
|
logoutEl.addEventListener('click', function() {
|
|
|
|
|
socket.emit('api:user.logout');
|
|
|
|
|
});
|
|
|
|
|
socket.on('api:user.logout', function(data) {
|
|
|
|
|
if (data.status === 'ok') alert('Logged out.');
|
|
|
|
|
});
|
2013-04-24 20:40:34 +00:00
|
|
|
})
|
2013-04-22 19:13:39 +00:00
|
|
|
}());
|