Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Mar 29, 2017
1 parent 876e165 commit e87cef2
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/com/github/sanity/kweb/Kweb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class Kweb(val port: Int,
}

private data class WSClientData(val id: String, @Volatile var outboundChannel: OutboundChannel, val handlers: MutableMap<Int, (String) -> Unit> = HashMap(), val debugTokens: MutableMap<String, DebugInfo> = HashMap()) {
fun send(message: S2CWebsocketMessage) = async(CommonPool){
fun send(message: S2CWebsocketMessage) {
outboundChannel.send(gson.toJson(message))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
package com.github.sanity.kweb.browserConnection

import kotlinx.coroutines.experimental.runBlocking
import org.jetbrains.ktor.websocket.Frame
import org.jetbrains.ktor.websocket.WebSocket
import java.util.concurrent.ConcurrentLinkedQueue

sealed class OutboundChannel {
abstract suspend fun send(message: String)
abstract fun send(message: String)

class WSChannel(private val channel: WebSocket) : OutboundChannel() {
override suspend fun send(message: String) {
channel.send(Frame.Text(message))
override fun send(message: String) {
runBlocking {
channel.send(Frame.Text(message))
}
}

}

class TemporarilyStoringChannel() : OutboundChannel() {
private @Volatile var queue: ConcurrentLinkedQueue<String>? = ConcurrentLinkedQueue<String>()

override suspend fun send(message: String) {
(queue ?: throw RuntimeException("Can't write to queue after it has been read")).add(message)
override fun send(message: String) {
queue.let {
it?.add(message) ?: throw RuntimeException("Can't write to queue after it has been read")
}
}

fun read(): List<String> {
val r = (queue ?: throw RuntimeException("Queue can only be read once")).toList()
queue = null
return r
queue.let {
if (it == null) throw RuntimeException("Queue can only be read once")
else {
val r = it.toList()
queue = null
return r
}
}
}

fun queueSize() = queue?.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class KwebHotswapPlugin {

@OnClassLoadEvent(classNameRegexp = ".*", events = arrayOf(LoadEvent.REDEFINE))
@JvmStatic fun onAnyReload() {
println("Reload detected")
logger.debug { "Hotswap load event detected, calling listeners" }
listeners.forEach { it.invoke() }
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/com/github/sanity/kweb/dom/element/Element.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ open class Element (open val webBrowser: WebBrowser, open var jsExpression: Stri
*
* @receiver This will be the parent element of any elements created with the returned
* [ElementCreator]
* @Param position What position among the parent's children should the new element have?
*
* @sample new_sample_1
*/
Expand All @@ -66,10 +67,12 @@ fun <T : Element> T.new(position : Int? = null) = ElementCreator(this, position)
/**
* A convenience wrapper around [new] which allows a nested DSL-style syntax
*
* @Param position What position among the parent's children should the new element have?
*
* @sample new_sample_2
*/
fun <T : Element> T.new(receiver: ElementCreator<T>.() -> Unit) : T {
receiver(new())
fun <T : Element> T.new(position : Int? = null, receiver: ElementCreator<T>.() -> Unit) : T {
receiver(new(position))
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ fun ElementCreator<Element>.h1(attributes: Map<String, Any> = attr) = H1Element(

open class PElement(parent: Element) : Element(parent)
fun ElementCreator<Element>.p(attributes: Map<String, Any> = attr) = PElement(element("p", attributes))

open class NavElement(parent: Element) : Element(parent)
fun ElementCreator<Element>.nav(attributes: Map<String, Any> = attr) = NavElement(element("nav", attributes))

open class SectionElement(parent: Element) : Element(parent)
fun ElementCreator<Element>.section(attributes: Map<String, Any> = attr) = SectionElement(element("section", attributes))
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ package com.github.sanity.kweb.plugins.foundation
import com.github.sanity.kweb.dom.element.Element
import com.github.sanity.kweb.dom.element.creation.ElementCreator
import com.github.sanity.kweb.plugins.KWebPlugin
import com.github.sanity.kweb.plugins.jqueryCore.jqueryCore

/**
* Created by ian on 3/24/17.
*/

val foundation = FoundationPlugin()

class FoundationPlugin : KWebPlugin() {
class FoundationPlugin : KWebPlugin(dependsOn = setOf(jqueryCore)) {
override fun decorate(startHead: StringBuilder, endHead: StringBuilder) {
endHead.appendln("""<script src="https://cdn.jsdelivr.net/foundation/6.2.4/foundation.min.js"></script>""")
endHead.appendln("""<link rel="stylesheet" href="https://cdn.jsdelivr.net/foundation/6.2.4/foundation.min.css">""")
}

override fun executeAfterPageCreation() = "$(document).foundation();"
}

val <ET : Element> ElementCreator<ET>.foundation : ElementCreator<FoundationElement<ET>> get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,41 @@ import com.github.sanity.kweb.plugins.foundation.foundation
* Created by ian on 3/28/17.
*/

open class FoundationTopBarElement(parent: DivElement) : FoundationElement<DivElement>(parent)
fun ElementCreator<FoundationElement<Element>>.topBar(attributes: Map<String, Any> = attr) = FoundationTopBarElement(div(attributes.classes("top-bar")))
open class FoundationTopBarElement(parent: NavElement) : NavElement(parent)
fun ElementCreator<FoundationElement<Element>>.topBar(attributes: Map<String, Any> = attr)
= FoundationTopBarElement(nav(attributes
.classes("top-bar")
.set("data-topbar", true)
.set("role", "navigation")))

open class FoundationTopBarLeftElement(parent: DivElement) : FoundationElement<DivElement>(parent)
fun ElementCreator<FoundationElement<FoundationTopBarElement>>.topBarLeft(attributes: Map<String, Any> = attr) = FoundationTopBarLeftElement(div(attributes.classes("top-bar-left")))
open class FoundationTitleAreaElement(parent: ULElement) : ULElement(parent)
fun ElementCreator<FoundationTopBarElement>.titleArea(attributes: Map<String, Any> = attr) = FoundationTitleAreaElement(ul(attributes.classes("title-area")))

open class FoundationNameElement(parent: LIElement) : LIElement(parent)
fun ElementCreator<FoundationTitleAreaElement>.name(attributes: Map<String, Any> = attr) = FoundationNameElement(this.li(attributes.classes("name")))

open class FoundationTopBarSectionElement(parent: Element) : Element(parent)
fun ElementCreator<FoundationTopBarElement>.topBarSection(attributes: Map<String, Any> = attr)
= FoundationTopBarSectionElement(section(attributes.classes("top-bar-section")))

open class FoundationLeftElement(parent: Element) : Element(parent)
fun ElementCreator<FoundationTopBarSectionElement>.left(attributes: Map<String, Any> = attr)
= FoundationRightElement(ul(attributes.classes("left")))

open class FoundationRightElement(parent: Element) : Element(parent)
fun ElementCreator<FoundationTopBarSectionElement>.right(attributes: Map<String, Any> = attr)
= FoundationRightElement(ul(attributes.classes("right")))

open class FoundationMenuElement(parent: ULElement) : ULElement(parent)
fun ElementCreator<FoundationElement<Element>>.menu(dropdown : Boolean? = null, attributes: Map<String, Any> = attr)
= FoundationMenuElement(ul(attributes.classes("menu").classes("dropdown", onlyIf = dropdown ?: false).set("data-drop-down", dropdown)))

fun main(args: Array<String>) {
foundation_menu_sample()
}

private fun foundation_menu_sample() {
Kweb(port = 1234, plugins = listOf(foundation)) {
Kweb(port = 1234, plugins = listOf(foundation), refreshPageOnHotswap = true) {
doc.body.new {
foundation.menu().new {
li().new().a(href = "#").text("Item 1")
li().new().a(href = "#").text("Item 2")
li().new().a(href = "#").text("Item 3")
li().new().a(href = "#").text("Item 4")
foundation.topBar().new {
titleArea().new().name().new().a(href="#").text("My Site")
}
}
}
Expand Down

0 comments on commit e87cef2

Please sign in to comment.