<commit_before>
open Firebug

module Dom_html = struct
  include Dom_html
  open Js
  class type _mediaElement = object
    inherit mediaElement
    method onprogress : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
    method ontimeupdate : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
    method onplay : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
    method onpause : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
    method onloadeddata : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
  end
  module Coerce = struct
    include CoerceTo
    let unsafeCoerce tag (e : #element t) = Js.some (Js.Unsafe.coerce e)
    let media :  #element t -> _mediaElement t opt = fun e -> unsafeCoerce "media" e
  end
end

let error f = Printf.ksprintf
    (fun s -> Firebug.console##error (Js.string s); failwith s) f
let debug f = Printf.ksprintf
    (fun s -> Firebug.console##log(Js.string s)) f
let alert f = Printf.ksprintf
    (fun s -> Dom_html.window##alert(Js.string s); failwith s) f

let (@>) s coerce =
  Js.Opt.get (coerce @@ Dom_html.getElementById s)
    (fun () -> error "can't find element %s" s)



let str s = Js.some @@ Js.string s

let ftime_to_min_sec t =
  let sec = int_of_float t in
  let min = sec / 60 in
  let sec = sec mod 60 in
  (min, sec)

let max_slide = 1000.

let set_visible b elt =
  let visibility = if b then "visible" else "hidden"
  in elt##.style##.visibility := Js.string visibility

let update_slider slider media =
  slider##.value := Js.string @@ Format.sprintf "%0.f" (
      if media##.duration = 0. then 0.
      else media##.currentTime /. media##.duration *. max_slide)

let update_slider_value slider_value media slider =
  let min, sec = ftime_to_min_sec @@
    ((Js.parseFloat slider##.value) /. max_slide *. media##.duration)
  in
  set_visible true slider_value;
  let padding =
    Format.sprintf "%0.fpx" (Js.parseFloat slider##.value /. max_slide *. (float_of_int slider##.scrollWidth))
  in
  slider_value##.style##.marginLeft := Js.string padding;
  slider_value##.textContent := Js.some @@ Js.string @@ Format.sprintf "%2dmin%2ds" min sec

let update_media media slider =
  media##.currentTime := (Js.parseFloat slider##.value) /. max_slide *. media##.duration

let update_state state media button =
  if state then media##play else media##pause


let update_time_a media time_a =
  let cmin, csec = ftime_to_min_sec media##.currentTime in
  let tmin, tsec = ftime_to_min_sec media##.duration in
  time_a##.textContent := str @@ Format.sprintf "%2d:%0d / %0d:%0d" cmin csec tmin tsec

let update_content elt b =
  elt##.textContent := Js.some @@ Js.string @@ if b then "Pause" else "Play"


let%sync reactive_player ~animate
    (play_pause : buttonElement Js.t)(* the button *)
    progress_bar (* the progress element *)
    media (* the video element *)
    time_a (* the a elt displaying time*)
    slider_value (* the a elt displaying time*)
  =

  let no_update = () in
  let state = Js.to_bool media##.autoplay in
  !(update_content play_pause (pre state));

  loop (
    present media##onplay !(
      update_content play_pause (pre state))

    || present media##onpause !(
      update_content play_pause (pre state))

    || present play_pause##onclick (
      emit state (not (pre state)))

    || present state !(
      update_state (pre state) media play_pause)
  ; pause)

  || loop (
    present progress_bar##oninput
      !(update_slider_value (pre slider_value) media progress_bar);
    pause
  )
  || loop (
    await progress_bar##onmousedown;
    trap t' (
      loop (
        emit no_update ();
        present progress_bar##onmouseup (
          !(set_visible false (pre slider_value);
           update_media media progress_bar);
          exit t'
          ); pause)); pause)

  || loop (
    present media##ontimeupdate (
      present no_update nothing !(update_slider progress_bar media)
      ||
      !(update_time_a media (pre time_a))
    ); pause)


let wrapper react f p = Dom_html.handler (fun _ -> f p;  react (); Js._true)<commit_msg>fix example<commit_after>
open Firebug

module Dom_html = struct
  include Dom_html
  open Js
  class type _mediaElement = object
    inherit mediaElement
    method onprogress : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
    method ontimeupdate : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
    method onplay : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
    method onpause : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
    method onloadeddata : (_mediaElement t, mouseEvent t) event_listener writeonly_prop
  end
  module Coerce = struct
    include CoerceTo
    let unsafeCoerce tag (e : #element t) = Js.some (Js.Unsafe.coerce e)
    let media :  #element t -> _mediaElement t opt = fun e -> unsafeCoerce "media" e
  end
end

let error f = Printf.ksprintf
    (fun s -> Firebug.console##error (Js.string s); failwith s) f
let debug f = Printf.ksprintf
    (fun s -> Firebug.console##log(Js.string s)) f
let alert f = Printf.ksprintf
    (fun s -> Dom_html.window##alert(Js.string s); failwith s) f

let (@>) s coerce =
  Js.Opt.get (coerce @@ Dom_html.getElementById s)
    (fun () -> error "can't find element %s" s)



let str s = Js.some @@ Js.string s

let ftime_to_min_sec t =
  let sec = int_of_float t in
  let min = sec / 60 in
  let sec = sec mod 60 in
  (min, sec)

let max_slide = 1000.

let set_visible b elt =
  let visibility = if b then "visible" else "hidden"
  in elt##.style##.visibility := Js.string visibility

let update_slider slider media =
  slider##.value := Js.string @@ Format.sprintf "%0.f" (
      if media##.duration = 0. then 0.
      else media##.currentTime /. media##.duration *. max_slide)

let update_slider_value slider_value media slider =
  let min, sec = ftime_to_min_sec @@
    ((Js.parseFloat slider##.value) /. max_slide *. media##.duration)
  in
  set_visible true slider_value;
  let padding =
    Format.sprintf "%0.fpx" (Js.parseFloat slider##.value /. max_slide *. (float_of_int slider##.scrollWidth))
  in
  slider_value##.style##.marginLeft := Js.string padding;
  slider_value##.textContent := Js.some @@ Js.string @@ Format.sprintf "%2dmin%2ds" min sec

let update_media media slider =
  media##.currentTime := (Js.parseFloat slider##.value) /. max_slide *. media##.duration

let update_state state media button =
  if state then media##play else media##pause


let update_time_a media time_a =
  let cmin, csec = ftime_to_min_sec media##.currentTime in
  let tmin, tsec = ftime_to_min_sec media##.duration in
  time_a##.textContent := str @@ Format.sprintf "%2d:%0d / %0d:%0d" cmin csec tmin tsec

let update_content elt b =
  elt##.textContent := Js.some @@ Js.string @@ if b then "Pause" else "Play"


open Dom_html
let%sync reactive_player ~animate
    (play_pause : buttonElement Js.t)
    (progress_bar : inputElement Js.t)
    (media : _mediaElement Js.t)
    (time_a : anchorElement Js.t)
    (slider_value : anchorElement Js.t)(* the a elt displaying time*)
  =

  let no_update = () in
  let state = Js.to_bool media##.autoplay in
  !(update_content play_pause (pre state));

  loop (
    present media##onplay !(
      update_content play_pause !!state)

    || present media##onpause !(
      update_content play_pause !!state)

    || present play_pause##onclick (emit state (not !!state))

    || present state !(
      update_state !!state media play_pause)
  ; pause)

  || loop (
    present progress_bar##oninput
      !(update_slider_value !!slider_value media progress_bar);
    pause
  )
  || loop (
    await progress_bar##onmousedown;
    trap t' (
      loop (
        emit no_update;
        present progress_bar##onmouseup (
          !(set_visible false (pre slider_value);
           update_media media progress_bar);
          exit t'
          ); pause)); pause)

  || loop (
    present media##ontimeupdate (
      present no_update nothing !(update_slider progress_bar media)
      ||
      !(update_time_a media (pre time_a))
    ); pause)


let wrapper react f p = Dom_html.handler (fun _ -> f p;  react (); Js._true)
{'fraction_non_alphanumeric': 0.10994380649890056, 'fraction_numerical': 0.004397752259956022, 'mean_word_length': 3.343236074270557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3931411', 'n_tokens_mistral': 2774, 'n_tokens_neox': 2663, 'n_words': 816}