jam-cloud/web/app/assets/javascripts/hoverMusician.js

153 lines
5.3 KiB
JavaScript

(function(context,$) {
"use strict";
context.JK = context.JK || {};
context.JK.MusicianHoverBubble = function(userId, x, y) {
var logger = context.JK.logger;
var rest = context.JK.Rest();
var hoverSelector = "#musician-hover";
this.showBubble = function() {
var mouseLeft = x < (document.body.clientWidth / 2);
var mouseTop = y < (document.body.clientHeight / 2);
var css = {};
if (mouseLeft)
css.left = x + 10 + 'px';
else
css.left = x - (7 + $(hoverSelector).width()) + 'px';
if (mouseTop)
css.top = y + 10 + 'px';
else
css.top = y - (7 + $(hoverSelector).height()) + 'px';
$(hoverSelector).css(css);
$(hoverSelector).fadeIn(500);
rest.getUserDetail({id: userId})
.done(function(response) {
$(hoverSelector).html('');
// instruments
var instrumentHtml = '';
$.each(response.instruments, function(index, val) {
instrumentHtml += '<div class="left mr10 mb"><img src="' + context.JK.getInstrumentIcon24(val.instrument_id) + '" width="24" height="24" /></div>';
});
// followings
var followingHtml = '';
$.each(response.followings, function(index, val) {
if (index < 4) { // display max of 4 followings
if (index % 2 === 0) {
followingHtml += '<tr>';
}
var avatarUrl, profilePath;
if (val.type === "band") {
avatarUrl = context.JK.resolveBandAvatarUrl(val.photo_url);
profilePath = "bandProfile";
}
else {
avatarUrl = context.JK.resolveAvatarUrl(val.photo_url);
profilePath = "profile";
}
followingHtml += '<td width="24"><a href="#" class="avatar-tiny"><img src="' + avatarUrl + '" /></a></td>';
followingHtml += '<td><a href="/client#/' + profilePath + '/' + val.id + '"><strong>' + val.name + '</strong></a></td>';
if (index % 2 > 0) {
followingHtml += '</tr>';
}
}
});
var template = $('#template-hover-musician').html();
if (response.biography == null) {
response.biography = 'No Biography Available';
}
var sessionDisplayStyle = 'none';
var sessionId = '';
var joinDisplayStyle = 'none';
if (response.sessions !== undefined && response.sessions.length > 0) {
sessionDisplayStyle = 'block';
var session = response.sessions[0];
sessionId = session.id;
if (context.jamClient && session.musician_access) {
joinDisplayStyle = 'inline';
}
}
var musicianHtml = context.JK.fillTemplate(template, {
userId: response.id,
avatar_url: context.JK.resolveAvatarUrl(response.photo_url),
name: response.name,
location: response.location,
instruments: instrumentHtml,
friend_count: response.friend_count,
follower_count: response.follower_count,
recording_count: response.recording_count,
session_count: response.session_count,
session_display: sessionDisplayStyle,
join_display: joinDisplayStyle,
sessionId: sessionId,
friendAction: response.is_friend ? "removeMusicianFriend" : (response.pending_friend_request ? "" : "sendMusicianFriendRequest"),
followAction: response.is_following ? "removeMusicianFollowing" : "addMusicianFollowing",
biography: response.biography,
followings: response.followings && response.followings.length > 0 ? followingHtml : "<tr><td>N/A</td></tr>",
profile_url: "/client#/profile/" + response.id
});
$(hoverSelector).append('<h2>Musician Detail</h2>' + musicianHtml);
configureActionButtons(response);
})
.fail(function(xhr) {
if(xhr.status >= 500) {
context.JK.fetchUserNetworkOrServerFailure();
}
else if(xhr.status == 404) {
context.JK.entityNotFound("User");
}
else {
context.JK.app.ajaxError(arguments);
}
});
};
function configureActionButtons(user) {
var btnFriendSelector = "#btnFriend";
var btnFollowSelector = "#btnFollow";
var btnMessageSelector = '#btnMessage';
// if unauthenticated or authenticated user is viewing his own profile
if (!context.JK.currentUserId || context.JK.currentUserId === user.id) {
$(btnFriendSelector, hoverSelector).hide();
$(btnFollowSelector, hoverSelector).hide();
$(btnMessageSelector, hoverSelector).hide();
}
else {
if (user.is_friend) {
$(btnFriendSelector, hoverSelector).html('DISCONNECT');
}
if (user.is_following) {
$(btnFollowSelector, hoverSelector).html('UNFOLLOW');
}
if (user.pending_friend_request) {
$(btnFriendSelector, hoverSelector).hide();
}
}
}
this.hideBubble = function() {
$(hoverSelector).hide();
};
this.id = function() {
return hoverSelector;
};
}
})(window,jQuery);