QuantumSavory / CSSMakieLayout.jl

MIT License
16 stars 2 forks source link

Using JSServe.jsrender #3

Open SimonDanisch opened 1 year ago

SimonDanisch commented 1 year ago

Awesome package :) Looking forward to see it being used & registered. One thing that should be changed though is this:

    landing2 = App() do session::Session
        CssMakieLayout.CurrentSession = session
        ...
    end

This is pretty hacky, since Session isn't something that's easily globally shared. Also, it's somewhat unnecessary for any user to set it, if you properly overload JSServe.jsrender(s::Session, my_object). I haven't looked into this in detail, but I'd propose something like this: All the functions like zstack should define a small custom struct, like this:


struct ZStack 
    items::Vector{Any}
    attributes::Dict{Symbol, Any}
end
# properly add attributes like: activeidx::Observable=nothing class="", anim=[:default], style="", md=false
zstack(items...; kw...)  = ZStack(Any[items...], Dict{Symbol, Any}(kw))

function JSServe.jsrender(session::Session, zstack::ZStack)
       .....
        # add on(activeidx) event
        onjs(session, activeidx, js"""function on_update(new_value) {
            const activefig_stack = $(item_div)
            for(i = 1; i <= $(height); ++i) {
                const element = activefig_stack.querySelector(":nth-child(" + i +")")
                element.classList.remove("CssMakieLayout_active");
                if(i == new_value) {
                    element.classList.add("CssMakieLayout_active");
                }
            }
        }
        """)
    end
    return item_div
end

Anything else will be pretty hacky, but this should pretty nicely integrate with JSServe, so one can return any CssMakieLayout struct in an App. Also, I'd put formatstyle into a css file and insert it in all jsrender calls as an asset: DOM.div(Asset("path/to/formatstyle.css")). JSServe will make sure, that it will get included only one time, so don't worry about putting it in every jsrender.

adrianariton commented 1 year ago

Thank you very much for this! I greatly appreciate it as I was looking for a way to not make the users set the session themselves.

I will get to this as soon as possible, fix it, and release it in the next version of the library along with some other new stuff!