-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfig.ml
252 lines (217 loc) · 8.27 KB
/
config.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
(*
Copyright 2013 Sébastien Ferré, IRISA, Université de Rennes 1, ferre@irisa.fr
This file is part of Sparklis.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
open Js_of_ocaml
open Js
open Jsutils
(* templates *)
class virtual input =
object
val mutable endpoint : string = ""
val mutable has_changed : bool = false
val mutable callbacks : (unit -> unit) list = []
method set_endpoint (url : string) : unit = endpoint <- url
method on_change callback = callbacks <- callback::callbacks
method private changed = has_changed <- true; List.iter (fun callback -> callback ()) callbacks
method has_changed : bool = has_changed
method reset_changed : unit = has_changed <- false
method virtual init : unit
method virtual reset : unit
method virtual get_permalink : (string * string) list
method virtual set_permalink : (string * string) list -> unit
end
class boolean_input ~(key : string) ~(input_selector : string) ~default () =
object (self)
inherit input
val mutable init_v : bool = default
val mutable current_v : bool = default
method set_value (v : bool) : unit =
if v <> current_v then begin
jquery_input input_selector (fun input -> input##.checked := bool v);
current_v <- v;
self#changed
end
method value : bool = current_v
method get_permalink =
if current_v <> default
then [(key, string_of_bool current_v)]
else []
method set_permalink args =
self#set_value (try bool_of_string (List.assoc key args) with _ -> default)
method init =
jquery_input input_selector (fun input ->
init_v <- to_bool input##.checked; (* default value from HTML *)
current_v <- init_v;
onclick
(fun input ev ->
let v = to_bool input##.checked in
if v <> current_v then begin
current_v <- v;
self#changed
end)
input)
method reset = self#set_value init_v
end
class integer_input ~(key : string) ~(input_selector : string) ?min ?max ~default () =
object (self)
inherit input
val mutable init_v : int = default (* default value for reset *)
val mutable current_v : int = default (* current value *)
method private set_input (v : int) : unit =
if v <> current_v then begin
jquery_input input_selector (fun input -> input##.value := string (string_of_int v));
current_v <- v;
self#changed
end
method value : int = current_v
method get_permalink =
if current_v <> default
then [(key, string_of_int current_v)]
else []
method set_permalink args =
self#set_input (try int_of_string (List.assoc key args) with _ -> default)
method init =
jquery_input input_selector (fun input ->
init_v <- (match integer_of_input ?min ?max input with Some v -> v | None -> default); (* default value from HTML *)
current_v <- init_v;
oninput
(fun input ev ->
match integer_of_input ?min ?max input with
| Some v ->
input##.style##.color := string "black";
if current_v <> v then begin
current_v <- v;
self#changed
end
| None ->
input##.style##.color := string "red")
input;
onchange
(fun input ev ->
input##.value := string (string_of_int current_v);
input##.style##.color := string "black")
input)
method reset = self#set_input init_v
end
class string_input ~(key : string) ~(input_selector : string) ~default () =
(* only requires a ##value property at [input_selector] *)
object (self)
inherit input
val mutable init_v : string = default (* default value for reset *)
val mutable current_v : string = default (* current value *)
method private set_input (v : string) : unit =
if v <> current_v then begin
jquery_input input_selector (fun input -> input##.value := string v);
current_v <- v;
self#changed
end
method value : string = current_v
method get_permalink =
if current_v <> default
then [(key, current_v)]
else []
method set_permalink args =
self#set_input (try List.assoc key args with _ -> default)
method init =
jquery_input input_selector (fun input ->
init_v <- to_string input##.value; (* default value from HTML *)
current_v <- init_v;
oninput
(fun input ev ->
let v = to_string input##.value in
if current_v <> v then begin
current_v <- v;
self#changed
end)
input)
method reset = self#set_input init_v
end
class select_input ~(key : string) ~(select_selector : string) ~default () =
object (self)
inherit input
val mutable init_v : string = default (* default value for reset *)
val mutable current_v : string = default (* current value *)
method private set_select (v : string) : unit =
if v <> current_v then begin
jquery_select select_selector
(fun select -> select##.value := string v);
current_v <- v;
self#changed
end
method value : string = current_v
method get_permalink =
if current_v <> default
then [(key, current_v)]
else []
method set_permalink args =
self#set_select (try List.assoc key args with _ -> default)
method init =
jquery_select select_selector (fun select ->
init_v <- to_string select##.value; (* default value from HTML *)
current_v <- init_v;
onchange
(fun select ev ->
let v = to_string select##.value in
if current_v <> v then begin
current_v <- v;
self#changed
end)
select)
method reset = self#set_select init_v
end
(* JS object for Sparklis extension *)
type hook = Unsafe.top optdef (* optionally defined ('a -> Promise('a or undefined, error)) functions on JS objects, which can be used to add side effects and to modify the data back into Sparklis *)
let apply_hook_opt (what : string) (hook : hook) (js_args : Unsafe.any array) (k : Unsafe.any option -> unit) : unit =
Optdef.case hook
(fun () -> k None) (* if hook undefined *)
(fun callback ->
Jsutils.firebug ("applying hook for " ^ what);
Jsutils.promise_then
(Unsafe.fun_call callback js_args)
(fun js_res ->
if js_res = Jsutils.Inject.undefined
then k None (* undefined result (side-effect only extension) *)
else k (Some js_res))
(fun js_error -> (* catching any error thrown by callback *)
Firebug.console##log js_error; (* logging the error *)
k None)) (* and falling back to default behavior *)
(* apply a hook, if defined, to some Sparklis data [x], given functions for injection to and extraction from JS objects. *)
let apply_hook_data (what : string) (hook : hook) (map : 'a js_map) (x : 'a) (k : 'a -> unit) : unit =
Optdef.case hook
(fun () -> k x) (* identity if hook undefined *)
(fun callback ->
Jsutils.firebug ("applying hook on " ^ what);
let js_x = map.inject x in
(* Firebug.console##log_2 (string "BEFORE hook: ") js_x; *)
Jsutils.promise_then
(Unsafe.fun_call callback [|js_x|])
(fun js_y ->
if js_y = Jsutils.Inject.undefined
then (
(* Jsutils.firebug "AFTER hook: undefined"; *)
k x) (* use original result if undefined result (side-effect only extension) *)
else (
(* Firebug.console##log_2 (string "AFTER hook: ") js_y; *)
let y = map.extract js_y in
k y))
(fun js_error -> (* catching any error thrown by callback *)
Firebug.console##log js_error; (* logging the error *)
k x)) (* and falling back to default data *)
let sparklis_extension =
object%js (self)
val mutable hookSparql : hook = undefined (* data: string (SPARQL query) *)
val mutable hookResults : hook = undefined (* data : Sparql_endpoint.results *)
val mutable hookSuggestions : hook = undefined (* data: Lis.freq_unit * Lis.suggestions *)
val mutable hookApplySuggestion : hook = undefined (* place -> increment -> place *)
end
let () = Js.export "sparklis_extension" sparklis_extension