Skip to content

Commit

Permalink
Added follow button to dataset and user profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmartin committed Sep 27, 2012
1 parent 8634f01 commit 8f6111e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 11 deletions.
13 changes: 13 additions & 0 deletions ckan/public/base/javascript/client.js
Expand Up @@ -25,6 +25,19 @@
return path;
},

action: function(path, data, fn) {
var url = this.url('/api/action/' + path);
jQuery.ajax({
contentType: 'application/json',
url: url,
data: data,
dataType: 'json',
processData: false,
type: 'POST',
success: fn
});
},

/* Requests a block of HTML from the snippet API endpoint. Optional
* parameters can also be provided to the template via the params
* object.
Expand Down
48 changes: 48 additions & 0 deletions ckan/public/base/javascript/modules/follow.js
@@ -0,0 +1,48 @@
this.ckan.module('follow', function($, _) {
return {
options : {
action: null,
type: null,
id: null,
loading: false,
i18n: {
follow: _('Follow'),
unfollow: _('Unfollow')
}
},
initialize: function () {
$.proxyAll(this, /_on/);
this.el.on('click', this._onClick);
},
_onClick: function(e) {
var options = this.options;
e.preventDefault();
if (
options.action
&& options.type
&& options.id
&& !options.loading
) {
var client = this.sandbox.client;
var path = options.action + '_' + options.type;
var data = JSON.stringify({ id : options.id });
options.loading = true;
this.el.addClass('disabled');
client.action(path, data, this._onClickLoaded);
}
return false;
},
_onClickLoaded: function(json) {
var options = this.options;
options.loading = false;
this.el.removeClass('disabled');
if (options.action == 'follow') {
options.action = 'unfollow';
this.el.html('<i class="icon-remove-sign"></i> ' + this.i18n('unfollow')).removeClass('btn-success').addClass('btn-danger');
} else {
options.action = 'follow';
this.el.html('<i class="icon-plus-sign"></i> ' + this.i18n('follow')).removeClass('btn-danger').addClass('btn-success');
}
}
};
});
1 change: 1 addition & 0 deletions ckan/public/base/javascript/resource.config
Expand Up @@ -41,3 +41,4 @@ main =
modules/autocomplete.js
modules/custom-fields.js
modules/related-item.js
modules/follow.js
1 change: 1 addition & 0 deletions ckan/templates/package/read.html
Expand Up @@ -11,6 +11,7 @@
{% endblock %}

{% block actions_content %}
<li>{{ h.follow_button('dataset', pkg.id) }}</li>
<li>{% link_for _('Related'), controller='related', action='list', id=pkg.name, class_='btn', icon='picture' %}</li>
{# NOTE: Not implemented in stage 1 #}
{# <li>{% link_for _('History'), controller='package', action='history', id=pkg.name, class_='btn', icon='undo' %}</li> #}
Expand Down
22 changes: 11 additions & 11 deletions ckan/templates/snippets/follow_button.html
@@ -1,11 +1,11 @@
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip=""
>
<py:choose test="following">
<a py:when="True" id="${obj_type}_follow_button" class="btn btn-mini" data-obj-id="${obj_id}" data-state="unfollow">Unfollow</a>
<a py:otherwise="" id="${obj_type}_follow_button" class="btn btn-mini" data-obj-id="${obj_id}" data-state="follow">Follow</a>
</py:choose>
</html>
{% if following %}
<a href="{{ h.url_for(obj_type, 'follow', obj_id) }}" class="btn btn-danger" data-module="follow" data-module-type="{{ obj_type }}" data-module-id="{{ obj_id }}" data-module-action="unfollow">
<i class="icon-remove-sign"></i>
{{ _('Unfollow') }}
</a>
{% else %}
<a href="{{ h.url_for(obj_type, 'unfollow', obj_id) }}" class="btn btn-success" data-module="follow" data-module-type="{{ obj_type }}" data-module-id="{{ obj_id }}" data-module-action="follow">
<i class="icon-plus-sign"></i>
{{ _('Follow') }}
</a>
{% endif %}
2 changes: 2 additions & 0 deletions ckan/templates/user/read.html
Expand Up @@ -14,6 +14,8 @@
{% block actions_content %}
{% if c.is_myself %}
<li>{% link_for _('Dashboard'), controller='user', action='dashboard', class_='btn', icon='dashboard' %}</li>
{% else %}
<li>{{ h.follow_button('user', user.id) }}</li>
{% endif %}
{% if h.check_access('user_update', user) %}
<li>{% link_for _('Edit'), controller='user', action='edit', id=user.name, class_='btn', icon='cog' %}</li>
Expand Down

0 comments on commit 8f6111e

Please sign in to comment.