257 lines
8.9 KiB
JavaScript
257 lines
8.9 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 = new context.JK.GenreSelector(app);
|
|
var sessions = {};
|
|
var invitationSessionGroup = {};
|
|
var friendSessionGroup = {};
|
|
var otherSessionGroup = {};
|
|
var sessionList;
|
|
|
|
// for unit tests
|
|
function getCategoryEnum() {
|
|
return CATEGORY;
|
|
}
|
|
|
|
// for unit tests
|
|
function getGenreSelector() {
|
|
return genreSelector;
|
|
}
|
|
|
|
function loadSessions() {
|
|
$.ajax({
|
|
type: "GET",
|
|
url: "/api/sessions",
|
|
success: startSessionLatencyChecks
|
|
});
|
|
}
|
|
|
|
function search() {
|
|
clearResults();
|
|
var queryString = '';
|
|
|
|
// musician filter
|
|
var musicians = getSelectedMusicians();
|
|
if (musicians != null && musicians.length > 0) {
|
|
queryString += "participants=" + musicians.join(',');
|
|
}
|
|
|
|
// genre filter
|
|
var genres = genreSelector.getSelectedGenres();
|
|
if (genres != null && genres.length > 0) {
|
|
if (queryString.length > 0) {
|
|
queryString += "&";
|
|
}
|
|
|
|
queryString += "genres=" + genres.join(',');
|
|
}
|
|
|
|
// TODO: keyword filter
|
|
|
|
if (queryString.length > 0) {
|
|
$.ajax({
|
|
type: "GET",
|
|
url: "/api/sessions?" + queryString,
|
|
success: startSessionLatencyChecks
|
|
});
|
|
}
|
|
else {
|
|
loadSessions();
|
|
}
|
|
}
|
|
|
|
function getSelectedMusicians() {
|
|
var selectedMusicians = [];
|
|
$('#musician-list-items :checked').each(function() {
|
|
selectedMusicians.push($(this).val());
|
|
});
|
|
|
|
return selectedMusicians;
|
|
}
|
|
|
|
function startSessionLatencyChecks(response) {
|
|
sessionLatency.subscribe(app.clientId, latencyResponse);
|
|
$.each(response, function(index, session) {
|
|
sessions[session.id] = session;
|
|
// store session in the appropriate bucket
|
|
if (containsInvitation(session)) {
|
|
invitationSessionGroup[session.id] = session;
|
|
}
|
|
else if (containsFriend(session)) {
|
|
friendSessionGroup[session.id] = session;
|
|
}
|
|
else {
|
|
otherSessionGroup[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, participant = null;
|
|
|
|
for (i=0; i < session.participants.length; i++) {
|
|
participant = session.participants[i];
|
|
// this session participant is a friend
|
|
//alert(participant.user.name);
|
|
if (participant.user.is_friend) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function latencyResponse(sessionId) {
|
|
renderSession(sessionId);
|
|
}
|
|
|
|
/**
|
|
* Not used normally. Allows modular unit testing
|
|
* of the renderSession method without having to do
|
|
* as much heavy setup.
|
|
*/
|
|
function setSession(session) {
|
|
invitationSessionGroup[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 (invitationSessionGroup[sessionId] != null) {
|
|
session = invitationSessionGroup[sessionId];
|
|
$tbGroup = $(CATEGORY.INVITATION.id);
|
|
}
|
|
else if (friendSessionGroup[sessionId] != null) {
|
|
session = friendSessionGroup[sessionId];
|
|
$tbGroup = $(CATEGORY.FRIENDS.id);
|
|
}
|
|
else if (otherSessionGroup[sessionId] != null) {
|
|
session = otherSessionGroup[sessionId];
|
|
$tbGroup = $(CATEGORY.OTHER.id);
|
|
}
|
|
else {
|
|
//alert('ERROR: No session with ID = ' + sessionId + ' found.');
|
|
return;
|
|
}
|
|
|
|
var row = sessionList.renderSession(session, sessionLatency, $tbGroup, $('#template-session-row').html(), $('#template-musician-info').html(),
|
|
// populate the musician filter with musicians that haven't already been added
|
|
function(musicianArray) {
|
|
var template = $('#template-musician-filter').html();
|
|
$.each(musicianArray, function(index, val) {
|
|
// check if this musician is already in the filter
|
|
if ( $('#musician-list-items input[value=' + val.id + ']').length == 0 ) {
|
|
var musicianOptionHtml = context.JK.fillTemplate(template, {value: val.id, label: val.name});
|
|
$('#musician-list-items').append(musicianOptionHtml);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
// TODO: refactor this and GenreSelector into common code
|
|
function toggleMusicianBox() {
|
|
var boxHeight = $('#musician-list').css("height");
|
|
// TODO: clean this up (check class name of arrow to determine current state)
|
|
if (boxHeight == "20px") {
|
|
$('#musician-list').css({height: "auto"});
|
|
$('#musician-list-arrow').removeClass("arrow-down").addClass("arrow-up");
|
|
|
|
}
|
|
else {
|
|
$('#musician-list').css({height: "20px"});
|
|
$('#musician-list-arrow').removeClass("arrow-up").addClass("arrow-down");
|
|
}
|
|
}
|
|
|
|
function afterShow(data) {
|
|
clearResults();
|
|
loadSessions();
|
|
}
|
|
|
|
function clearResults() {
|
|
$('#sessions-invitations').children(':not(:first-child)').remove();
|
|
$('#sessions-friends').children(':not(:first-child)').remove();
|
|
$('#sessions-other').children(':not(:first-child)').remove();
|
|
}
|
|
|
|
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);
|
|
$('#musician-list-header').on("click", toggleMusicianBox);
|
|
$('#musician-list-arrow').on("click", toggleMusicianBox);
|
|
$('#btn-refresh').on("click", search);
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
this.afterShow = afterShow;
|
|
|
|
// Following exposed for easier testing.
|
|
this.setSession = setSession;
|
|
this.clearResults = clearResults;
|
|
this.getCategoryEnum = getCategoryEnum;
|
|
this.getGenreSelector = getGenreSelector;
|
|
|
|
return this;
|
|
};
|
|
|
|
})(window,jQuery); |