Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file lwt_js_events.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550(* Js_of_ocaml library
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2010 Vincent Balat
* Laboratoire PPS - CNRS Université Paris Diderot
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*)openJs_of_ocamllet(>>=)=Lwt.bindletasyncf=Lwt.async(fun()->Lwt_js.yield()>>=f)letmake_eventevent_kind?(use_capture=false)target=letel=refJs.nullinlett,w=Lwt.task()inletcancel()=Js.Opt.iter!elDom_html.removeEventListenerinLwt.on_canceltcancel;el:=Js.some(Dom.addEventListenertargetevent_kind(Dom_html.handler(fun(ev:#Dom_html.eventJs.t)->cancel();Lwt.wakeupwev;Js.booltrue))(* true because we do not want to prevent default ->
the user can use the preventDefault function
above. *)(Js.booluse_capture));tletcatch_cancelfx=Lwt.catch(fun()->fx)(function|Lwt.Canceled->Lwt.return()|e->Lwt.faile)letwith_error_logfx=Lwt.catch(fun()->fx)(fune->Firebug.console##log(Js.string(Printexc.to_stringe));Lwt.return())letseq_loopevh?(cancel_handler=false)?use_capturetargethandler=letcancelled=reffalseinletcur=ref(Lwt.fail(Failure"Lwt_js_event"))in(* Using Lwt.fail as default, to be polymorphic *)letcur_handler=ref(Lwt.return())inletlt,_lw=Lwt.task()inLwt.on_cancellt(fun()->Lwt.cancel!cur;ifcancel_handlerthenLwt.cancel!cur_handler;cancelled:=true);letrecaux()=ifnot!cancelled(* In the case it has been cancelled
during the previous handler,
we do not reinstall the event handler *)then(lett=evh?use_capturetargetincur:=t;t>>=fune->cur_handler:=with_error_log(handlere)lt;!cur_handler>>=aux)elseLwt.return()inLwt.async(catch_cancelaux);ltletasync_loopevh?use_capturetargethandler=letcancelled=reffalseinletcur=ref(Lwt.fail(Failure"Lwt_js_event"))inletlt,_lw=Lwt.task()inLwt.on_cancellt(fun()->Lwt.cancel!cur;cancelled:=true);letrecaux()=ifnot!cancelledthen(lett=evh?use_capturetargetincur:=t;t>>=fune->Lwt.async(fun()->with_error_log(handlere)lt);aux())elseLwt.return()inLwt.async(catch_cancelaux);ltletbuffered_loopevh?(cancel_handler=false)?(cancel_queue=true)?use_capturetargethandler=letcancelled=reffalseinletqueue=ref[]inletcur=ref(Lwt.fail(Failure"Lwt_js_event"))inletcur_handler=ref(Lwt.return())inletlt,_lw=Lwt.task()inletspawn=Lwt_condition.create()inLwt.on_cancellt(fun()->Lwt.cancel!cur;ifcancel_handlerthenLwt.cancel!cur_handler;ifcancel_queuethenqueue:=[];cancelled:=true);letrecspawner()=ifnot!cancelledthen(lett=evh?use_capturetargetincur:=t;t>>=fune->queue:=e::!queue;Lwt_condition.signalspawn();spawner())elseLwt.return()inletrecrunner()=cur_handler:=Lwt.return();ifnot!cancelledthen(match!queuewith|[]->Lwt_condition.waitspawn>>=runner|e::tl->queue:=tl;cur_handler:=with_error_log(handlere)lt;!cur_handler>>=runner)elseLwt.return()inLwt.async(catch_cancelspawner);Lwt.asyncrunner;ltletfunc_limited_loopeventlimited_func?use_capturetargethandler=letcount=ref0inasync_loopevent?use_capturetarget(funevlt->incrcount;letnb=!countinlimited_func()>>=fun_->if!count=nbthenhandlerevltelseLwt.return())letlimited_loopevent?(elapsed_time=0.1)=func_limited_loopevent(fun()->Lwt_js.sleepelapsed_time)letclick?use_capturetarget=make_eventDom_html.Event.click?use_capturetargetletdblclick?use_capturetarget=make_eventDom_html.Event.dblclick?use_capturetargetletmousedown?use_capturetarget=make_eventDom_html.Event.mousedown?use_capturetargetletmouseup?use_capturetarget=make_eventDom_html.Event.mouseup?use_capturetargetletmouseover?use_capturetarget=make_eventDom_html.Event.mouseover?use_capturetargetletmousemove?use_capturetarget=make_eventDom_html.Event.mousemove?use_capturetargetletmouseout?use_capturetarget=make_eventDom_html.Event.mouseout?use_capturetargetletkeypress?use_capturetarget=make_eventDom_html.Event.keypress?use_capturetargetletkeydown?use_capturetarget=make_eventDom_html.Event.keydown?use_capturetargetletkeyup?use_capturetarget=make_eventDom_html.Event.keyup?use_capturetargetletchange?use_capturetarget=make_eventDom_html.Event.change?use_capturetargetletinput?use_capturetarget=make_eventDom_html.Event.input?use_capturetargetlettimeupdate?use_capturetarget=make_eventDom_html.Event.timeupdate?use_capturetargetletdragstart?use_capturetarget=make_eventDom_html.Event.dragstart?use_capturetargetletdragend?use_capturetarget=make_eventDom_html.Event.dragend?use_capturetargetletdragenter?use_capturetarget=make_eventDom_html.Event.dragenter?use_capturetargetletdragover?use_capturetarget=make_eventDom_html.Event.dragover?use_capturetargetletdragleave?use_capturetarget=make_eventDom_html.Event.dragleave?use_capturetargetletdrag?use_capturetarget=make_eventDom_html.Event.drag?use_capturetargetletdrop?use_capturetarget=make_eventDom_html.Event.drop?use_capturetargetletfocus?use_capturetarget=make_eventDom_html.Event.focus?use_capturetargetletblur?use_capturetarget=make_eventDom_html.Event.blur?use_capturetargetletscroll?use_capturetarget=make_eventDom_html.Event.scroll?use_capturetargetletsubmit?use_capturetarget=make_eventDom_html.Event.submit?use_capturetargetletselect?use_capturetarget=make_eventDom_html.Event.select?use_capturetargetletabort?use_capturetarget=make_eventDom_html.Event.abort?use_capturetargetleterror?use_capturetarget=make_eventDom_html.Event.error?use_capturetargetletload?use_capturetarget=make_eventDom_html.Event.load?use_capturetargetletcanplay?use_capturetarget=make_eventDom_html.Event.canplay?use_capturetargetletcanplaythrough?use_capturetarget=make_eventDom_html.Event.canplaythrough?use_capturetargetletdurationchange?use_capturetarget=make_eventDom_html.Event.durationchange?use_capturetargetletemptied?use_capturetarget=make_eventDom_html.Event.emptied?use_capturetargetletended?use_capturetarget=make_eventDom_html.Event.ended?use_capturetargetletloadeddata?use_capturetarget=make_eventDom_html.Event.loadeddata?use_capturetargetletloadedmetadata?use_capturetarget=make_eventDom_html.Event.loadedmetadata?use_capturetargetletloadstart?use_capturetarget=make_eventDom_html.Event.loadstart?use_capturetargetletpause?use_capturetarget=make_eventDom_html.Event.pause?use_capturetargetletplay?use_capturetarget=make_eventDom_html.Event.play?use_capturetargetletplaying?use_capturetarget=make_eventDom_html.Event.playing?use_capturetargetletratechange?use_capturetarget=make_eventDom_html.Event.ratechange?use_capturetargetletseeked?use_capturetarget=make_eventDom_html.Event.seeked?use_capturetargetletseeking?use_capturetarget=make_eventDom_html.Event.seeking?use_capturetargetletstalled?use_capturetarget=make_eventDom_html.Event.stalled?use_capturetargetletsuspend?use_capturetarget=make_eventDom_html.Event.suspend?use_capturetargetletvolumechange?use_capturetarget=make_eventDom_html.Event.volumechange?use_capturetargetletwaiting?use_capturetarget=make_eventDom_html.Event.waiting?use_capturetarget(* special case for mousewheel, because it depends on the browser *)letmousewheel?(use_capture=false)target=letel=refJs.nullinlett,w=Lwt.task()inletcancel()=Js.Opt.iter!elDom_html.removeEventListenerinLwt.on_canceltcancel;el:=Js.some(Dom_html.addMousewheelEventListenertarget(fun(ev:#Dom_html.eventJs.t)~dx~dy->Firebug.console##logev;cancel();Lwt.wakeupw(ev,(dx,dy));Js.booltrue)(* true because we do not want to prevent default ->
the user can use the preventDefault function
above. *)(Js.booluse_capture));t(* let _DOMMouseScroll ?use_capture target =
make_event Dom_html.Event._DOMMouseScroll ?use_capture target
*)lettouchstart?use_capturetarget=make_eventDom_html.Event.touchstart?use_capturetargetlettouchmove?use_capturetarget=make_eventDom_html.Event.touchmove?use_capturetargetlettouchend?use_capturetarget=make_eventDom_html.Event.touchend?use_capturetargetlettouchcancel?use_capturetarget=make_eventDom_html.Event.touchcancel?use_capturetargetletclicks?cancel_handler?use_capturet=seq_loopclick?cancel_handler?use_capturetletdblclicks?cancel_handler?use_capturet=seq_loopdblclick?cancel_handler?use_capturetletmousedowns?cancel_handler?use_capturet=seq_loopmousedown?cancel_handler?use_capturetletmouseups?cancel_handler?use_capturet=seq_loopmouseup?cancel_handler?use_capturetletmouseovers?cancel_handler?use_capturet=seq_loopmouseover?cancel_handler?use_capturetletmousemoves?cancel_handler?use_capturet=seq_loopmousemove?cancel_handler?use_capturetletmouseouts?cancel_handler?use_capturet=seq_loopmouseout?cancel_handler?use_capturetletkeypresses?cancel_handler?use_capturet=seq_loopkeypress?cancel_handler?use_capturetletkeydowns?cancel_handler?use_capturet=seq_loopkeydown?cancel_handler?use_capturetletkeyups?cancel_handler?use_capturet=seq_loopkeyup?cancel_handler?use_capturetletchanges?cancel_handler?use_capturet=seq_loopchange?cancel_handler?use_capturetletinputs?cancel_handler?use_capturet=seq_loopinput?cancel_handler?use_capturetlettimeupdates?cancel_handler?use_capturet=seq_looptimeupdate?cancel_handler?use_capturetletdragstarts?cancel_handler?use_capturet=seq_loopdragstart?cancel_handler?use_capturetletdragends?cancel_handler?use_capturet=seq_loopdragend?cancel_handler?use_capturetletdragenters?cancel_handler?use_capturet=seq_loopdragenter?cancel_handler?use_capturetletdragovers?cancel_handler?use_capturet=seq_loopdragover?cancel_handler?use_capturetletdragleaves?cancel_handler?use_capturet=seq_loopdragleave?cancel_handler?use_capturetletdrags?cancel_handler?use_capturet=seq_loopdrag?cancel_handler?use_capturetletdrops?cancel_handler?use_capturet=seq_loopdrop?cancel_handler?use_capturetletmousewheels?cancel_handler?use_capturet=seq_loopmousewheel?cancel_handler?use_capturetlettouchstarts?cancel_handler?use_capturet=seq_looptouchstart?cancel_handler?use_capturetlettouchmoves?cancel_handler?use_capturet=seq_looptouchmove?cancel_handler?use_capturetlettouchends?cancel_handler?use_capturet=seq_looptouchend?cancel_handler?use_capturetlettouchcancels?cancel_handler?use_capturet=seq_looptouchcancel?cancel_handler?use_capturetletfocuses?cancel_handler?use_capturet=seq_loopfocus?cancel_handler?use_capturetletblurs?cancel_handler?use_capturet=seq_loopblur?cancel_handler?use_capturetletscrolls?cancel_handler?use_capturet=seq_loopscroll?cancel_handler?use_capturetletsubmits?cancel_handler?use_capturet=seq_loopsubmit?cancel_handler?use_capturetletselects?cancel_handler?use_capturet=seq_loopselect?cancel_handler?use_capturetletaborts?cancel_handler?use_capturet=seq_loopabort?cancel_handler?use_capturetleterrors?cancel_handler?use_capturet=seq_looperror?cancel_handler?use_capturetletloads?cancel_handler?use_capturet=seq_loopload?cancel_handler?use_capturetletcanplays?cancel_handler?use_capturet=seq_loopcanplay?cancel_handler?use_capturetletcanplaythroughs?cancel_handler?use_capturet=seq_loopcanplaythrough?cancel_handler?use_capturetletdurationchanges?cancel_handler?use_capturet=seq_loopdurationchange?cancel_handler?use_capturetletemptieds?cancel_handler?use_capturet=seq_loopemptied?cancel_handler?use_capturetletendeds?cancel_handler?use_capturet=seq_loopended?cancel_handler?use_capturetletloadeddatas?cancel_handler?use_capturet=seq_looploadeddata?cancel_handler?use_capturetletloadedmetadatas?cancel_handler?use_capturet=seq_looploadedmetadata?cancel_handler?use_capturetletloadstarts?cancel_handler?use_capturet=seq_looploadstart?cancel_handler?use_capturetletpauses?cancel_handler?use_capturet=seq_looppause?cancel_handler?use_capturetletplays?cancel_handler?use_capturet=seq_loopplay?cancel_handler?use_capturetletplayings?cancel_handler?use_capturet=seq_loopplaying?cancel_handler?use_capturetletratechanges?cancel_handler?use_capturet=seq_loopratechange?cancel_handler?use_capturetletseekeds?cancel_handler?use_capturet=seq_loopseeked?cancel_handler?use_capturetletseekings?cancel_handler?use_capturet=seq_loopseeking?cancel_handler?use_capturetletstalleds?cancel_handler?use_capturet=seq_loopstalled?cancel_handler?use_capturetletsuspends?cancel_handler?use_capturet=seq_loopsuspend?cancel_handler?use_capturetletvolumechanges?cancel_handler?use_capturet=seq_loopvolumechange?cancel_handler?use_capturetletwaitings?cancel_handler?use_capturet=seq_loopwaiting?cancel_handler?use_capturetlettransition_evn=lazy(lete=Dom_html.createDivDom_html.documentintrysnd(List.find(fun(propname,_evname)->Js.Unsafe.gete##.stylepropname!=Js.undefined)["WebkitTransition",[Dom.Event.make"webkitTransitionEnd"];"MozTransition",[Dom.Event.make"transitionend"];("OTransition",[Dom.Event.make"oTransitionEnd";Dom.Event.make"otransitionend"]);"transition",[Dom.Event.make"transitionend"]])withNot_found->[])lettransitionendelt=matchLazy.forcetransition_evnwith|[]->Lwt.return()|l->Lwt.pick(List.map(funev->make_eventevelt)l)>>=fun_->Lwt.return()lettransitionends?cancel_handlereltf=seq_loop(fun?use_capture:_target->transitionendtarget)?cancel_handlerelt(fun_cancel->fcancel)letrequest_animation_frame()=lett,s=Lwt.wait()inDom_html._requestAnimationFrame(Js.wrap_callback(fun()->Lwt.wakeups()));tletonload()=make_eventDom_html.Event.loadDom_html.windowletdomContentLoaded=letcomplete=Js.string"complete"inletdoc=Dom_html.window##.documentinfun()->ifdoc##.readyState=completethenLwt.return_unitelselett,w=Lwt.task()inletwakeupw_=ifLwt.is_sleepingtthenLwt.wakeupw()inletwakeup_exnwe=ifLwt.is_sleepingtthenLwt.wakeup_exnwein(* https://github.com/dperini/ContentLoaded/blob/master/src/contentloaded.js *)letregular=make_eventDom_html.Event.domContentLoadeddocinLwt.on_anyregular(wakeupw)(wakeup_exnw);(* ie8 *)letreadystatechange=async_loop(make_event(Dom.Event.make"readystatechange"))doc(fune_->ifdoc##.readyState=completethenwakeupwe;Lwt.return_unit)in(* fallback, just in case *)letinit=make_eventDom_html.Event.loadDom_html.windowinLwt.on_anyinit(wakeupw)(wakeup_exnw);(* clean and return *)Lwt.bindt(fun_e->Lwt.cancelregular;Lwt.cancelreadystatechange;Lwt.cancelinit;Lwt.return_unit)letonunload()=make_eventDom_html.Event.unloadDom_html.windowletonbeforeunload()=make_eventDom_html.Event.beforeunloadDom_html.windowletonresize()=make_eventDom_html.Event.resizeDom_html.windowletonorientationchange()=make_eventDom_html.Event.orientationchangeDom_html.windowletonpopstate()=make_eventDom_html.Event.popstateDom_html.windowletonhashchange()=make_eventDom_html.Event.hashchangeDom_html.windowletonorientationchange_or_onresize()=Lwt.pick[onresize();onorientationchange()]letonresizest=seq_loop(fun?use_capture:_()->onresize())()tletonorientationchangest=seq_loop(fun?use_capture:_()->onorientationchange())()tletonpopstatest=seq_loop(fun?use_capture:_()->onpopstate())()tletonhashchangest=seq_loop(fun?use_capture:_()->onhashchange())()tletonorientationchanges_or_onresizest=seq_loop(fun?use_capture:_()->onorientationchange_or_onresize())()tletlimited_onresizes?elapsed_timet=limited_loop(fun?use_capture:_()->onresize())?elapsed_time()tletlimited_onorientationchanges?elapsed_timet=limited_loop(fun?use_capture:_()->onorientationchange())?elapsed_time()tletlimited_onorientationchanges_or_onresizes?elapsed_timet=limited_loop(fun?use_capture:_()->onorientationchange_or_onresize())?elapsed_time()t