Skip to content

Dan select component #1048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ This is considered a Supervised Learning problem, because we have a labeled data

### Training Data

First things first, we need to record some user clicks on our search results. We'll create a new table to store our training data, which are the observed inputs and output of our new relevance function. In a real system, we'd probably have separate tables to record **sessions**, **searches**, **results**, **clicks** and other events, but for simplicity in this example, we'll just record the exact information we need to train our model in a single table. Everytime we perform a search, we'll record the `ts_rank` for the both the **title** and **body**, and whether the user **clicked** on the result.
First things first, we need to record some user clicks on our search results. We'll create a new table to store our training data, which are the observed inputs and output of our new relevance function. In a real system, we'd probably have separate tables to record **sessions**, **searches**, **results**, **clicks** and other events, but for simplicity in this example, we'll just record the exact information we need to train our model in a single table. Everytime we perform a search, we'll record the `ts_rank` for both the **title** and **body**, and whether the user **clicked** on the result.

!!! generic

Expand Down
7 changes: 6 additions & 1 deletion pgml-dashboard/src/components/dropdown/dropdown.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
display: flex;
justify-content: space-between;
font-weight: $font-weight-normal;
padding: 16px 20px;

--bs-btn-border-color: transparent;
--bs-btn-border-width: 1px;
Expand All @@ -48,6 +49,10 @@
--bs-btn-active-color: #{$gray-100};
--bs-btn-hover-color: #{$gray-100};

&.error {
border-color: #{$error};
}

.material-symbols-outlined {
color: #{$neon-shade-100};
}
Expand All @@ -73,7 +78,7 @@
}

