Skip to content

Commit

Permalink
Add minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Etschbeijer committed Dec 11, 2024
1 parent 488a75b commit dc3e485
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/Client/MainComponents/Widgets.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module private MoveEventListener =

open Fable.Core.JsInterop

let ensurePositionInsideWindow (element:IRefValue<HTMLElement option>) (position: Rect) =
let ensurePositionInsideWindow (element: IRefValue<HTMLElement option>) (position: Rect) =
let maxX = Browser.Dom.window.innerWidth - element.current.Value.offsetWidth;
let tempX = position.X
let newX = System.Math.Min(System.Math.Max(tempX,0),int maxX)
Expand All @@ -43,18 +43,18 @@ module private MoveEventListener =
let newY = System.Math.Min(System.Math.Max(tempY,0),int maxY)
{X = newX; Y = newY}

let calculatePosition (element:IRefValue<HTMLElement option>) (startPosition: Rect) = fun (e: Event) ->
let calculatePosition (element: IRefValue<HTMLElement option>) (startPosition: Rect) = fun (e: Event) ->
let e : MouseEvent = !!e
let tempX = int e.clientX - startPosition.X
let tempY = int e.clientY - startPosition.Y
let tempPosition = {X = tempX; Y = tempY}
ensurePositionInsideWindow element tempPosition

let onmousemove (element:IRefValue<HTMLElement option>) (startPosition: Rect) setPosition = fun (e: Event) ->
let onmousemove (element: IRefValue<HTMLElement option>) (startPosition: Rect) setPosition = fun (e: Event) ->
let nextPosition = calculatePosition element startPosition e
setPosition (Some nextPosition)

let onmouseup (prefix,element:IRefValue<HTMLElement option>) onmousemove =
let onmouseup (prefix,element: IRefValue<HTMLElement option>) onmousemove =
Browser.Dom.document.removeEventListener("mousemove", onmousemove)
if element.current.IsSome then
let rect = element.current.Value.getBoundingClientRect()
Expand All @@ -77,7 +77,7 @@ module private ResizeEventListener =
let onmouseup (prefix, element: IRefValue<HTMLElement option>) onmousemove =
Browser.Dom.document.removeEventListener("mousemove", onmousemove)
if element.current.IsSome then
Size.write(prefix,{X = int element.current.Value.offsetWidth; Y = int element.current.Value.offsetHeight})
Size.write(prefix, {X = int element.current.Value.offsetWidth; Y = int element.current.Value.offsetHeight})

module private Elements =

Expand Down
4 changes: 3 additions & 1 deletion src/Client/Pages/ProtocolTemplates/ProtocolState.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ module Protocol =
let nextModel = {
model with
Model.ProtocolState.TemplateSelected = Some prot
Model.ProtocolState.TemplatesSelected = []
Model.PageState.SidebarPage = Routing.SidebarPage.Protocol
}
state, Cmd.ofMsg (UpdateModel nextModel)
| SelectProtocols prots ->
log "SelectProtocols"
let newProts = prots |> List.rev
let newProts = prots
let nextModel = {
model with
Model.ProtocolState.TemplateSelected = None
Model.ProtocolState.TemplatesSelected = newProts
Model.PageState.SidebarPage = Routing.SidebarPage.Protocol
}
Expand Down
46 changes: 35 additions & 11 deletions src/Client/Pages/ProtocolTemplates/SelectiveTemplateFromDB.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,45 @@ type SelectiveTemplateFromDBModal =
]

static member AddFromDBToTableButton (model: Model) selectionInformation importType useTemplateName dispatch =
let addTemplate (templatePot: Template option, selectedColumns) =
if model.ProtocolState.TemplateSelected.IsNone && model.ProtocolState.TemplatesSelected.Length = 0 then
let addTemplate (model: Model, selectedColumns) =
let template =
if model.ProtocolState.TemplateSelected.IsNone && model.ProtocolState.TemplatesSelected.Length = 0 then
failwith "No template selected!"
if model.ProtocolState.TemplateSelected.IsSome then
model.ProtocolState.TemplateSelected.Value
else
model.ProtocolState.TemplatesSelected.Head
SpreadsheetInterface.AddTemplate(template.Table, selectedColumns, importType, useTemplateName) |> InterfaceMsg |> dispatch
Html.div [
prop.className "join flex flex-row justify-center gap-2"
prop.children [
let isDisabled =
model.ProtocolState.TemplateSelected.IsSome || model.ProtocolState.TemplatesSelected.Length = 0
ModalElements.Button("Add template", addTemplate, (model, selectionInformation.Columns), isDisabled)
if model.ProtocolState.TemplateSelected.IsSome || model.ProtocolState.TemplatesSelected.Length > 0 then
Daisy.button.a [
button.outline
prop.onClick (fun _ -> Protocol.RemoveSelectedProtocol |> ProtocolMsg |> dispatch)
button.error
Html.i [prop.className "fa-solid fa-times"] |> prop.children
]
]
]

