From 605c700895e291fe88ba5821ab3c4ebbd829ad52 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Sat, 29 Sep 2012 10:01:29 -0700 Subject: [PATCH 1/1] Clean up search JS Move it to index.js/edit.js and eliminate the global doSearch function. Not enough code is shared to justify it. --- app/assets/javascripts/edit.js | 15 +++++++++++++-- app/assets/javascripts/index.js | 20 +++++++++++++++++--- app/views/site/_search.html.erb | 30 +----------------------------- config/i18n-js.yml | 1 + 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/app/assets/javascripts/edit.js b/app/assets/javascripts/edit.js index f5ada1508..0b2b329b5 100644 --- a/app/assets/javascripts/edit.js +++ b/app/assets/javascripts/edit.js @@ -28,5 +28,16 @@ function minimiseMap() { handleResize(); } -$(document).ready(handleResize); -$(window).resize(handleResize); +$(document).ready(function () { + $(window).resize(handleResize); + handleResize(); + + $("#search_form").submit(function () { + $("#sidebar_title").html(I18n.t('site.sidebar.search_results')); + $("#sidebar_content").load($(this).attr("action"), { + query: $("#query").val() + }, openSidebar); + + return false; + }); +}); diff --git a/app/assets/javascripts/index.js b/app/assets/javascripts/index.js index 5f057b234..0ceb015a7 100644 --- a/app/assets/javascripts/index.js +++ b/app/assets/javascripts/index.js @@ -126,8 +126,22 @@ $(document).ready(function () { $("#exportanchor").click(); } - var query; - if (query = getArgs(window.location.toString()).query) { - doSearch(query); + $("#search_form").submit(function () { + var extent = unproj(map.getExtent()); + + $("#sidebar_title").html(I18n.t('site.sidebar.search_results')); + $("#sidebar_content").load($(this).attr("action"), { + query: $("#query").val(), + minlon: extent.left, + minlat: extent.bottom, + maxlon: extent.right, + maxlat: extent.top + }, openSidebar); + + return false; + }); + + if ($("#query").val()) { + $("#search_form").submit(); } }); diff --git a/app/views/site/_search.html.erb b/app/views/site/_search.html.erb index 9adc3dafe..bcab7e404 100644 --- a/app/views/site/_search.html.erb +++ b/app/views/site/_search.html.erb @@ -10,34 +10,6 @@ zoom: args["zoom"] }, openSidebar); } - - function doSearch(query) { - $("#sidebar_title").html("<%= t 'site.sidebar.search_results' %>"); - - <% if params[:action] == 'index' -%> - var extent = unproj(map.getExtent()); - - $("#sidebar_content").load("<%= url_for :controller => :geocoder, :action => :search %>", { - query: $("#query").val(), - minlon: extent.left, - minlat: extent.bottom, - maxlon: extent.right, - maxlat: extent.top - }, openSidebar); - <% else -%> - $("#sidebar_content").load("<%= url_for :controller => :geocoder, :action => :search %>", { - query: $("#query").val() - }, openSidebar); - <% end -%> - } - - $(document).ready(function () { - $("#search_form").submit(function (event) { - doSearch($("#query").val()); - - return false; - }); - }); // --> @@ -45,7 +17,7 @@
- <%= form_tag "#", :id => "search_form" do %> + <%= form_tag url_for(:controller => :geocoder, :action => :search), :id => "search_form" do %> <%= submit_tag t('site.search.submit_text') %> <%= text_field_tag :query, h(params[:query]), :tabindex => "1", :placeholder => t('site.search.search') %> <% end %> diff --git a/config/i18n-js.yml b/config/i18n-js.yml index f80b142c5..29855fe48 100644 --- a/config/i18n-js.yml +++ b/config/i18n-js.yml @@ -27,3 +27,4 @@ translations: - "*.export.start_rjs.*" - "*.javascripts.*" - "*.site.index.remote_failed" + - "*.site.sidebar.search_results" -- 2.39.5