Fmlib_browser.CommandCommands to be executed.
An elementary command consists of a Task to be executed.
msg t is the type of a command generating an object of type msg to inject it into the update function of the application.
val none : _ tAn empty command.
val time_zone : (Time.Zone.t -> 'm) -> 'm tGet the time zone.
val focus : string -> 'm tfocus id
Focus the element id. If the element does not exist, then nothing is done. This command does not return any message.
val blur : string -> 'm tblur id
Blur the element id. If the element does not exist, then nothing is done. This command does not return any message.
val focus_with_info : string -> 'm -> 'm -> 'm tfocus_with_info id ok not_found
Focus the element id and return ok. Return not_found, if the element does not exist.
val blur_with_info : string -> 'm -> 'm -> 'm tblur_with_info id ok not_found
Blur the element id and return ok. Return not_found, if the element does not exist.
select_file media_types f Show the browser's file selection dialog and produce f file when the user selected a file. The given list of media_types allows restricting what file types are visible in the dialog (users can still select different file types if they want to).
NOTE: This command only works if it is triggered in reaction to a user event, such as a mouse click. This restriction is imposed by browsers for security reasons (websites should not be able to ask for file access without user interaction).
select_files media_types f The same as select_file but allows selecting multiple files at once.
NOTE: This command only works if it is triggered in reaction to a user event, such as a mouse click. This restriction is imposed by browsers for security reasons (websites should not be able to ask for file access without user interaction).
val file_text :
File.t ->
((string, Task.read_failed) Stdlib.result -> 'm) ->
'm tfile_text file f
Read the contents of file into a string result and produce the message f result when reading has finished. Reading can fail, e.g. in case of missing filesystem permissions.
val log_string : string -> 'm tPrint a string to the console, don't return a message.
val notify : int -> 'm -> 'm tnotify millis msg
Send msg in millis milliseconds.
val push_url : 'm Navigation.key -> string -> 'm tpush_url navigation_key url
Set the browser address bar to url and add an entry to the browser history. This requires a navigation_key which is only available in full web applications. See Navigation.key for details.
NOTE: For security reasons browsers don't allow setting an invalid URL or a URL with a different origin (protocol, hostname and port) than the current URL. Browsers may also enforce a rate limit for changing the URL. This means that if url is not a valid URL of the same origin or if we try to change the URL too frequently, an exception is thrown.
val replace_url : 'm Navigation.key -> string -> 'm treplace_url navigation_key url
Like push_url, but do not add a new entry to the browser history.
This is useful for changing the query part of the URL according to user input, e.g. ?search=hats. If push_url was used in this case, navigating back would undo a single keystroke whereas users would rather expect to go to the previous page.
NOTE: For security reasons browsers don't allow setting an invalid URL or a URL with a different origin (protocol, hostname and port) than the current URL. Browsers may also enforce a rate limit for changing the URL. This means that if url is not a valid URL of the same origin or if we try to change the URL too frequently, an exception is thrown.
val back : 'm Navigation.key -> int -> 'm tback navigation_key count
Navigate back count entries in the browser history. This requires a navigation_key which is only available in full web applications. See Navigation.key for details.
val forward : 'm Navigation.key -> int -> 'm tforward navigation_key count
Navigate forward count entries in the browser history. This requires a navigation_key which is only available in full web applications. See Navigation.key for details.
val load : string -> 'm tload url
Load the given url. This causes a traditional page load. For navigating to a different page within a single-page application, use push_url instead.
val reload : unit -> 'm treload ()
Reload the current page.
More details on reference nodes see Html.reference.
set_reference name vdom
Display vdom in the reference node name.
If a reference node name does not yet exist, then create a reference node.
val http_request :
string ->
string ->
(string * string) list ->
Http.Body.t ->
'm Http.Expect.t ->
(Http.error -> 'm) ->
'm thttp_request method url headers body expect on_error
Make an http method request to url with headers and body. expect specifies the expected response format and can be used to produce a message. on_error transforms a Http.error into a message.
Examples:
(* Send an empty body and expect a string response *)
let on_success msg = Got_message msg in
let on_error _ = Got_error "Failed to obtain message" in
http_request
"GET"
"/message/123"
[]
Http.Body.empty
(Http.Expect.map on_success Http.Expect.string)
on_error
(* Send file contents as the body and expect a json object with
field "url" *)
let on_success url = Got_file_uploaded url in
let on_error _ = Got_error "file upload failed" in
http_request
"PUT"
"/upload/my_file.txt"
[]
(Http.Body.file file)
(Http.Expect.json Decoder.(map on_success (field "url" string)))
on_errorIf a command wants to execute chains of simple commands before returning a message to the application, then it is necessary to create a task which does the more complex operation and perform the task within a command.
An object of type ('a, 'e) Task.t is a task which in case of success returns a value of type 'a and in case of failure returns a value of type 'e.
An object of type ('a, Task.empty) Task.t is a task which cannot fail.
attempt f task Attempt the possibly failing task and map the result via the function f into a message to send to the application.
val perform : ('m, Task.empty) Task.t -> 'm tperform task Perform the non failing task and send the message generated by the task to the application.
val just_do : (unit, Task.empty) Task.t -> 'm tperform task Perform the non failing task and don't send any message to the application.