diff --git a/src/main/scala/angulate2/core/Directive.scala b/src/main/scala/angulate2/core/Directive.scala index 1ce9ca0..0177b6c 100644 --- a/src/main/scala/angulate2/core/Directive.scala +++ b/src/main/scala/angulate2/core/Directive.scala @@ -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} @@ -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._ diff --git a/src/main/scala/angulate2/core/EventEmitter.scala b/src/main/scala/angulate2/core/EventEmitter.scala new file mode 100644 index 0000000..2703837 --- /dev/null +++ b/src/main/scala/angulate2/core/EventEmitter.scala @@ -0,0 +1,16 @@ +// Project: angulate2 (https://github.com/jokade/angulate2) +// Description: + +// Copyright (c) 2016 Johannes.Kastner +// 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 +} diff --git a/src/main/scala/angulate2/core/Output.scala b/src/main/scala/angulate2/core/Output.scala new file mode 100644 index 0000000..fcddfb6 --- /dev/null +++ b/src/main/scala/angulate2/core/Output.scala @@ -0,0 +1,57 @@ +// Project: angulate2 (https://github.com/jokade/angulate2) +// Description: + +// Copyright (c) 2016 Johannes.Kastner +// 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) + } + } + + } + +} diff --git a/src/main/scala/angulate2/ops.scala b/src/main/scala/angulate2/ops.scala index 83e4f25..4d93d53 100644 --- a/src/main/scala/angulate2/ops.scala +++ b/src/main/scala/angulate2/ops.scala @@ -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 { diff --git a/src/main/scala/angulate2/std.scala b/src/main/scala/angulate2/std.scala index 94b477f..70e6c4f 100644 --- a/src/main/scala/angulate2/std.scala +++ b/src/main/scala/angulate2/std.scala @@ -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