Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin/JS

Jossi Wolf
February 21, 2018

 Kotlin/JS

Slides from a talk about Kotlin/JS from KUGHH February 2018 meetup.

Jossi Wolf

February 21, 2018
Tweet

More Decks by Jossi Wolf

Other Decks in Programming

Transcript

  1. AGENDA - INTRODUCTION - WHY KOTLIN? - GETTING STARTED -

    DIFFERENCES - USING NODEJS - CREATING A WEBSITE - CONCLUSION @jossiwolf
  2. STATICALLY TYPED DYNAMICALLY TYPED var hello = "Hello" hello =

    5 ❌ let hello = "Hello" hello = 5 ✅ @jossiwolf
  3. buildscript { ext.kotlin_version = '1.2.10' repositories { mavenCentral() } dependencies

    { classpath "$kotlin_gradle_plugin:$kotlin_version" } } apply plugin: 'kotlin-js' repositories { mavenCentral() } dependencies { compile "$kotlin_stdlib_js:$kotlin_version" } @jossiwolf
  4. compileKotlin2Js.kotlinOptions { sourceMap = true //Generate a source-map for debug

    sourceMapEmbedSources = "always" //And embed the source } @jossiwolf
  5. compileKotlin2Js.kotlinOptions { sourceMap = true //Generate a source-map for debug

    sourceMapEmbedSources = "always" //And embed the source moduleKind = "plain" //The module loader to be used } @jossiwolf
  6. compileKotlin2Js.kotlinOptions { sourceMap = true //Generate a source-map for debug

    sourceMapEmbedSources = "always" //And embed the source moduleKind = "plain" //The module loader to be used outputFile = "web/index.js" //Output file that we use } @jossiwolf
  7. task assembleWeb(type: Sync) { configurations.compile.each { File file -> from(zipTree(file.absolutePath),

    { includeEmptyDirs = false include { fileTreeElement -> def path = fileTreeElement.path path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/")) } }) } from compileKotlin2Js.destinationDir into "${projectDir}/web" dependsOn classes } assemble.dependsOn assembleWeb @jossiwolf
  8. <!DOCTYPE html> <html lang="en"> <head> <title>Kotlin & JS <3</title> </head>

    <body> </body> <!-- The Kotlin libraries --> <script src="web/kotlin.js"></script> <script src="web/kotlinx-html-js.js"></script> <!-- Make sure this is at the bottom --> <script src="web/index.js"></script> </html> @jossiwolf
  9. <div class="mdc-card"> <section class="mdc-card__primary"> <h1 class="mdc-card__title mdc-card__title--large">Title goes here</h1> <h2

    class="mdc-card__subtitle">Subtitle here</h2> </section> <section class="mdc-card__supporting-text"> Some text goes here </section> <section class="mdc-card__actions"> <button class="mdc-button mdc-button--compact mdc-card__action"> Action 1 </button> <button class="mdc-button mdc-button--compact mdc-card__action"> Action 2 </button> </section> </div> @jossiwolf
  10. verticalLayout { val name = editText() button("Say Hello") { onClick

    { toast("Hello, ${name.text}!") } } } @jossiwolf
  11. div("mdc-card") { section("mdc-card__primary") { h1("mdc-card__title) { +"This is the title"

    } h2("mdc-card__subtitle") { +"Subtitle" } } section("mdc-card__supporting-text") { +"This is the card's text" } } @jossiwolf
  12. fun DIV.card(title: String, subtitle: String, text: String) { div("mdc-card") {

    section("mdc-card__primary") { h1("mdc-card__title") { +title } h2("mdc-card__subtitle") { +subtitle } } section("mdc-card__supporting-text") { +text } } } @jossiwolf
  13. fun DIV.card(title: String, subtitle: String, text: String) { div("mdc-card") {

    section("mdc-card__primary") { h1("mdc-card__title") { +title } h2("mdc-card__subtitle") { +subtitle } } section("mdc-card__supporting-text") { +text } } } @jossiwolf
  14. card { title = "Card Title" subtitle = "Card Subtitle"

    text = "Hello Kotlin Meetup!„ } @jossiwolf
  15. card { title = "Card Title" subtitle = "Card Subtitle"

    text = "Hello Kotlin Meetup!" action { text = "Action!" } elevation = 6 } @jossiwolf
  16. fun higherOrderFunction(block: (String) -> String): String { return block("Hello from

    up here") } The function takes a string as parameter @jossiwolf
  17. fun higherOrderFunction(block: (String) -> String): String { return block("Hello from

    up here") } The function takes a string as parameter @jossiwolf
  18. fun higherOrderFunction(block: (String) -> String): String { return block("Hello from

    up here") } The function takes a string as parameter Its return type is also String @jossiwolf
  19. fun higherOrderFunction(block: (String) -> String): String { return block("Hello from

    up here") } println(higherOrderFunction { it }) //Prints „Hello from up here“ The function takes a String as parameter Its return type is Also String @jossiwolf
  20. fun higherOrderFunction(block: (String) -> String): String { return block("Hello from

    up here") } println(higherOrderFunction { return@higherOrderFunction it }) //Prints „Hello from up here“ The function takes a String as parameter Its return type is Also String @jossiwolf
  21. card { title = "Card Title" subtitle = "Card Subtitle"

    text = "Hello Kotlin Meetup!" } @jossiwolf
  22. card { title = "Hello" subtitle = "Subtitle" } {

    "title_xesk5l$_0": "Hello", "subtitle_x7q17f$_0": "Subtitle", "actions": [] } @jossiwolf
  23. fun DIV.card(block: MaterialCardOptions.() -> Unit) { val options = MaterialCardOptions().apply(block)

    div("mdc-card") { section("mdc-card__primary") { h1 { classes = setOf("mdc-card__title") +options.title } h2("mdc-card__subtitle") { +options.subtitle } } section("mdc-card__supporting-text") { +options.text } } } @jossiwolf
  24. document.body?.append { div { card { title = "Shiba Inu"

    subtitle = "Japanese Dog Breed" text = "Shiba Inu, also known as Doge, is a Japanese dog breed." } } } @jossiwolf