aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2013-10-31 18:05:46 +0000
committerLouise Crow <louise.crow@gmail.com>2013-12-04 09:32:44 +0000
commit433b03ec0c4bbf0dd024e1c96fad62ac36e4ab4f (patch)
treed552b8d02565ca4f7ed202c87b59ad030ca71707
parentc9f34235a0c249b8c99f895c0ffc50ccbfe20f99 (diff)
Javascript enhancements.
Add or remove all buttons, ajax search as you type.
-rw-r--r--app/assets/javascripts/select-authorities.js67
-rw-r--r--app/assets/stylesheets/main.scss5
-rw-r--r--app/controllers/request_controller.rb24
-rw-r--r--app/views/request/select_authorities.html.erb7
-rw-r--r--config/application.rb2
-rw-r--r--spec/controllers/request_controller_spec.rb86
6 files changed, 156 insertions, 35 deletions
diff --git a/app/assets/javascripts/select-authorities.js b/app/assets/javascripts/select-authorities.js
new file mode 100644
index 000000000..843f5c0ad
--- /dev/null
+++ b/app/assets/javascripts/select-authorities.js
@@ -0,0 +1,67 @@
+$(document).ready(function() {
+
+ function add_option(selector, value, text) {
+ var optionExists = ($(selector + ' option[value=' + value + ']').length > 0);
+ if(!optionExists){
+ $(selector).append("<option value=" + value + ">" + text + "</option>");
+ }
+ }
+ // Transfer a set of select options defined by 'from_selector' to another select,
+ // defined by 'to_selector'
+ function transfer_options(from_selector, to_selector){
+ $(from_selector).each(function()
+ {
+ add_option(to_selector, $(this).val(), $(this).text());
+ $(this).remove();
+ })
+ $('#public_body_query').val('');
+ return false;
+ }
+
+ // Submit the search form once the text reaches a certain length
+ $("#public_body_query").keypress($.debounce( 300, function() {
+ if ($('#public_body_query').val().length >= 3) {
+ $('#body_search_form').submit();
+ }
+ }));
+
+ // Populate the candidate list with json search results
+ $('#body_search_form').on('ajax:success', function(event, data, status, xhr) {
+ $('#select_body_candidates').empty();
+ $.each(data, function(key, value)
+ {
+ add_option('#select_body_candidates', value['id'], value['name']);
+ });
+ });
+
+ // Add a hidden element to the submit form for every option in the selected list
+ $('#body_submit_button').click(function(){
+ $('#select_body_selections option').each(function()
+ {
+ $('#body_submit_form').append('<input type="hidden" value="' + $(this).val() + '" name="public_body_ids[]">' );
+ })
+ })
+
+ // Transfer selected candidates to selected list
+ $('#body_select_button').click(function(){
+ return transfer_options('#select_body_candidates option:selected', '#select_body_selections');
+ })
+
+ // Transfer selected selected options back to candidate list
+ $('#body_deselect_button').click(function(){
+ return transfer_options('#select_body_selections option:selected', '#select_body_candidates');
+ })
+
+ // Transfer all candidates to selected list
+ $('#body_select_all_button').click(function(){
+ return transfer_options('#select_body_candidates option', '#select_body_selections');
+ })
+
+ // Transfer all selected back to candidate list
+ $('#body_deselect_all_button').click(function(){
+ return transfer_options('#select_body_selections option', '#select_body_candidates');
+ })
+
+ // Show the buttons for selecting and deselecting all
+ $('.select_all_button').show();
+})
diff --git a/app/assets/stylesheets/main.scss b/app/assets/stylesheets/main.scss
index 278106d2a..2a057dab0 100644
--- a/app/assets/stylesheets/main.scss
+++ b/app/assets/stylesheets/main.scss
@@ -1788,6 +1788,7 @@ text-decoration:none;
width: 45%;
}
+#body_selection .body_list #body_select_all_button,
#body_selection .body_list #body_deselect_button{
float: right;
}
@@ -1805,6 +1806,10 @@ text-decoration:none;
width:100%;
}
+#body_selection .select_all_button {
+ display: none;
+}
+
#body_selection .body_select {
width: 100%;
}
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb
index a5f333128..bcee7d9f8 100644
--- a/app/controllers/request_controller.rb
+++ b/app/controllers/request_controller.rb
@@ -47,13 +47,25 @@ class RequestController < ApplicationController
if !params[:public_body_query].nil?
@search_bodies = perform_search_typeahead(params[:public_body_query], PublicBody, 1000)
end
- if !params[:public_body_ids].nil?
- if !params[:remove_public_body_ids].nil?
- body_ids = params[:public_body_ids] - params[:remove_public_body_ids]
- else
- body_ids = params[:public_body_ids]
+ respond_to do |format|
+ format.html do
+ if !params[:public_body_ids].nil?
+ if !params[:remove_public_body_ids].nil?
+ body_ids = params[:public_body_ids] - params[:remove_public_body_ids]
+ else
+ body_ids = params[:public_body_ids]
+ end
+ @public_bodies = PublicBody.where({:id => body_ids}).all
+ end
+ end
+ format.json do
+ if @search_bodies
+ render :json => @search_bodies.results.map{ |result| {:name => result[:model].name,
+ :id => result[:model].id } }
+ else
+ render :json => []
+ end
end
- @public_bodies = PublicBody.where({:id => body_ids}).all
end
end
diff --git a/app/views/request/select_authorities.html.erb b/app/views/request/select_authorities.html.erb
index 616cb3f50..e16bcc191 100644
--- a/app/views/request/select_authorities.html.erb
+++ b/app/views/request/select_authorities.html.erb
@@ -6,7 +6,7 @@
</p>
<div>
- <%= form_tag(select_authorities_path, {:method => 'get', :id => 'body_search_form'}) do %>
+ <%= form_tag(select_authorities_path, {:method => 'get', :id => 'body_search_form', :remote => true, "data-type" => 'json'}) do %>
<%= text_field_tag 'public_body_query', params[:public_body_query], { :size => 30, :title => "type your search term here" } %>
<% if !@public_bodies.blank? %>
<%- @public_bodies.each do |public_body| %>
@@ -20,6 +20,7 @@
<div id="body_lists">
<div id="body_candidates" class="body_list">
<%= form_tag(select_authorities_path, {:id => "body_select_form"}) do %>
+ <%= submit_tag _(' >> '), :id => 'body_select_all_button', :class => 'select_all_button' %>
<%= submit_tag _(' > '), :id => 'body_select_button' %>
<%= hidden_field_tag "public_body_query", params[:public_body_query], { :id => 'public_body_select_query' } %>
<% if !@public_bodies.blank? %>
@@ -41,7 +42,9 @@
<div id="body_selections" class="body_list">
<%= form_tag(select_authorities_path, {:id => "body_deselect_form"}) do %>
+
<%= submit_tag _(' < '), :id => 'body_deselect_button' %>
+ <%= submit_tag _(' << '), :id => 'body_deselect_all_button', :class => 'select_all_button' %>
<%= hidden_field_tag "public_body_query", params[:public_body_query], { :id => 'public_body_deselect_query' } %>
<% if @public_bodies %>
<% @public_bodies.each do |public_body| %>
@@ -71,4 +74,4 @@
</div>
</div>
</div>
-
+<%= javascript_include_tag 'jquery_ujs.js', 'select-authorities.js' %>
diff --git a/config/application.rb b/config/application.rb
index dba3a0c57..91006b669 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -96,6 +96,8 @@ module Alaveteli
config.assets.precompile += ['jquery.fancybox-1.3.4.pack.js',
'jquery.Jcrop.css',
'excanvas.min.js',
+ 'select-authorities.js',
+ 'jquery_ujs.js',
'fonts.css',
'print.css',
'admin.css',
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb
index be5a97776..8a9cd93c2 100644
--- a/spec/controllers/request_controller_spec.rb
+++ b/spec/controllers/request_controller_spec.rb
@@ -2509,7 +2509,7 @@ describe RequestController, "when caching fragments" do
end
-describe RequestController, "#new_batch", :focus => true do
+describe RequestController, "#new_batch" do
context "when batch requests is enabled" do
@@ -2675,37 +2675,69 @@ describe RequestController, "#select_authorities" do
@user = FactoryGirl.create(:user, :can_make_batch_requests => true)
end
- it 'should be successful' do
- get :select_authorities, {}, {:user_id => @user.id}
- response.should be_success
- end
+ context 'when asked for HTML' do
- it 'should render the "select_authorities" template' do
- get :select_authorities, {}, {:user_id => @user.id}
- response.should render_template('request/select_authorities')
- end
+ it 'should be successful' do
+ get :select_authorities, {}, {:user_id => @user.id}
+ response.should be_success
+ end
- it 'should assign a list of search results to the view if passed a query' do
- get :select_authorities, {:public_body_query => "Quango"}, {:user_id => @user.id}
- assigns[:search_bodies].results.size.should == 1
- assigns[:search_bodies].results[0][:model].name.should == public_bodies(:geraldine_public_body).name
- end
+ it 'should render the "select_authorities" template' do
+ get :select_authorities, {}, {:user_id => @user.id}
+ response.should render_template('request/select_authorities')
+ end
+
+ it 'should assign a list of search results to the view if passed a query' do
+ get :select_authorities, {:public_body_query => "Quango"}, {:user_id => @user.id}
+ assigns[:search_bodies].results.size.should == 1
+ assigns[:search_bodies].results[0][:model].name.should == public_bodies(:geraldine_public_body).name
+ end
+
+ it 'should assign a list of public bodies to the view if passed a list of ids' do
+ get :select_authorities, {:public_body_ids => [public_bodies(:humpadink_public_body).id]},
+ {:user_id => @user.id}
+ assigns[:public_bodies].size.should == 1
+ assigns[:public_bodies][0].name.should == public_bodies(:humpadink_public_body).name
+ end
+
+ it 'should subtract a list of public bodies to remove from the list of bodies assigned to
+ the view' do
+ get :select_authorities, {:public_body_ids => [public_bodies(:humpadink_public_body).id,
+ public_bodies(:geraldine_public_body).id],
+ :remove_public_body_ids => [public_bodies(:geraldine_public_body).id]},
+ {:user_id => @user.id}
+ assigns[:public_bodies].size.should == 1
+ assigns[:public_bodies][0].name.should == public_bodies(:humpadink_public_body).name
+ end
- it 'should assign a list of public bodies to the view if passed a list of ids' do
- get :select_authorities, {:public_body_ids => [public_bodies(:humpadink_public_body).id]},
- {:user_id => @user.id}
- assigns[:public_bodies].size.should == 1
- assigns[:public_bodies][0].name.should == public_bodies(:humpadink_public_body).name
end
- it 'should subtract a list of public bodies to remove from the list of bodies assigned to
- the view' do
- get :select_authorities, {:public_body_ids => [public_bodies(:humpadink_public_body).id,
- public_bodies(:geraldine_public_body).id],
- :remove_public_body_ids => [public_bodies(:geraldine_public_body).id]},
- {:user_id => @user.id}
- assigns[:public_bodies].size.should == 1
- assigns[:public_bodies][0].name.should == public_bodies(:humpadink_public_body).name
+ context 'when asked for JSON', :focus => true do
+
+ it 'should be successful' do
+ get :select_authorities, {:public_body_query => "Quan", :format => 'json'}, {:user_id => @user.id}
+ response.should be_success
+ end
+
+ it 'should return a list of public body names and ids' do
+ get :select_authorities, {:public_body_query => "Quan", :format => 'json'},
+ {:user_id => @user.id}
+
+ JSON(response.body).should == [{ 'id' => public_bodies(:geraldine_public_body).id,
+ 'name' => public_bodies(:geraldine_public_body).name }]
+ end
+
+ it 'should return an empty list if no search is passed' do
+ get :select_authorities, {:format => 'json' },{:user_id => @user.id}
+ JSON(response.body).should == []
+ end
+
+ it 'should return an empty list if there are no bodies' do
+ get :select_authorities, {:public_body_query => 'fknkskalnr', :format => 'json' },
+ {:user_id => @user.id}
+ JSON(response.body).should == []
+ end
+
end
end