檢視原始碼 Phoenix.LiveDashboard.PageBuilder 行為 (LiveDashboard v0.8.4)
網頁產生器是建立自訂看板頁面的預設機制。
每個看板頁面皆為與額外自訂選單外觀及自動重新整理的迴呼的 LiveView。
一個簡單又直接的自訂頁面範例為隨看板配送的 Phoenix.LiveDashboard.EtsPage
defmodule Phoenix.LiveDashboard.EtsPage do
@moduledoc false
use Phoenix.LiveDashboard.PageBuilder
@impl true
def menu_link(_, _) do
{:ok, "ETS"}
end
@impl true
def render(assigns) do
~H"""
<.live_table
id="ets-table"
dom_id="ets-table"
page={@page}
title="ETS"
row_fetcher={&fetch_ets/2}
row_attrs={&row_attrs/1}
rows_name="tables"
>
<:col field={:name} header="Name or module" />
<:col field={:protection} />
<:col field={:type} />
<:col field={:size} text_align="right" sortable={:desc} />
<:col field={:memory} text_align="right" sortable={:desc} :let={ets}>
<%= format_words(ets[:memory]) %>
</:col>
<:col field={:owner} :let={ets} >
<%= encode_pid(ets[:owner]) %>
</:col>
</.live_table>
"""
end
defp fetch_ets(params, node) do
%{search: search, sort_by: sort_by, sort_dir: sort_dir, limit: limit} = params
# Here goes the code that goes through all ETS tables, searches
# (if not nil), sorts, and limits them.
#
# It must return a tuple where the first element is list with
# the current entries (up to limit) and an integer with the
# total amount of entries.
# ...
end
defp row_attrs(table) do
[
{"phx-click", "show_info"},
{"phx-value-info", encode_ets(table[:id])},
{"phx-page-loading", true}
]
end
end
定義頁面後,必須在 live_dashboard
路由中宣告,如下所示
live_dashboard "/dashboard",
additional_pages: [
route_name: MyAppWeb.MyCustomPage
]
或使用替代方式
live_dashboard "/dashboard",
additional_pages: [
route_name: {MyAppWeb.MyCustomPage, some_option: ...}
]
元組的第二個引數將傳送給 init/1
迴呼。若未給元組,init/1
將收到一組空值。
use 巨集選項
使用 PageBuilder
模組時,可以指定下列選項
refresher?
- 布林值,用於啟用或停用頁面的自動重新整理。
元件
頁面可以在 render/1
迴呼傳回任何有效的 HEEx 範本,也可以使用與此頁面列出的元件。
我們目前支援 card/1
、fields_card/1
、row/1
、shared_usage_card/1
及 usage_card/1
;以及 live_layered_graph/1
、live_nav_bar/1
及 live_table/1
等即時元件。
輔助函式
提供了一些用於頁面建立的輔助函式。支援的輔助函式為:live_dashboard_path/2
、live_dashboard_path/3
、encode_app/1
、encode_ets/1
、encode_pid/1
、encode_port/1
及 encode_socket/1
。
摘要
函式
對 URL 進行應用程式編碼。
對 URL 進行 ETS 參考編碼。
對 URL 進行 PID 編碼。
對 URL 進行 Port 編碼。
對 URL 進行 Sockets 編碼。
計算路由器路徑到目前的網頁。
計算路由器路徑到目前的網頁,但參數已合併。
組件
@spec card(assigns :: Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
卡片元件。
您可以在首頁和作業系統資料網頁中看到它的用法。
屬性
title
(:string
) - 卡片上方的標題。預設為nil
。hint
(:string
) - 靠近標題顯示的文字提示。預設為nil
。inner_title
(:string
) - 卡片內的標題。預設為nil
。inner_hint
(:string
) - 靠近內部標題顯示的文字提示。預設為nil
。dom_id
(:string
) - HTML 主標籤的 id 屬性。預設為nil
。
插槽
inner_block
(必填) - 卡片將顯示的值。
@spec card_title(assigns :: Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
卡片標題組件。
屬性
title
(:string
) - 卡片上方的標題。預設為nil
。hint
(:string
) - 靠近標題顯示的文字提示。預設為nil
。
欄位卡片組件。
您可以在環境區段的首頁看到它的用法。
屬性
fields
(:list
) (必填) - 將會顯示在卡片內的關鍵值配對元素清單。title
(:string
) - 卡片上方的標題。預設為nil
。hint
(:string
) - 靠近標題顯示的文字提示。預設為nil
。inner_title
(:string
) - 卡片內的標題。預設為nil
。inner_hint
(:string
) - 靠近內部標題顯示的文字提示。預設為nil
。
@spec hint(assigns :: Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
提示彈出文字組件
屬性
text
(:string
) (必填) - 要在提示中顯示的文字。
標籤值清單。
您可以在 Ports 或 Processes 頁面中的模態對話框中看到實際應用。
插槽
elem
(必填) - 清單中每個元素的值。接受屬性label
(:string
) (必填) - 元素標籤。
@spec live_layered_graph(assigns :: Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
用於繪製圖層圖表的組件。
這對於呈現管線非常有用,例如我們在 BroadwayDashboard 上的每個圖層都指向下方圖層的節點。它會從上到下繪製圖層。
根據選項自動計算圖層和位置。
屬性
id
(:any
) (必填) - 因為是一個有狀態的Phoenix.LiveComponent
,所以需要一個唯一的 ID。title
(:string
) - 組件標題。預設為nil
。hint
(:string
) - 靠近標題顯示的文字提示。預設為nil
。layers
(:list
) (必填) - 圖層和節點的圖形。它們表示我們的圖形結構(請見範例)。每個圖層都是節點的清單,其中每個節點都有以下欄位:id
- 指定節點的 ID。:children
- 子節點的 ID。:data
- 字串或映射。如果是映射,則必填的欄位為detail
和label
。
show_grid?
(:boolean
) - 啟用或停用格線顯示。這對於開發很有用。預設為false
。y_label_offset
(:integer
) - 標籤相對於其圓心「Y」軸的偏移量。預設為5
。y_detail_offset
(:integer
) - 說明文字相對於其圓心「Y」軸的偏移量。預設為18
。background
(:any
) - 根據節點資料計算節點背景的函式。預設值:fn _node_data -> "gray" end
。"format_label
(:any
) - 格式化標籤的函式。預設為一個會回傳標籤或資料(如果資料是二進位)的函式。format_detail
(:any
) - 格式化說明文字欄位的函式。只有在資料是映射時才會呼叫此函式。預設值:fn node_data -> node_data.detail end
。
範例
iex> layers = [
...> [
...> %{
...> id: "a1",
...> data: "a1",
...> children: ["b1"]
...> }
...> ],
...> [
...> %{
...> id: "b1"
...> data: %{
...> detail: 0,
...> label: "b1"
...> },
...> children: []
...> }
...> ]
...> ]
@spec live_table(assigns :: Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
表格即時組件。
可以在應用程式、處理程序、Socket 頁面或其他多個頁面中看到使用方法。
屬性
id
(:any
) (必填) - 因為是一個有狀態的Phoenix.LiveComponent
,所以需要一個唯一的 ID。page
(Phoenix.LiveDashboard.PageBuilder
)(必填) - 首頁資訊看板頁面。row_fetcher
(:any
)(必填) - 接受參數和節點的函數,並傳回一組行和總數:(params(), node() -> {list(), integer() | binary()})
,函數如果需要保留狀態,也可以定義成一組,第一個元素是函數,第二個元素是初始狀態。這種情況下,函數會接收狀態當成第三個參數,並且必須傳回一組具有下列元素的行、總數和新狀態以便後續呼叫:{(params(), node(), term() -> {list(), integer() | binary(), term()}), term()}
rows_name
(:string
) - 將行的代表命名為字串。預設值是根據目前頁面計算。row_attrs
(:any
) - 具有表格列 HTML 屬性的清單。它也可以是接收列作為引數並傳回包含 HTML 屬性名稱和值的 2 元素元組清單的函數。預設為
nil
。default_sort_by
(:any
) - 要根據其排序的預設欄。預設為可排序的第一個欄。預設為nil
。title
(:string
) (必須) - 表格標題。limit
(:any
) - 可設定為false
以停用limit
。預設為[50, 100, 500, 1000, 5000]
。search
(:boolean
) - 布林值,表示是否啟用搜尋功能。預設為true
。hint
(:string
) - 靠近標題顯示的文字提示。預設為nil
。dom_id
(:string
) - HTML 主標籤的 id 屬性。預設為nil
。
插槽
col
(必須) - 表格的欄。接受屬性field
(:atom
) (必須) - 欄的識別項。sortable
(:atom
) - 設定時,欄位標頭可按一下,且會根據新的順序再次擷取列。至少一個欄位必備。必須為:asc
或:desc
之一。header
(:string
) - 顯示在目前欄位的標籤。預設值從:field
計算。text_align
(:string
) - 欄中文字的文字對齊。預設:nil
。必須是"left"
、"center"
、"right"
或"justify"
之一。
@spec row(assigns :: Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
列組件。
您可以在首頁和 OS 資料頁面上看到其用途。
插槽
col
(必須) - 元件清單。它最多可以接收 3 個元件。每個元素都會是一個欄位。
@spec usage_card(assigns :: Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
使用量卡片組件。
您可以在首頁和 OS 資料頁面上看到其用途。
屬性
title
(:string
) - 卡片上方的標題。預設為nil
。hint
(:string
) - 靠近標題顯示的文字提示。預設為nil
。dom_id
(:string
)(必填) - 此卡片中所有用量的唯一識別碼。csp_nonces
(:any
)(必填) - 複製的 CSP 非隨機數 (@csp_nonces
),用於安全地呈現頁面。
插槽
usage
(必填) - 要顯示的用量清單。接受屬性current
(:integer
)(必填) - 使用量的目前值。limit
(:integer
)(必填) - 用量的最大值。dom_id
(:string
)(必填) - 使用量的唯一識別碼,會串接至dom_id
。percent
(:string
) - 使用量的已用百分比。title
(:string
) - 用量的標題。hint
(:string
) - 靠近用量標題顯示的文字提示。
類型
呼叫回函
@callback handle_event(event :: binary(), unsigned_params(), socket :: Socket.t()) :: {:noreply, Socket.t()} | {:reply, map(), Socket.t()}
當事件被呼叫時,呼叫回呼函式。
請注意,show_info
事件會自動由 Phoenix.LiveDashboard.PageBuilder
處理,但 info
參數 (phx-value-info
) 需要使用某一個 encode_*
輔助函式進行編碼。
更多詳情,請參閱 Phoenix.LiveView bindings
@callback handle_info(msg :: term(), socket :: Socket.t()) :: {:noreply, Socket.t()}
@callback handle_params(unsigned_params(), uri :: String.t(), socket :: Socket.t()) :: {:noreply, Socket.t()}
@callback handle_refresh(socket :: Socket.t()) :: {:noreply, Socket.t()}
當啟用自動重新整理時,呼叫回呼函式。
@callback init(term()) :: {:ok, session()} | {:ok, session(), requirements()}
當路由器中宣告某個網頁時,呼叫回呼函式。
它接收路由器選項,並且必須傳回元組 {:ok, session, requirements}
。
頁面工作階段會序列化至用戶端,而且會在 mount
中接收。
需求是一組可選的關鍵字,用於偵測節點狀態。
這種偵測的結果會在 menu_link/2
回呼中傳遞為第二個參數。可能的數值是
:applications
執行中或未執行的應用程式清單。:modules
已載入或未載入的模組清單。:pids
運作中或未運作中的程序清單。
@callback mount(unsigned_params(), session(), socket :: Socket.t()) :: {:ok, Socket.t()} | {:ok, Socket.t(), keyword()}
@callback render(assigns :: Socket.assigns()) :: Phoenix.LiveView.Rendered.t()
函式
對 URL 進行應用程式編碼。
範例
此函式可將應用程式編碼為事件值
<button phx-click="show-info" phx-value-info=<%= encode_app(@my_app) %>/>
對 URL 進行 ETS 參考編碼。
範例
此函式可將 ETS 參考編碼為事件值
<button phx-click="show-info" phx-value-info=<%= encode_ets(@reference) %>/>
對 URL 進行 PID 編碼。
範例
此函式可將 PID 編碼為事件值
<button phx-click="show-info" phx-value-info=<%= encode_pid(@pid) %>/>
對 URL 進行 Port 編碼。
範例
此函式可將 Port 編碼為事件值
<button phx-click="show-info" phx-value-info=<%= encode_port(@port) %>/>
對 URL 進行 Sockets 編碼。
範例
此函式可將 @socket
編碼為事件值
<button phx-click="show-info" phx-value-info=<%= encode_socket(@socket) %>/>
@spec live_dashboard_path( Socket.t(), page :: %Phoenix.LiveDashboard.PageBuilder{ allow_destructive_actions: term(), info: term(), module: term(), node: term(), params: term(), route: term(), tick: term() } ) :: binary()
計算路由器路徑到目前的網頁。
@spec live_dashboard_path( Socket.t(), page :: %Phoenix.LiveDashboard.PageBuilder{ allow_destructive_actions: term(), info: term(), module: term(), node: term(), params: term(), route: term(), tick: term() }, map() | Keyword.t() ) :: binary()
計算路由器路徑到目前的網頁,但參數已合併。