package alba

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file web_application.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
module type DECODER =
sig
    type _ t

    val return: 'msg -> 'msg t

    val float:  float  t
    val int:    int    t
    val string: string t
    val bool:   bool   t

    val field:  string -> 'msg t -> 'msg t
    val map: ('a -> 'b) -> 'a t -> 'b t
end



module type ENCODER =
sig
    type t

    val string: string -> t
    val bool:   bool -> t
    val object_: (string * t) list -> t
end




module type WEB_APPLICATION =
sig
    type _ decoder

    type encoder


    module Attribute:
    sig
        type 'msg t =
        | Style of string * string
        | Attribute of string * string
        | Property of string * encoder
        | On of string * 'msg decoder
    end


    module Dom:
    sig
        type 'msg t =
        | Text of string
        | Node of string * 'msg Attribute.t list * 'msg t list
    end


    module Command:
    sig
        type 'msg t =
        | None
        | Batch of 'msg t list

        | Http of
            string  (* type: GET, POST, ... *)
            *
            string  (* url *)
            *
            string  (* data to be sent with the request *)
            *
            ((string,int) result -> 'msg)
              (* response text or status e.g. 404 for not found *)
    end


    module Subscription:
    sig
        type 'msg t =
        | None
        | Batch of 'msg t list

        | Root of string * 'msg decoder (* events on the root element *)
    end
end




module type BROWSER =
sig
    module Decoder: DECODER

    module Encoder: ENCODER

    module Make:
    functor (Vapp: WEB_APPLICATION
                    with type 'msg decoder = 'msg Decoder.t
                    and  type encoder = Encoder.t)
    ->
    sig
        val sandbox:
            'model
            -> ('model -> 'msg Vapp.Dom.t)
            -> ('msg -> 'model -> 'model)
            -> unit


        val element:
            'a Decoder.t
            -> ('a -> 'model * 'msg Vapp.Command.t)
            -> ('model -> 'msg Vapp.Dom.t)
            -> ('msg -> 'model -> 'model * 'msg Vapp.Command.t)
            -> ('model -> 'msg Vapp.Subscription.t)
            -> unit
    end
end




module Make (Browser: BROWSER) =
struct
    module Decoder = Browser.Decoder
    module Encoder = Browser.Encoder

    type 'msg decoder = 'msg Decoder.t

    type encoder = Encoder.t


    module Attribute =
    struct
        type 'msg t =
        | Style of string * string
        | Attribute of string * string
        | Property of string * encoder
        | On of string * 'msg decoder

        let style (name: string) (value: string): 'msg t =
            Style (name, value)


        let attribute (name: string) (value: string): 'msg t =
            Attribute (name, value)


        let property (name: string) (value: encoder): 'msg t =
            Property (name, value)

        let on (name: string) (handler: 'msg decoder): 'msg t =
            On (name, handler)




        let string_property (name: string) (value: string): 'msg t =
            property name (Encoder.string value)

        let bool_property (name: string) (value: bool): 'msg t =
            property name (Encoder.bool value)

        let placeholder (value: string): 'msg t =
            attribute "placeholder" value

        let value (value: string): 'msg t =
            string_property "value" value

        let checked (value: bool): 'msg t =
            bool_property "checked" value

        let type_ (value: string): 'msg t =
            attribute "type" value

        let class_ (value: string): 'msg t =
            attribute "class" value


        let onClick (msg: 'msg): 'msg t =
            on "click" (Decoder.return msg)

        let onDoubleClick (msg: 'msg): 'msg t =
            on "doubleclick" (Decoder.return msg)

        let onMouseDown (msg: 'msg): 'msg t =
            on "mousedown" (Decoder.return msg)

        let onMouseUp (msg: 'msg): 'msg t =
            on "mouseup" (Decoder.return msg)

        let onMouseEnter (msg: 'msg): 'msg t =
            on "mouseenter" (Decoder.return msg)

        let onMouseLeave (msg: 'msg): 'msg t =
            on "mouseleave" (Decoder.return msg)

        let onMouseOver (msg: 'msg): 'msg t =
            on "mouseover" (Decoder.return msg)

        let onMouseOut (msg: 'msg): 'msg t =
            on "mouseout" (Decoder.return msg)




        let onKeyDown (f: string -> 'msg): 'msg t =
            on
                "keydown"
                Decoder.(map f (field "key" string))

        let onKeyUp (f: string -> 'msg): 'msg t =
            on
                "keyup"
                Decoder.(map f (field "key" string))




        let onInput (f: string -> 'msg): 'msg t =
            on
                "input"
                Decoder.(
                    field
                        "target"
                        (map
                            f
                            (field "value" string)))


        let onCheck (f: bool -> 'msg): 'msg t =
            on
                "click"
                Decoder.(
                    field
                        "target"
                        (map f
                             (field "checked" bool)))
    end



    module Dom =
    struct
        type 'msg t =
        | Text of string
        | Node of string * 'msg Attribute.t list * 'msg t list


        type 'msg attributes = 'msg Attribute.t list
        type 'msg children   = 'msg t list


        let text (s: string): 'msg t =
            Text s


        let node
            (tag: string)
            (attrs: 'msg attributes)
            (children: 'msg children)
            : 'msg t
            =
            Node (tag, attrs, children)


        let div (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "div" attrs children


        let span (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "span" attrs children



        let pre (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "pre" attrs children


        let p (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "p" attrs children


        let ol (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "ol" attrs children


        let ul (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "ul" attrs children


        let li (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "li" attrs children


        let h1 (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "h1" attrs children


        let h2 (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "h2" attrs children


        let h3 (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "h3" attrs children


        let h4 (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "h4" attrs children


        let h5 (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "h5" attrs children


        let h6 (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "h6" attrs children


        let b (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "b" attrs children

        let i (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "i" attrs children

        let strong (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "strong" attrs children





        let button (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "button" attrs children


        let input (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "input" attrs children

        let textarea (attrs: 'msg attributes) (children: 'msg children): 'msg t =
            node "textarea" attrs children
    end


    module Command =
    struct
        type 'msg t =
        | None
        | Batch of 'msg t list

        | Http of
            string  (* type: GET, POST, ... *)
            *
            string  (* url *)
            *
            string  (* data to be sent with the request *)
            *
            ((string,int) result -> 'msg)
              (* response text or status e.g. 404 for not found *)

        let http_get
            (url: string)
            (handler: (string,int) result -> 'msg)
            : 'msg t
            =
            Http ("GET", url, "", handler)
    end


    module Subscription =
    struct
        type 'msg t =
        | None
        | Batch of 'msg t list

        | Root of string * 'msg decoder (* events on the root element *)
    end
end

OCaml

Innovation. Community. Security.