Skip to content

Commit

Permalink
Closes #43: support Output annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
jokade committed Dec 20, 2016
1 parent 684077b commit 36f59ce
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/scala/angulate2/core/Directive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package angulate2.core

import angulate2.core.HostListener.HostListenerDecorator
import angulate2.core.Input.InputDecorator
import angulate2.core.Output.OutputDecorator
import angulate2.internal.ClassDecorator

import scala.annotation.{StaticAnnotation, compileTimeOnly}
Expand Down Expand Up @@ -38,7 +39,7 @@ class Directive(selector: String = null,
object Directive {

private[angulate2] abstract class BaseMacro extends ClassDecorator
with InputDecorator with HostListenerDecorator
with InputDecorator with OutputDecorator with HostListenerDecorator

private[angulate2] class Macro(val c: whitebox.Context) extends BaseMacro {
import c.universe._
Expand Down
16 changes: 16 additions & 0 deletions src/main/scala/angulate2/core/EventEmitter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Project: angulate2 (https://github.com/jokade/angulate2)
// Description:

// Copyright (c) 2016 Johannes.Kastner <jokade@karchedon.de>
// Distributed under the MIT License (see included LICENSE file)
package angulate2.core

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport

@js.native
@JSImport("@angular/core","EventEmitter")
class EventEmitter[T](isAsync: js.UndefOr[Boolean] = js.undefined) extends js.Any {
def emit(value: js.UndefOr[T] = js.undefined): Unit = js.native
def subscribe(generatorOrNext: js.UndefOr[js.Any] = js.undefined, error: js.UndefOr[js.Any] = js.undefined, complete: js.UndefOr[js.Any] = js.undefined): js.Any = js.native
}
57 changes: 57 additions & 0 deletions src/main/scala/angulate2/core/Output.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Project: angulate2 (https://github.com/jokade/angulate2)
// Description:

// Copyright (c) 2016 Johannes.Kastner <jokade@karchedon.de>
// Distributed under the MIT License (see included LICENSE file)
package angulate2.core

import angulate2.internal.ClassDecorator

import scala.annotation.StaticAnnotation

class Output extends StaticAnnotation {
def this(bindingPropertyName: String) = this()
}

object Output {

protected[angulate2] trait OutputDecorator extends ClassDecorator {
import c.universe._

override def analyze: Analysis = super.analyze andThen {
case (cls: ClassParts, data) =>
import cls._

val inputs = (body collect {
case ValDef(OutputAnnot(externalName),term,_,_) => OutputAnnot(fullName,term.toString,externalName)
case DefDef(OutputAnnot(externalName),term,_,_,_,_) =>
val method = term.toString
val setter =
if(method.endsWith("_$eq")) method.stripSuffix("_$eq")
else {
error("@Output() may only be used on vars and setters (i.e. 'def foo_=(a: Any) {}')")
""
}
OutputAnnot(fullName,setter,externalName)
}).map(_.toJS)

(cls,ClassDecoratorData.addSjsxStatic(data,inputs))

case default => default
}

object OutputAnnot {
def unapply(annotations: Seq[Tree]): Option[Option[String]] = findAnnotation(annotations,"Output")
.map( t => extractAnnotationParameters(t,Seq("bindingPropertyName")).apply("bindingPropertyName").flatMap(extractStringConstant) )
def unapply(modifiers: Modifiers): Option[Option[String]] = unapply(modifiers.annotations)

def apply(prototype: String, method: String, property: Option[String]): MethodDecoration = {
val decorator = if(property.isDefined) s"core.Output('${property.get}')" else "core.Output()"
// val designType = DecorationMetadata.designType(null)
MethodDecoration(decorator,prototype,method,Nil)
}
}

}

}
1 change: 1 addition & 0 deletions src/main/scala/angulate2/ops.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private[angulate2] trait OpsTrait {
def @@[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12]: js.Array[js.Any] = macro OpsMacros.jsRefArray12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12]

def @@@[T](items: T*): js.Array[T] = items.toJSArray

}

private[angulate2] class OpsMacros(val c: blackbox.Context) extends AngulateBlackboxMacroTools {
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/angulate2/std.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ object std extends OpsTrait {
}

type Input = core.Input
type Output = core.Output
type OnInit = core.OnInit
type OnDestroy = core.OnDestroy
type OnChanges = core.OnChanges
Expand Down

0 comments on commit 36f59ce

Please sign in to comment.