refactored for new project name
This commit is contained in:
parent
0039146cd4
commit
82ab1d1ea5
113 changed files with 417 additions and 412 deletions
72
lib/shift73k_web/views/error_helpers.ex
Normal file
72
lib/shift73k_web/views/error_helpers.ex
Normal file
|
@ -0,0 +1,72 @@
|
|||
defmodule Shift73kWeb.ErrorHelpers do
|
||||
@moduledoc """
|
||||
Conveniences for translating and building error messages.
|
||||
"""
|
||||
|
||||
use Phoenix.HTML
|
||||
|
||||
@doc """
|
||||
Generates tag for inlined form input errors.
|
||||
"""
|
||||
def error_tag(form, field, opts \\ []) do
|
||||
opts = error_opts(form, field, opts)
|
||||
|
||||
form.errors
|
||||
|> Keyword.get_values(field)
|
||||
|> Enum.map(fn error -> content_tag(:span, translate_error(error), opts) end)
|
||||
end
|
||||
|
||||
defp error_opts(form, field, opts) do
|
||||
append = "invalid-feedback"
|
||||
input_id = input_id(form, field)
|
||||
|
||||
opts
|
||||
|> Keyword.put_new(:id, error_id(input_id))
|
||||
|> Keyword.put_new(:phx_feedback_for, input_id)
|
||||
|> Keyword.update(:class, append, fn c -> "#{append} #{c}" end)
|
||||
end
|
||||
|
||||
def error_id(%Phoenix.HTML.Form{} = form, field), do: input_id(form, field) |> error_id()
|
||||
def error_id(input_id) when is_binary(input_id), do: "#{input_id}_feedback"
|
||||
|
||||
def input_class(form, field, classes \\ "") do
|
||||
case form.source.action do
|
||||
nil ->
|
||||
classes
|
||||
|
||||
_ ->
|
||||
case Keyword.has_key?(form.errors, field) do
|
||||
true -> "#{classes} is-invalid"
|
||||
_ -> "#{classes} is-valid"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Translates an error message using gettext.
|
||||
"""
|
||||
def translate_error({msg, opts}) do
|
||||
# When using gettext, we typically pass the strings we want
|
||||
# to translate as a static argument:
|
||||
#
|
||||
# # Translate "is invalid" in the "errors" domain
|
||||
# dgettext("errors", "is invalid")
|
||||
#
|
||||
# # Translate the number of files with plural rules
|
||||
# dngettext("errors", "1 file", "%{count} files", count)
|
||||
#
|
||||
# Because the error messages we show in our forms and APIs
|
||||
# are defined inside Ecto, we need to translate them dynamically.
|
||||
# This requires us to call the Gettext module passing our gettext
|
||||
# backend as first argument.
|
||||
#
|
||||
# Note we use the "errors" domain, which means translations
|
||||
# should be written to the errors.po file. The :count option is
|
||||
# set by Ecto and indicates we should also apply plural rules.
|
||||
if count = opts[:count] do
|
||||
Gettext.dngettext(Shift73kWeb.Gettext, "errors", msg, msg, count, opts)
|
||||
else
|
||||
Gettext.dgettext(Shift73kWeb.Gettext, "errors", msg, opts)
|
||||
end
|
||||
end
|
||||
end
|
16
lib/shift73k_web/views/error_view.ex
Normal file
16
lib/shift73k_web/views/error_view.ex
Normal file
|
@ -0,0 +1,16 @@
|
|||
defmodule Shift73kWeb.ErrorView do
|
||||
use Shift73kWeb, :view
|
||||
|
||||
# If you want to customize a particular status code
|
||||
# for a certain format, you may uncomment below.
|
||||
# def render("500.html", _assigns) do
|
||||
# "Internal Server Error"
|
||||
# end
|
||||
|
||||
# By default, Phoenix returns the status message from
|
||||
# the template name. For example, "404.html" becomes
|
||||
# "Not Found".
|
||||
def template_not_found(template, _assigns) do
|
||||
Phoenix.Controller.status_message_from_template(template)
|
||||
end
|
||||
end
|
35
lib/shift73k_web/views/icon_helpers.ex
Normal file
35
lib/shift73k_web/views/icon_helpers.ex
Normal file
|
@ -0,0 +1,35 @@
|
|||
defmodule Shift73kWeb.IconHelpers do
|
||||
@moduledoc """
|
||||
Generate SVG sprite use tags for SVG icons
|
||||
"""
|
||||
|
||||
use Phoenix.HTML
|
||||
alias Shift73kWeb.Router.Helpers, as: Routes
|
||||
|
||||
def icon_div(conn, name, div_opts \\ [], svg_opts \\ []) do
|
||||
content_tag(:div, tag_opts(name, div_opts)) do
|
||||
icon_svg(conn, name, svg_opts)
|
||||
end
|
||||
end
|
||||
|
||||
def icon_svg(conn, name, opts \\ []) do
|
||||
opts = aria_hidden?(opts)
|
||||
|
||||
content_tag(:svg, tag_opts(name, opts)) do
|
||||
tag(:use, "xlink:href": Routes.static_path(conn, "/images/icons.svg##{name}"))
|
||||
end
|
||||
end
|
||||
|
||||
defp tag_opts(name, opts) do
|
||||
Keyword.update(opts, :class, name, fn c -> "#{c} #{name}" end)
|
||||
end
|
||||
|
||||
defp aria_hidden?(opts) do
|
||||
case Keyword.get(opts, :aria_hidden) do
|
||||
"false" -> Keyword.drop(opts, [:aria_hidden])
|
||||
false -> Keyword.drop(opts, [:aria_hidden])
|
||||
"true" -> opts
|
||||
_ -> Keyword.put(opts, :aria_hidden, "true")
|
||||
end
|
||||
end
|
||||
end
|
24
lib/shift73k_web/views/layout_view.ex
Normal file
24
lib/shift73k_web/views/layout_view.ex
Normal file
|
@ -0,0 +1,24 @@
|
|||
defmodule Shift73kWeb.LayoutView do
|
||||
use Shift73kWeb, :view
|
||||
|
||||
alias Shift73k.Accounts.User
|
||||
alias Shift73kWeb.Roles
|
||||
|
||||
def nav_link_opts(conn, opts) do
|
||||
case Keyword.get(opts, :to) == Phoenix.Controller.current_path(conn) do
|
||||
false -> opts
|
||||
true -> Keyword.update(opts, :class, "active", fn c -> "#{c} active" end)
|
||||
end
|
||||
end
|
||||
|
||||
def alert_kinds do
|
||||
[
|
||||
success: "alert-success",
|
||||
info: "alert-info",
|
||||
error: "alert-danger",
|
||||
warning: "alert-warning",
|
||||
primary: "alert-primary",
|
||||
secondary: "alert-secondary"
|
||||
]
|
||||
end
|
||||
end
|
3
lib/shift73k_web/views/other_view.ex
Normal file
3
lib/shift73k_web/views/other_view.ex
Normal file
|
@ -0,0 +1,3 @@
|
|||
defmodule Shift73kWeb.OtherView do
|
||||
use Shift73kWeb, :view
|
||||
end
|
29
lib/shift73k_web/views/pagination.ex
Normal file
29
lib/shift73k_web/views/pagination.ex
Normal file
|
@ -0,0 +1,29 @@
|
|||
defmodule Shift73kWeb.Pagination do
|
||||
def generate_page_list(_, total_pages) when total_pages < 5,
|
||||
do: 1..total_pages |> Enum.to_list()
|
||||
|
||||
def generate_page_list(current, total_pages),
|
||||
do: first_half(1, current) ++ [current] ++ second_half(current, total_pages)
|
||||
|
||||
defp first_half(first, current) do
|
||||
prev = current - 1
|
||||
|
||||
cond do
|
||||
first == current -> []
|
||||
prev <= first -> [first]
|
||||
prev - first > 2 -> [first, -1, prev]
|
||||
true -> first..prev |> Enum.to_list()
|
||||
end
|
||||
end
|
||||
|
||||
defp second_half(current, last) do
|
||||
next = current + 1
|
||||
|
||||
cond do
|
||||
last == current -> []
|
||||
next >= last -> [last]
|
||||
last - next > 2 -> [next, -1, last]
|
||||
true -> next..last |> Enum.to_list()
|
||||
end
|
||||
end
|
||||
end
|
4
lib/shift73k_web/views/user_confirmation_view.ex
Normal file
4
lib/shift73k_web/views/user_confirmation_view.ex
Normal file
|
@ -0,0 +1,4 @@
|
|||
defmodule Shift73kWeb.UserConfirmationView do
|
||||
use Shift73kWeb, :view
|
||||
alias Shift73k.Accounts.User
|
||||
end
|
4
lib/shift73k_web/views/user_reset_password_view.ex
Normal file
4
lib/shift73k_web/views/user_reset_password_view.ex
Normal file
|
@ -0,0 +1,4 @@
|
|||
defmodule Shift73kWeb.UserResetPasswordView do
|
||||
use Shift73kWeb, :view
|
||||
alias Shift73k.Accounts.User
|
||||
end
|
4
lib/shift73k_web/views/user_session_view.ex
Normal file
4
lib/shift73k_web/views/user_session_view.ex
Normal file
|
@ -0,0 +1,4 @@
|
|||
defmodule Shift73kWeb.UserSessionView do
|
||||
use Shift73kWeb, :view
|
||||
alias Shift73k.Accounts.User
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue