Skip to content

Commit 0531739

Browse files
committed
Restructure project file layout and inject TOML
The file organisation in the web app has been changed to group things togather a little more logically. Also, the app is now using the documentation metadata generated by the Ruby server.
1 parent a60d3d6 commit 0531739

32 files changed

+2590
-88
lines changed

etc/docs/lib/docs_web.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule DocsWeb do
3030
def view do
3131
quote do
3232
use Phoenix.View,
33-
root: "lib/docs_web/templates",
33+
root: "lib/docs_web/templates", pattern: "**/*",
3434
namespace: DocsWeb
3535

3636
# Import convenience functions from controllers

etc/docs/lib/docs_web/live/page_live.ex

Lines changed: 0 additions & 39 deletions
This file was deleted.

etc/docs/lib/docs_web/live/page_live.html.leex

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule DocsWeb.PageLive.ContentComponent do
2+
use DocsWeb, :live_component
3+
4+
@impl true
5+
def update(%{content: content, active_tab: active_tab}, socket) do
6+
{:ok, assign(socket, content: content, active_tab: active_tab)}
7+
end
8+
9+
@impl true
10+
def render(assigns) when assigns.active_tab == :fx or assigns.active_tab == :synths do
11+
#test = Map.take(assigns.content, [:doc])
12+
Phoenix.View.render(DocsWeb.PageView, "components/synths_or_fx_component.html", assigns)
13+
end
14+
15+
# TODO: Store active_page per tab? this would allow us to check whether a page has been chosen on each tab,
16+
# and if not, render a default. (Also to render the last active page per tab when switching between them).
17+
@impl true
18+
def render(assigns) when assigns.active_tab == nil do
19+
Phoenix.View.render(DocsWeb.PageView, "components/welcome_component.html", %{})
20+
end
21+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule DocsWeb.PageLive.LangComponent do
2+
use DocsWeb, :live_component
3+
4+
@impl true
5+
def update(%{content: content}, socket) do
6+
{:ok, assign(socket, content: content)}
7+
end
8+
9+
@impl true
10+
def render(assigns) do
11+
Phoenix.View.render(DocsWeb.PageView, "components/lang_component.html", assigns)
12+
end
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule DocsWeb.PageLive.NavbarComponent do
2+
use DocsWeb, :live_component
3+
@impl true
4+
def render(assigns) do
5+
Phoenix.View.render(DocsWeb.PageView, "components/navbar_component.html", assigns)
6+
end
7+
8+
@impl true
9+
def update(%{active_tab: active_tab}, socket) do
10+
{:ok, assign(socket, active_tab: active_tab)}
11+
end
12+
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule DocsWeb.PageLive.SamplesComponent do
2+
use DocsWeb, :live_component
3+
4+
@impl true
5+
def update(%{content: content}, socket) do
6+
{:ok, assign(socket, content: content)}
7+
end
8+
9+
@impl true
10+
def render(assigns) do
11+
Phoenix.View.render(DocsWeb.PageView, "components/samples_component.html", assigns)
12+
end
13+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require IEx
2+
3+
defmodule DocsWeb.PageLive.SideNavComponent do
4+
use DocsWeb, :live_component
5+
6+
@impl true
7+
def render(assigns) do
8+
Phoenix.View.render(DocsWeb.PageView, "components/side_nav_component.html", assigns)
9+
end
10+
11+
@impl true
12+
def update(%{active_page: active_page, active_tab: active_tab, active_pages: active_pages, page_titles: page_titles}, socket) do
13+
case active_tab do
14+
nil -> {:ok, assign(socket, active_pages: active_pages, page_titles: page_titles)}
15+
_ -> {:ok, assign(socket, active_page: active_page, active_pages: %{active_pages | active_tab => active_page}, page_titles: page_titles)}
16+
end
17+
end
18+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule DocsWeb.PageLive.SynthsComponent do
2+
use DocsWeb, :live_component
3+
4+
@impl true
5+
def update(%{content: content}, socket) do
6+
{:ok, assign(socket, content: content)}
7+
end
8+
9+
@impl true
10+
def render(assigns) do
11+
Phoenix.View.render(DocsWeb.PageView, "components/synths_component.html", assigns)
12+
end
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule DocsWeb.PageLive.SynthsOrFxComponent do
2+
use DocsWeb, :live_component
3+
4+
@impl true
5+
def update(%{content: content}, socket) do
6+
{:ok, assign(socket, content: content)}
7+
end
8+
9+
@impl true
10+
def render(assigns) do
11+
Phoenix.View.render(DocsWeb.PageView, "components/synths_or_fx_component.html", assigns)
12+
end
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule DocsWeb.PageLive.WelcomeComponent do
2+
use DocsWeb, :live_component
3+
4+
@impl true
5+
def update(%{content: content}, socket) do
6+
{:ok, assign(socket, content: content)}
7+
end
8+
9+
@impl true
10+
def render(assigns) do
11+
Phoenix.View.render(DocsWeb.PageView, "components/welcome_component.html", assigns)
12+
end
13+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require IEx
2+
3+
defmodule DocsWeb.PageLive do
4+
use DocsWeb, :live_view
5+
6+
@impl true
7+
def mount(_params, %{"metadata" => metadata}, socket) do
8+
metadata = Enum.reject(metadata, fn {k, _v} -> k == :_build end)
9+
page_titles = Enum.into(metadata, %{}, fn {k, v} -> {k, Enum.flat_map(v, fn x -> Map.keys(x) end)} end)
10+
active_pages = Enum.into(metadata, %{}, fn {k, v} -> {k, hd(Enum.flat_map(v, fn x -> Map.keys(x) end))} end)
11+
{:ok, assign(socket, metadata: metadata, active_tab: :fx, active_page: :bitcrusher, active_pages: active_pages, page_titles: page_titles[:fx], content: nil)}
12+
end
13+
14+
@impl true
15+
def render(assigns) do
16+
Phoenix.View.render(DocsWeb.PageView, "page.html", assigns)
17+
end
18+
19+
@impl true
20+
def handle_event("change_tab", %{"active_tab" => active_tab}, socket) do
21+
active_tab = String.to_atom(active_tab)
22+
page_titles = socket.assigns.metadata[active_tab]
23+
|> Enum.flat_map(&Map.keys/1)
24+
|> Enum.uniq
25+
26+
{:noreply, assign(socket, active_tab: active_tab, active_pages: socket.assigns.active_pages, page_titles: page_titles)}
27+
end
28+
29+
@impl true
30+
def handle_event("change_page", %{"active_page" => active_page}, socket) do
31+
active_tab = socket.assigns.active_tab
32+
pages = socket.assigns.metadata[active_tab]
33+
page = String.to_atom(active_page)
34+
content = Enum.find(pages, fn p -> Enum.member?(Map.keys(p), page) end)
35+
%{^page => data} = content
36+
{:noreply, assign(socket, active_pages: %{socket.assigns.active_pages | active_tab => page}, content: data)}
37+
end
38+
39+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require IEx
2+
3+
defmodule DocsWeb.Plug.InjectToml do
4+
import Plug.Conn
5+
6+
defp file_list(dir) do
7+
toml = Path.wildcard("#{:code.priv_dir(:docs)}/toml/#{dir}/*.toml")
8+
Enum.map(toml, fn file ->
9+
{:ok, data} = Toml.decode_file(file, keys: :atoms)
10+
data
11+
end)
12+
end
13+
14+
15+
defp directory_list do
16+
dirs = File.cd!(
17+
"#{:code.priv_dir(:docs)}/toml",
18+
fn -> File.ls! |> Enum.filter(&File.dir?(Path.join("#{:code.priv_dir(:docs)}/toml", &1))) end
19+
)
20+
for dir <- dirs, into: %{}, do: {String.to_atom(dir), file_list(dir)}
21+
end
22+
23+
def metadata(f) do
24+
key = make_ref()
25+
fn ->
26+
case :ets.lookup(:session, key) do
27+
[{^key, val}] -> val
28+
[] ->
29+
val = f.()
30+
:ets.insert(:session, {key, val})
31+
val
32+
end
33+
end
34+
end
35+
36+
def init(default), do: default
37+
38+
def call(conn, _default) do
39+
conn
40+
|> put_session(:metadata, metadata(&directory_list/0).())
41+
end
42+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1><%= inspect(@content) %></h1>
2+
<p><%= test %></p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1><%= inspect(@content) %></h1>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<ul class="nav nav-pills justify-content-center">
2+
<li class="nav-item">
3+
<button
4+
class={is_active(@active_tab, :tutorial)}
5+
phx-click="change_tab"
6+
phx-value-active_tab={:tutorial}
7+
aria-current="page"
8+
>
9+
Tutorial
10+
</button>
11+
</li>
12+
<li class="nav-item">
13+
<button
14+
class={is_active(@active_tab, :examples)}
15+
phx-click="change_tab"
16+
phx-value-active_tab={:examples}
17+
>
18+
Examples
19+
</button>
20+
</li>
21+
<li class="nav-item">
22+
<button
23+
class={is_active(@active_tab, :synths)}
24+
phx-click="change_tab"
25+
phx-value-active_tab={:synths}
26+
>
27+
Synths
28+
</button>
29+
</li>
30+
<li class="nav-item">
31+
<button
32+
class={is_active(@active_tab, :fx)}
33+
phx-click="change_tab"
34+
phx-value-active_tab={:fx}
35+
>
36+
Fx
37+
</button>
38+
</li>
39+
<li class="nav-item">
40+
<button
41+
class={is_active(@active_tab, :samples)}
42+
phx-click="change_tab"
43+
phx-value-active_tab={:samples}
44+
>
45+
Samples
46+
</button>
47+
</li>
48+
<li class="nav-item">
49+
<button
50+
class={is_active(@active_tab, :lang)}
51+
phx-click="change_tab"
52+
phx-value-active_tab={:lang}
53+
>
54+
Lang
55+
</button>
56+
</li>
57+
</ul>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1><%= inspect(@content) %></h1>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<ul class="nav nav-pills flex-column">
2+
<%= for title <- @page_titles do %>
3+
<li class="nav-item">
4+
<button
5+
class={is_active(@active_page, title)}
6+
phx-click="change_page"
7+
phx-value-active_page={title}
8+
aria-current="page">
9+
<%= title %>
10+
</button>
11+
</li>
12+
<% end %>
13+
</ul>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1><%= inspect(@content) %></h1>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Welcome</h1>

0 commit comments

Comments
 (0)