2013-03-04 03:38:12 +00:00
|
|
|
(function(context,$) {
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
context.JK = context.JK || {};
|
|
|
|
|
context.JK.Sidebar = function(app) {
|
|
|
|
|
var logger = context.JK.logger;
|
2013-03-31 13:54:00 +00:00
|
|
|
var friends = [];
|
2013-04-05 03:51:01 +00:00
|
|
|
var notifications = [];
|
2013-03-04 03:38:12 +00:00
|
|
|
|
2013-03-10 03:00:34 +00:00
|
|
|
function initializeFriendsPanel() {
|
2013-03-04 03:38:12 +00:00
|
|
|
|
2013-03-10 01:57:09 +00:00
|
|
|
$('#sidebar-search-header').hide();
|
|
|
|
|
|
2013-03-04 03:38:12 +00:00
|
|
|
var url = "/api/users/" + context.JK.currentUserId + "/friends"
|
|
|
|
|
$.ajax({
|
|
|
|
|
type: "GET",
|
|
|
|
|
dataType: "json",
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
url: url,
|
|
|
|
|
processData: false,
|
|
|
|
|
success: function(response) {
|
2013-03-10 01:57:09 +00:00
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
friends = response;
|
|
|
|
|
updateFriendList(response);
|
2013-03-10 01:57:09 +00:00
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
// set friend count
|
|
|
|
|
$('#sidebar-friend-count').html(response.length);
|
|
|
|
|
},
|
|
|
|
|
error: app.ajaxError
|
|
|
|
|
});
|
2013-03-10 01:57:09 +00:00
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2013-03-10 01:57:09 +00:00
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
function updateFriendList(response) {
|
|
|
|
|
$('#sidebar-friend-list').empty();
|
2013-03-10 01:57:09 +00:00
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
// show online friends first (sort by first name within online/offline groups)
|
|
|
|
|
response.sort(function(a, b) {
|
2013-03-04 03:38:12 +00:00
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
var a_online = a.online;
|
|
|
|
|
var b_online = b.online;
|
2013-03-04 03:38:12 +00:00
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
var a_firstname = a.first_name.toLowerCase();
|
|
|
|
|
var b_firstname = b.first_name.toLowerCase();
|
2013-03-04 03:38:12 +00:00
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
if (b_online != a_online) {
|
|
|
|
|
if (b_online < a_online) return -1;
|
|
|
|
|
if (b_online > a_online) return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-03-04 03:38:12 +00:00
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
if (a_firstname < b_firstname) return -1;
|
|
|
|
|
if (a_firstname > b_firstname) return 1;
|
|
|
|
|
return 0;
|
2013-03-04 03:38:12 +00:00
|
|
|
});
|
|
|
|
|
|
2013-03-31 13:54:00 +00:00
|
|
|
$.each(response, function(index, val) {
|
|
|
|
|
|
|
|
|
|
var css = val.online ? '' : 'offline';
|
|
|
|
|
|
|
|
|
|
friends[val.id] = val;
|
|
|
|
|
|
|
|
|
|
// fill in template for Connect pre-click
|
|
|
|
|
var template = $('#template-friend-panel').html();
|
|
|
|
|
var searchResultHtml = context.JK.fillTemplate(template, {
|
|
|
|
|
userId: val.id,
|
|
|
|
|
cssClass: css,
|
|
|
|
|
avatar_url: context.JK.resolveAvatarUrl(val.photo_url),
|
|
|
|
|
userName: val.name,
|
|
|
|
|
status: val.online ? 'Available' : 'Offline',
|
|
|
|
|
extra_info: '',
|
|
|
|
|
info_image_url: ''
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#sidebar-friend-list').append(searchResultHtml);
|
|
|
|
|
});
|
2013-03-04 03:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-10 03:00:34 +00:00
|
|
|
function initializeNotificationsPanel() {
|
2013-04-05 03:51:01 +00:00
|
|
|
// retrieve pending notifications for this user
|
|
|
|
|
var url = "/api/users/" + context.JK.currentUserId + "/notifications"
|
|
|
|
|
$.ajax({
|
|
|
|
|
type: "GET",
|
|
|
|
|
dataType: "json",
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
url: url,
|
|
|
|
|
processData: false,
|
|
|
|
|
success: function(response) {
|
|
|
|
|
|
|
|
|
|
notifications = response;
|
|
|
|
|
updateNotificationList(response);
|
|
|
|
|
|
|
|
|
|
// set notification count
|
|
|
|
|
$('#sidebar-notification-count').html(response.length);
|
|
|
|
|
},
|
|
|
|
|
error: app.ajaxError
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateNotificationList(response) {
|
|
|
|
|
$('#sidebar-notification-list').empty();
|
|
|
|
|
|
|
|
|
|
$.each(response, function(index, val) {
|
|
|
|
|
|
2013-04-14 02:59:43 +00:00
|
|
|
notifications[val.notification_id] = val;
|
2013-04-05 03:51:01 +00:00
|
|
|
|
|
|
|
|
// fill in template for Connect pre-click
|
|
|
|
|
var template = $('#template-notification-panel').html();
|
|
|
|
|
var notificationHtml = context.JK.fillTemplate(template, {
|
2013-04-14 02:59:43 +00:00
|
|
|
notificationId: val.notification_id,
|
2013-04-05 03:51:01 +00:00
|
|
|
avatar_url: context.JK.resolveAvatarUrl(val.photo_url),
|
|
|
|
|
text: val.formatted_msg,
|
|
|
|
|
date: val.created_at
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#sidebar-notification-list').append(notificationHtml);
|
2013-04-07 01:41:43 +00:00
|
|
|
|
2013-04-14 02:59:43 +00:00
|
|
|
initializeActions(val, val.description);
|
2013-04-05 03:51:01 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-14 02:59:43 +00:00
|
|
|
function initializeActions(notification, type) {
|
|
|
|
|
// wire up "x" button to delete notification
|
|
|
|
|
$('li[notification-id=' + notification.notification_id + ']').find('#img-delete-notification').click(deleteNotification);
|
|
|
|
|
|
|
|
|
|
if (type == context.JK.MessageType.FRIEND_REQUEST) {
|
|
|
|
|
var $action_btn = $('li[notification-id=' + notification.notification_id + ']').find('#btn-notification-action');
|
|
|
|
|
$action_btn.text('ACCEPT');
|
|
|
|
|
$action_btn.click(function() {
|
|
|
|
|
acceptFriendRequest(notification.friend_request_id);
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-04-14 12:45:13 +00:00
|
|
|
else if (type == context.JK.MessageType.FRIEND_REQUEST_ACCEPTED) {
|
|
|
|
|
$('li[notification-id=' + notification.notification_id + ']').find('#div-actions').hide();
|
|
|
|
|
}
|
2013-04-14 02:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-05 03:51:01 +00:00
|
|
|
function deleteNotification(evt) {
|
2013-04-07 01:41:43 +00:00
|
|
|
evt.stopPropagation();
|
|
|
|
|
|
|
|
|
|
var notificationId = $(this).attr('notification-id');
|
|
|
|
|
|
|
|
|
|
var url = "/api/users/" + context.JK.currentUserId + "/notifications/" + notificationId;
|
|
|
|
|
$.ajax({
|
|
|
|
|
type: "DELETE",
|
|
|
|
|
dataType: "json",
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
url: url,
|
|
|
|
|
processData: false,
|
|
|
|
|
success: function(response) {
|
|
|
|
|
delete notifications[notificationId];
|
|
|
|
|
$('li[notification-id=' + notificationId + ']').hide();
|
|
|
|
|
decrementNotificationCount();
|
|
|
|
|
},
|
|
|
|
|
error: app.ajaxError
|
|
|
|
|
});
|
2013-03-10 03:00:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function initializeChatPanel() {
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-10 01:57:09 +00:00
|
|
|
function search(query) {
|
|
|
|
|
|
|
|
|
|
logger.debug('query=' + query);
|
|
|
|
|
if (query !== '') {
|
2013-03-15 05:18:16 +00:00
|
|
|
context.JK.search(query, app, onSearchSuccess);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-10 01:57:09 +00:00
|
|
|
|
2013-03-15 05:18:16 +00:00
|
|
|
function onSearchSuccess(response) {
|
|
|
|
|
$.each(response.musicians, function(index, val) {
|
|
|
|
|
|
|
|
|
|
// fill in template for Connect pre-click
|
|
|
|
|
var template = $('#template-sidebar-search-result').html();
|
|
|
|
|
var searchResultHtml = context.JK.fillTemplate(template, {
|
|
|
|
|
userId: val.id,
|
|
|
|
|
avatar_url: context.JK.resolveAvatarUrl(val.photo_url),
|
|
|
|
|
userName: val.name,
|
|
|
|
|
location: val.location
|
2013-03-10 01:57:09 +00:00
|
|
|
});
|
2013-03-15 05:18:16 +00:00
|
|
|
|
|
|
|
|
$('#sidebar-search-results').append(searchResultHtml);
|
|
|
|
|
|
|
|
|
|
// fill in template for Connect post-click
|
|
|
|
|
template = $('#template-sidebar-invitation-sent').html();
|
|
|
|
|
var invitationSentHtml = context.JK.fillTemplate(template, {
|
|
|
|
|
userId: val.id,
|
|
|
|
|
first_name: val.first_name,
|
|
|
|
|
profile_url: "/users/" + val.id
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#sidebar-search-results').append(invitationSentHtml);
|
|
|
|
|
|
|
|
|
|
// initialize visibility of the divs
|
|
|
|
|
$('div[layout=sidebar] div[user-id=' + val.id + '].sidebar-search-connected').hide();
|
|
|
|
|
$('div[layout=sidebar] div[user-id=' + val.id + '].sidebar-search-result').show();
|
|
|
|
|
|
2013-04-07 00:42:18 +00:00
|
|
|
// wire up button click handler if search result is not a friend
|
|
|
|
|
if (!val.is_friend) {
|
|
|
|
|
$('div[layout=sidebar] div[user-id=' + val.id + ']').find('#btn-connect-friend').click(sendFriendRequest);
|
|
|
|
|
}
|
|
|
|
|
// hide the button if the search result is already a friend
|
|
|
|
|
else {
|
|
|
|
|
$('div[layout=sidebar] div[user-id=' + val.id + ']').find('#btn-connect-friend').hide();
|
|
|
|
|
}
|
2013-03-15 05:18:16 +00:00
|
|
|
});
|
|
|
|
|
|
2013-03-22 00:17:28 +00:00
|
|
|
// show header
|
2013-03-15 05:18:16 +00:00
|
|
|
$('#sidebar-search-header').show();
|
2013-03-10 01:57:09 +00:00
|
|
|
|
2013-03-22 00:17:28 +00:00
|
|
|
// hide panels
|
2013-03-10 01:57:09 +00:00
|
|
|
$('[layout-panel="contents"]').hide();
|
|
|
|
|
$('[layout-panel="contents"]').css({"height": "1px"});
|
2013-03-22 00:17:28 +00:00
|
|
|
|
|
|
|
|
// resize search results area
|
|
|
|
|
$('#sidebar-search-results').height(getHeight() + 'px');
|
2013-03-10 01:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-10 04:28:21 +00:00
|
|
|
function getHeight() {
|
|
|
|
|
// TODO: refactor this - copied from layout.js
|
|
|
|
|
var sidebarHeight = $(context).height() - 75 - 2 * 60 + $('[layout-sidebar-expander]').height();
|
|
|
|
|
var combinedHeaderHeight = $('[layout-panel="contents"]').length * 36;
|
|
|
|
|
var searchHeight = $('.sidebar .search').first().height();
|
|
|
|
|
var expanderHeight = $('[layout-sidebar-expander]').height();
|
|
|
|
|
var expandedPanelHeight = sidebarHeight - (combinedHeaderHeight + expanderHeight + searchHeight);
|
|
|
|
|
return expandedPanelHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function showFriendsPanel() {
|
|
|
|
|
|
|
|
|
|
var $expandedPanelContents = $('[layout-id="panelFriends"] [layout-panel="contents"]');
|
|
|
|
|
var expandedPanelHeight = getHeight();
|
|
|
|
|
|
|
|
|
|
// hide all other contents
|
|
|
|
|
$('[layout-panel="contents"]').hide();
|
|
|
|
|
$('[layout-panel="contents"]').css({"height": "1px"});
|
|
|
|
|
|
|
|
|
|
// show the appropriate contens
|
|
|
|
|
$expandedPanelContents.show();
|
|
|
|
|
$expandedPanelContents.animate({"height": expandedPanelHeight + "px"}, 400);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-07 01:04:18 +00:00
|
|
|
function friendRequestCallback(userId) {
|
|
|
|
|
// toggle the pre-click and post-click divs
|
|
|
|
|
$('div[layout=sidebar] div[user-id=' + userId + '].sidebar-search-connected').show();
|
|
|
|
|
$('div[layout=sidebar] div[user-id=' + userId + '].sidebar-search-result').hide();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-04 03:38:12 +00:00
|
|
|
function sendFriendRequest(evt) {
|
|
|
|
|
evt.stopPropagation();
|
|
|
|
|
var userId = $(this).parent().attr('user-id');
|
2013-04-07 01:04:18 +00:00
|
|
|
context.JK.sendFriendRequest(app, userId, friendRequestCallback);
|
2013-03-04 03:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-10 01:57:09 +00:00
|
|
|
function hideSearchResults() {
|
|
|
|
|
emptySearchResults();
|
|
|
|
|
$('#search-input').val('');
|
|
|
|
|
$('#sidebar-search-header').hide();
|
2013-03-10 04:28:21 +00:00
|
|
|
showFriendsPanel();
|
2013-03-10 01:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function emptySearchResults() {
|
|
|
|
|
$('#sidebar-search-results').empty();
|
2013-03-10 04:28:21 +00:00
|
|
|
$('#sidebar-search-results').height('0px');
|
2013-03-10 01:57:09 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-07 00:23:39 +00:00
|
|
|
function incrementNotificationCount() {
|
|
|
|
|
var count = parseInt($('#sidebar-notification-count').html());
|
|
|
|
|
$('#sidebar-notification-count').html(count + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function decrementNotificationCount() {
|
|
|
|
|
var count = parseInt($('#sidebar-notification-count').html());
|
|
|
|
|
$('#sidebar-notification-count').html(count - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-14 02:59:43 +00:00
|
|
|
function acceptFriendRequest(friend_request_id) {
|
|
|
|
|
var friend_request = {};
|
|
|
|
|
friend_request.status = 'accept';
|
|
|
|
|
|
|
|
|
|
var jsonData = JSON.stringify(friend_request);
|
|
|
|
|
|
|
|
|
|
var url = "/api/users/" + context.JK.currentUserId + "/friend_requests/" + friend_request_id;
|
|
|
|
|
$.ajax({
|
|
|
|
|
type: "POST",
|
|
|
|
|
dataType: "json",
|
|
|
|
|
contentType: 'application/json',
|
|
|
|
|
url: url,
|
|
|
|
|
data: jsonData,
|
|
|
|
|
processData: false,
|
|
|
|
|
success: function(response) {
|
|
|
|
|
initializeFriendsPanel(); // refresh friends panel when request is accepted
|
|
|
|
|
},
|
|
|
|
|
error: app.ajaxError
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// default handler for incoming notification
|
|
|
|
|
function handleNotification(payload, type) {
|
|
|
|
|
// update notifications panel in sidebar
|
|
|
|
|
notifications[payload.notification_id] = {
|
|
|
|
|
"id": payload.notification_id,
|
|
|
|
|
"photo_url": payload.photo_url,
|
|
|
|
|
"formatted_msg": payload.msg,
|
|
|
|
|
"created_at": payload.created_at
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
incrementNotificationCount();
|
|
|
|
|
|
|
|
|
|
var template = $("#template-notification-panel").html();
|
|
|
|
|
var notificationHtml = context.JK.fillTemplate(template, {
|
|
|
|
|
notificationId: payload.notification_id,
|
|
|
|
|
avatar_url: context.JK.resolveAvatarUrl(payload.photo_url),
|
|
|
|
|
text: payload.msg,
|
|
|
|
|
date: payload.created_at
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#sidebar-notification-list').prepend(notificationHtml);
|
|
|
|
|
|
|
|
|
|
initializeActions(payload, type);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-07 01:41:43 +00:00
|
|
|
// TODO: optimize so we're not hitting server on each keyup once > 2 characters are entered
|
2013-03-04 03:38:12 +00:00
|
|
|
function events() {
|
2013-03-10 01:57:09 +00:00
|
|
|
$('#search-input').keyup(function(evt) {
|
|
|
|
|
// ENTER KEY
|
|
|
|
|
if (evt.which === 13) {
|
|
|
|
|
return hideSearchResults();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ESCAPE KEY
|
|
|
|
|
if (evt.which === 27) {
|
|
|
|
|
return hideSearchResults();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = $(this).val();
|
|
|
|
|
|
|
|
|
|
if (query === '') {
|
|
|
|
|
return hideSearchResults();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query !== '' && query.length > 2) {
|
|
|
|
|
emptySearchResults();
|
|
|
|
|
search(query);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#sidebar-search-expand').click(function(evt) {
|
|
|
|
|
$('#searchForm').submit();
|
|
|
|
|
hideSearchResults();
|
|
|
|
|
});
|
2013-03-31 13:54:00 +00:00
|
|
|
|
|
|
|
|
// wire up FRIEND_UPDATE handler
|
|
|
|
|
context.JK.JamServer.registerMessageCallback(context.JK.MessageType.FRIEND_UPDATE, function(header, payload) {
|
|
|
|
|
logger.debug("Handling FRIEND_UPDATE msg " + JSON.stringify(payload));
|
|
|
|
|
|
|
|
|
|
// update friends panel in sidebar
|
|
|
|
|
friends[payload.user_id].online = payload.online;
|
|
|
|
|
updateFriendList(friends);
|
|
|
|
|
|
|
|
|
|
// display notification
|
|
|
|
|
var online_text = payload.online ? "online" : "offline";
|
|
|
|
|
app.notify({
|
|
|
|
|
"title": "Friend is now " + online_text,
|
|
|
|
|
"text": payload.msg,
|
|
|
|
|
"avatar_url": payload.photo_url
|
|
|
|
|
});
|
|
|
|
|
});
|
2013-04-14 02:59:43 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
2013-03-31 13:54:00 +00:00
|
|
|
|
|
|
|
|
// wire up FRIEND_REQUEST handler
|
|
|
|
|
context.JK.JamServer.registerMessageCallback(context.JK.MessageType.FRIEND_REQUEST, function(header, payload) {
|
|
|
|
|
logger.debug("Handling FRIEND_REQUEST msg " + JSON.stringify(payload));
|
|
|
|
|
|
2013-04-14 02:59:43 +00:00
|
|
|
handleNotification(payload, header.type);
|
2013-04-05 03:51:01 +00:00
|
|
|
|
2013-04-14 02:59:43 +00:00
|
|
|
// display notification
|
|
|
|
|
app.notify({
|
|
|
|
|
"title": "New Friend Request",
|
|
|
|
|
"text": payload.msg,
|
|
|
|
|
"avatar_url": payload.photo_url
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"ok_text": "ACCEPT",
|
|
|
|
|
"ok_callback": acceptFriendRequest,
|
|
|
|
|
"ok_callback_args": payload.friend_request_id
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
2013-04-05 03:51:01 +00:00
|
|
|
|
2013-04-14 02:59:43 +00:00
|
|
|
// wire up FRIEND_REQUEST_ACCEPTED handler
|
|
|
|
|
context.JK.JamServer.registerMessageCallback(context.JK.MessageType.FRIEND_REQUEST_ACCEPTED, function(header, payload) {
|
|
|
|
|
logger.debug("Handling FRIEND_REQUEST_ACCEPTED msg " + JSON.stringify(payload));
|
2013-04-05 03:51:01 +00:00
|
|
|
|
2013-04-14 02:59:43 +00:00
|
|
|
handleNotification(payload, header.type);
|
|
|
|
|
|
|
|
|
|
// refresh friends panel
|
|
|
|
|
initializeFriendsPanel();
|
2013-03-31 13:54:00 +00:00
|
|
|
|
|
|
|
|
// display notification
|
|
|
|
|
app.notify({
|
2013-04-14 02:59:43 +00:00
|
|
|
"title": "Friend Request Accepted",
|
2013-03-31 13:54:00 +00:00
|
|
|
"text": payload.msg,
|
|
|
|
|
"avatar_url": payload.photo_url
|
|
|
|
|
});
|
|
|
|
|
});
|
2013-04-14 02:59:43 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
2013-03-04 03:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.initialize = function() {
|
|
|
|
|
events();
|
2013-03-10 03:00:34 +00:00
|
|
|
initializeFriendsPanel();
|
|
|
|
|
initializeChatPanel();
|
|
|
|
|
initializeNotificationsPanel();
|
2013-03-04 03:38:12 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
})(window,jQuery);
|