Skip to content

Commit

Permalink
add buttons are now ajax'd!
Browse files Browse the repository at this point in the history
  • Loading branch information
brownan committed Oct 27, 2010
1 parent 12c5291 commit 6d05f59
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
Binary file added playlist/images/spinner.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion playlist/templates/add.html
Expand Up @@ -2,4 +2,4 @@
({{song.addDisallowed.1}})
{% else %}
<a class="boxed add" href="{% url playlist.views.add song.id %}">Add!</a>
{% endif %}
{% endif %}
35 changes: 35 additions & 0 deletions playlist/templates/master.html
Expand Up @@ -232,6 +232,40 @@
$(".playing .votes a[data-vote="+score+"]").addClass("voted");
}

function setupAdds() {
var addbuttons = $("a.add");
addbuttons.click(function (e) {
// button was clicked
e.preventDefault();

var button = $(this);
var parent = button.parent();

// Get the add url
var addurl = button.attr("href");

// remove the button and put a spinner in its place

button.remove();
var spinner = $("<img alt='adding...' src='images/spinner.gif' />");
parent.append(spinner);

// fire of a call to add the song
$.getJSON(addurl, function (data) {
// Json event returned from adding a song
spinner.remove();
if (data[0] == true) {
parent.append("(Added)");
} else {
parent.append(button);
}
for (i=1; i<data.length; i++) {
addMessage(data[i]);
}
});
});
}

function setupVotes() {
$("#votebuttonslow > a").click(function () {
args = {};
Expand Down Expand Up @@ -324,6 +358,7 @@
//comment box stuff
setupComment();
setupVotes();
setupAdds();

//ajax events stuff
ajax_args['position'] = songPosition;
Expand Down
27 changes: 23 additions & 4 deletions playlist/views.py
Expand Up @@ -1060,10 +1060,20 @@ def add(request, songid=0):
raise Http404
profile = request.user.get_profile()
oldtokens = profile.tokens

# is this an ajax request?
try:
isajax = (request.META['HTTP_X_REQUESTED_WITH'] == "XMLHttpRequest")
toret = [1]
except KeyError:
isajax = False

try:
song.playlistAdd(request.user)
except AddError, e:
msg = "Error: %s" % (e.args[0])
if isajax:
return HttpResponse(json.dumps((0, msg)))
request.user.message_set.create(message=msg)
return HttpResponseRedirect(reverse("playlist"))

Expand All @@ -1072,17 +1082,26 @@ def add(request, songid=0):
msg = "You already had a dong on the playlist, so you've used up a token to add this one. You have %d left" % (profile.tokens)
else:
msg = "You already had a dong on the playlist, so you've used up a token to add this one. That was your last one!"
request.user.message_set.create(message=msg)
else:
if not isajax:
request.user.message_set.create(message=msg)
else:
toret.append(msg)
elif not isajax:
request.user.message_set.create(message="Track added successfully!")

if song.isOrphan():
song.uploader = request.user
song.save()
msg = "This dong was an orphan, so you have automatically adopted it. Take good care of it!"
request.user.message_set.create(message=msg)
if isajax:
toret.append(msg)
else:
request.user.message_set.create(message=msg)

return HttpResponseRedirect(reverse("playlist"))
if isajax:
return HttpResponse(json.dumps(toret))
else:
return HttpResponseRedirect(reverse("playlist"))

def next(request, authid):
"""Go to the next song in the playlist, and return a string for ices to parse.
Expand Down

0 comments on commit 6d05f59

Please sign in to comment.