Skip to content

Attributes + dependencies #15

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 1 commit into from
Mar 25, 2014
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ The goal of this project is to provide a thin-but-idiomatic-scala interface to m
Extensions
----------

Apart from `Color`, Scala-js-dom contains some useful helpers in `org.scalajs.dom.extensions` that serve no purpose other than to make your use of the DOM more pleasant. Examples include the `Ajax.get` and `Ajax.post` methods which let you avoid messing with `dom.XMLHttpRequest` directly, or `KeyCodes` which provides a nice list of the keycodes that result from pressing various keys on the keyboard.
Apart from `Color`, Scala-js-dom contains some useful helpers and implicit classes in `org.scalajs.dom.extensions` that serve no purpose other than to make your use of the DOM more pleasant.
Examples include the `Ajax.get` and `Ajax.post` methods which let you avoid messing with `dom.XMLHttpRequest` directly, or `KeyCodes` which provides a nice list of the keycodes that result from pressing various keys on the keyboard.

Usage
-----
Expand Down
6 changes: 4 additions & 2 deletions project/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
resolvers += Resolver.url("scala-js-releases", url("http://dl.bintray.com/content/scala-js/scala-js-releases"))( Resolver.ivyStylePatterns) //for scalatools

addSbtPlugin("me.lessis" % "bintray-sbt" % "0.1.1")

addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.4.0")
addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.4.1")

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.1")
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
42 changes: 42 additions & 0 deletions src/main/scala/org/scalajs/dom/extensions/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.scalajs.dom

import scala.scalajs.js
import org.scalajs.dom
import scala.collection.mutable

package object extensions {

Expand Down Expand Up @@ -71,4 +72,45 @@ package object extensions {
}
}


/**
* Implicit class to deal with attributes as with normal mutable Map
* @param attributes
*/
implicit class Attributes(attributes:NamedNodeMap) extends mutable.Map[String,Attr] {
self =>

override def iterator: Iterator[(String, Attr)] = new Iterator[(String, Attr)] {
var index = 0


override def next(): (String, Attr) = {
val n: Attr = attributes.item(index)
this.index = this.index + 1
(n.name, n)
}

override def hasNext: Boolean = index < self.length
}


override def get(key: String): Option[Attr] = attributes.getNamedItem(key) match {
case null => None
case attr => Some(attr)
}

def length: Int = attributes.length.toInt


override def -=(key: String) = {
attributes.removeNamedItem(key)
this
}

override def +=(kv: (String, Attr)) = {
attributes.setNamedItem(kv._2)
this}
}


}