hooked up socket calls to usercard

main
psychobunny 9 years ago
parent 06c02a63fe
commit 9db6b31238

@ -1,6 +1,6 @@
"use strict"; "use strict";
/*globals ajaxify, config, utils, templates, app, Slideout, NProgress*/ /*globals ajaxify, config, utils, templates, app, socket, Slideout, NProgress*/
$(document).ready(function() { $(document).ready(function() {
setupNProgress(); setupNProgress();
@ -166,44 +166,64 @@ $(document).ready(function() {
function setupHoverCards() { function setupHoverCards() {
require(['components'], function(components) { require(['components'], function(components) {
components.get('user/picture') components.get('user/picture')
.on('mouseover', function() { .on('mouseover', generateUserCard);
var avatar = $(this), });
index = avatar.parents('[data-index]').attr('data-index'), }
data = (ajaxify.data.topics || ajaxify.data.posts)[index].user;
function generateUserCard() {
templates.parse('modules/usercard', data, function(html) { var avatar = $(this),
var card = $(html); index = avatar.parents('[data-index]').attr('data-index'),
avatar.parents('a').after(card.hide()); data = (ajaxify.data.topics || ajaxify.data.posts)[index].user;
if (parseInt(app.user.uid, 10) === parseInt(data.uid, 10)) { socket.emit('user.isFollowing', {uid: data.uid}, function(err, isFollowing) {
card.find('.btn-morph').hide(); templates.parse('modules/usercard', data, function(html) {
} else { var card = $(html);
card.find('.btn-morph').click(function(ev){ avatar.parents('a').after(card.hide());
$(this).toggleClass('plus').toggleClass('heart');
if ($(this).find('b.drop').length === 0) {
$(this).prepend('<b class="drop"></b>');
}
var drop = $(this).find('b.drop').removeClass('animate'),
x = ev.pageX - drop.width() / 2 - $(this).offset().left,
y = ev.pageY - drop.height() / 2 - $(this).offset().top;
drop.css({top: y + 'px', left: x + 'px'}).addClass('animate');
});
}
utils.makeNumbersHumanReadable($('.human-readable-number')); if (parseInt(app.user.uid, 10) === parseInt(data.uid, 10)) {
card.find('.btn-morph').hide();
} else {
card.find('.btn-morph').click(function(ev) {
var type = $(this).hasClass('plus') ? 'follow' : 'unfollow';
card.on('mouseleave', function() { socket.emit('user.' + type, {uid: data.uid}, function(err) {
card.fadeOut(function() { if (err) {
card.remove(); return app.alertError(err.message);
}); }
app.alertSuccess('[[global:alert.' + type + ', ' + data.username + ']]');
}); });
card.fadeIn(); $(this).toggleClass('plus').toggleClass('heart');
if ($(this).find('b.drop').length === 0) {
$(this).prepend('<b class="drop"></b>');
}
var drop = $(this).find('b.drop').removeClass('animate'),
x = ev.pageX - drop.width() / 2 - $(this).offset().left,
y = ev.pageY - drop.height() / 2 - $(this).offset().top;
drop.css({top: y + 'px', left: x + 'px'}).addClass('animate');
});
if (isFollowing) {
$('.btn-morph').addClass('heart');
} else {
$('.btn-morph').addClass('plus');
}
}
utils.makeNumbersHumanReadable($('.human-readable-number'));
card.on('mouseleave', function() {
card.fadeOut(function() {
card.remove();
}); });
}); });
});
card.fadeIn();
});
});
} }
}); });

@ -22,7 +22,7 @@
<span class="human-readable-number">{reputation}</span> <span class="human-readable-number">{reputation}</span>
</div> </div>
<button class="btn-morph fab plus"> <button class="btn-morph fab">
<span> <span>
<span class="s1"></span> <span class="s1"></span>
<span class="s2"></span> <span class="s2"></span>

Loading…
Cancel
Save