Source file vue_js.ml
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
open Mjs
let set_version = Version.set
type 'all vue = 'all t
class type ['data, 'all, 'state] vue_arg = object
inherit ['data, 'all] Vue_component.Internal.component_common
method components : (top, top) Vue_component.Internal.component_arg t table optdef readonly_prop
method router : Vue_router.router t optdef readonly_prop
method store : 'state Vuex.store t optdef readonly_prop
end
type ('data, 'all, 'state) vue_v2 = (('data, 'all, 'state) vue_arg t -> 'all vue) constr
class type ['data, 'all, 'state] vue_v3 = object
method createApp : ('data, 'all, 'state) vue_arg t -> 'all vue meth
end
let create ?computed ?watch ?methods ?data ?(lifecycle = []) ?error_captured
?directives ?filters ?components ?delimiters ?functional
?model ?inherit_attrs ? ?router ?store ?render ?static_renders
?(version= !Version.v) () =
let data = optdef (fun d -> wrap_callback (fun () -> d)) data in
let arg : ('data, 'all, 'state) vue_arg t = object%js
val render = Optdef.option render
val staticRenderFns = Optdef.option static_renders
val data = data
val computed = optdef (to_tablef (fun c -> wrap_meth_callback (fun this () -> c this))) computed
val watch = optdef (to_tablef wrap_meth_callback) watch
val methods = optdef to_table methods
val beforeCreate = optdef wrap_meth_callback @@ List.assoc_opt "beforeCreate" lifecycle
val created = optdef wrap_meth_callback @@ List.assoc_opt "created" lifecycle
val beforeMount = optdef wrap_meth_callback @@ List.assoc_opt "beforeMount" lifecycle
val mounted = optdef wrap_meth_callback @@ List.assoc_opt "mounted" lifecycle
val beforeUpdate = optdef wrap_meth_callback @@ List.assoc_opt "beforeUpdate" lifecycle
val updated = optdef wrap_meth_callback @@ List.assoc_opt "updated" lifecycle
val activated = optdef wrap_meth_callback @@ List.assoc_opt "activated" lifecycle
val deactivated = optdef wrap_meth_callback @@ List.assoc_opt "deactivated" lifecycle
val beforeDestroy = optdef wrap_meth_callback @@ List.assoc_opt "beforeDestroy" lifecycle
val destroyed = optdef wrap_meth_callback @@ List.assoc_opt "destroyed" lifecycle
val errorCaptured = optdef (fun f -> wrap_callback (fun x y s -> optdef bool @@ f x y (to_string s))) error_captured
val directives = optdef (Table.makef (fun l -> Table.makef wrap_callback l)) directives
val filters = optdef (Table.makef wrap_callback) filters
val components = optdef to_table components
val delimiters = optdef (fun (a, b) -> array [| string a; string b |]) delimiters
val functional = optdef bool functional
val model = optdef (fun (prop, event) -> object%js val prop = optdef string prop val event = optdef string event end) model
val inheritAttrs = optdef bool inherit_attrs
val comments = optdef bool comments
val router = Optdef.option router
val store = Optdef.option store
end in
match version with
| `v2 ->
let g : ('data, 'all, 'state) vue_v2 = Unsafe.pure_js_expr "Vue" in
new%js g arg
| `v3 ->
let g : ('data, 'all, 'state) vue_v3 t = Unsafe.pure_js_expr "Vue" in
g##createApp arg
let mount ?(version= !Version.v) ~app id =
let id = string ("#" ^ id) in
match version with
| `v2 -> Unsafe.meth_call app "$mount" [| Unsafe.inject id |]
| `v3 -> (Unsafe.coerce app)##mount id
let make ?computed ?watch ?methods ?data ?lifecycle ?error_captured
?directives ?filters ?components ?delimiters ?functional
?model ?inherit_attrs ? ?router ?store ?render ?static_renders
?version id =
let app =
create ?computed ?watch ?methods ?data ?lifecycle ?error_captured
?directives ?filters ?components ?delimiters ?functional
?model ?inherit_attrs ?comments ?router ?store ?render ?static_renders
?version () in
mount ?version ~app id
let set_global s v = Unsafe.set (Unsafe.pure_js_expr "Vue.prototype") ("$" ^ s) v
let get_prop vm s = Unsafe.get vm @@ "$" ^ s
let get_ref vm id =
let refs = get_prop vm "refs" in
Unsafe.get refs id
let next_tick vm f =
Unsafe.meth_call vm "$nextTick" [| Unsafe.inject @@ wrap_meth_callback f |]
let get_router vm : Vue_router.router t = get_prop vm "router"
let get_store vm : 'state Vuex.store t = get_prop vm "store"
let get_route vm : ('b, 'c) Vue_router.route t = get_prop vm "route"
let store_2way ?(prefix="update") name =
(fun this -> Unsafe.get (Vuex.state (get_store this)) name),
(fun this value ->
Vuex.commit (get_store this) (prefix ^ "_" ^ name)
~payload:(to_any value))
let emit vm a = Unsafe.meth_call vm "$emit" a
let emit0 vm event = emit vm [| to_any @@ string event |]
let emit1 vm event o = emit vm [| to_any @@ string event; to_any o |]
let unhide ?(suffix="loading") id =
(match Dom_html.getElementById_opt id with
| None -> ()
| Some app -> app##.style##.display := string "block");
match Dom_html.getElementById_opt (id ^ "-" ^ suffix) with
| None -> ()
| Some loading -> loading##.style##.display := string "none"
module Make(S : sig
type data
type all
val id : string
end) = struct
type data = S.data
include Vue_component.Tables(struct type all = S.all end)
let app : all t ref = ref (Unsafe.obj [||])
let create ?computed ?methods ?watch ?components ?data ?router ?store
?render ?static_renders ?version () =
merge_lists_component ?computed ?methods ?watch ?components ();
app := create ?data
~methods:(T methods_t)
~watch:(T watch_t)
~computed:(T computed_t)
~components:(T components_t)
?router
?store
?render
?static_renders
?version ();
!app
let mount ?(export=true) ?(show=false) ?suffix ?version () =
app := mount ?version ~app:!app S.id;
if show then unhide ?suffix S.id;
if export then Mjs.export S.id !app;
!app
let init ?computed ?methods ?watch ?components
?(export=true) ?data ?(show=false) ?suffix ?router ?store ?render
?static_renders ?version () =
merge_lists_component ?computed ?methods ?watch ?components ();
app :=
make
?data
~methods:(T methods_t)
~watch:(T watch_t)
~computed:(T computed_t)
~components:(T components_t)
?router
?store
?render
?static_renders
?version
S.id;
if show then unhide ?suffix S.id;
if export then Mjs.export S.id !app;
!app
let app () = !app
end
module Router(S : sig
type data
type all
val id : string
end) = struct
type data = S.data
include Vue_component.Tables(struct type all = S.all end)
include Vue_router.Tables
let app : all vue ref = ref (Unsafe.obj [||])
let router : Vue_router.router t ref = ref (Unsafe.obj [||])
let init ?computed ?methods ?watch ?components ?routes
?(show=false) ?(export=true) ?suffix ?data ?mode ?store
?render ?static_renders ?version () =
merge_lists_component ?computed ?methods ?watch ?components ();
merge_routes ?routes ();
let rt = Vue_router.Internal.make_base ?version ?mode !routes_t in
router := rt;
app :=
make
?data
~methods:(T methods_t)
~watch:(T watch_t)
~computed:(T computed_t)
~components:(T components_t)
~router:rt
?store
?render ?static_renders ?version
S.id;
if show then unhide ?suffix S.id;
if export then Mjs.export S.id !app;
!app
let router () = !router
let app () = !app
end
module SPA(S : sig
type data
type all
type state
type getters
val name : string
val element : all Vue_component.component_element
val props : string list
end) = struct
type data = S.data
include Vuex.Module(struct type state = S.state type getters = S.getters end)
include Vue_component.Tables(struct type all = S.all end)
include Vue_router.Tables
let store : (state, getters) Vuex.module_obj t ref = ref (Unsafe.obj [||])
let route : (S.data, all) Vue_router.route t ref = ref (Unsafe.obj [||])
let add_2way ?(prefix="update") name =
let get, set = store_2way ~prefix name in
add_2way_computed name ~get ~set;
add_mutation (prefix ^ "_" ^ name) (fun st x -> Unsafe.set st name x)
let init ?getters ?mutations ?actions ?modules ?computed ?methods ?watch ?components
?children ?enter ?update ?path ?data ?state ?(state_to_computed=[]) ?(two_way=[])
() =
let data = match data with None -> Unsafe.obj [||] | Some d -> d in
let state = match state with None -> Unsafe.obj [||] | Some s -> s in
merge_lists_component ?computed ?methods ?watch ?components ();
merge_routes ?routes:children ();
List.iter add_2way two_way;
let template, render, static_renders = match S.element with
| Vue_component.CTemplate t -> Some t, None, None
| Vue_component.CRender (r, s) -> None, Some r, Some s in
let st = get ?getters ?mutations ?actions ?modules ~namespaced:true state in
let computed = Table.merge [
computed_t;
Vuex.Map.state ~namespace:S.name state_to_computed;
Vuex.Map.getters ~namespace:S.name !getters_to_computed ] in
let methods = Table.merge [
methods_t;
Vuex.Map.mutations ~namespace:S.name !mutations_to_methods;
Vuex.Map.actions ~namespace:S.name !actions_to_methods;
] in
let component = Some {
Vue_component.empty with
Vue_component.template = template;
render;
static_renders;
props = Some (Vue_component.PrsArray S.props);
data = Some (fun _ -> data);
computed = Some (T computed);
methods = Some (T methods);
watch = Some (T watch_t);
components = T components_t;
hook_enter = enter;
hook_update = update;
name = Some S.name } in
let path = match path with None -> "/" ^ S.name | Some path -> path in
let r = Vue_router.Internal.make_route_base {
(Vue_router.empty path) with
Vue_router.component;
props = (match S.props with [] -> None | _ -> Some (Vue_router.PrBool true));
children = Some !routes_t;
} in
store := st;
route := r;
S.name, st, r
let make ?getters ?mutations ?actions ?modules ?computed ?methods ?watch ?components
?children ?enter ?update ?path ?data ?state ?state_to_computed ?two_way () =
let name, st, r = init ?getters ?mutations ?actions ?modules ?computed ?methods ?watch ?components
?children ?enter ?update ?path ?data ?state ?state_to_computed ?two_way () in
name, coerce st, coerce r
let store () = !store
let route () = !route
end
module Root(S : sig
type data
type all
type state
type getters
val id : string
end) = struct
type data = S.data
include Vuex.Make(struct type state = S.state type getters = S.getters end)
include Vue_component.Tables(struct type all = S.all end)
include Vue_router.Tables
let app : all vue ref = ref (Unsafe.obj [||])
let router : Vue_router.router t ref = ref (Unsafe.obj [||])
let add_spa (name, store, route) =
add_route route;
add_module name store
let add_2way ?(prefix="update") name =
let get, set = store_2way ~prefix name in
add_2way_computed name ~get ~set;
add_mutation (prefix ^ "_" ^ name) (fun st x -> Unsafe.set st name x)
let init ?getters ?mutations ?actions ?modules ?computed ?methods ?watch
?components ?routes ?strict ?devtools ?plugins ?mode
?(show=false) ?(export=true) ?suffix ?data ?state ?(state_to_computed=[]) ?(two_way=[])
?render ?static_renders ?version () =
let data = match data with None -> Unsafe.obj [||] | Some d -> d in
let state = match state with None -> Unsafe.obj [||] | Some s -> s in
merge_lists_component ?computed ?methods ?watch ?components ();
merge_routes ?routes ();
List.iter add_2way two_way;
let store = init ?plugins ?getters ?mutations ?actions ?modules ?strict ?devtools state in
let computed = Table.merge [
computed_t;
Vuex.Map.state state_to_computed;
Vuex.Map.getters !getters_to_computed;
] in
let methods = Table.merge [
methods_t;
Vuex.Map.mutations !mutations_to_methods;
Vuex.Map.actions !actions_to_methods;
] in
let rt = Vue_router.Internal.make_base ?version ?mode !routes_t in
router := rt;
app :=
make
~data
~methods:(T methods)
~watch:(T watch_t)
~computed:(T computed)
~components:(T components_t)
~router:rt
~store
?render ?static_renders
?version
S.id;
if show then unhide ?suffix S.id;
if export then Mjs.export S.id !app;
!app
let router () = !router
let app () = !app
end