148 lines
5.6 KiB
JavaScript
148 lines
5.6 KiB
JavaScript
(function(context,$) {
|
|
|
|
"use strict";
|
|
|
|
context.JK = context.JK || {};
|
|
context.JK.SessionList = function(app) {
|
|
var LATENCY = {
|
|
GOOD : {description: "GOOD", min: 0.0, max: 100.0},
|
|
MEDIUM : {description: "MEDIUM", min: 100.0, max: 200.0},
|
|
BAD : {description: "BAD", min: 200.0, max: 10000000000.0}
|
|
};
|
|
|
|
var AUDIENCE = {
|
|
OPEN_TO_FANS: "Open to Fans",
|
|
MUSICIANS_ONLY:"Musicians Only"
|
|
};
|
|
|
|
var instrument_logos = { "accordion": '',
|
|
"acoustic guitar": '',
|
|
"banjo": '',
|
|
"bass guitar": '',
|
|
"cello": '',
|
|
"clarinet": '',
|
|
"computer": '',
|
|
"drums": '',
|
|
"electric guitar": '../assets/content/icon_instrument_guitar24.png',
|
|
"euphonium": '',
|
|
"flute": '',
|
|
"french horn": '',
|
|
"harmonica": '',
|
|
"keyboard": '../assets/content/icon_instrument_keyboard24.png',
|
|
"mandolin": '',
|
|
"oboe": '',
|
|
"saxophone": '',
|
|
"trombone": '',
|
|
"trumpet": '',
|
|
"tuba": '',
|
|
"ukulele": '',
|
|
"viola": '',
|
|
"violin": '',
|
|
"voice": '../assets/content/icon_instrument_vocal24.png'
|
|
}
|
|
|
|
var _logger = context.JK.logger;
|
|
var _sessionLatency;
|
|
|
|
/**
|
|
* Render a single session line into the table.
|
|
* It will be inserted at the appropriate place according to the
|
|
* sortScore in sessionLatency.
|
|
*/
|
|
function renderSession(session, sessionLatency, tbGroup, rowTemplate, musicianTemplate, onMusiciansComplete) {
|
|
// latency
|
|
var latencyInfo = sessionLatency.sessionInfo(session.id);
|
|
var latencyDescription = "";
|
|
var latencyStyle = "";
|
|
|
|
if (latencyInfo.averageLatency <= LATENCY.GOOD.max) {
|
|
latencyDescription = LATENCY.GOOD.description;
|
|
latencyStyle = "latency-green";
|
|
}
|
|
else if (latencyInfo.averageLatency > LATENCY.MEDIUM.min && latencyInfo.averageLatency <= LATENCY.MEDIUM.max) {
|
|
latencyDescription = LATENCY.MEDIUM.description;
|
|
latencyStyle = "latency-medium";
|
|
}
|
|
else {
|
|
latencyDescription = LATENCY.BAD.description;
|
|
latencyStyle = "latency-red";
|
|
}
|
|
|
|
// audience
|
|
var audience = AUDIENCE.OPEN_TO_FANS;
|
|
if (!(session.fan_access)) {
|
|
audience = AUDIENCE.MUSICIANS_ONLY;
|
|
}
|
|
|
|
var i, participant = null;
|
|
var musicians = '';
|
|
var musicianArray = [];
|
|
for (i=0; i < session.participants.length; i++) {
|
|
participant = session.participants[i];
|
|
|
|
var instrumentLogoHtml = '';
|
|
var j;
|
|
|
|
// loop through the tracks to get the instruments
|
|
for (j=0; j < participant.tracks.length; j++) {
|
|
var track = participant.tracks[j];
|
|
instrumentLogoHtml += '<img src="' + instrument_logos[track.instrument_id] + '" width="24" height="24" /> ';
|
|
}
|
|
|
|
var id = participant.user.id;
|
|
var name = participant.user.name;
|
|
var musicianVals = {
|
|
avatar_url: participant.user.photo_url,
|
|
profile_url: "users/" + id,
|
|
musician_name: name,
|
|
instruments: instrumentLogoHtml
|
|
}
|
|
|
|
var musician = {};
|
|
musician.id = id;
|
|
musician.name = name;
|
|
musicianArray[i] = musician;
|
|
|
|
var musicianInfo = context.JK.fillTemplate(musicianTemplate, musicianVals);
|
|
musicians += musicianInfo;
|
|
}
|
|
|
|
onMusiciansComplete(musicianArray);
|
|
|
|
var sessionVals = {
|
|
id: session.id,
|
|
genres: session.genres.join (', '),
|
|
description: session.description || "(No description)",
|
|
musician_template: musicians,
|
|
audience: audience,
|
|
latency_text: latencyDescription,
|
|
latency_style: latencyStyle,
|
|
sortScore: latencyInfo.sortScore,
|
|
join_url: "#/session/" + session.id
|
|
};
|
|
|
|
var row = context.JK.fillTemplate(rowTemplate, sessionVals);
|
|
var insertedEarly = false;
|
|
$.each($('tr', tbGroup), function(index, nextRow) {
|
|
var $nextRow = $(nextRow);
|
|
var rowSortScore = parseInt($nextRow.attr('data-sortScore'), 10);
|
|
if (sessionVals.sortScore > rowSortScore) {
|
|
$nextRow.before(row);
|
|
insertedEarly = true;
|
|
return false; // break
|
|
}
|
|
});
|
|
if (!insertedEarly) {
|
|
return $(tbGroup).append(row);
|
|
}
|
|
}
|
|
|
|
function events() {
|
|
}
|
|
|
|
this.renderSession = renderSession;
|
|
|
|
return this;
|
|
};
|
|
|
|
})(window,jQuery); |