Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file form.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178(* Js_of_ocaml library
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2011 Pierre Chambart
* 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.
*)openJsopenDom_htmlopen!ImportclasstypeformData=objectmethodappend:js_stringt->js_stringt->unitmethmethodappend_blob:js_stringt->File.blobt->unitmethendletformData:formDatatconstr=Js.Unsafe.global##._FormDataletformData_form:(formElementt->formDatat)constr=Js.Unsafe.global##._FormDatatypeform_elt=[`Stringofjs_stringt|`FileofFile.filet]typeform_contents=[`Fieldsof(string*form_elt)listref|`FormDataofformDatat]letrecfilter_mapf=function|[]->[]|v::q->(matchfvwith|None->filter_mapfq|Somev'->v'::filter_mapfq)classtypesubmittableElement=objectmethoddisabled:booltpropmethodname:js_stringtreadonly_propmethodvalue:js_stringtpropendlethave_content(elt:submittableElementt)=elt##.name##.length>0&¬(Js.to_boolelt##.disabled)letget_textarea_val(elt:textAreaElementt)=ifhave_content(elt:>submittableElementt)thenletname=to_stringelt##.namein[name,`Stringelt##.value]else[]letget_select_val(elt:selectElementt)=ifhave_content(elt:>submittableElementt)thenletname=to_stringelt##.nameinifto_boolelt##.multiplethenletoptions=Array.initelt##.options##.length(funi->Opt.to_option(elt##.options##itemi))infilter_map(function|None->None|Somee->ifJs.to_boole##.selectedthenSome(name,`Stringe##.value)elseNone)(Array.to_listoptions)else[name,`Stringelt##.value]else[]classtypefile_input=objectinheritinputElementmethodfiles:File.fileListtoptdefreadonly_propmethodmultiple:booloptdefreadonly_propendletget_input_val?(get=false)(elt:inputElementt)=ifhave_content(elt:>submittableElementt)thenletname=to_stringelt##.nameinletvalue=elt##.valueinmatchto_bytestringelt##._type##toLowerCasewith|"checkbox"|"radio"->ifto_boolelt##.checkedthen[name,`Stringvalue]else[]|"submit"|"reset"->[]|"text"|"password"->[name,`Stringvalue]|"file"->(ifgetthen[name,`Stringvalue]elseletelt:file_inputt=Unsafe.coerceeltinmatchOptdef.to_optionelt##.fileswith|None->[]|Somelist->(iflist##.length=0then[name,`String(Js.string"")]elsematchOptdef.to_optionelt##.multiplewith|None|Somefalse->(matchOpt.to_option(list##item0)with|None->[]|Somefile->[name,`Filefile])|Sometrue->filter_map(funf->matchOpt.to_optionfwith|None->None|Somefile->Some(name,`Filefile))(Array.to_list(Array.initlist##.length(funi->list##itemi)))))|_->[name,`Stringvalue]else[]letform_elements?get(form:formElementt)=letlength=form##.elements##.lengthinletelements=Array.to_list(Array.initlength(funi->Opt.to_option(form##.elements##itemi)))inletcontents=List.flatten(List.map(function|None->[](* shouldn't happen *)|Somev->(matchtaggedvwith|Selectv->get_select_valv|Inputv->get_input_val?getv|Textareav->get_textarea_valv|_->[]))elements)incontentsletappend(form_contents:form_contents)(form_elt:string*form_elt)=matchform_contentswith|`Fieldslist->list:=form_elt::!list|`FormDataf->(matchform_eltwith|name,`Strings->f##append(stringname)s|name,`Filefile->f##append_blob(stringname)(file:>File.blobt))letempty_form_contents()=matchOptdef.to_option(Js.defformData)with|None->`Fields(ref[])|Someconstr->`FormData(new%jsconstr)letpost_form_contentsform=letcontents=empty_form_contents()inList.iter(appendcontents)(form_elementsform);contentsletget_form_contentsform=List.map(function|name,`Strings->name,to_strings|_->assertfalse)(form_elements~get:trueform)