add tag autocompletion

This commit is contained in:
byte[] 2019-11-26 21:19:07 -05:00
parent ae7f3834fd
commit 47f379d82d
3 changed files with 45 additions and 1 deletions

View file

@ -0,0 +1,41 @@
defmodule PhilomenaWeb.Tag.AutocompleteController do
use PhilomenaWeb, :controller
alias Philomena.Tags.Tag
def show(conn, params) do
tags =
case query(params) do
nil ->
[]
term ->
Tag.search_records(
%{
query: %{
bool: %{
should: [
%{prefix: %{name: term}},
%{prefix: %{name_in_namespace: term}}
]
}
},
sort: %{images: :desc}
},
%{page_size: 5},
Tag
)
|> Enum.map(&%{label: "#{&1.name} (#{&1.images_count})", value: &1.name})
end
conn
|> json(tags)
end
defp query(%{"term" => term}) when is_binary(term) and byte_size(term) > 2 do
term
|> String.downcase()
|> String.trim()
end
defp query(_params), do: nil
end

View file

@ -90,6 +90,9 @@ defmodule PhilomenaWeb.Router do
resources "/tag_changes", Image.TagChangeController, only: [:index] resources "/tag_changes", Image.TagChangeController, only: [:index]
resources "/source_changes", Image.SourceChangeController, only: [:index] resources "/source_changes", Image.SourceChangeController, only: [:index]
end end
scope "/tags", Tag, as: :tag do
resources "/autocomplete", AutocompleteController, only: [:show], singleton: true
end
resources "/tags", TagController, only: [:index, :show] resources "/tags", TagController, only: [:index, :show]
resources "/search", SearchController, only: [:index] resources "/search", SearchController, only: [:index]
resources "/forums", ForumController, only: [:index, :show] do resources "/forums", ForumController, only: [:index, :show] do

View file

@ -8,7 +8,7 @@ elixir:
.js-tag-block class="fancy-tag-#{@type}" .js-tag-block class="fancy-tag-#{@type}"
= textarea @f, @name, html_options = textarea @f, @name, html_options
.js-taginput.input.input--wide.tagsinput.hidden class="js-taginput-fancy" data-click-focus=".js-taginput-input.js-taginput-#{@name}" .js-taginput.input.input--wide.tagsinput.hidden class="js-taginput-fancy" data-click-focus=".js-taginput-input.js-taginput-#{@name}"
input.input class="js-taginput-input js-taginput-#{@name}" id="taginput-fancy-#{@name}" type="text" placeholder="add a tag" autocapitalize="none" data-ac="true" data-ac-min-length="3" data-ac-source='/tags/autocomplete.json?term=' input.input class="js-taginput-input js-taginput-#{@name}" id="taginput-fancy-#{@name}" type="text" placeholder="add a tag" autocapitalize="none" data-ac="true" data-ac-min-length="3" data-ac-source='/tags/autocomplete?term='
button.button.button--state-primary.button--bold class="js-taginput-show" data-click-show=".js-taginput-fancy,.js-taginput-hide" data-click-hide=".js-taginput-plain,.js-taginput-show" data-click-focus=".js-taginput-input.js-taginput-#{@name}" button.button.button--state-primary.button--bold class="js-taginput-show" data-click-show=".js-taginput-fancy,.js-taginput-hide" data-click-hide=".js-taginput-plain,.js-taginput-show" data-click-focus=".js-taginput-input.js-taginput-#{@name}"
= hidden_input :fuck_ie, :fuck_ie, value: "fuck_ie" = hidden_input :fuck_ie, :fuck_ie, value: "fuck_ie"
' Fancy Editor ' Fancy Editor