2012-10-06 16:36:05 +00:00
|
|
|
(function(context,$) {
|
|
|
|
|
|
2012-12-07 00:28:48 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
2012-10-06 16:36:05 +00:00
|
|
|
context.JK = context.JK || {};
|
|
|
|
|
context.JK.FindSessionScreen = function(app) {
|
2013-01-21 05:52:28 +00:00
|
|
|
var CATEGORY = {
|
2013-02-07 04:24:35 +00:00
|
|
|
INVITATION : {index: 0, id: "table#sessions-invitations"},
|
2013-02-07 07:14:45 +00:00
|
|
|
FRIEND : {index: 1, id: "table#sessions-friends"},
|
2013-02-07 04:24:35 +00:00
|
|
|
OTHER : {index: 2, id: "table#sessions-other"}
|
2013-02-10 18:51:37 +00:00
|
|
|
};
|
2013-01-21 05:52:28 +00:00
|
|
|
|
2012-10-14 15:48:56 +00:00
|
|
|
var logger = context.JK.logger;
|
2012-11-28 02:29:46 +00:00
|
|
|
var sessionLatency;
|
2012-12-01 23:50:28 +00:00
|
|
|
var sessions = {};
|
2013-01-27 20:17:46 +00:00
|
|
|
var invitationSessionGroup = {};
|
|
|
|
|
var friendSessionGroup = {};
|
|
|
|
|
var otherSessionGroup = {};
|
2013-02-07 07:14:45 +00:00
|
|
|
var sessionCounts = [0, 0, 0];
|
2013-01-25 08:00:53 +00:00
|
|
|
var sessionList;
|
2012-10-06 16:36:05 +00:00
|
|
|
|
2013-01-30 00:36:54 +00:00
|
|
|
// for unit tests
|
|
|
|
|
function getCategoryEnum() {
|
|
|
|
|
return CATEGORY;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 03:43:56 +00:00
|
|
|
function removeSpinner() {
|
|
|
|
|
$('<div[layout-id=findSession] .content .spinner').remove();// remove any existing spinners
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addSpinner() {
|
|
|
|
|
removeSpinner();
|
|
|
|
|
$('<div[layout-id=findSession] .content').append('<div class="spinner spinner-large"></div>')
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadSessions(queryString) {
|
|
|
|
|
|
|
|
|
|
addSpinner();
|
|
|
|
|
|
|
|
|
|
// squelch nulls and undefines
|
|
|
|
|
queryString = !!queryString ? queryString : "";
|
|
|
|
|
|
2012-10-06 16:36:05 +00:00
|
|
|
$.ajax({
|
|
|
|
|
type: "GET",
|
2013-09-21 03:43:56 +00:00
|
|
|
url: "/api/sessions?" + queryString,
|
|
|
|
|
success: afterLoadSessions,
|
|
|
|
|
complete: removeSpinner,
|
|
|
|
|
error: app.ajaxError
|
2012-11-28 02:29:46 +00:00
|
|
|
});
|
2012-10-06 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-30 15:43:29 +00:00
|
|
|
function search() {
|
2013-09-15 12:37:57 +00:00
|
|
|
logger.debug("Searching for sessions...");
|
2013-01-30 15:43:29 +00:00
|
|
|
clearResults();
|
|
|
|
|
var queryString = '';
|
|
|
|
|
|
|
|
|
|
// genre filter
|
2013-06-09 17:02:53 +00:00
|
|
|
var genres = context.JK.GenreSelectorHelper.getSelectedGenres('#find-session-genre');
|
|
|
|
|
if (genres !== null && genres.length > 0) {
|
2013-01-30 15:43:29 +00:00
|
|
|
queryString += "genres=" + genres.join(',');
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-02 23:53:57 +00:00
|
|
|
// keyword filter
|
|
|
|
|
var keyword = $('#session-keyword-srch').val();
|
2013-06-09 17:02:53 +00:00
|
|
|
if (keyword !== null && keyword.length > 0 && keyword !== 'Search by Keyword') {
|
2013-02-02 23:53:57 +00:00
|
|
|
if (queryString.length > 0) {
|
|
|
|
|
queryString += "&";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queryString += "keyword=" + $('#session-keyword-srch').val();
|
|
|
|
|
}
|
2013-01-30 15:43:29 +00:00
|
|
|
|
2013-09-21 03:43:56 +00:00
|
|
|
loadSessions(queryString);
|
2013-01-30 15:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-12 00:39:27 +00:00
|
|
|
function refreshDisplay() {
|
2013-02-07 07:14:45 +00:00
|
|
|
var priorVisible;
|
|
|
|
|
|
|
|
|
|
var INVITATION = 'div#sessions-invitations';
|
|
|
|
|
var FRIEND = 'div#sessions-friends';
|
|
|
|
|
var OTHER = 'div#sessions-other';
|
|
|
|
|
|
|
|
|
|
// INVITATION
|
2013-06-09 17:43:44 +00:00
|
|
|
logger.debug("sessionCounts[CATEGORY.INVITATION.index]=" + sessionCounts[CATEGORY.INVITATION.index]);
|
2013-06-12 00:39:27 +00:00
|
|
|
if (sessionCounts[CATEGORY.INVITATION.index] === 0) {
|
2013-02-07 07:14:45 +00:00
|
|
|
priorVisible = false;
|
|
|
|
|
$(INVITATION).hide();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
priorVisible = true;
|
|
|
|
|
$(INVITATION).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FRIEND
|
|
|
|
|
if (!priorVisible) {
|
|
|
|
|
$(FRIEND).removeClass('mt35');
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-09 17:43:44 +00:00
|
|
|
logger.debug("sessionCounts[CATEGORY.FRIEND.index]=" + sessionCounts[CATEGORY.FRIEND.index]);
|
2013-06-12 00:39:27 +00:00
|
|
|
if (sessionCounts[CATEGORY.FRIEND.index] === 0) {
|
2013-02-07 07:14:45 +00:00
|
|
|
priorVisible = false;
|
|
|
|
|
$(FRIEND).hide();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
priorVisible = true;
|
|
|
|
|
$(FRIEND).show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OTHER
|
|
|
|
|
if (!priorVisible) {
|
|
|
|
|
$(OTHER).removeClass('mt35');
|
|
|
|
|
}
|
2013-02-10 18:51:37 +00:00
|
|
|
|
2013-06-09 17:43:44 +00:00
|
|
|
logger.debug("sessionCounts[CATEGORY.OTHER.index]=" + sessionCounts[CATEGORY.OTHER.index]);
|
2013-06-12 00:39:27 +00:00
|
|
|
if (sessionCounts[CATEGORY.OTHER.index] === 0) {
|
2013-02-07 07:14:45 +00:00
|
|
|
$(OTHER).hide();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$(OTHER).show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-21 03:43:56 +00:00
|
|
|
function afterLoadSessions(sessionList) {
|
|
|
|
|
|
|
|
|
|
// display the 'no sessions' banner if appropriate
|
|
|
|
|
var $noSessionsFound = $('#sessions-none-found');
|
|
|
|
|
if(sessionList.length == 0) {
|
|
|
|
|
$noSessionsFound.show();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$noSessionsFound.hide();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
startSessionLatencyChecks(sessionList);
|
2013-09-27 15:33:52 +00:00
|
|
|
|
|
|
|
|
context.JK.GA.trackFindSessions(sessionList.length);
|
2013-09-21 03:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function startSessionLatencyChecks(sessionList) {
|
2013-09-22 18:56:51 +00:00
|
|
|
logger.debug("Starting latency checks on " + sessionList.length + " sessions");
|
|
|
|
|
|
2012-12-02 21:12:25 +00:00
|
|
|
sessionLatency.subscribe(app.clientId, latencyResponse);
|
2013-09-21 03:43:56 +00:00
|
|
|
$.each(sessionList, function(index, session) {
|
2012-12-01 23:50:28 +00:00
|
|
|
sessions[session.id] = session;
|
|
|
|
|
sessionLatency.sessionPings(session);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-25 08:00:53 +00:00
|
|
|
function containsInvitation(session) {
|
2013-02-01 06:05:57 +00:00
|
|
|
var i, invitation = null;
|
2013-10-03 12:09:31 +00:00
|
|
|
|
2013-11-06 03:14:30 +00:00
|
|
|
if (session !== undefined) {
|
|
|
|
|
if ("invitations" in session) {
|
|
|
|
|
// user has invitations for this session
|
|
|
|
|
for (i=0; i < session.invitations.length; i++) {
|
|
|
|
|
invitation = session.invitations[i];
|
|
|
|
|
// session contains an invitation for this user
|
|
|
|
|
if (invitation.receiver_id == context.JK.currentUserId) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-10-03 12:09:31 +00:00
|
|
|
}
|
2013-02-01 06:05:57 +00:00
|
|
|
}
|
2013-01-25 08:00:53 +00:00
|
|
|
}
|
2013-02-01 06:05:57 +00:00
|
|
|
|
|
|
|
|
return false;
|
2013-01-25 08:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function containsFriend(session) {
|
2013-01-30 15:43:29 +00:00
|
|
|
var i, participant = null;
|
2013-01-25 08:00:53 +00:00
|
|
|
|
2013-11-06 03:14:30 +00:00
|
|
|
if (session !== undefined) {
|
|
|
|
|
if ("participants" in session) {
|
|
|
|
|
for (i=0; i < session.participants.length; i++) {
|
|
|
|
|
participant = session.participants[i];
|
|
|
|
|
// this session participant is a friend
|
|
|
|
|
if (participant !== null && participant !== undefined && participant.user.is_friend) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-10-03 12:09:31 +00:00
|
|
|
}
|
2013-01-25 08:00:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-01 23:50:28 +00:00
|
|
|
function latencyResponse(sessionId) {
|
2013-09-15 12:37:57 +00:00
|
|
|
logger.debug("Received latency response for session " + sessionId);
|
2012-12-01 23:50:28 +00:00
|
|
|
renderSession(sessionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Not used normally. Allows modular unit testing
|
|
|
|
|
* of the renderSession method without having to do
|
|
|
|
|
* as much heavy setup.
|
|
|
|
|
*/
|
2013-02-10 18:51:37 +00:00
|
|
|
function setSession(session) {
|
2013-01-30 00:36:54 +00:00
|
|
|
invitationSessionGroup[session.id] = session;
|
|
|
|
|
}
|
2012-12-01 23:50:28 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Render a single session line into the table.
|
|
|
|
|
* It will be inserted at the appropriate place according to the
|
|
|
|
|
* sortScore in sessionLatency.
|
|
|
|
|
*/
|
|
|
|
|
function renderSession(sessionId) {
|
2013-06-09 18:19:23 +00:00
|
|
|
// store session in the appropriate bucket and increment category counts
|
|
|
|
|
var session = sessions[sessionId];
|
|
|
|
|
if (containsInvitation(session)) {
|
|
|
|
|
invitationSessionGroup[sessionId] = session;
|
|
|
|
|
sessionCounts[CATEGORY.INVITATION.index]++;
|
|
|
|
|
}
|
|
|
|
|
else if (containsFriend(session)) {
|
|
|
|
|
friendSessionGroup[sessionId] = session;
|
|
|
|
|
sessionCounts[CATEGORY.FRIEND.index]++;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
otherSessionGroup[sessionId] = session;
|
|
|
|
|
sessionCounts[CATEGORY.OTHER.index]++;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-09 16:45:11 +00:00
|
|
|
// hack to prevent duplicate rows from being rendered when filtering
|
2013-06-09 18:19:23 +00:00
|
|
|
var sessionAlreadyRendered = false;
|
2013-01-25 08:00:53 +00:00
|
|
|
var $tbGroup;
|
|
|
|
|
|
2013-06-09 18:19:23 +00:00
|
|
|
logger.debug('Rendering session ID = ' + sessionId);
|
|
|
|
|
|
2013-01-27 20:17:46 +00:00
|
|
|
if (invitationSessionGroup[sessionId] != null) {
|
2013-01-25 08:00:53 +00:00
|
|
|
$tbGroup = $(CATEGORY.INVITATION.id);
|
2013-02-09 16:45:11 +00:00
|
|
|
|
|
|
|
|
if ($("table#sessions-invitations tr[id='" + sessionId + "']").length > 0) {
|
2013-06-09 18:19:23 +00:00
|
|
|
sessionAlreadyRendered = true;
|
2013-02-09 16:45:11 +00:00
|
|
|
}
|
2012-12-13 04:53:15 +00:00
|
|
|
}
|
2013-06-09 18:19:23 +00:00
|
|
|
else if (friendSessionGroup[sessionId] != null) {;
|
2013-02-07 07:14:45 +00:00
|
|
|
$tbGroup = $(CATEGORY.FRIEND.id);
|
2013-02-09 16:45:11 +00:00
|
|
|
|
|
|
|
|
if ($("table#sessions-friends tr[id='" + sessionId + "']").length > 0) {
|
2013-06-09 18:19:23 +00:00
|
|
|
sessionAlreadyRendered = true;
|
2013-02-09 16:45:11 +00:00
|
|
|
}
|
2013-01-25 08:00:53 +00:00
|
|
|
}
|
2013-01-27 20:17:46 +00:00
|
|
|
else if (otherSessionGroup[sessionId] != null) {
|
2013-01-25 08:00:53 +00:00
|
|
|
$tbGroup = $(CATEGORY.OTHER.id);
|
2013-02-09 16:45:11 +00:00
|
|
|
|
|
|
|
|
if ($("table#sessions-other tr[id='" + sessionId + "']").length > 0) {
|
2013-06-09 18:19:23 +00:00
|
|
|
sessionAlreadyRendered = true;
|
2013-02-09 16:45:11 +00:00
|
|
|
}
|
2013-01-25 08:00:53 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2013-06-09 18:19:23 +00:00
|
|
|
logger.debug('ERROR: No session with ID = ' + sessionId + ' found.');
|
2013-01-25 08:00:53 +00:00
|
|
|
return;
|
2012-12-01 23:50:28 +00:00
|
|
|
}
|
2013-01-25 08:00:53 +00:00
|
|
|
|
2013-06-09 18:19:23 +00:00
|
|
|
if (!sessionAlreadyRendered) {
|
2013-10-06 05:22:28 +00:00
|
|
|
var row = sessionList.renderSession(session, sessionLatency, $tbGroup, $('#template-session-row').html(), $('#template-musician-info').html());
|
2013-02-09 16:45:11 +00:00
|
|
|
}
|
2013-06-12 00:49:42 +00:00
|
|
|
|
|
|
|
|
refreshDisplay();
|
2013-01-30 15:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-09 17:02:53 +00:00
|
|
|
function beforeShow(data) {
|
|
|
|
|
context.JK.GenreSelectorHelper.render('#find-session-genre');
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-06 16:36:05 +00:00
|
|
|
function afterShow(data) {
|
2013-01-30 00:36:54 +00:00
|
|
|
clearResults();
|
2013-06-23 00:02:38 +00:00
|
|
|
refreshDisplay();
|
2012-10-06 16:36:05 +00:00
|
|
|
loadSessions();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-25 08:00:53 +00:00
|
|
|
function clearResults() {
|
2013-02-07 07:38:35 +00:00
|
|
|
$('table#sessions-invitations').children(':not(:first-child)').remove();
|
2013-02-07 07:41:46 +00:00
|
|
|
$('table#sessions-friends').children(':not(:first-child)').remove();
|
2013-02-07 07:38:35 +00:00
|
|
|
$('table#sessions-other').children(':not(:first-child)').remove();
|
|
|
|
|
|
|
|
|
|
sessionCounts = [0, 0, 0];
|
2013-06-09 17:43:44 +00:00
|
|
|
|
|
|
|
|
sessions = {};
|
|
|
|
|
invitationSessionGroup = {};
|
|
|
|
|
friendSessionGroup = {};
|
|
|
|
|
otherSessionGroup = {};
|
2013-01-25 08:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-06 16:36:05 +00:00
|
|
|
function deleteSession(evt) {
|
2012-12-07 00:28:48 +00:00
|
|
|
var sessionId = $(evt.currentTarget).attr("action-id");
|
2012-10-06 16:36:05 +00:00
|
|
|
if (sessionId) {
|
|
|
|
|
$.ajax({
|
|
|
|
|
type: "DELETE",
|
2013-09-21 03:43:56 +00:00
|
|
|
url: "/api/sessions/" + sessionId,
|
|
|
|
|
error: app.ajaxError
|
2012-10-06 16:36:05 +00:00
|
|
|
}).done(loadSessions);
|
|
|
|
|
}
|
2013-02-07 07:38:35 +00:00
|
|
|
}
|
2012-10-06 16:36:05 +00:00
|
|
|
|
|
|
|
|
function events() {
|
2013-02-02 23:53:57 +00:00
|
|
|
|
|
|
|
|
$('#session-keyword-srch').focus(function() {
|
|
|
|
|
$(this).val('');
|
|
|
|
|
});
|
|
|
|
|
|
2013-10-06 05:22:28 +00:00
|
|
|
$("#session-keyword-srch").keypress(function(evt) {
|
|
|
|
|
if (evt.which === 13) {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
search();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2013-01-30 15:43:29 +00:00
|
|
|
$('#btn-refresh').on("click", search);
|
2012-10-06 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-01 23:50:28 +00:00
|
|
|
/**
|
|
|
|
|
* Initialize, providing an instance of the SessionLatency class.
|
|
|
|
|
*/
|
2013-01-25 08:00:53 +00:00
|
|
|
function initialize(latency) {
|
2013-02-10 18:51:37 +00:00
|
|
|
|
|
|
|
|
var screenBindings = {
|
2013-06-09 17:02:53 +00:00
|
|
|
'beforeShow': beforeShow,
|
2013-02-10 18:51:37 +00:00
|
|
|
'afterShow': afterShow
|
|
|
|
|
};
|
|
|
|
|
app.bindScreen('findSession', screenBindings);
|
|
|
|
|
|
2013-01-25 08:00:53 +00:00
|
|
|
if (latency) {
|
|
|
|
|
sessionLatency = latency;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2012-12-01 23:50:28 +00:00
|
|
|
logger.warn("No sessionLatency provided.");
|
2012-11-28 02:29:46 +00:00
|
|
|
}
|
2013-01-25 08:00:53 +00:00
|
|
|
|
|
|
|
|
sessionList = new context.JK.SessionList(app);
|
|
|
|
|
|
2012-10-14 15:48:56 +00:00
|
|
|
events();
|
2012-12-01 23:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.initialize = initialize;
|
|
|
|
|
this.renderSession = renderSession;
|
2013-01-30 00:36:54 +00:00
|
|
|
this.afterShow = afterShow;
|
2012-12-01 23:50:28 +00:00
|
|
|
|
|
|
|
|
// Following exposed for easier testing.
|
2013-02-10 18:51:37 +00:00
|
|
|
this.setSession = setSession;
|
2013-01-30 00:36:54 +00:00
|
|
|
this.clearResults = clearResults;
|
|
|
|
|
this.getCategoryEnum = getCategoryEnum;
|
2012-12-01 23:50:28 +00:00
|
|
|
|
2013-01-21 05:52:28 +00:00
|
|
|
return this;
|
2012-10-06 16:36:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
})(window,jQuery);
|