-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComponents.jl
95 lines (85 loc) · 2.62 KB
/
Components.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module Components
include("utils.jl")
using GenieFramework
export Component, variables, handlers, ui, get_code
struct Component
variables::Expr
handlers::Expr
ui::Expr
prefix::String
function Component(vars::Expr, handlers::Expr, ui::Expr, prefix::String)
var_list = extract_var_names(vars)
prefixed_vars = add_prefix!(vars, prefix, var_list)
prefixed_handlers = add_prefix!(handlers, prefix, var_list)
prefixed_ui = add_prefix!(ui, prefix, var_list)
return new(prefixed_vars, prefixed_handlers, prefixed_ui, prefix)
end
end
variables(c::Component, M::Module=@__MODULE__) = Base.eval(M, c.variables)
handlers(c::Component, M::Module=@__MODULE__) = Base.eval(M, c.handlers)
ui(c::Component; M::Module=@__MODULE__) = Base.eval(M, c.ui)
(c::Component)(M) = begin
variables(c, M)
handlers(c, M)
"Instantiated component $(c.prefix)"
end
#= (c::Component)(uicode; M=@__MODULE__) = begin =#
#= variables(c, M) =#
#= handlers(c, M) =#
#= if uicode == "jl" =#
#= return extractLetBlock(repr(c.ui)) =#
#= else =#
#= return ui(c) =#
#= end =#
#= end =#
#= function handlers(c, print_code) =#
#= return =#
#= end =#
function ui(c, lang::String)
if lang == "jl"
return extract_code(string(c.ui))
else
return ui(c)
end
end
function get_code(c, field::Symbol, lang::String="jl")
e = getfield(c, field)
if is_empty(e)
return ""
end
if lang == "jl"
return extract_code(string(e))
else
return ui(c)
end
end
function extract_var_names(expr::Expr)
var_names = Symbol[]
if expr.head == :block || expr.head == :macrocall
for arg in expr.args
if isa(arg, Expr) && arg.head == :(=) && isa(arg.args[1], Symbol)
push!(var_names, arg.args[1])
elseif isa(arg, Expr)
append!(var_names, extract_var_names(arg))
end
end
end
return var_names
end
function add_prefix!(expr::Expr, prefix::String, vars::Vector{Symbol})
if isa(expr, Expr)
for i in eachindex(expr.args)
if isa(expr.args[i], Expr)
add_prefix!(expr.args[i], prefix, vars)
elseif isa(expr.args[i], QuoteNode) && expr.args[i].value in vars
expr.args[i] = QuoteNode(Symbol(prefix, string(expr.args[i].value)))
elseif isa(expr.args[i], Symbol) && expr.args[i] in vars
expr.args[i] = Symbol(prefix, string(expr.args[i]))
elseif expr.args[i] == :prefix
expr.args[i] = prefix
end
end
end
return expr
end
end