123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184(*********************************************************************************)(* Ojs-base *)(* *)(* Copyright (C) 2014-2021 INRIA. All rights reserved. *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Library General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** *)openJs_of_ocamlmoduleSMap=Map.Make(String)let(+=)map(key,v)=map:=SMap.addkeyv!maplet(-=)mapkey=map:=SMap.removekey!maptypeid=stringletlogs=Firebug.console##log(Js.strings);;letmk_msg_of_wsdataserver_msg_of_yojson=funs->trymatchserver_msg_of_yojson(Yojson.Safe.from_strings)withErrors->failwith(s^"\n"^s)|Okmsg->Somemsgwithe->log(Printexc.to_stringe);Noneletclass_s="ojs-"^sletsetup_wsurlmsg_of_data~onopen~onmessage=leton_messagews_event=trylog"message received on ws";matchmsg_of_data(Js.to_stringevent##.data)withNone->Js._false|Somemsg->onmessagewsmsg;Js._falsewithe->log(Printexc.to_stringe);Js._falseintrylog("connecting with websocket to "^url);letws=new%jsWebSockets.webSocket(Js.stringurl)inws##.onmessage:=Dom.full_handler(on_messagews);ws##.onclose:=Dom.handler(fun_->log"WS now CLOSED";Js._false);ws##.onopen:=Dom.handler(fun_->onopenws;Js._false);Somewswithe->log("Could not connect to "^url);log(Printexc.to_stringe);None;;letsend_msg(ws:WebSockets.webSocketJs.t)data=ws##send(Js.stringdata)letclear_childrennode=letchildren=node##.childNodesinfori=0tochildren##.length-1doJs.Opt.iternode##.firstChild(funn->Dom.removeChildnoden)doneletnode_by_idid=letnode=Dom_html.document##getElementById(Js.stringid)inJs.Opt.casenode(fun_->failwith("No node with id = "^id))(funx->x)letgen_id=letn=ref0infun()->incrn;Printf.sprintf"ojsid%d"!nletset_onclicknodef=ignore(Dom_html.addEventListenernodeDom_html.Event.click(Dom.handler(fune->fe;Js.booltrue))(Js.booltrue))(*c==v=[String.split_string]=1.2====*)letsplit_string?(keep_empty=false)schars=letlen=String.lengthsinletreciteraccpos=ifpos>=lenthenmatchaccwith""->ifkeep_emptythen[""]else[]|_->[acc]elseifList.mems.[pos]charsthenmatchaccwith""->ifkeep_emptythen""::iter""(pos+1)elseiter""(pos+1)|_->acc::(iter""(pos+1))elseiter(Printf.sprintf"%s%c"accs.[pos])(pos+1)initer""0(*/c==v=[String.split_string]=1.2====*)letget_classesnode=lets=Js.to_stringnode##.classNameinsplit_strings[' ']letnode_unset_classnodecl=node##.classList##remove(Js.stringcl)letnode_set_classnodecl=node##.classList##add(Js.stringcl)letunset_class~idcl=tryletnode=node_by_ididinnode_unset_classnodeclwithFailuremsg->logmsgletset_class~idcl=tryletnode=node_by_ididinnode_set_classnodeclwithFailuremsg->logmsgletmsg_base_class=class_"msg"letmsg_class_s=Printf.sprintf"%s-%s"msg_base_classsletdisplay_message?(timeout=3000.0)?(cl=msg_class_"info")idmsg_nodes=letdoc=Dom_html.documentinletnode=node_by_ididinletdiv=doc##createElement(Js.string"div")innode_set_classdivcl;node_set_classdivmsg_base_class;iftimeout>0.thenignore(Dom_html.window##setTimeout(Js.wrap_callback(fun()->Dom.removeChildnodediv))timeout)else(letb=doc##createElement(Js.string"span")innode_set_classb(msg_class_"close");lett=doc##createTextNode(Js.string"✘")inset_onclickb(fun_->Dom.removeChildnodediv);Dom.appendChilddivb;Dom.appendChildbt);Dom.appendChildnodediv;List.iter(Dom.appendChilddiv)msg_nodesletdisplay_erroridnodes=display_message~timeout:0.~cl:(msg_class_"error")idnodesletdisplay_text_message?timeout?clidtext=lett=Dom_html.document##createTextNode(Js.stringtext)indisplay_message?timeout?clid[t]letdisplay_text_erroridtext=lett=Dom_html.document##createTextNode(Js.stringtext)indisplay_errorid[t]