2022-02-14 17:09:29 +01:00
|
|
|
let log = Format.printf
|
|
|
|
|
|
2022-02-18 01:37:25 +01:00
|
|
|
type image_size =
|
|
|
|
|
| Big
|
|
|
|
|
| Small
|
|
|
|
|
|
|
|
|
|
let of_string = function
|
2022-02-18 20:17:24 +01:00
|
|
|
| "post-image" -> Some Small
|
|
|
|
|
| "post-image-big" -> Some Big
|
2022-02-18 01:37:25 +01:00
|
|
|
| _ -> None
|
|
|
|
|
|
2022-02-18 20:17:24 +01:00
|
|
|
let to_string = function Small -> "post-image" | Big -> "post-image-big"
|
2022-02-18 01:37:25 +01:00
|
|
|
|
2022-02-23 14:30:06 +01:00
|
|
|
let document = Jv.get Jv.global "document"
|
|
|
|
|
|
2022-02-14 17:09:29 +01:00
|
|
|
(*change postImage class to make it bigger/smaller on click*)
|
|
|
|
|
let image_click post_image event =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "image_click@\n";
|
2022-02-14 17:09:29 +01:00
|
|
|
let current_class =
|
|
|
|
|
Jv.to_string @@ Jv.call post_image "getAttribute" [| Jv.of_string "class" |]
|
|
|
|
|
in
|
|
|
|
|
let new_class =
|
2022-02-18 01:37:25 +01:00
|
|
|
match of_string current_class with
|
2022-03-15 00:12:36 +01:00
|
|
|
| Some image_size -> ( match image_size with Big -> Small | Small -> Big )
|
2022-02-18 01:37:25 +01:00
|
|
|
| None -> failwith "invalid image class name"
|
2022-02-14 17:09:29 +01:00
|
|
|
in
|
|
|
|
|
ignore
|
|
|
|
|
@@ Jv.call post_image "setAttribute"
|
2022-03-15 00:12:36 +01:00
|
|
|
[| Jv.of_string "class"; Jv.of_string (to_string new_class) |];
|
|
|
|
|
let id =
|
|
|
|
|
Jv.to_string
|
|
|
|
|
@@ Jv.call post_image "getAttribute" [| Jv.of_string "data-id" |]
|
|
|
|
|
in
|
|
|
|
|
let src =
|
|
|
|
|
match new_class with
|
|
|
|
|
| Small -> Format.sprintf "/img/s/%s" id
|
|
|
|
|
| Big -> Format.sprintf "/img/%s" id
|
|
|
|
|
in
|
|
|
|
|
ignore
|
|
|
|
|
@@ Jv.call post_image "setAttribute"
|
|
|
|
|
[| Jv.of_string "src"; Jv.of_string src |];
|
2022-02-21 04:11:25 +01:00
|
|
|
(*prevent redirect to /img/:img*)
|
|
|
|
|
ignore @@ Jv.call event "preventDefault" [||];
|
|
|
|
|
ignore @@ Jv.call event "stopPropagation" [||]
|
2022-02-14 17:09:29 +01:00
|
|
|
|
|
|
|
|
let render_time date_span =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "render time@\n";
|
2022-02-18 03:22:24 +01:00
|
|
|
let t =
|
2022-02-23 14:30:06 +01:00
|
|
|
Jv.to_float
|
|
|
|
|
(Jv.call date_span "getAttribute" [| Jv.of_string "data-time" |])
|
2022-02-14 17:09:29 +01:00
|
|
|
in
|
2022-02-18 03:22:24 +01:00
|
|
|
let t = Unix.localtime t in
|
|
|
|
|
let date =
|
|
|
|
|
Format.sprintf "%02d-%02d-%02d %02d:%02d" (1900 + t.tm_year) (1 + t.tm_mon)
|
|
|
|
|
t.tm_mday t.tm_hour t.tm_min
|
2022-02-14 17:09:29 +01:00
|
|
|
in
|
2022-02-18 03:22:24 +01:00
|
|
|
ignore @@ Jv.set date_span "innerHTML" (Jv.of_string date)
|
2022-02-14 17:09:29 +01:00
|
|
|
|
2022-02-18 18:31:55 +01:00
|
|
|
let make_pretty _event =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "make pretty@\n";
|
2022-02-14 17:09:29 +01:00
|
|
|
|
|
|
|
|
let times =
|
|
|
|
|
Jv.to_jv_list
|
|
|
|
|
@@ Jv.call document "getElementsByClassName" [| Jv.of_string "date" |]
|
|
|
|
|
in
|
|
|
|
|
List.iter render_time times;
|
|
|
|
|
|
|
|
|
|
(*add event image_click to all postImage*)
|
|
|
|
|
let post_images =
|
|
|
|
|
Jv.to_jv_list
|
2022-02-18 20:17:24 +01:00
|
|
|
@@ Jv.call document "getElementsByClassName" [| Jv.of_string "post-image" |]
|
2022-02-14 17:09:29 +01:00
|
|
|
in
|
|
|
|
|
let add_click el =
|
|
|
|
|
ignore
|
|
|
|
|
@@ Jv.call el "addEventListener"
|
|
|
|
|
[| Jv.of_string "click"; Jv.repr (image_click el) |]
|
|
|
|
|
in
|
2022-02-22 01:48:37 +01:00
|
|
|
List.iter add_click post_images
|
2022-02-14 17:09:29 +01:00
|
|
|
|
|
|
|
|
(*make pretty after page load*)
|
|
|
|
|
let () =
|
2022-02-19 00:59:01 +01:00
|
|
|
log "add load eventlistener to make pretty@\n";
|
2022-02-14 17:09:29 +01:00
|
|
|
let window = Jv.get Jv.global "window" in
|
|
|
|
|
ignore
|
|
|
|
|
@@ Jv.call window "addEventListener"
|
2022-02-18 01:37:25 +01:00
|
|
|
[| Jv.of_string "load"; Jv.repr make_pretty |]
|