forked from pinax/pinax-likes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked the widget form, including the JavaScript
- Turned the JavaScript code in to a jQuery plugin, removed most of the initialization code from the individual widget templates to a external JavaScript file, and added a {% phileo_js %} tag to load this plugin. - Each like button gets a unique ID, so multiple like buttons can appear on a single page - Made the widget a plain old form. This way, it works without JavaScript. Additionally, the CSRF stuff is included in the form by default, so the ajax_csrf.js script is obsolete
- Loading branch information
Showing
10 changed files
with
109 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
jQuery(function($) { | ||
|
||
var PhileoLikes = function(form, options) { | ||
this.options = $.extend({}, $.fn.phileo.defaults, options); | ||
this.$form = $(form); | ||
|
||
this.$count = $(this.options.count); | ||
|
||
var self = this; | ||
this.$form.submit(function(event) { | ||
event.preventDefault(); | ||
|
||
$.ajax({ | ||
url: self.$form.attr('action'), | ||
type: "POST", | ||
data: self.$form.serialize(), | ||
statusCode: { | ||
200: function(data, textStatus, jqXHR) { | ||
self.$count.text(data.likes_count); | ||
self.$form[data.liked ? 'addClass' : 'removeClass'](self.options.toggle_class); | ||
} | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
$.fn.phileo = function(options) { | ||
$(this).each(function(i, el) { | ||
var phileo = new PhileoLikes(el, options); | ||
$(el).data('Phileo', {instance: phileo}); | ||
}); | ||
return this; | ||
}; | ||
|
||
$.fn.phileo.defaults = { | ||
toggle_class: 'phileo-liked', | ||
count: false | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<script src="{{ STATIC_URL }}phileo/js/jquery.phileo.js"></script> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,23 @@ | ||
<div class="phileo"> | ||
<a class="{{ toggle_class }}" id="{{ like_link }}"></a> | ||
<span class="{{ like_span_total }}">{{ likes_count }}</span> | ||
</div> | ||
{% block phileo_widget_form %} | ||
<form class="phileo {{ is_liked }} {{ like_type }}" action="{{ like_url }}" method="POST" id="{{ widget_id }}">{% csrf_token %} | ||
{% block phileo_widget_button %} | ||
<input type="submit" class="phileo-toggle" value="Like" /> | ||
{% endblock phileo_widget_button %} | ||
{% block phileo_widget_count %} | ||
<span class="phileo-count"> | ||
<span id="{{ like_count_id }}">{{ like_count }}</span> | ||
{{ like_type }}{{ like_count|pluralize }} | ||
</span> | ||
{% endblock phileo_widget_count %} | ||
</form> | ||
{% endblock phileo_widget_form %} | ||
{% block phileo_widget_js %} | ||
<script type="text/javascript"> | ||
jQuery(function ($) { | ||
$("#{{ widget_id }}").phileo({ | ||
count: "#{{ like_count_id }}", | ||
toggle_class: "{{ toggle_class }}" | ||
}); | ||
}); | ||
</script> | ||
{% endblock phileo_widget_js %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters