Skip to content
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

Add LatestHTMLEvents.of #75

Merged
merged 3 commits into from
Nov 14, 2021
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
13 changes: 13 additions & 0 deletions LatestEvent/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enablePlugins(ScalaJSPlugin)

import Ordering.Implicits._

libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "1.2.0"

libraryDependencies += {
if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
"com.thoughtworks.binding" %%% "binding" % "12.1.0"
} else {
"com.thoughtworks.binding" %%% "binding" % "11.9.0"
}
}
35 changes: 35 additions & 0 deletions LatestHTMLEvents/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
enablePlugins(ScalaJSBundlerPlugin)

enablePlugins(ScalaJSPlugin)

enablePlugins(Example)

libraryDependencies += "org.scalatest" %%% "scalatest" % "3.2.10" % Test

libraryDependencies += "org.lrng.binding" %%% "html" % "1.0.3"

Test / requireJsDomEnv := true

import Ordering.Implicits._

publish / skip := VersionNumber(scalaBinaryVersion.value).numbers < Seq(2L, 12L)

// Enable macro annotations by setting scalac flags for Scala 2.13
scalacOptions ++= {
import Ordering.Implicits._
if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
Seq("-Ymacro-annotations")
} else {
Nil
}
}

// Enable macro annotations by adding compiler plugins for Scala 2.11 and 2.12
libraryDependencies ++= {
import Ordering.Implicits._
if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
Nil
} else {
Seq(compilerPlugin("org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.thoughtworks.binding

import org.scalajs.dom.raw.EventTarget
import org.scalajs.dom.Event
import org.lrng.binding.html.NodeBinding.Interpolated.MountPointBuilder
import scala.scalajs.js
import org.scalajs.dom.Element
import org.lrng.binding.html.AttributeFactory
import com.thoughtworks.binding.bindable.Bindable

/** Including [[LatestHTMLEvents.of]], which automatically infers the event type based on the type class defined in
* [[org.lrng.binding.html]]
*
* @example
* Given an HTML button element,
* {{{
* import org.lrng.binding.html, html.autoImports.xml.attributes
* import org.scalajs.dom.MouseEvent
* @html val myButton = <button>Click me!</button>
* }}}
*
* when creating the latest click event binding with the help of [[LatestHTMLEvents.of]],
*
* {{{
* val latestClickEvent = LatestHTMLEvents.of(myButton.value, attributes.onclick)
* }}}
*
* the event type should be inferred as [[org.scalajs.dom.MouseEvent]],
*
* {{{
* val _ = latestClickEvent: LatestEvent[MouseEvent]
* }}}
*
* and the `MouseEvent` properties, like `clientX`, can be used in the derived [[Binding]]s
*
* {{{
* def clientXBinding = Binding {
* val mouseEvent: Option[MouseEvent] = latestClickEvent.bind
* mouseEvent.map(_.clientX)
* }
* }}}
*/
object LatestHTMLEvents {

def of[E <: org.scalajs.dom.Element, AttributeObject <: AttributeFactory.Typed, InferredEvent <: Event](
target: E,
attribute: AttributeObject
)(implicit
mountPointBuilder: MountPointBuilder[E, AttributeObject, js.Function1[InferredEvent, _]]
): LatestEvent[InferredEvent] = {
val propertyName = attribute.getClass.getSimpleName
if (propertyName.startsWith("on") && propertyName.endsWith("$")) {
return new LatestEvent(target, propertyName.substring("on".length(), propertyName.length() - "$".length()))
} else {
throw new IllegalArgumentException(
"attribute must be an object defined in [[org.lrng.binding.html.autoImports.xml.attributes]] whose name starts with \"on\""
)
}
}
}
20 changes: 5 additions & 15 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
ThisBuild / organization := "com.thoughtworks.binding"

name := "LatestEvent"
ThisBuild / evictionErrorLevel := Level.Info

enablePlugins(ScalaJSPlugin)
lazy val LatestEvent = project

import Ordering.Implicits._
lazy val LatestHTMLEvents = project.dependsOn(LatestEvent)

if (VersionNumber(scalaJSVersion).numbers >= Seq(1L)) {
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "2.0.0"
} else {
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "1.2.0"
}
publish / skip := true

libraryDependencies += {
if (VersionNumber(scalaVersion.value).numbers >= Seq(2L, 13L)) {
"com.thoughtworks.binding" %%% "binding" % "12.1.0"
} else {
"com.thoughtworks.binding" %%% "binding" % "11.9.0"
}
}
enablePlugins(ScalaUnidocPlugin)
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ addSbtPlugin("com.thoughtworks.example" % "sbt-example" % "8.1.0")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.7.1")

addSbtPlugin("com.thoughtworks.sbt-scala-js-map" % "sbt-scala-js-map" % "4.1.0")

addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.20.0")
6 changes: 6 additions & 0 deletions project/plugins.sbt.scalajs06
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ dependencyOverrides += Defaults.sbtPluginExtra(
sbtBinaryVersion.value,
scalaBinaryVersion.value,
)

dependencyOverrides += Defaults.sbtPluginExtra(
"ch.epfl.scala" % "sbt-scalajs-bundler" % "0.15.0-0.6",
sbtBinaryVersion.value,
scalaBinaryVersion.value,
)