Skip to content

Instantly share code, notes, and snippets.

@rajeevprasanna

rajeevprasanna/Main.scala Secret

Last active Oct 5, 2017
Embed
What would you like to do?
enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin, WorkbenchPlugin)
name := "Playground"
version := "1.0"
scalaVersion := "2.12.1"
libraryDependencies += "in.nvilla" %%% "monadic-html" % "0.3.0"
libraryDependencies += "com.lihaoyi" %%% "upickle" % "0.4.4"
libraryDependencies += "com.thoughtworks.binding" %%% "dom" % "10.0.1"
libraryDependencies += "fr.hmil" %%% "roshttp" % "2.0.2"
libraryDependencies += "be.doeraene" %%% "scalajs-jquery" % "0.9.1"
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
webpackConfigFile in fastOptJS := Some(baseDirectory.value / "dev.webpack.config.js")
npmDependencies in Compile ++= Seq(
"uuid" -> "3.1.0",
"jquery" -> "3.2.1"
)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Test APP</title>
</head>
<body>
<div id="application-container"> this is just html template</div>
<!-- Office JavaScript API -->
<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
<script type="application/javascript" src="./target/scala-2.12/scalajs-bundler/main/playground-fastopt-bundle.js"></script>
</body>
</html>
import com.thoughtworks.binding.Binding._
import com.thoughtworks.binding.{Binding, dom}
import org.scalajs.dom.{Event, document}
import org.scalajs.dom.html.{Button, Table, TableRow}
import org.scalajs.dom.raw._
import org.scalajs.jquery.JQueryStatic
import scala.scalajs.js
import scala.scalajs.js.JSApp
import scala.scalajs.js.annotation.{JSExport, JSGlobal, JSImport}
/**
* Created by rajeevprasanna on 9/26/17.
*/
@js.native
@JSImport("jquery", JSImport.Namespace)
object jquery extends JQueryStatic
@js.native
trait item extends js.Object {
val itemType:String = js.native
val conversationId:String = js.native
val internetMessageId:String = js.native
}
@js.native
trait mailbox extends js.Object {
@js.native
val item:item = js.native
}
@js.native
trait context extends js.Object {
@js.native
val mailbox:mailbox = js.native
}
@js.native
@JSGlobal
object Office extends js.Object {
var initialize:js.Function1[String, _] = js.native
val context:context = js.native
}
@js.native
@JSGlobal
object JSON extends js.Object {
def parse(text:String):js.Any = js.native
def stringify(value:js.Any):String = js.native
}
@js.native
@JSImport("uuid", JSImport.Namespace)
object UUIDV4 extends js.Object
{
def v4():String = js.native
}
@JSExport
object MainClass extends JSApp {
implicit def makeIntellijHappy[T<:org.scalajs.dom.raw.Node](x: scala.xml.Node): Binding[T] =
throw new AssertionError("This should never execute.")
case class Contact(name: Var[String], email: Var[String])
var data = Vars(Contact(Var("Test user1"), Var("testuser@testing.com")))
@dom
def bindingButton(contact: Contact): Binding[Button] = {
<button
onclick={ event: Event =>
contact.name := "Modified Name"
}
>
Modify the name
</button>
}
@dom
def addContact():Binding[Button] = {
<button onclick={ event:Event =>
data.get += Contact(Var("rajeev"), Var("rajeev@xenovus.com"))
}
>Add Contact</button>
}
@dom
def bindingTr(contact: Contact): Binding[TableRow] = {
<tr>
<td>{ contact.name.bind }</td>
<td>{ contact.email.bind }</td>
<td>{ bindingButton(contact).bind }</td>
<td>{addContact.bind}</td>
<td>{
<button
onclick={ event: Event =>
contact.email := "testing changes@dkfjghdfkg.com"
}
>
modify this contact
</button>
}</td>
</tr>
}
@dom
def v4String():Binding[HTMLDivElement] = {
val v4String = UUIDV4.v4()
<div>{v4String}</div>
}
def makeRestAPI() = {
println("Button clicked to make API call...")
// RestAPI.fireRequest()
RestAPI.parseJson()
}
@dom
def bindingTable(contacts: BindingSeq[Contact]): Binding[Table] = {
<table>
<tbody>
<tr><button onclick={ event:Event =>
makeRestAPI()
}> make an API call </button></tr>
{
for (contact <- contacts) yield {
bindingTr(contact).bind
}
}
</tbody>
</table>
}
def main(): Unit = {
jquery(document).ready(() => {
println(s"ready state => ${document.readyState}")
def callback = (reason:String) => {
val conversationId = Office.context.mailbox.item.conversationId
println(s"conversationId ==> ${conversationId}")
println(s"reason called in callback function => $reason")
}
Office.initialize = { r:String =>
println(s"function initialized. reason => $r")
callback(r)
}
})
dom.render(document.getElementById("application-container"), bindingTable(data))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.