jam-cloud/app/assets/javascripts/findSession.js

178 lines
5.7 KiB
JavaScript

(function(context,$) {
"use strict";
context.JK = context.JK || {};
context.JK.FindSessionScreen = function(app) {
var CATEGORY = {
INVITATION : {index: 0, id: "#sessions-invitations"},
FRIENDS : {index: 1, id: "#sessions-friends"},
OTHER : {index: 2, id: "#sessions-other"}
};
var logger = context.JK.logger;
var sessionLatency;
var genreSelector;
var sessions = {};
var sessionGroups = [[]];
var sessionList;
function loadSessions() {
$.ajax({
type: "GET",
url: "/api/sessions",
success: startSessionLatencyChecks
});
}
function startSessionLatencyChecks(response) {
sessionLatency.subscribe(app.clientId, latencyResponse);
$.each(response, function(index, session) {
sessions[session.id] = session;
sessionGroups[CATEGORY.INVITATION.index][session.id] = session;
// store session in the appropriate bucket
/*if (containsInvitation(session)) {
//alert(session.id + " has invitation");
sessionGroups[CATEGORY.INVITATION.index][session.id] = session;
}
else if (containsFriend(session)) {
//alert(session.id + " has friend");
sessionGroups[CATEGORY.FRIENDS.index][session.id] = session;
}
else {
//alert(session.id + " has other");
sessionGroups[CATEGORY.OTHER.index][session.id] = session;
}*/
sessionLatency.sessionPings(session);
});
}
function containsInvitation(session) {
// user has invitations for this session
if (session.invitations.length > 0) {
return true;
}
else {
return false;
}
}
function containsFriend(session) {
var i, p, score = 0, participant = null;
for (i=0, p=session.participants.length; i < p; i++) {
participant = session.participants[i];
// this session participant is a friend
//alert(participant.user_id);
if (participant.user_id != null) {
return true;
}
}
return false;
}
function latencyResponse(sessionId) {
sessionList
renderSession(sessionId);
}
/**
* Not used normally. Allows modular unit testing
* of the renderSession method without having to do
* as much heavy setup.
*/
function setSession(session) { sessions[session.id] = session; }
/**
* 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) {
var session = null;
var $tbGroup;
if (sessionGroups[CATEGORY.INVITATION.index][sessionId] != null) {
session = sessionGroups[CATEGORY.INVITATION.index][sessionId];
$tbGroup = $(CATEGORY.INVITATION.id);
}
else if (sessionGroups[CATEGORY.FRIENDS.index][sessionId] != null) {
session = sessionGroups[CATEGORY.FRIENDS.index][sessionId];
$tbGroup = $(CATEGORY.FRIENDS.id);
}
else if (sessionGroups[CATEGORY.OTHER.index][sessionId] != null) {
session = sessionGroups[CATEGORY.OTHER.index][sessionId];
$tbGroup = $(CATEGORY.OTHER.id);
}
else {
alert('ERROR: No session with ID = ' + sessionId + ' found.');
return;
}
sessionList.renderSession(session, sessionLatency, $tbGroup, $('#template-session-row').html(), $('#template-musician-info').html());
}
function afterShow(data) {
//clearResults();
loadSessions();
}
function clearResults() {
var $tb = $(CATEGORY.INVITATION.id);
$tb.empty();
$tb = $(CATEGORY.FRIENDS.id);
$tb.empty();
$tb = $(CATEGORY.OTHER.id);
$tb.empty();
}
function deleteSession(evt) {
var sessionId = $(evt.currentTarget).attr("action-id");
if (sessionId) {
$.ajax({
type: "DELETE",
url: "/api/sessions/" + sessionId
}).done(loadSessions);
}
}
function events() {
//$('#findSession-tableBody').on("click", '[action="delete"]', deleteSession);
}
/**
* Initialize, providing an instance of the SessionLatency class.
*/
function initialize(latency) {
genreSelector = new context.JK.GenreSelector(app);
genreSelector.initialize('Any genre', 0, $('#find-session-form'));
if (latency) {
sessionLatency = latency;
}
else {
logger.warn("No sessionLatency provided.");
}
sessionList = new context.JK.SessionList(app);
var screenBindings = {
'afterShow': afterShow
};
app.bindScreen('findSession', screenBindings);
events();
}
this.initialize = initialize;
this.renderSession = renderSession;
// Following exposed for easier testing.
this.setSession = setSession;
return this;
};
})(window,jQuery);