├── .gitignore ├── project ├── build.properties └── plugins.sbt ├── .scalafmt.conf ├── src └── main │ └── scala │ └── akka │ └── ui │ ├── package.scala │ ├── SourceBuilder.scala │ └── SinkBuilder.scala ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.log 3 | target/ 4 | -------------------------------------------------------------------------------- /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.2.8 2 | -------------------------------------------------------------------------------- /.scalafmt.conf: -------------------------------------------------------------------------------- 1 | version = 2.0.0-RC5 2 | align = none 3 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.24") 2 | 3 | addSbtPlugin("org.foundweekends" % "sbt-bintray" % "0.5.4") 4 | 5 | addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.0.0") 6 | -------------------------------------------------------------------------------- /src/main/scala/akka/ui/package.scala: -------------------------------------------------------------------------------- 1 | package akka 2 | 3 | import akka.stream._ 4 | import akka.stream.scaladsl._ 5 | import org.scalajs.dom.ext._ 6 | import org.scalajs.dom.raw._ 7 | import scala.language.experimental.macros 8 | 9 | package object ui { 10 | implicit class RichDOMTokenList(tokens: DOMTokenList) 11 | extends EasySeq[String](tokens.length, tokens.apply) 12 | 13 | val preventDefault = SourceBuilder.Config("preventDefault") 14 | val stopPropagation = SourceBuilder.Config("stopPropagation") 15 | 16 | implicit class SourceChainer[T <: EventTarget](t: T) { 17 | def source( 18 | eventType: String, 19 | configs: SourceBuilder.Config* 20 | ): Any = macro SourceBuilder.source[T] 21 | } 22 | 23 | implicit class SinkChainer[T <: Element](t: T) { 24 | def sink(property: String): Any = macro SinkBuilder.sink[T] 25 | def styleSink(style: String): Any = macro SinkBuilder.styleSink[T] 26 | 27 | def childrenSink( 28 | implicit materializer: Materializer 29 | ): Sink[Seq[Element], NotUsed] = SinkBuilder.childrenSink(t) 30 | 31 | def classSink( 32 | implicit materializer: Materializer 33 | ): Sink[Seq[String], NotUsed] = SinkBuilder.classSink(t) 34 | 35 | def dummySink( 36 | implicit materializer: Materializer 37 | ): Sink[Any, NotUsed] = SinkBuilder.dummySink(t) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/scala/akka/ui/SourceBuilder.scala: -------------------------------------------------------------------------------- 1 | package akka.ui 2 | 3 | import akka.NotUsed 4 | import akka.stream._ 5 | import akka.stream.scaladsl._ 6 | import org.scalajs.dom.raw._ 7 | import scala.collection.mutable 8 | import scala.reflect.macros.whitebox.Context 9 | import scala.scalajs.js 10 | 11 | object SourceBuilder { 12 | case class Config(flag: String) 13 | 14 | val sourceBindings = 15 | mutable.Map.empty[EventTarget, mutable.Map[ 16 | String, 17 | (SourceQueueWithComplete[_ <: Event], Source[_ <: Event, NotUsed]) 18 | ]] 19 | 20 | def build[E <: Event]( 21 | elem: EventTarget, 22 | eventName: String, 23 | setter: js.Function1[E, Unit] => Unit, 24 | configs: Config* 25 | )( 26 | implicit materializer: Materializer 27 | ): Source[E, NotUsed] = { 28 | def createHub() = { 29 | val (queue, source) = Source 30 | .queue[E](10, OverflowStrategy.dropNew) 31 | .toMat(BroadcastHub.sink)(Keep.both) 32 | .run() 33 | val hasPreventDefault = configs.contains(preventDefault) 34 | val hasStopPropagation = configs.contains(stopPropagation) 35 | setter { e => 36 | if (hasPreventDefault) { 37 | e.preventDefault() 38 | } 39 | if (hasStopPropagation) { 40 | e.stopPropagation() 41 | } 42 | queue.offer(e) 43 | } 44 | (queue, source) 45 | } 46 | val (_, source) = sourceBindings 47 | .getOrElseUpdate(elem, mutable.Map.empty) 48 | .getOrElseUpdate(eventName, createHub()) 49 | source.asInstanceOf[Source[E, NotUsed]] 50 | } 51 | 52 | def source[T: c.WeakTypeTag](c: Context)( 53 | eventType: c.Expr[String], 54 | configs: c.Expr[Config]* 55 | ): c.Expr[Any] = { 56 | import c.universe._ 57 | 58 | val q"$_[$_]($elem).$_(..$_)" = c.macroApplication 59 | 60 | val eventName = eventType.tree match { 61 | case Literal(Constant(s: String)) => s 62 | case _ => 63 | c.abort(c.enclosingPosition, "eventType must be a string literal.") 64 | } 65 | 66 | val listenerName = "on" + eventName 67 | val listenerTerm = TermName(listenerName) 68 | 69 | val E = { 70 | val listener = weakTypeOf[T].member(listenerTerm) 71 | if (listener.fullName == "") { 72 | c.abort( 73 | c.enclosingPosition, 74 | s"Couldn't find $listenerName listener on ${weakTypeOf[T]}." 75 | ) 76 | } else { 77 | listener.typeSignature.resultType.typeArgs.head 78 | } 79 | } 80 | 81 | val res = q""" 82 | akka.ui.SourceBuilder.build[$E]( 83 | $elem, 84 | $eventName, 85 | f => $elem.$listenerTerm = f, 86 | ..$configs 87 | ) 88 | """ 89 | 90 | //println(showCode(res)) 91 | c.Expr(res) 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/scala/akka/ui/SinkBuilder.scala: -------------------------------------------------------------------------------- 1 | package akka.ui 2 | 3 | import akka.NotUsed 4 | import akka.stream._ 5 | import akka.stream.scaladsl._ 6 | import org.scalajs.dom.ext._ 7 | import org.scalajs.dom.raw._ 8 | import scala.collection.mutable 9 | import scala.concurrent.ExecutionContext.Implicits.global 10 | import scala.reflect.macros.whitebox.Context 11 | 12 | object SinkBuilder { 13 | val sinkBindings = 14 | mutable.Map.empty[Element, mutable.Map[ 15 | String, 16 | (Sink[_, NotUsed], SinkQueueWithCancel[_]) 17 | ]] 18 | 19 | def build[V]( 20 | elem: Element, 21 | propertyName: String, 22 | setter: V => Unit 23 | )( 24 | implicit materializer: Materializer 25 | ): Sink[V, NotUsed] = { 26 | def createHub() = { 27 | val (sink, queue) = MergeHub 28 | .source[V] 29 | .toMat(Sink.queue[V])(Keep.both) 30 | .run() 31 | 32 | def pull(): Unit = { 33 | queue.pull().foreach { 34 | case Some(v) => 35 | setter(v) 36 | pull() 37 | case None => 38 | println("You should not come here.") 39 | } 40 | } 41 | pull() 42 | 43 | (sink, queue) 44 | } 45 | val (sink, _) = sinkBindings 46 | .getOrElseUpdate(elem, mutable.Map.empty) 47 | .getOrElseUpdate(propertyName, createHub()) 48 | sink.asInstanceOf[Sink[V, NotUsed]] 49 | } 50 | 51 | def sink[T: c.WeakTypeTag](c: Context)( 52 | property: c.Expr[String] 53 | ): c.Expr[Any] = { 54 | import c.universe._ 55 | 56 | val q"$_[$_]($elem).$_($_)" = c.macroApplication 57 | 58 | val propertyName = property.tree match { 59 | case Literal(Constant(s: String)) => s 60 | case _ => 61 | c.abort(c.enclosingPosition, "property must be a string literal.") 62 | } 63 | 64 | val propertyTerm = TermName(propertyName) 65 | 66 | val V = { 67 | val member = weakTypeOf[T].member(propertyTerm) 68 | if (member.fullName == "") { 69 | c.abort( 70 | c.enclosingPosition, 71 | s"Couldn't find $propertyName property on ${weakTypeOf[T]}." 72 | ) 73 | } else { 74 | member.typeSignature.resultType 75 | } 76 | } 77 | 78 | val res = q""" 79 | akka.ui.SinkBuilder.build[$V]( 80 | $elem, 81 | $propertyName, 82 | v => $elem.$propertyTerm = v 83 | ) 84 | """ 85 | 86 | //println(showCode(res)) 87 | c.Expr(res) 88 | } 89 | 90 | def styleSink[T: c.WeakTypeTag](c: Context)( 91 | style: c.Expr[String] 92 | ): c.Expr[Any] = { 93 | import c.universe._ 94 | 95 | val q"$_[$_]($elem).$_($_)" = c.macroApplication 96 | 97 | val styleName = style.tree match { 98 | case Literal(Constant(s: String)) => s 99 | case _ => 100 | c.abort(c.enclosingPosition, "style must be a string literal.") 101 | } 102 | 103 | val styleTerm = TermName(styleName) 104 | 105 | val V = { 106 | val member = weakTypeOf[T] 107 | .member(TermName("style")) 108 | .typeSignature 109 | .resultType 110 | .member(styleTerm) 111 | if (member.fullName == "") { 112 | c.abort( 113 | c.enclosingPosition, 114 | s"Couldn't find $styleName style on ${weakTypeOf[T]}." 115 | ) 116 | } else { 117 | member.typeSignature.resultType 118 | } 119 | } 120 | 121 | val res = q""" 122 | akka.ui.SinkBuilder.build[$V]( 123 | $elem, 124 | "style." + $styleName, 125 | v => $elem.style.$styleTerm = v 126 | ) 127 | """ 128 | 129 | //println(showCode(res)) 130 | c.Expr(res) 131 | } 132 | 133 | def childrenSink(parent: Element)( 134 | implicit materializer: Materializer 135 | ): Sink[Seq[Element], NotUsed] = { 136 | val setter = (children: Seq[Element]) => { 137 | //appendChild can relocate the elements which already exist 138 | children.foreach(child => parent.appendChild(child)) 139 | //remove the remaining children 140 | parent.children 141 | .dropRight(children.size) 142 | .foreach { child => 143 | // remove the bindings 144 | (child +: child.querySelectorAll("*")) 145 | .collect { case (e: Element) => e } 146 | .foreach { elem => 147 | SourceBuilder.sourceBindings 148 | .get(elem) 149 | .foreach { eventMap => 150 | eventMap.foreach { 151 | case (event, res) => 152 | //println(s"remove source binding for $event") 153 | res._1.complete() 154 | } 155 | SourceBuilder.sourceBindings -= elem 156 | } 157 | sinkBindings 158 | .get(elem) 159 | .foreach { propertyMap => 160 | propertyMap.foreach { 161 | case (property, res) => 162 | //println(s"remove sink binding for $property") 163 | res._2.cancel() 164 | } 165 | sinkBindings -= elem 166 | } 167 | } 168 | // remove the child 169 | parent.removeChild(child) 170 | } 171 | } 172 | 173 | build[Seq[Element]](parent, "$children", setter) 174 | } 175 | 176 | def classSink(elem: Element)( 177 | implicit materializer: Materializer 178 | ): Sink[Seq[String], NotUsed] = { 179 | val setter = (classes: Seq[String]) => { 180 | classes.foreach(elem.classList.add) 181 | elem.classList 182 | .filterNot(classes contains _) 183 | .foreach(elem.classList remove _) 184 | } 185 | 186 | build[Seq[String]](elem, "$class", setter) 187 | } 188 | 189 | def dummySink(elem: Element)( 190 | implicit materializer: Materializer 191 | ): Sink[Any, NotUsed] = { 192 | build[Any](elem, "$dummy", _ => ()) 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AkkaUI 2 | 3 | Build your reactive UI using [Akka.js](https://github.com/akka-js/akka.js) 4 | 5 | ``` scala 6 | import akka.ui._ 7 | 8 | implicit val system = ActorSystem("AkkaUI") 9 | implicit val materializer = ActorMaterializer() 10 | 11 | val textBox = input(placeholder := "Name").render 12 | val name = span().render 13 | 14 | val source: Source[Event, NotUsed] = textBox.source("input") 15 | val sink: Sink[String, NotUsed] = name.sink("textContent") 16 | 17 | source.map(_ => textBox.value).runWith(sink) 18 | 19 | val root = div( 20 | textBox, 21 | h2("Hello ", name) 22 | ) 23 | 24 | document.querySelector("#root").appendChild(root.render) 25 | ``` 26 | 27 | ### Installation 28 | 29 | ``` scala 30 | libraryDependencies += "net.pishen" %%% "akka-ui" % "0.5.0" 31 | ``` 32 | 33 | AkkaUI is built on top of [Scala.js](https://www.scala-js.org/), [Akka.js](https://github.com/akka-js/akka.js), and [scala-js-dom](https://github.com/scala-js/scala-js-dom). 34 | 35 | ### Setup Akka 36 | 37 | Since AkkaUI uses Akka Streams underneath, we have to provide an Akka environment for it: 38 | 39 | ``` scala 40 | implicit val system = ActorSystem("AkkaUI") 41 | implicit val materializer = ActorMaterializer() 42 | ``` 43 | 44 | ### Creating Sources 45 | 46 | AkkaUI supports creating a `Source` from an `EventTarget`: 47 | 48 | ``` scala 49 | import akka.ui._ 50 | import akka.stream.scaladsl.Source 51 | import org.scalajs.dom.raw.HTMLButtonElement 52 | import org.scalajs.dom.raw.MouseEvent 53 | import scalatags.JsDom.all._ 54 | 55 | val btn: HTMLButtonElement = button("Click me!").render 56 | val source: Source[MouseEvent, akka.NotUsed] = btn.source("click") 57 | ``` 58 | 59 | Here we use [Scalatags](https://github.com/lihaoyi/scalatags) to generate a `HTMLButtonElement` (which is also an `EventTarget`) in scala-js-dom. (Scalatags is not required. You can use whatever tool you want to build a DOM element.) 60 | 61 | After getting the `HTMLButtonElement`, we can build a `Source` from it using `.source()`. (If you are not familiar with `Source`, you may check the [document](https://akka.io/docs/) of Akka Streams). The `.source()` function will expect an event type which is available on `HTMLButtonElement`. For example, `"click"`, `"focus"`, or `"mouseover"`. It will then check if this event has corresponding listener on the element using Scala Macros. For example, it will check if `onclick` is available on `HTMLButtonElement` when you give it `"click"`. This validation happens in compile time so you don't have to worry about misspelled event name: 62 | 63 | ``` 64 | [error] .../Main.scala:75:14: Couldn't find onclic listener on org.scalajs.dom.html.Button. 65 | [error] .source("clic") 66 | [error] ^ 67 | ``` 68 | 69 | The `Source[MouseEvent, NotUsed]` returned by `.source()` can be materialized multiple times to achieve an event broadcasting effect. Also, it's OK to call `.source()` on the same element with same event type multiple times. 70 | 71 | #### PreventDefault and StopPropagation 72 | When calling `.source()`, it's possible to config the source using `preventDefault`, `stopPropagation`, or both: 73 | 74 | ``` scala 75 | form.source("submit", preventDefault, stopPropagation) 76 | ``` 77 | 78 | But, for now, it will only accept these configurations on the first call to the `.source()` function. This may be fixed in the future version: 79 | 80 | ``` scala 81 | val src1 = form.source("submit", preventDefault) 82 | 83 | //the stopPropagation will not work here 84 | val src2 = form.source("submit", stopPropagation) 85 | ``` 86 | 87 | ### Creating Sinks 88 | 89 | Similar to what you saw in previous section, AkkaUI also supports creating a `Sink` from `Element`: 90 | 91 | ``` scala 92 | import akka.ui._ 93 | import akka.stream.scaladsl.Sink 94 | import org.scalajs.dom.raw.HTMLSpanElement 95 | import scalatags.JsDom.all._ 96 | 97 | val name: HTMLSpanElement = span().render 98 | val sink: Sink[String, akka.NotUsed] = name.sink("textContent") 99 | ``` 100 | 101 | Given an `Element` instance, we can create a `Sink` on its... 102 | 103 | * Properties 104 | 105 | ```scala 106 | val sink: Sink[String, NotUsed] = span.sink("textContent") 107 | val sink: Sink[Double, NotUsed] = span.sink("scrollTop") 108 | ... 109 | ``` 110 | 111 | * Styles 112 | 113 | ``` scala 114 | val sink: Sink[String, NotUsed] = span.styleSink("backgroundColor") 115 | ... 116 | ``` 117 | 118 | * Children 119 | 120 | ``` scala 121 | val sink: Sink[Seq[Element], NotUsed] = div.childrenSink 122 | ``` 123 | 124 | * Class 125 | 126 | ``` scala 127 | val sink: Sink[Seq[String], NotUsed] = div.classSink 128 | ``` 129 | 130 | When creating the Sink from properties or styles, the String input will also be validated by Scala Macros to prevent misspelling. And the Sinks can be created or materialized any number of times. 131 | 132 | Here is a simple example which mixes three kinds of Sinks: 133 | 134 | ``` scala 135 | import akka.ui._ 136 | import org.scalajs.dom.ext._ 137 | 138 | def todoItem(content: String) = { 139 | val checkbox = input(`type` := "checkbox").render 140 | val status = span(cls := "font-weight-bold").render 141 | 142 | val clsSink = status.classSink.contramap[String] { stat => 143 | if (stat == "TODO") { 144 | status.classList.filterNot(_ == "text-success") :+ "text-primary" 145 | } else { 146 | status.classList.filterNot(_ == "text-primary") :+ "text-success" 147 | } 148 | } 149 | val txtSink = status.sink("textContent") 150 | 151 | checkbox.source("change") 152 | .scan("TODO")((old, event) => if (old == "TODO") "DONE" else "TODO") 153 | .alsoTo(clsSink) 154 | .to(txtSink) 155 | .run() 156 | 157 | div(checkbox, " ", status, " ", content).render 158 | } 159 | 160 | val content = input().render 161 | val btn = button("Add").render 162 | val todoList = div().render 163 | 164 | btn.source("click") 165 | .map(_ => todoList.children :+ todoItem(content.value)) 166 | .runWith(todoList.childrenSink) 167 | 168 | val root = div(content, btn, todoList) 169 | document.querySelector("#root").appendChild(root.render) 170 | ``` 171 | 172 | Note that when we import `org.scalajs.dom.ext._` and `akka.ui._`, we will be able to operate `Element.children` and `Element.classList` like an immutable `Seq`, thanks to implicit classes. 173 | 174 | If you want to keep some states in your stream, try using the `scan()` function from Akka Streams like above. 175 | 176 | ### Prevent Memory Leak 177 | 178 | Each time you materialize a stream (with `run`, `runForeach`, or `runWith`), there will be several actors created underneath to handle the stream messages. These actors will not be terminated until the stream is completed from the `Source` or canceled from the `Sink`. Furthermore, if you materialize a stream using `Source.actorRef()` or `Sink.actorRef()`, the `Source` and `Sink` actors will keep listening for new message and will never complete. Hence, it's user's responsibility to terminate the streams by himself. (By sending a `PoisonPill` to the `Source` or `Sink` actors for example.) 179 | 180 | In AkkaUI, when you create a `Source` or `Sink` from an `Element`, we will keep a binding information in the internal HashMap. When the `Element` is going to be removed from the DOM by `childrenSink`, all the `Source` and `Sink` related to this `Element` will be completed or canceled, hence prevent the stream from leaking. (These streams will not be terminated if you are removing the DOM element by yourself, so make sure you use `childrenSink` to do the modification.) 181 | 182 | ### Dynamic Stream Handling 183 | 184 | In some use cases, we may have to reference back to a `Source` that's not yet materialized, or merge more `Source` into an already materialized stream. In these cases, one can use Akka's `MergeHub` and `BroadcastHub` to achieve the task. Following is another Todo-list example which add a "Remove" button after each Todo item, and cycle the "Remove" signal back to list's source: 185 | 186 | ``` scala 187 | val contentInput = input().render 188 | val addBtn = button("Add").render 189 | val todoList = div().render 190 | 191 | val (removeSink, removeSource) = MergeHub.source[String] 192 | .map("remove" -> _) 193 | .alsoTo(todoList.dummySink) // prevent memory leak 194 | .toMat(BroadcastHub.sink[(String, String)])(Keep.both) 195 | .run() 196 | 197 | def todoItem(content: String) = { 198 | val checkbox = input(`type` := "checkbox").render 199 | val contentSpan = span(content).render 200 | val removeBtn = button("Remove").render 201 | 202 | checkbox.source("change") 203 | .scan(false)((done, _) => !done) 204 | .runWith( 205 | contentSpan.styleSink("textDecoration").contramap( 206 | done => if (done) "line-through" else "none" 207 | ) 208 | ) 209 | 210 | // connect removeBtn to removeSink 211 | removeBtn.source("click").map(_ => content).runWith(removeSink) 212 | 213 | div(checkbox, " ", contentSpan, " ", removeBtn).render 214 | } 215 | 216 | addBtn.source("click") 217 | .map(_ => "add" -> contentInput.value) 218 | .merge(removeSource) 219 | .scan(Map.empty[String, HTMLDivElement]) { 220 | case (map, ("add", content)) => 221 | map + (content -> todoItem(content)) 222 | case (map, ("remove", content)) => 223 | map - content 224 | } 225 | .map(_.values.toSeq) 226 | .runWith(todoList.childrenSink) 227 | 228 | val root = div(contentInput, addBtn, todoList) 229 | document.querySelector("#root").appendChild(root.render) 230 | ``` 231 | 232 | Starting from a source that will merge the "add" and "remove" signal, we eventually convert each signal into several Todo items, where each Todo item will contain a Remove button which sends its "remove" signal (click) back to the starting source. To achieve this, we use `MergeHub` and `BroadcastHub` to get the `Sink` that can consume signals from the Remove button(s) and the `Source` which can be connected to `todoList`'s `childrenSink`. 233 | 234 | Notice that when we call `run()`, an unhandled stream will be materialized, which we have to terminate by ourselves to prevent memory leak. Here we use a trick that add one more `Sink` to the `Source` before materializing it, which is the `dummySink` on `todoList`, this `Sink` will consume and ignore all the signals sending to it, and will cancel the stream when its binded DOM element is removed. When the stream is canceled, the materialized `MergeHub` and `BroadcastHub` will be terminated as well, hence preventing the memory leak. We can use this technique to connect the unhandled materialized stream to a DOM element which has the same life-cycle as the stream. 235 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------