Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update included tags and jquery plugin

- Remove unnecessary blocks
- Break apart js and widget
- Add ability to control language in widget
  • Loading branch information...
commit 6cadfdd9d3d8ea474fc0994ba1d115726d9f88f7 1 parent c3dfe04
@paltman paltman authored
View
16 docs/templatetags.rst
@@ -19,10 +19,11 @@ likes_css
This renders some css style sheets that will style the widget.::
- {% likes_css %}
+ {% phileo_css %}
It renders "phileo/_css.html" and can be overridden as desired.
+
render_like
-----------
@@ -45,6 +46,7 @@ basis:
* phileo/app_name/like.html
* phileo/_like.html
+
phileo_js
---------
@@ -62,11 +64,21 @@ This renders a fragement of html that will be what the user will click
on to unlike or like objects. It only has two required parameters, which
are the user and the object.::
- {% likes_widget user object [widget_id="unique_id" like_type="likes" toggle_class="phileo-liked"] %}
+ {% likes_widget user object like_text="Like|Unlike" counts_text="like|likes" [widget_id="unique_id" like_type="likes" toggle_class="phileo-liked"] %}
It renders "phileo/_widget.html" and can be overridden as desired.
+
+phileo_widget_js
+----------------
+
+This renders a script tag that will use the jquery plugin that `phileo_js`
+includes into the page.
+
+It renders "phileo/_widget_js.html" and can be overriden as desired.
+
+
liked
-----
View
5 phileo/static/phileo/js/jquery.phileo.js
@@ -16,8 +16,11 @@ jQuery(function($) {
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);
+ var submit = self.$form.find("input[type=submit]");
+ submit.val(data.liked ? submit.attr("data-unlike-text") : submit.attr("data-like-text"));
+ var count_text = (data.likes_count > 1 || data.likes_count == 0) ? self.$count.attr("data-counts-text-plural") : self.$count.attr("data-counts-text")
+ self.$count.text(data.likes_count + " " + count_text);
}
}
});
View
42 phileo/templates/phileo/_widget.html
@@ -1,25 +1,17 @@
-{% 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 %}
- {% if user.is_authenticated %}
- <input type="submit" class="phileo-toggle" value="Like" />
- {% endif %}
- {% 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 %}
+<form class="phileo {{ is_liked }} {{ like_type }}" action="{{ like_url }}" method="POST" id="{{ widget_id }}">
+ {% csrf_token %}
+ {% if user.is_authenticated %}
+ <input type="submit"
+ class="phileo-toggle"
+ data-like-text="{{ like_text.0 }}"
+ data-unlike-text="{{ like_text.1 }}"
+ value="{% if is_liked %}{{ like_text.1 }}{% else %}{{ like_text.0 }}{% endif %}" />
+ {% endif %}
+ <span class="phileo-count">
+ <span id="{{ like_count_id }}"
+ data-counts-text="{{ counts_text.0 }}"
+ data-counts-text-plural="{{ counts_text.1 }}">
+ {{ like_count }} {% if like_count > 1 or like_count == 0 %}{{ counts_text.1 }}{% else %}{{ counts_text.0 }}{% endif %}
+ </span>
+ </span>
+</form>
View
8 phileo/templates/phileo/_widget_js.html
@@ -0,0 +1,8 @@
+<script type="text/javascript">
+ jQuery(function ($) {
+ $("#{{ widget_id }}").phileo({
+ count: "#{{ like_count_id }}",
+ toggle_class: "{{ toggle_class }}"
+ });
+ });
+</script>
View
33 phileo/templatetags/phileo_tags.py
@@ -131,10 +131,37 @@ def phileo_js():
return {"STATIC_URL": settings.STATIC_URL}
+@register.inclusion_tag("phileo/_widget_js.html")
+def phileo_widget_js(user, obj, widget_id=None, like_type="like", toggle_class="phileo-liked"):
+ ct = ContentType.objects.get_for_model(obj)
+
+ if widget_id == None:
+ widget_id = "phileo_%s_%s_%s" % (like_type, ct.pk, obj.pk)
+
+ like_count_id = "%s_count" % widget_id
+
+ return {
+ "user": user,
+ "widget_id": widget_id,
+ "like_count_id": like_count_id,
+ "toggle_class": toggle_class
+ }
+
+
@register.inclusion_tag("phileo/_widget.html")
-def phileo_widget(user, obj, widget_id=None, like_type="like", toggle_class="phileo-liked"):
+def phileo_widget(user, obj, like_text="Like|Unlike", counts_text="like|likes", widget_id=None, like_type="like", toggle_class="phileo-liked"):
ct = ContentType.objects.get_for_model(obj)
+ if "|" in like_text:
+ like_text = like_text.split("|")
+ else:
+ like_text = ("Like", "Unlike")
+
+ if "|" in counts_text:
+ counts_text = counts_text.split("|")
+ else:
+ counts_text = ("like", "likes")
+
like_count = Like.objects.filter(
receiver_content_type = ct,
receiver_object_id = obj.pk
@@ -167,7 +194,9 @@ def phileo_widget(user, obj, widget_id=None, like_type="like", toggle_class="phi
"like_count": like_count,
"like_count_id": like_count_id,
"toggle_class": toggle_class,
- "is_liked": toggle_class if liked else ""
+ "is_liked": toggle_class if liked else "",
+ "counts_text": counts_text,
+ "like_text": like_text
}
Please sign in to comment.
Something went wrong with that request. Please try again.