|
| 1 | +use crate::Form; |
| 2 | +use leptos::prelude::*; |
| 3 | +use leptos::web_sys; |
| 4 | +use leptos_struct_table::*; |
| 5 | + |
| 6 | +const ROW_HEIGHT: usize = 30; |
| 7 | +const ROW_HEIGHT_HALF: usize = ROW_HEIGHT / 2; |
| 8 | + |
| 9 | +wrapper_render_fn!( |
| 10 | + /// g |
| 11 | + GRenderer, |
| 12 | + g, |
| 13 | +); |
| 14 | + |
| 15 | +#[allow(non_snake_case)] |
| 16 | +pub fn SvgTbodyRenderer( |
| 17 | + content: impl IntoView, |
| 18 | + class: Signal<String>, |
| 19 | + body_ref: BodyRef, |
| 20 | +) -> impl IntoView { |
| 21 | + view! { <g class=class use:body_ref>{content}</g> } |
| 22 | +} |
| 23 | + |
| 24 | +#[allow(unused_variables, non_snake_case)] |
| 25 | +pub fn SvgRowRenderer( |
| 26 | + class: Signal<String>, |
| 27 | + row: RwSignal<Form>, |
| 28 | + index: usize, |
| 29 | + selected: Signal<bool>, |
| 30 | + on_select: EventHandler<web_sys::MouseEvent>, |
| 31 | + // Columns to show and their order. |
| 32 | + columns: RwSignal<Vec<usize>>, |
| 33 | +) -> impl IntoView { |
| 34 | + let transform = y_transform_from_index(index); |
| 35 | + |
| 36 | + view! { |
| 37 | + <g |
| 38 | + class=class |
| 39 | + transform=transform |
| 40 | + on:click=move |mouse_event| on_select.run(mouse_event) |
| 41 | + > |
| 42 | + <line |
| 43 | + x1="5" |
| 44 | + y1="0" |
| 45 | + x2="150" |
| 46 | + y2="0" |
| 47 | + stroke-width="1px" |
| 48 | + stroke="black" |
| 49 | + opacity="0.1" |
| 50 | + ></line> |
| 51 | + {TableRow::render_row(row, index, columns)} |
| 52 | + </g> |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn y_transform_from_index(index: usize) -> String { |
| 57 | + format!("translate(0, {})", (index + 1) * ROW_HEIGHT) |
| 58 | +} |
| 59 | + |
| 60 | +#[allow(non_snake_case)] |
| 61 | +pub fn SvgErrorRowRenderer(err: String, index: usize, _col_count: usize) -> impl IntoView { |
| 62 | + let transform = y_transform_from_index(index); |
| 63 | + |
| 64 | + view! { |
| 65 | + <g transform=transform> |
| 66 | + <text x="0" y=ROW_HEIGHT_HALF dominant-baseline="central"> |
| 67 | + {err} |
| 68 | + </text> |
| 69 | + </g> |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[allow(non_snake_case, unstable_name_collisions)] |
| 74 | +pub fn SvgLoadingRowRenderer( |
| 75 | + class: Signal<String>, |
| 76 | + _get_cell_class: Callback<(usize,), String>, |
| 77 | + get_inner_cell_class: Callback<(usize,), String>, |
| 78 | + index: usize, |
| 79 | + _col_count: usize, |
| 80 | +) -> impl IntoView { |
| 81 | + let transform = y_transform_from_index(index); |
| 82 | + |
| 83 | + view! { |
| 84 | + <g class=class transform=transform> |
| 85 | + <text x="0" y=ROW_HEIGHT_HALF class=get_inner_cell_class.run((0,)) dominant-baseline="central"> |
| 86 | + Loading... |
| 87 | + </text> |
| 88 | + </g> |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[component] |
| 93 | +#[allow(unused_variables)] |
| 94 | +pub fn SvgHeadCellRenderer<F, Column>( |
| 95 | + /// The class attribute for the head element. Generated by the classes provider. |
| 96 | + #[prop(into)] |
| 97 | + class: Signal<String>, |
| 98 | + /// The class attribute for the inner element. Generated by the classes provider. |
| 99 | + #[prop(into)] |
| 100 | + inner_class: String, |
| 101 | + /// The index of the column. Starts at 0 for the first column. The order of the columns is the same as the order of the fields in the struct. |
| 102 | + index: usize, |
| 103 | + /// The sort priority of the column. `None` if the column is not sorted. `0` means the column is the primary sort column. |
| 104 | + #[prop(into)] |
| 105 | + sort_priority: Signal<Option<usize>>, |
| 106 | + /// The sort direction of the column. See [`ColumnSort`]. |
| 107 | + #[prop(into)] |
| 108 | + sort_direction: Signal<ColumnSort>, |
| 109 | + /// The event handler for the click event. Has to be called with [`TableHeadEvent`]. |
| 110 | + on_click: F, |
| 111 | + children: Children, |
| 112 | + /// unused here |
| 113 | + drag_state: DragStateRwSignal<Column>, |
| 114 | + drag_handler: HeadDragHandler<Column>, |
| 115 | + columns: RwSignal<Vec<Column>>, |
| 116 | +) -> impl IntoView |
| 117 | +where |
| 118 | + F: Fn(TableHeadEvent<usize>) + 'static, |
| 119 | +{ |
| 120 | + let style = default_th_sorting_style(sort_priority, sort_direction); |
| 121 | + |
| 122 | + let transform = transform_from_index(index, 0); |
| 123 | + |
| 124 | + view! { |
| 125 | + <g |
| 126 | + class=class |
| 127 | + transform=transform |
| 128 | + on:click=move |mouse_event| on_click(TableHeadEvent { |
| 129 | + index, |
| 130 | + mouse_event, |
| 131 | + }) |
| 132 | + |
| 133 | + style=style |
| 134 | + > |
| 135 | + <text x="0" y=ROW_HEIGHT_HALF class=inner_class dominant-baseline="central"> |
| 136 | + {children()} |
| 137 | + </text> |
| 138 | + </g> |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +#[component] |
| 143 | +#[allow(unused_variables)] |
| 144 | +pub fn SvgTextCellRenderer<T>( |
| 145 | + class: String, |
| 146 | + value: Signal<T>, |
| 147 | + row: RwSignal<Form>, |
| 148 | + index: usize, |
| 149 | +) -> impl IntoView |
| 150 | +where |
| 151 | + T: IntoView + Clone + Send + Sync + 'static, |
| 152 | +{ |
| 153 | + let x = x_from_index(index); |
| 154 | + |
| 155 | + view! { |
| 156 | + <text x=x y=ROW_HEIGHT_HALF class=class dominant-baseline="central"> |
| 157 | + {value} |
| 158 | + </text> |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +#[component] |
| 163 | +#[allow(unused_variables)] |
| 164 | +pub fn SvgPathCellRenderer( |
| 165 | + #[prop(into)] class: String, |
| 166 | + value: Signal<String>, |
| 167 | + row: RwSignal<Form>, |
| 168 | + index: usize, |
| 169 | +) -> impl IntoView { |
| 170 | + let transform = transform_from_index(index, 3); |
| 171 | + |
| 172 | + view! { <path transform=transform class=class d=value></path> } |
| 173 | +} |
| 174 | + |
| 175 | +fn transform_from_index(index: usize, y: usize) -> String { |
| 176 | + format!("translate({}, {y})", x_from_index(index)) |
| 177 | +} |
| 178 | + |
| 179 | +fn x_from_index(index: usize) -> usize { |
| 180 | + 5 + index * 100 |
| 181 | +} |
0 commit comments