Skip to content

Commit

Permalink
chore: Clean code.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongl committed Sep 18, 2024
1 parent 8c763e3 commit ad79852
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 71 deletions.
7 changes: 1 addition & 6 deletions src/Main.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import scala.scalajs.js

import org.scalajs.dom.HTMLElement
import org.scalajs.dom.MutationObserver
import org.scalajs.dom.MutationObserverInit
import org.scalajs.dom.URL
import org.scalajs.dom.document
import org.scalajs.dom.window
import org.scalajs.dom.*

import core.*
import garmin.*
Expand Down
30 changes: 17 additions & 13 deletions src/core/Initialize.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package core

import org.scalajs.dom.Element
import org.scalajs.dom.*
import org.scalajs.dom.HTMLDivElement as Div
import org.scalajs.dom.Node
import org.scalajs.dom.document

type Initialization = (String, Node => Unit)
trait Initialize[+A <: Element] extends (Initialization => A):
trait Initialize[+A <: Element]:
extension (id: String)
inline def elementAt(pos: Node => Unit) = this(id, pos)
inline def reset(): Unit = this(id, _ => ())
def elementAt(pos: Node => Unit): A
def reset(): Unit

object Initialize:
given html: Initialize[Div] = (id, pos) =>
Option(document.getElementById(id)).foreach(_.remove())
val a = document.createElement("div").asInstanceOf[Div]
a.id = id
pos(a)
a
given Initialize[Div] with
extension (id: String)
def reset(): Unit = init(id, _ => ())
def elementAt(pos: Node => Unit): Div = init(id, pos)

private inline def init(id: String, pos: Node => Unit) =
Option(document.getElementById(id)).foreach(_.remove())
val a = document.createElement("div").asInstanceOf[Div]
a.id = id
pos(a)
a
end given
end Initialize
3 changes: 1 addition & 2 deletions src/garmin/Activities.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package garmin

import java.util.NoSuchElementException as Complain

import org.scalajs.dom.HTMLElement
import org.scalajs.dom.document
import org.scalajs.dom.{URL as _, *}

import core.*
import sourcecode.Name
Expand Down
5 changes: 2 additions & 3 deletions src/garmin/Profile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ object Profile:
end page

private inline def isRunning(e: Element): Boolean =
val t = for
(for
case `class`(s"icon-activity-${t}") <- `i[class^="icon-activity-"]`(e)
if t.endsWith("running")
yield t
t.isDefined
yield t).isDefined
end isRunning

private val `class` = implicitly[Name].value |> Attribute
Expand Down
5 changes: 2 additions & 3 deletions src/garmin/Selector.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package garmin

import org.scalajs.dom.Element
import org.scalajs.dom.NodeSelector
import org.scalajs.dom.*

final class Selector[F[_]: FlatMapwise](expr: String):

Expand All @@ -11,6 +10,6 @@ final class Selector[F[_]: FlatMapwise](expr: String):
object all:
def apply(fa: F[NodeSelector]): Seq[Element] = fa.seq(s => s.querySelectorAll(expr))
def unapplySeq(fa: F[NodeSelector]): Seq[Element] = this(fa)
end Selector
object Selector:
def apply[F[_]: FlatMapwise]: String => Selector[F] = new Selector[F](_)
1 change: 0 additions & 1 deletion src/garmin/read/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package garmin.read
import scala.scalajs.js

import core.*
import core.Safety

given Read[Gauge.BeatPerMinute, js.Dynamic, Double] = Read:
Safety[Lap, Double](_.averageHR.round)
Expand Down
5 changes: 1 addition & 4 deletions src/plotly/Axis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package plotly
import scala.scalajs.js

import typings.plotlyJs.anon.PartialLayoutAxis
import typings.plotlyJs.plotlyJsStrings.array
import typings.plotlyJs.plotlyJsStrings.reversed
import typings.plotlyJs.plotlyJsStrings.right
import typings.plotlyJs.plotlyJsStrings.y2
import typings.plotlyJs.plotlyJsStrings.*

import core.*

Expand Down
3 changes: 1 addition & 2 deletions src/plotly/ColorPalette.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package plotly

import scala.language.implicitConversions

import typings.plotlyJs.anon.PartialLayout
import typings.plotlyJs.anon.PartialLayoutAxis
import typings.plotlyJs.anon.*

import convs.given

Expand Down
7 changes: 2 additions & 5 deletions src/plotly/CorrelatePlot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package plotly
import scala.language.implicitConversions
import scala.scalajs.js

import typings.plotlyJs.anon.PartialLayout
import typings.plotlyJs.anon.PartialLayoutAxis
import typings.plotlyJs.anon.PartialLegendBgcolor
import typings.plotlyJs.anon.PartialPlotDataAutobinx
import typings.plotlyJs.anon.*
import typings.plotlyJs.mod.Data
import typings.plotlyJs.plotlyJsStrings.legendonly
import typings.plotlyJs.plotlyJsStrings.*

