diff --git a/app/javascript/src/javascripts/autocomplete.js.erb b/app/javascript/src/javascripts/autocomplete.js.erb index 828e5d901..62d91763f 100644 --- a/app/javascript/src/javascripts/autocomplete.js.erb +++ b/app/javascript/src/javascripts/autocomplete.js.erb @@ -1,5 +1,4 @@ import CurrentUser from './current_user' -import SavedSearch from './saved_searches' let Autocomplete = {}; @@ -35,6 +34,7 @@ Autocomplete.initialize_all = function() { this.initialize_pool_autocomplete($('[data-autocomplete="pool"]')); this.initialize_user_autocomplete($('[data-autocomplete="user"]')); this.initialize_wiki_autocomplete($('[data-autocomplete="wiki-page"]')); + this.initialize_saved_search_autocomplete($('[data-autocomplete="saved-search-label"]')); } } @@ -132,7 +132,7 @@ Autocomplete.initialize_tag_autocomplete = function() { Autocomplete.favorite_group_source(term, resp, metatag); break; case "search": - Autocomplete.saved_search_source(term, resp); + Autocomplete.saved_search_source(term, resp, "search:"); break; case "tag": Autocomplete.normal_source(term, resp); @@ -238,6 +238,17 @@ Autocomplete.initialize_wiki_autocomplete = function($fields) { }); }; +Autocomplete.initialize_saved_search_autocomplete = function($fields) { + $fields.autocomplete({ + search: function() { + $(this).data("ui-autocomplete").menu.bindings = $(); + }, + source: function(req, resp) { + Autocomplete.saved_search_source(req.term, resp); + }, + }); +}; + Autocomplete.normal_source = function(term, resp) { if (term === "") { resp([]); @@ -474,15 +485,15 @@ Autocomplete.favorite_group_source = function(term, resp, metatag) { }); } -Autocomplete.saved_search_source = function(term, resp) { - return SavedSearch.labels(term).then(function(labels) { - resp(labels.map(function(label) { - return { - label: label.replace(/_/g, " "), - value: "search:" + label, - }; - })); - }); +Autocomplete.saved_search_source = async function(term, resp, prefix = "") { + let labels = await $.getJSON("/saved_searches/labels", { "search[label]": term + "*", "limit": 10 }); + + resp(labels.map(function(label) { + return { + label: label.replace(/_/g, " "), + value: prefix + label, + }; + })); } $(document).ready(function() { diff --git a/app/javascript/src/javascripts/posts.js.erb b/app/javascript/src/javascripts/posts.js.erb index 14ccc3f23..d4974c4bd 100644 --- a/app/javascript/src/javascripts/posts.js.erb +++ b/app/javascript/src/javascripts/posts.js.erb @@ -3,7 +3,6 @@ import Utility from './utility' import Hammer from 'hammerjs' import Cookie from './cookie' import Note from './notes' -import SavedSearch from './saved_searches' let Post = {}; @@ -549,22 +548,6 @@ Post.approve = function(post_id) { } Post.initialize_saved_searches = function() { - $("#new_saved_search #saved_search_label_string").autocomplete({ - search: function() { - $(this).data("ui-autocomplete").menu.bindings = $(); - }, - source: function(req, resp) { - SavedSearch.labels(req.term).then(function(labels) { - resp(labels.map(function(label) { - return { - label: label.replace(/_/g, " "), - value: label - }; - })); - }); - } - }); - $("#save-search-dialog").dialog({ width: 700, modal: true, diff --git a/app/javascript/src/javascripts/saved_searches.js b/app/javascript/src/javascripts/saved_searches.js index af0069c45..2425265dc 100644 --- a/app/javascript/src/javascripts/saved_searches.js +++ b/app/javascript/src/javascripts/saved_searches.js @@ -6,13 +6,6 @@ SavedSearch.initialize_all = function() { } } -SavedSearch.labels = function(term) { - return $.getJSON("/saved_searches/labels", { - "search[label]": term + "*", - "limit": 10 - }); -} - $(SavedSearch.initialize_all); export default SavedSearch diff --git a/app/views/saved_searches/_interface.html.erb b/app/views/saved_searches/_interface.html.erb index 422db5381..3e1b6b3ed 100644 --- a/app/views/saved_searches/_interface.html.erb +++ b/app/views/saved_searches/_interface.html.erb @@ -2,7 +2,7 @@ <%= simple_form_for(SavedSearch.new, remote: true) do |f| %> <%= f.input :query, as: :string, input_html: { value: params[:tags], data: { autocomplete: "tag-query" } } %> - <%= f.input :label_string, label: "Labels", hint: "A list of tags to help categorize this search. Space delimited." %> + <%= f.input :label_string, label: "Labels", hint: "A list of tags to help categorize this search. Space delimited.", input_html: { "data-autocomplete": "saved-search-label" } %> <%= f.input :disable_labels, label: "Don't show this dialog again", as: :boolean %> <% end %> diff --git a/app/views/saved_searches/edit.html.erb b/app/views/saved_searches/edit.html.erb index d661e2c6e..407d134ed 100644 --- a/app/views/saved_searches/edit.html.erb +++ b/app/views/saved_searches/edit.html.erb @@ -6,7 +6,7 @@ <%= simple_form_for(@saved_search) do |f| %> <%= f.input :query, :as => :string %> - <%= f.input :label_string, :label => "Labels", :hint => "A list of tags to help categorize this search. Space delimited." %> + <%= f.input :label_string, label: "Labels", hint: "A list of tags to help categorize this search. Space delimited.", input_html: { "data-autocomplete": "saved-search-label" } %> <%= f.button :submit, :value => "Submit" %> <% end %> diff --git a/app/views/saved_searches/index.html.erb b/app/views/saved_searches/index.html.erb index 370e1cc8d..c03b0edc8 100644 --- a/app/views/saved_searches/index.html.erb +++ b/app/views/saved_searches/index.html.erb @@ -9,7 +9,7 @@ <%= search_form_for(saved_searches_path) do |f| %> <%= f.input :query_ilike, label: "Query", hint: "Use * for wildcard", input_html: { value: params[:search][:query_ilike] } %> - <%= f.input :label, label: "Label", input_html: { value: params[:search][:label] } %> + <%= f.input :label, label: "Label", input_html: { value: params[:search][:label] }, input_html: { "data-autocomplete": "saved-search-label" } %> <%= f.input :order, collection: [%w[Newest id], %w[Query query], %w[Label label]], include_blank: false, selected: params[:search][:order] %> <%= f.submit "Search" %> <% end %>