Skip to content

Commit

Permalink
Merge pull request #1531 from CPAN-API/leo/switch_favs_to_client
Browse files Browse the repository at this point in the history
switch favs to client side
  • Loading branch information
ranguard committed May 22, 2015
2 parents 636f4da + 87fe226 commit 6ddcd19
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
18 changes: 12 additions & 6 deletions root/inc/favorite.html
@@ -1,11 +1,17 @@
<% IF user_exists -%>
<script type="text/javascript">
MetaCPAN.favs_to_check['<% module.distribution %>'] = 1;
</script>

<div id="<% module.distribution %>-fav" class="logged_in">
<form action="/account/favorite/add" style="display: inline" onsubmit="return favDistribution(this)">
<% IF module.myfavorite %><input type="hidden" name="remove" value="1"><% END -%>
<input type="hidden" name="remove" value="0">
<input type="hidden" name="release" value="<% module.release || module.name %>">
<input type="hidden" name="author" value="<% module.author %>">
<input type="hidden" name="distribution" value="<% module.distribution %>">
<button type="submit" class="favorite<% IF module.favorites || release.favorites %> highlight<% END %><% IF module.myfavorite %> active<% END %>"><span><% module.favorites || release.favorites %></span> ++</button>
<button type="submit" class="favorite<% IF module.favorites || release.favorites %> highlight<% END %>"><span><% module.favorites || release.favorites %></span> ++</button>
</form>
<% ELSE -%>
<a href="" onclick="alert('Please sign in to add favorites'); return false" class="favorite<% IF module.favorites || release.favorites %> highlight<% END %>"><span>
<% module.favorites || release.favorites %></span> ++</a><% END -%>
</div>
<div class="logged_out">
<a href="" onclick="alert('Please sign in to add favorites'); return false" class="favorite<% IF module.favorites || release.favorites %> highlight<% END %>">
<span><% module.favorites || release.favorites %></span> ++</a>
</div>
9 changes: 9 additions & 0 deletions root/static/css/user.css
@@ -0,0 +1,9 @@
/* Specific css for User actions */

.logged_in {
display: none;
}

.logged_out {
display: none;
}
48 changes: 42 additions & 6 deletions root/static/js/cpan.js
@@ -1,5 +1,10 @@
/* jshint white: true, lastsemic: true */

// Store global data in this object
var MetaCPAN = {};
// Collect favs we need to check after dom ready
MetaCPAN.favs_to_check = {};

document.cookie = "hideTOC=; expires=" + (new Date(0)).toGMTString() + "; path=/";

$.fn.textWidth = function() {
Expand Down Expand Up @@ -71,6 +76,8 @@ function toggleTOC() {
}

$(document).ready(function() {

// User customisations
processUserData();

$(".ttip").tooltip();
Expand Down Expand Up @@ -401,13 +408,38 @@ function logInPAUSE(a) {
}

function processUserData() {
// TODO: use localstorage for cacheing

// Could do some fancy localStorage thing here
// but as the user favs data is cached at fastly
// not worth the effort yet

// TODO: get this working to save hits and 403's
// if(document.cookie.match('metacpan_secure')) {
// getFavDataFromServer();
// } else {
// // Can't be logged in
// $('.logged_out').css('display', 'inline');
// }

getFavDataFromServer();
}

function showUserData(fav_data) {
// User is logged in, so show it
$('.logged_in').css('display', 'inline');

// process users current favs
$.each(fav_data.faves, function(index, value) {
var distribution = value.distribution;

// On the page... make it deltable and styled as 'active'
if (MetaCPAN.favs_to_check[distribution]) {
$('#' + distribution + '-fav input[name="remove"]').val(1);
$('#' + distribution + '-fav button').addClass('active');
}

});

}

function getFavDataFromServer() {
Expand All @@ -418,8 +450,8 @@ function getFavDataFromServer() {
showUserData(databack);
},
error: function() {
// Can't be logged in
$('.logged_out').show();
// Can't be logged in, should be getting 403
$('.logged_out').css('display', 'inline');
}
});
return true;
Expand All @@ -439,13 +471,17 @@ function favDistribution(form) {
var count = counter.text();
if (button.hasClass('active')) {
counter.text(count ? parseInt(count, 10) + 1 : 1);
form.append('<input type="hidden" name="remove" value="1">');
// now added let users remove
form.find('input[name="remove"]').val(1);
if (!count)
button.toggleClass('highlight');
} else {
// can't delete what's already deleted
form.find('input[name="remove"]').val(0);

counter.text(parseInt(count, 10) - 1);
form.find('input[name="remove"]').remove();
if (counter.text() === 0) {

if (counter.text() == 0) {
counter.text("");
button.toggleClass('highlight');
}
Expand Down
3 changes: 1 addition & 2 deletions t/html.t
Expand Up @@ -12,7 +12,7 @@ my %skip = map { $_ => 1 } (
'root/inc/dependencies-graph.html', 'root/recent/log.html',
'root/author.html', 'root/mirrors.html',
'root/source.html', 'root/wrapper.html',
'root/about/contributors.html',
'root/about/contributors.html', 'root/inc/favorite.html',
);

my $rule = Path::Iterator::Rule->new;
Expand All @@ -26,4 +26,3 @@ for my $file ( $rule->all('root') ) {
}

done_testing;

0 comments on commit 6ddcd19

Please sign in to comment.