import convs.given

Expand Down
14 changes: 2 additions & 12 deletions src/plotly/Event.scala
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
package plotly

import scala.language.implicitConversions


import typings.plotlyJs.mod.LegendClickEvent
import typings.plotlyJs.mod.PlotHoverEvent
import typings.plotlyJs.mod.PlotMouseEvent
import typings.plotlyJs.plotlyJsStrings.plotly_click
import typings.plotlyJs.plotlyJsStrings.plotly_hover
import typings.plotlyJs.plotlyJsStrings.plotly_legendclick
import typings.plotlyJs.plotlyJsStrings.plotly_unhover

import typings.plotlyJs.mod.*
import typings.plotlyJs.plotlyJsStrings.*

trait Event[A]:
type Data
def instance: A
object Event:
def apply[A](using e: Event[A]) = e

given Event[plotly_click] with
type Data = PlotMouseEvent
Expand Down
3 changes: 1 addition & 2 deletions src/plotly/Listen.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package plotly

import scala.language.implicitConversions
import scala.scalajs.js

import typings.plotlyJs.mod.PlotlyHTMLElement
Expand All @@ -13,7 +12,7 @@ object Listen:
inline def handler(d: js.Any): Unit = f(d.asInstanceOf[e.Data])
p.asInstanceOf[js.Dynamic].applyDynamic("on")(e.instance, handler)

given [T <: js.Any, B](using e: Event[T])(using Registry[B, e.Data]): Listen[T, B] =
given [T <: js.Any, A](using e: Event[T])(using Registry[A, e.Data]): Listen[T, A] =
(p, b) => Listen[T](p)(b.handler(p, _))

given [A]: Listen[EmptyTuple, A] = (_, _) => ()
Expand Down
3 changes: 1 addition & 2 deletions src/plotly/Registry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import scala.language.implicitConversions
import scala.scalajs.js

import typings.plotlyJs.mod.PlotlyHTMLElement
import typings.plotlyJsDistMin.mod.react
import typings.plotlyJsDistMin.mod.restyle
import typings.plotlyJsDistMin.mod.*

import convs.given

Expand Down
25 changes: 14 additions & 11 deletions src/plotly/Style.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package plotly

import scala.language.implicitConversions
import scala.scalajs.js

import org.scalajs.dom.Element
import org.scalajs.dom.HTMLElement

import typings.plotlyJs.anon.PartialPlotDataAutobinx
import typings.plotlyJs.anon.PartialScatterLine
import typings.plotlyJs.mod.Data
import typings.plotlyJs.mod.PlotHoverEvent
import typings.plotlyJs.mod.PlotMouseEvent
import typings.plotlyJs.anon.*
import typings.plotlyJs.mod.*

import core.*

Expand All @@ -19,12 +16,18 @@ object Style:
given [A]: Style[History[A], PlotHoverEvent] with
extension (a: History[A])
def style(e: HTMLElement): Data =
e.querySelector(".nsewdrag").asInstanceOf[HTMLElement].style.cursor = "pointer"
Data.PartialPlotDataAutobinx().setLine(PartialScatterLine().setWidth(2))
e.querySelector(".nsewdrag").cursor("pointer")
DataLineWidth(2)

given [A]: Style[History[A], PlotMouseEvent] with
extension (a: History[A])
def style(e: HTMLElement): Data =
e.querySelector(".nsewdrag").asInstanceOf[HTMLElement].style.cursor = ""
Data.PartialPlotDataAutobinx().setLine(PartialScatterLine().setWidth(1))
e.querySelector(".nsewdrag").cursor("")
DataLineWidth(1)

private inline def DataLineWidth(n: Int) =
Data.PartialPlotDataAutobinx().setLine(PartialScatterLine().setWidth(n))

extension (e: Element) private inline def cursor(value: String): Unit =
e.asInstanceOf[HTMLElement].style.cursor = value
end Style
7 changes: 2 additions & 5 deletions src/plotly/Trace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import scala.language.implicitConversions
import scala.scalajs.js

import typings.plotlyJs.anon.PartialScatterLine
import typings.plotlyJs.mod.Data
import typings.plotlyJs.mod.Datum
import typings.plotlyJs.mod.PlotType
import typings.plotlyJs.mod.{Gauge as _, *}
import typings.plotlyJs.plotlyJsBooleans.`false`
import typings.plotlyJs.plotlyJsStrings.y
import typings.plotlyJs.plotlyJsStrings.yPlussignname
import typings.plotlyJs.plotlyJsStrings.*

import convs.given
import core.*
Expand Down

0 comments on commit ad79852

Please sign in to comment.