From 8f6111eccd43cc1f92b7a34e66846cb96fb0c08a Mon Sep 17 00:00:00 2001 From: John Martin Date: Thu, 27 Sep 2012 11:53:55 +0100 Subject: [PATCH] Added follow button to dataset and user profiles --- ckan/public/base/javascript/client.js | 13 +++++ ckan/public/base/javascript/modules/follow.js | 48 +++++++++++++++++++ ckan/public/base/javascript/resource.config | 1 + ckan/templates/package/read.html | 1 + ckan/templates/snippets/follow_button.html | 22 ++++----- ckan/templates/user/read.html | 2 + 6 files changed, 76 insertions(+), 11 deletions(-) create mode 100644 ckan/public/base/javascript/modules/follow.js diff --git a/ckan/public/base/javascript/client.js b/ckan/public/base/javascript/client.js index a7125bb22af..cebefe4e566 100644 --- a/ckan/public/base/javascript/client.js +++ b/ckan/public/base/javascript/client.js @@ -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. diff --git a/ckan/public/base/javascript/modules/follow.js b/ckan/public/base/javascript/modules/follow.js new file mode 100644 index 00000000000..144b9c1d4a7 --- /dev/null +++ b/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(' ' + this.i18n('unfollow')).removeClass('btn-success').addClass('btn-danger'); + } else { + options.action = 'follow'; + this.el.html(' ' + this.i18n('follow')).removeClass('btn-danger').addClass('btn-success'); + } + } + }; +}); diff --git a/ckan/public/base/javascript/resource.config b/ckan/public/base/javascript/resource.config index 3af45427c83..03d748fa008 100644 --- a/ckan/public/base/javascript/resource.config +++ b/ckan/public/base/javascript/resource.config @@ -41,3 +41,4 @@ main = modules/autocomplete.js modules/custom-fields.js modules/related-item.js + modules/follow.js diff --git a/ckan/templates/package/read.html b/ckan/templates/package/read.html index ab2fa6589a8..4d6a4bd9895 100644 --- a/ckan/templates/package/read.html +++ b/ckan/templates/package/read.html @@ -11,6 +11,7 @@ {% endblock %} {% block actions_content %} +
  • {{ h.follow_button('dataset', pkg.id) }}
  • {% link_for _('Related'), controller='related', action='list', id=pkg.name, class_='btn', icon='picture' %}
  • {# NOTE: Not implemented in stage 1 #} {#
  • {% link_for _('History'), controller='package', action='history', id=pkg.name, class_='btn', icon='undo' %}
  • #} diff --git a/ckan/templates/snippets/follow_button.html b/ckan/templates/snippets/follow_button.html index 1e0a0d99e51..f282839ebc0 100644 --- a/ckan/templates/snippets/follow_button.html +++ b/ckan/templates/snippets/follow_button.html @@ -1,11 +1,11 @@ - - - Unfollow - Follow - - +{% if following %} + + + {{ _('Unfollow') }} + +{% else %} + + + {{ _('Follow') }} + +{% endif %} diff --git a/ckan/templates/user/read.html b/ckan/templates/user/read.html index 9389e6cae60..14840f40691 100644 --- a/ckan/templates/user/read.html +++ b/ckan/templates/user/read.html @@ -14,6 +14,8 @@ {% block actions_content %} {% if c.is_myself %}
  • {% link_for _('Dashboard'), controller='user', action='dashboard', class_='btn', icon='dashboard' %}
  • +{% else %} +
  • {{ h.follow_button('user', user.id) }}
  • {% endif %} {% if h.check_access('user_update', user) %}
  • {% link_for _('Edit'), controller='user', action='edit', id=user.name, class_='btn', icon='cog' %}