-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.ex
More file actions
138 lines (113 loc) · 3.38 KB
/
Copy pathtemplates.ex
File metadata and controls
138 lines (113 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
defmodule FunWithFlags.UI.Templates do
@moduledoc false
require EEx
alias FunWithFlags.Flag
alias FunWithFlags.UI.{AuditLogFormatter, Utils}
import FunWithFlags.UI.HTMLEscape, only: [html_escape: 1]
@templates [
_head: "_head",
index: "index",
details: "details",
new: "new",
settings: "settings",
not_found: "not_found",
_boolean_row: "rows/_boolean",
_actor_row: "rows/_actor",
_group_row: "rows/_group",
_percentage_row: "rows/_percentage",
_new_actor_row: "rows/_new_actor",
_new_group_row: "rows/_new_group",
_percentage_form_row: "rows/_percentage_form",
audit_logs: "audit_logs",
_audit_log_table: "rows/_audit_log_table",
_audit_log_pagination: "rows/_audit_log_pagination",
_audit_log_section: "rows/_audit_log_section",
]
for {fn_name, file_name} <- @templates do
EEx.function_from_file :def, fn_name, Path.expand("./templates/#{file_name}.html.eex", __DIR__), [:assigns]
end
def html_smart_status_for(flag) do
case Utils.get_flag_status(flag) do
:fully_open ->
~s(<span class="text-success">Enabled</span>)
:half_open ->
~s(<span class="text-warning">Enabled</span>)
:closed ->
~s(<span class="text-danger">Disabled</span>)
end
end
def html_status_for({:ok, bool}) do
html_status_for(bool)
end
def html_status_for(:missing) do
~s{<span class="badge badge-default">Disabled (missing)</span>}
end
def html_status_for(bool) when is_boolean(bool) do
if bool do
~s(<span class="badge badge-success">Enabled</span>)
else
~s(<span class="badge badge-danger">Disabled</span>)
end
end
@gate_type_order [
:boolean,
:actor,
:group,
:percentage_of_actors,
:percentage_of_time,
]
|> Enum.with_index()
|> Map.new()
def html_gate_list(%Flag{gates: gates}) do
gates
|> Enum.map(&(&1.type))
|> Enum.uniq()
|> Enum.sort_by(&Map.get(@gate_type_order, &1, 99))
|> Enum.join(", ")
end
def path(conn, path) do
Path.join(conn.assigns[:namespace], path)
end
def url_safe(val) do
val
|> to_string()
|> URI.encode()
end
def describe_audit_record(record) do
AuditLogFormatter.describe(record)
end
def format_utc_timestamp(%DateTime{} = dt) do
Calendar.strftime(dt, "%Y-%m-%d %H:%M:%S")
end
def format_utc_timestamp(%NaiveDateTime{} = ndt) do
Calendar.strftime(ndt, "%Y-%m-%d %H:%M:%S")
end
def format_utc_timestamp(nil), do: ""
def audit_log_page_path(conn, page, assigns) do
page_param = assigns[:page_param] || "page"
base_path = assigns[:pagination_base_path] || path(conn, "/audit_logs")
params = %{page_param => page}
# Preserve search flag_name for the dedicated audit logs page
params =
case assigns[:search_flag_name] do
nil -> params
"" -> params
name -> Map.put(params, "flag_name", name)
end
query = URI.encode_query(params)
"#{base_path}?#{query}"
end
def pagination_range(_current, total) when total <= 7 do
Enum.to_list(1..total)
end
def pagination_range(current, total) do
cond do
current <= 4 ->
Enum.to_list(1..5) ++ [:ellipsis, total]
current >= total - 3 ->
[1, :ellipsis] ++ Enum.to_list((total - 4)..total)
true ->
[1, :ellipsis] ++ Enum.to_list((current - 1)..(current + 1)) ++ [:ellipsis, total]
end
end
end