aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-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
4 files changed, 95 insertions, 8 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' %>