Skip to content

Commit 14c4e7c

Browse files
committed
Merge pull request #15 from antonkulaga/master
Attributes + dependencies
2 parents a92de2f + 4d5d941 commit 14c4e7c

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ The goal of this project is to provide a thin-but-idiomatic-scala interface to m
2121
Extensions
2222
----------
2323

24-
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.
24+
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.
25+
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.
2526

2627
Usage
2728
-----

project/build.sbt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
resolvers += Resolver.url("scala-js-releases", url("http://dl.bintray.com/content/scala-js/scala-js-releases"))( Resolver.ivyStylePatterns) //for scalatools
2+
13
addSbtPlugin("me.lessis" % "bintray-sbt" % "0.1.1")
24

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

5-
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.1")
7+
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")

src/main/scala/org/scalajs/dom/extensions/package.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.scalajs.dom
22

33
import scala.scalajs.js
44
import org.scalajs.dom
5+
import scala.collection.mutable
56

67
package object extensions {
78

@@ -71,4 +72,45 @@ package object extensions {
7172
}
7273
}
7374

75+
76+
/**
77+
* Implicit class to deal with attributes as with normal mutable Map
78+
* @param attributes
79+
*/
80+
implicit class Attributes(attributes:NamedNodeMap) extends mutable.Map[String,Attr] {
81+
self =>
82+
83+
override def iterator: Iterator[(String, Attr)] = new Iterator[(String, Attr)] {
84+
var index = 0
85+
86+
87+
override def next(): (String, Attr) = {
88+
val n: Attr = attributes.item(index)
89+
this.index = this.index + 1
90+
(n.name, n)
91+
}
92+
93+
override def hasNext: Boolean = index < self.length
94+
}
95+
96+
97+
override def get(key: String): Option[Attr] = attributes.getNamedItem(key) match {
98+
case null => None
99+
case attr => Some(attr)
100+
}
101+
102+
def length: Int = attributes.length.toInt
103+
104+
105+
override def -=(key: String) = {
106+
attributes.removeNamedItem(key)
107+
this
108+
}
109+
110+
override def +=(kv: (String, Attr)) = {
111+
attributes.setNamedItem(kv._2)
112+
this}
113+
}
114+
115+
74116
}

0 commit comments

Comments
 (0)