/
progress.ex
110 lines (89 loc) · 3.06 KB
/
progress.ex
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
defmodule Bh.Bh3.Progress do
@moduledoc """
Twitter Bootstrap 3 progress bar helpers for Phoenix.
"""
use Phoenix.HTML
@allowed_opts [:percentage, :context, :striped, :animated]
@contexts [:success, :info, :warning, :danger]
@doc """
Generates progress bar HTML markup.
## Options
* `:percentage` - used as a value of a progress bar. Default value - 0
(zero progress).
* `:context` - context of the progress bar. Be default context is not
needed, so helper renderes blue proggress bar. Allowed options: `:success`,
`:info`, `:warning` and `:danger`.
* `:striped` - boolean value. Pass `true` if you need striped progress
bar. By default progress bar is not striped.
* `:animated` - boolean value. Pass `true` if you need animated progress
bar. Be default progress bar has no CSS3 amination. Note, that only striped
progress bars can be animated.
## Examples
Render markup for progress:
<%= bh_progress percentage: 60 %>
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;">
<span class="sr-only">60% Complete</span>
</div>
</div>
"""
def bh_progress(opts \\ []) do
opts = Bh.Service.leave_allowed_opts(opts, @allowed_opts)
final_opts =
[]
|> Keyword.put(:class, "progress-bar")
|> put_percentage(opts)
|> put_progress_context(opts)
|> put_striped(opts)
|> put_animated(opts)
render_bh_progress(final_opts)
end
defp render_bh_progress(final_opts) do
inner_opts = [
class: final_opts[:class],
role: "progressbar",
"aria-valuenow": final_opts[:percentage],
"aria-valuemin": 0,
"aria-valuemax": 100,
style: "width: #{final_opts[:percentage]}%;"
]
content_tag :div, class: "progress" do
content_tag :div, inner_opts do
content_tag :span, class: "sr-only" do
"#{final_opts[:percentage]}% Complete"
end
end
end
end
defp put_progress_context(final_opts, opts) do
if Keyword.has_key?(opts, :context) && opts[:context] in @contexts do
with extra_class = "progress-bar-#{opts[:context]}" do
Keyword.put(final_opts, :class, "#{final_opts[:class]} #{extra_class}")
end
else
final_opts
end
end
defp put_striped(final_opts, opts) do
if Keyword.has_key?(opts, :striped) && opts[:striped] == true do
final_classes = "#{final_opts[:class]} progress-bar-striped"
Keyword.put(final_opts, :class, final_classes)
else
final_opts
end
end
defp put_animated(final_opts, opts) do
if Keyword.has_key?(opts, :animated) && opts[:animated] == true do
Keyword.put(final_opts, :class, "#{final_opts[:class]} active")
else
final_opts
end
end
defp put_percentage(final_opts, opts) do
if Keyword.has_key?(opts, :percentage) do
Keyword.put(final_opts, :percentage, opts[:percentage])
else
Keyword.put(final_opts, :percentage, 0)
end
end
end