1 module awebview.jsbuilder;
2 
3 import awebview.wrapper;
4 import awebview.gui.activity;
5 
6 import carbon.utils;
7 
8 import std.string;
9 import std.array;
10 import std.format;
11 import std.traits;
12 
13 
14 struct JSExpression
15 {
16     this(string jsexpr, Activity activity = null)
17     {
18         _expr = jsexpr;
19         _activity = activity;
20     }
21 
22 
23     static
24     JSExpression literal(Int)(Int n, Activity activity = null)
25     if(is(Int : ulong))
26     {
27         return JSExpression(to!string(n), activity);
28     }
29 
30 
31     static
32     JSExpression literal(string str, Activity activity = null)
33     {
34         return JSExpression(toLiteral(str), activity);
35     }
36 
37 
38     static
39     JSExpression literal(bool b, Activity activity = null)
40     {
41         return JSExpression(b ? "true" : "false", activity);
42     }
43 
44 
45     string jsExpr() const @property pure nothrow @safe @nogc
46     {
47         return _expr;
48     }
49 
50 
51     JSExpression opIndex(string str)
52     {
53         return JSExpression(format(`(%s)[%s]`, _expr, toLiteral(str)), _activity);
54     }
55 
56 
57     JSExpression opIndexAssign(JSExpression rhs, string str)
58     {
59         return JSExpression(format(`(%s)[%s] = (%s)`, _expr, toLiteral(str), rhs._expr), _activity);
60     }
61 
62 
63     JSExpression opIndexAssign(T)(T rhs, string str)
64     if(is(typeof(JSExpression.literal(rhs)) == JSExpression))
65     {
66         return opIndexAssign(literal(rhs), str);
67     }
68 
69 
70     JSExpression opIndex(size_t n)
71     {
72         return JSExpression(format("(%s)[%s]", _expr, n), _activity);
73     }
74 
75 
76     JSExpression opIndexAssign(JSExpression rhs, size_t n)
77     {
78         return JSExpression(format(`(%s)[%s] = (%s)`, _expr, n, rhs._expr), _activity);
79     }
80 
81 
82     JSExpression opIndexAssign(T)(T rhs, size_t n)
83     if(is(typeof(JSExpression.literal(rhs)) == JSExpression))
84     {
85         return opIndexAssign(literal(rhs), n);
86     }
87 
88 
89     JSExpression invoke(S, T...)(S name, T args)
90     {
91         auto app = appender!string();
92         app.formattedWrite("(%s).%s(", _expr, name);
93         foreach(i, ref e; args){
94             static if(isSomeString!(typeof(e)))
95                 app.put(toLiteral(e));
96             else
97                 app.formattedWrite("%s", e);
98 
99             if(i != T.length - 1)
100                 app.put(",");
101         }
102         app.put(")");
103 
104         return JSExpression(app.data, _activity);
105     }
106 
107 
108     JSExpression invoke(string name, T...)(T args)
109     {
110         return invoke(name, args);
111     }
112 
113 
114     JSValue eval()
115     {
116         return evalOn(_activity);
117     }
118 
119 
120     void run()
121     {
122         runOn(_activity);
123     }
124 
125 
126     JSValue evalOn(Activity activity)
127     {
128         return activity.evalJS(_expr);
129     }
130 
131 
132     void runOn(Activity activity)
133     {
134         activity.runJS(_expr);
135     }
136 
137 
138     T eval(T)()
139     {
140         return evalOn!T(_activity);
141     }
142 
143 
144     T evalOn(T)(Activity activity)
145     {
146         return activity.evalJS(_expr).get!T;
147     }
148 
149 
150     Activity activity() @property pure nothrow @safe @nogc { return _activity; }
151 
152 
153   private:
154     string _expr;
155     Activity _activity;
156 }
157 
158 
159 //auto querySelector(Activity activity, string query)
160 //{
161 //    return JSExpression(format(q{document.querySelector(%s)}), toLiteral(query), activity);
162 //}
163 
164 
165 //auto querySelectorAll(Activity activity, string query)
166 //{
167 //    return JSExpression(format(q{document.querySelectorAll(%s)}), toLiteral(query), activity);
168 //}