static member AddTemplatesFromDBToTableButton (model: Model) selectionInformation importType useTemplateName dispatch =
let addTemplate (model: Model, selectedColumns) =
let templates = model.ProtocolState.TemplatesSelected
if templates.Length = 0 then
failwith "No template selected!"
if templatePot.IsSome || model.ProtocolState.TemplatesSelected.Length = 1 then
let table = templatePot.Value.Table
SpreadsheetInterface.AddTemplate(table, selectedColumns, importType, useTemplateName) |> InterfaceMsg |> dispatch
if model.ProtocolState.TemplatesSelected.Length > 1 then
let table = templatePot.Value.Table
SpreadsheetInterface.AddTemplate(table, selectedColumns, importType, useTemplateName) |> InterfaceMsg |> dispatch
let reversedTables = templates |> List.rev |> List.map (fun item -> item.Table) |> Array.ofList
SpreadsheetInterface.AddTemplates(reversedTables, selectedColumns, importType, useTemplateName) |> InterfaceMsg |> dispatch
Html.div [
prop.className "join flex flex-row justify-center gap-2"
prop.children [
let isDisabled =
model.ProtocolState.TemplateSelected.IsSome || model.ProtocolState.TemplatesSelected.Length = 0
ModalElements.Button("Add template", addTemplate, (model.ProtocolState.TemplateSelected, selectionInformation.Columns), isDisabled)
ModalElements.Button("Add template", addTemplate, (model, selectionInformation.Columns), isDisabled)
if model.ProtocolState.TemplateSelected.IsSome || model.ProtocolState.TemplatesSelected.Length > 0 then
Daisy.button.a [
button.outline
Expand All @@ -93,7 +117,6 @@ type SelectiveTemplateFromDBModal =
let selectedColumns, setSelectedColumns = React.useState(SelectedColumns.init length)
let importTypeState, setImportTypeState = React.useState(SelectiveImportModalState.init)
let useTemplateName, setUseTemplateName = React.useState(AdaptTableName.init)
log("model.ProtocolState.TemplatesSelected.Length", model.ProtocolState.TemplatesSelected.Length)
SidebarComponents.SidebarLayout.LogicContainer [
Html.div [
SelectiveTemplateFromDBModal.ToProtocolSearchElement model dispatch
Expand Down Expand Up @@ -132,8 +155,9 @@ type SelectiveTemplateFromDBModal =
SelectiveTemplateFromDBModal.displaySelectedProtocolElements(Some template, selectedColumns, setSelectedColumns, dispatch, false))
]
else if model.ProtocolState.TemplatesSelected.Length > 1 then
for i in 0..model.ProtocolState.TemplatesSelected.Length-1 do
let template = model.ProtocolState.TemplatesSelected.[i]
let templates = model.ProtocolState.TemplatesSelected |> List.rev
for i in 0..templates.Length-1 do
let template = templates.[i]
Html.div [
ModalElements.Box(
template.Name,
Expand Down
1 change: 1 addition & 0 deletions src/Client/States/SpreadsheetInterface.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Msg =
| AddDataAnnotation of {| fragmentSelectors: string []; fileName: string; fileType: string; targetColumn: DataAnnotator.TargetColumn |}
/// This function will do preprocessing on the table to join
| AddTemplate of ArcTable * bool[] * SelectiveImportModalState * string option
| AddTemplates of ArcTable[] * bool[] * SelectiveImportModalState * string option
| JoinTable of ArcTable * columnIndex: int option * options: TableJoinOptions option
| UpdateArcFile of ArcFiles
/// Inserts TermMinimal to selected fields of one column
Expand Down

0 comments on commit dc3e485

Please sign in to comment.