Permalink
Browse files

Fixed cancelling bug in AjaxAction. Major updates to DOM lookups in h…

…yperscala package. Release 2.1.10.
  • Loading branch information...
darkfrog26 committed Jan 26, 2017
1 parent c40c441 commit d914212d40bc7903aa625ab62c48af0ae3af264a
@@ -16,12 +16,16 @@ class AjaxAction(request: AjaxRequest) {
def cancelled: StateChannel[Boolean] = request.cancelled
private[ajax] def start(manager: AjaxManager): Unit = {
- _state := ActionState.Running
- future.onComplete { result =>
- _state := ActionState.Finished
+ if (!cancelled()) {
+ _state := ActionState.Running
+ future.onComplete { result =>
+ _state := ActionState.Finished
+ manager.remove(this)
+ }
+ request.send()
+ } else {
manager.remove(this)
}
- request.send()
}
// TODO: dequeue if not already running
@@ -2,29 +2,22 @@ package org
import com.outr.scribe.Logging
import org.hyperscala.manager.ClientConnection
-import org.scalajs.dom._
-import org.scalajs.dom.ext._
import org.scalajs.dom.raw.HTMLElement
+import org.scalajs.dom.{DOMList, Element, document, html}
import scala.language.implicitConversions
-package object hyperscala extends Logging {
- def byTag[T <: Element](tagName: String): Vector[T] = {
- document.getElementsByTagName(tagName).toVector.map(_.asInstanceOf[T])
- }
- def byClass[T <: Element](className: String): Vector[T] = {
- document.getElementsByClassName(className).toVector.map(_.asInstanceOf[T])
+package object hyperscala extends ExtendedElement(None) with Logging {
+ def bySelector[T <: Element](selectors: String, root: Option[Element] = None): Vector[T] = {
+ root.map(_.querySelectorAll(selectors)).getOrElse(document.querySelectorAll(selectors)).toVector.asInstanceOf[Vector[T]]
}
- def getById[T <: Element](id: String): Option[T] = Option(document.getElementById(id).asInstanceOf[T])
+ def firstBySelector[T <: Element](selectors: String, root: Option[Element] = None): Option[T] = {
+ bySelector[T](selectors, root).headOption
+ }
- def byId[T <: Element](id: String): T = getById[T](id) match {
- case Some(t) => t
- case None => {
- val message = s"Unable to find element by id '$id'."
- logger.error(message)
- throw new RuntimeException(message)
- }
+ def oneBySelector[T <: Element](selectors: String, root: Option[Element] = None): T = {
+ firstBySelector[T](selectors, root).getOrElse(throw new RuntimeException(s"Unable to find element by selector: $selectors."))
}
implicit def connection2ClientConnection(connection: Connection): ClientConnection = connection.asInstanceOf[ClientConnection]
@@ -38,13 +31,7 @@ package object hyperscala extends Logging {
}
}
- implicit class ElementExtras(e: Element) {
- def byTag[T <: Element](tagName: String): Vector[T] = {
- e.getElementsByTagName(tagName).toVector.map(_.asInstanceOf[T])
- }
- def byClass[T <: Element](className: String): Vector[T] = {
- e.getElementsByClassName(className).toVector.map(_.asInstanceOf[T])
- }
+ implicit class ElementExtras(e: Element) extends ExtendedElement(Some(e)) {
def parentByTag[T <: HTMLElement](tagName: String): Option[T] = findParentRecursive[T](e.asInstanceOf[HTMLElement], (p: HTMLElement) => {
p.tagName == tagName
})
@@ -73,4 +60,13 @@ package object hyperscala extends Logging {
list.item(position)
}
}
+}
+
+class ExtendedElement(element: Option[Element]) {
+ import hyperscala._
+
+ def byTag[T <: Element](tagName: String): Vector[T] = bySelector[T](tagName, element)
+ def byClass[T <: Element](className: String): Vector[T] = bySelector[T](s".$className", element)
+ def getById[T <: Element](id: String): Option[T] = firstBySelector[T](s"#$id", element)
+ def byId[T <: Element](id: String): T = oneBySelector[T](s"#$id", element)
}
@@ -120,7 +120,7 @@ object HyperscalaBuild extends Build {
object Details {
val organization = "org.hyperscala"
val name = "hyperscala"
- val version = "2.1.9"
+ val version = "2.1.10"
val url = "http://hyperscala.org"
val licenseType = "MIT"
val licenseURL = "http://opensource.org/licenses/MIT"

0 comments on commit d914212

Please sign in to comment.