Skip to content

Commit

Permalink
switch favs to client side
Browse files Browse the repository at this point in the history
  • Loading branch information
ranguard committed May 21, 2015
1 parent e1f425e commit 4b099c0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
16 changes: 11 additions & 5 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 -%>
</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><% END -%>
<% 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;
}
27 changes: 25 additions & 2 deletions root/static/js/cpan.js
@@ -1,5 +1,12 @@
/* jshint white: true, lastsemic: true */

// Store global data in this object
var MetaCPAN = {};
// Store our actual favs
MetaCPAN.fav_data = {};
// 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 @@ -75,6 +82,8 @@ $(document).ready(function() {

$(".ttip").tooltip();

processUserData();

$('#signin-button').mouseenter(function() {
$('#signin').show()
});
Expand Down Expand Up @@ -401,13 +410,27 @@ function logInPAUSE(a) {
}

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

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

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

// create a lookup
MetaCPAN.fav_data[distribution] = 1;

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

});

}

function getFavDataFromServer() {
Expand All @@ -418,7 +441,7 @@ function getFavDataFromServer() {
showUserData(databack);
},
error: function() {
// Can't be logged in
// Can't be logged in, should be getting 403
$('.logged_out').show();
}
});
Expand Down

0 comments on commit 4b099c0

Please sign in to comment.