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,11 +166,16 @@ $(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);
});
}
function generateUserCard() {
var avatar = $(this), var avatar = $(this),
index = avatar.parents('[data-index]').attr('data-index'), index = avatar.parents('[data-index]').attr('data-index'),
data = (ajaxify.data.topics || ajaxify.data.posts)[index].user; data = (ajaxify.data.topics || ajaxify.data.posts)[index].user;
socket.emit('user.isFollowing', {uid: data.uid}, function(err, isFollowing) {
templates.parse('modules/usercard', data, function(html) { templates.parse('modules/usercard', data, function(html) {
var card = $(html); var card = $(html);
avatar.parents('a').after(card.hide()); avatar.parents('a').after(card.hide());
@ -179,6 +184,16 @@ $(document).ready(function() {
card.find('.btn-morph').hide(); card.find('.btn-morph').hide();
} else { } else {
card.find('.btn-morph').click(function(ev) { card.find('.btn-morph').click(function(ev) {
var type = $(this).hasClass('plus') ? 'follow' : 'unfollow';
socket.emit('user.' + type, {uid: data.uid}, function(err) {
if (err) {
return app.alertError(err.message);
}
app.alertSuccess('[[global:alert.' + type + ', ' + data.username + ']]');
});
$(this).toggleClass('plus').toggleClass('heart'); $(this).toggleClass('plus').toggleClass('heart');
if ($(this).find('b.drop').length === 0) { if ($(this).find('b.drop').length === 0) {
@ -191,6 +206,12 @@ $(document).ready(function() {
drop.css({top: y + 'px', left: x + 'px'}).addClass('animate'); 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')); utils.makeNumbersHumanReadable($('.human-readable-number'));
@ -204,6 +225,5 @@ $(document).ready(function() {
card.fadeIn(); 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