.menu-item {
a {
a, div {
padding: 8px 12px;
overflow: hidden;
text-overflow: ellipsis;
Expand Down
46 changes: 40 additions & 6 deletions pgml-dashboard/src/components/dropdown/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::components::navigation::dropdown_link::DropdownLink;
use crate::components::stimulus::stimulus_target::StimulusTarget;
use pgml_components::component;
use pgml_components::Component;
use sailfish::TemplateOnce;
Expand All @@ -21,35 +23,57 @@ pub struct Dropdown {
/// The currently selected value.
value: DropdownValue,

/// The list of dropdown links to render.
links: Vec<StaticNavLink>,
/// The list of dropdown items to render.
items: Vec<Component>,

/// Position of the dropdown menu.
offset: String,

/// Whether or not the dropdown is collapsble.
/// Whether or not the dropdown is collapsable.
collapsable: bool,
offset_collapsed: String,

/// Where the dropdown menu should appear
menu_position: String,
expandable: bool,

/// target to control value
value_target: StimulusTarget,
}

impl Dropdown {
pub fn new(links: Vec<StaticNavLink>) -> Self {
pub fn new() -> Self {
Dropdown {
items: Vec::new(),
value: DropdownValue::Text("Dropdown".to_owned().into()),
offset: "0, 10".to_owned(),
offset_collapsed: "68, -44".to_owned(),
menu_position: "".to_owned(),
..Default::default()
}
}

pub fn nav(links: Vec<StaticNavLink>) -> Self {
let binding = links
.iter()
.filter(|link| link.active)
.collect::<Vec<&StaticNavLink>>();

let active = binding.first();
let value = if let Some(active) = active {
active.name.to_owned()
} else {
"Menu".to_owned()
"Dropdown Nav".to_owned()
};

let mut items = Vec::new();
for link in links {
let item = DropdownLink::new(link);
items.push(item.into());
}

Dropdown {
links,
items,
value: DropdownValue::Text(value.into()),
offset: "0, 10".to_owned(),
offset_collapsed: "68, -44".to_owned(),
Expand All @@ -58,6 +82,11 @@ impl Dropdown {
}
}

pub fn items(mut self, items: Vec<Component>) -> Self {
self.items = items;
self
}

pub fn text(mut self, value: Component) -> Self {
self.value = DropdownValue::Text(value);
self
Expand Down Expand Up @@ -97,6 +126,11 @@ impl Dropdown {
self.expandable = true;
self
}

pub fn value_target(mut self, value_target: StimulusTarget) -> Self {
self.value_target = value_target;
self
}
}

component!(Dropdown);
14 changes: 4 additions & 10 deletions pgml-dashboard/src/components/dropdown/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
data-bs-toggle="dropdown"
data-bs-offset="<%= offset %>"
aria-expanded="false">
<span class="btn-dropdown-text"><%+ text %></span>
<span class="btn-dropdown-text" <%- value_target %>><%+ text %></span>
<span class="material-symbols-outlined">
expand_more
</span>
Expand All @@ -41,15 +41,9 @@
</div>
<% } %>

<ul class="dropdown-menu <%= menu_position %>">
<% for link in links { %>
<li class="menu-item d-flex align-items-center <% if link.disabled { %>disabled<% } %>">
<% if link.disabled { %>
<a type="button" class="dropdown-item" disabled href="#"><%= link.name %></a>
<% } else { %>
<a type="button" class="dropdown-item" href="<%- link.href %>"><%= link.name %></a>
<% } %>
</li>
<ul class="dropdown-menu overflow-auto <%= menu_position %>">
<% for item in items { %>
<%+ item %>
<% } %>
</ul>
</div>
Expand Down
4 changes: 4 additions & 0 deletions pgml-dashboard/src/components/inputs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
pub mod range_group;
pub use range_group::RangeGroup;

// src/components/inputs/select
pub mod select;
pub use select::Select;

// src/components/inputs/text
pub mod text;
111 changes: 111 additions & 0 deletions pgml-dashboard/src/components/inputs/select/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use crate::components::stimulus::stimulus_action::{StimulusAction, StimulusEvents};
use pgml_components::component;
use pgml_components::Component;
use sailfish::TemplateOnce;

#[derive(TemplateOnce, Default)]
#[template(path = "inputs/select/template.html")]
pub struct Select {
options: Vec<Component>,
value: String,
offset: String,
collapsable: bool,
offset_collapsed: String,
menu_position: String,
expandable: bool,
name: String,
}

impl Select {
pub fn new() -> Select {
Select {
options: Vec::new(),
value: "Select".to_owned(),
offset: "0, 10".to_owned(),
offset_collapsed: "68, -44".to_owned(),
menu_position: "".to_owned(),
name: "input_name".to_owned(),
..Default::default()
}
.options(vec![
"option1".to_owned(),
"option2".to_owned(),
"option3".to_owned(),
])
}

pub fn options(mut self, values: Vec<String>) -> Self {
let mut options = Vec::new();
self.value = values.first().unwrap().to_owned();

for value in values {
let item = Option::new(
value,
StimulusAction::new()
.controller("inputs-select")
.method("choose")
.action(StimulusEvents::Click),
);
options.push(item.into());
}

self.options = options;
self
}

pub fn name(mut self, name: &str) -> Self {
self.name = name.to_owned();
self
}

pub fn text(mut self, value: String) -> Self {
self.value = value;
self
}

pub fn collapsable(mut self) -> Self {
self.collapsable = true;
self
}

pub fn menu_end(mut self) -> Self {
self.menu_position = "dropdown-menu-end".to_owned();
self
}

pub fn menu_start(mut self) -> Self {
self.menu_position = "dropdown-menu-start".to_owned();
self
}

pub fn offset(mut self, offset: &str) -> Self {
self.offset = offset.to_owned();
self
}

pub fn offset_collapsed(mut self, offset: &str) -> Self {
self.offset_collapsed = offset.to_owned();
self
}

pub fn expandable(mut self) -> Self {
self.expandable = true;
self
}
}

#[derive(TemplateOnce)]
#[template(path = "inputs/select/option.html")]
pub struct Option {
value: String,
action: StimulusAction,
}

impl Option {
pub fn new(value: String, action: StimulusAction) -> Self {
Option { value, action }
}
}

component!(Option);
component!(Select);
4 changes: 4 additions & 0 deletions pgml-dashboard/src/components/inputs/select/option.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

<li class="menu-item d-flex align-items-center">
<button type="button" class="dropdown-item" data-action="<%- action %>"><%= value %></button>
</li>
3 changes: 3 additions & 0 deletions pgml-dashboard/src/components/inputs/select/select.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
div[data-controller="inputs-select"] {

}
11 changes: 11 additions & 0 deletions pgml-dashboard/src/components/inputs/select/select_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static targets = ["input", "value"]

choose(e) {
this.inputTarget.value = e.target.innerHTML
this.valueTarget.innerHTML = e.target.innerHTML
}

}
33 changes: 33 additions & 0 deletions pgml-dashboard/src/components/inputs/select/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%
use crate::components::dropdown::Dropdown;
use crate::components::stimulus::stimulus_target::StimulusTarget;
%>
<div data-controller="inputs-select">

<% let mut dropdown = Dropdown::new()
.items(options)
.offset(&offset)
.offset_collapsed(&offset_collapsed)
.text(value.clone().into());

if menu_position == "dropdown-menu-end" {
dropdown = dropdown.menu_end();
} else if menu_position == "dropdown-menu-start" {
dropdown = dropdown.menu_start();
}

if collapsable {
dropdown = dropdown.collapsable();
}

if expandable {
dropdown = dropdown.expandable();
}

dropdown = dropdown.value_target(StimulusTarget::new().controller("inputs-select").name("value"));
%>

<%+ dropdown %>

<input type="hidden" name="<%= name %>" value="<%= value %>" data-inputs-select-target="input">
</div>
40 changes: 1 addition & 39 deletions pgml-dashboard/src/components/inputs/text/editable_header/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::stimulus::stimulus_target::StimulusTarget;
use pgml_components::component;
use sailfish::runtime::{Buffer, Render};
use sailfish::TemplateOnce;
use std::fmt;

Expand All @@ -25,44 +25,6 @@ impl fmt::Display for Headers {
}
}

pub struct StimulusTarget {
controller: Option<String>,
target_name: Option<String>,
}

impl StimulusTarget {
pub fn new() -> StimulusTarget {
StimulusTarget {
controller: None,
target_name: None,
}
}

pub fn controller(mut self, controller: &str) -> Self {
self.controller = Some(controller.to_string());
self
}

pub fn target_name(mut self, target_name: &str) -> Self {
self.target_name = Some(target_name.to_string());
self
}
}

impl Render for StimulusTarget {
fn render(&self, b: &mut Buffer) -> Result<(), sailfish::RenderError> {
if self.controller.is_none() || self.target_name.is_none() {
return format!("").render(b);
}
format!(
"data-{}-target=\"{}\"",
self.controller.to_owned().unwrap(),
self.target_name.to_owned().unwrap()
)
.render(b)
}
}

#[derive(TemplateOnce)]
#[template(path = "inputs/text/editable_header/template.html")]
pub struct EditableHeader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= value %>
</span>

<input type="text" class="form-control" value="<%= value %>" style="display: none" maxlength="50"
<input type="text" class="form-control" value="<%= value %>" style="display: none" maxlength="50" autocomplete="off"
name='<%= input_name.unwrap_or_else(|| "".to_string()) %>'
data-inputs-text-editable-header-target="input"
<%- input_target %> >
Expand Down
Loading