All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.data2viz.viz.Selector.kt Maven / Gradle / Ivy

There is a newer version: 0.8.0-RC5
Show newest version
package io.data2viz.viz

import kotlin.reflect.KProperty

// types
//val circle  = TypeSelector(newCircle())
//val line    = TypeSelector(newLine())
//val rect    = TypeSelector(newRect())
//val text    = TypeSelector(newText())


fun cssclass() = CssClassDelegate()

class CssClassDelegate {
    operator fun getValue(from: Any?, property: KProperty<*>): CssClass {
        return CssClass(property.name)
    }
}

data class CssClass(val name: String)

abstract class Selector {

    var parent: Pair? = null

    val selectors = mutableListOf()

    abstract fun copy(): Selector

    fun copyParentAndSelectors(to: Selector) {
        parent?.let { to.parent = it.first to it.second }
        selectors.forEach {
            to.selectors.add(it.copy())
        }
    }

    infix fun  children(rightSelector: TypeSelector): TypeSelector {
        val copy = rightSelector.copy()
        copy.parent = copy() to Combinator.CHILDREN
        return copy as TypeSelector
    }

}

fun c(cssClass: CssClass): ClassSelector = ClassSelector(cssClass.name)

data class ClassSelector(val name: String) : Selector() {

    override fun copy(): Selector {
        val copy = ClassSelector(name)
        copyParentAndSelectors(copy)
        return copy
    }

    override fun toString() = ".$name" + selectors.joinToString("")

    fun c(cssClass: CssClass): ClassSelector {
        val copy = copy()
        copy.selectors.add(ClassSelector(cssClass.name))
        return copy as ClassSelector
    }

}


enum class Combinator(val asText: String) {
    CHILDREN(">"), DESCENDENT(" ")
}

@Deprecated("Old design, do not use.")
class TypeSelector(val prototype: E) : Selector() {
    val name: String
        get() = prototype::class.simpleName!!

    override fun copy(): Selector  {
        val copy = TypeSelector(prototype)
        copyParentAndSelectors(copy)
        return copy
    }

    fun c(cssClass: CssClass): TypeSelector {
        val copy = copy()
        copy.selectors.add(ClassSelector(cssClass.name))
        return copy as TypeSelector
    }

    fun asText() = (parent?.let { "${it.first} ${it.second.asText} " } ?: "") +
            name.dropLast("Jfx".length) + selectors.joinToString("")


    override fun toString(): String {
        return asText()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy