Browse Source

data in events

Namdak Tonpa 8 years ago
parent
commit
92a712aa3a
1 changed files with 12 additions and 9 deletions
  1. 12 9
      src/actions/action_event.erl

+ 12 - 9
src/actions/action_event.erl

@@ -1,18 +1,21 @@
 -module(action_event).
--author('Maxim Sokhatsky').
+
 -include_lib("nitro/include/nitro.hrl").
 -compile(export_all).
 
 render_action(#event{source=undefined}) -> [];
-render_action(#event{postback=Postback,actions=_Actions,source=Source,target=Control,type=Type,delegate=Delegate,validation=Validation}) ->
+render_action(#event{postback=Postback,actions=_A,source=Source,target=Control,type=Type,delegate=D,validation=V}) ->
     Element = nitro:to_list(Control),
-    Data=list_to_binary([<<"[tuple(tuple(utf8_toByteArray('">>,Element,<<"'),bin('detail')),[])">>,
-         [ begin {SrcType,Src2}=case is_atom(Src) of
-                 true -> { <<"atom">>,atom_to_list(Src) };
-                 false -> { <<"utf8_toByteArray">>,Src } end,
-             [ <<",tuple(">>,SrcType,<<"('">>,Src2,<<"'),querySource('">>,Src2,<<"'))">> ]
-             end || Src <- Source ],<<"]">>]),
-    PostbackBin = wf_event:new(Postback, Element, Delegate, event, Data, Source, Validation),
+    ValidationSource = [ S || S <- Source, not is_tuple(S) ],
+    PostbackBin = wf_event:new(Postback, Element, D, event, data(Element,Source), ValidationSource, V),
     [list_to_binary([<<"{ var x=qi('">>,Element,<<"'); x && x.addEventListener('">>,
         nitro:to_binary(Type),<<"',function (event){ ">>,
         PostbackBin,<<"});};">>])].
+
+data(Element,SourceList) ->
+    Type=fun(A) when is_atom(A) -> [ "atom('",atom_to_list(A),"')" ]; (A) -> [ "utf8_toByteArray('",A,"')" ] end,
+    list_to_binary([<<"[tuple(tuple(utf8_toByteArray('">>,Element,<<"'),bin('detail')),[])">>,
+        [ begin case S of
+                {Id,JsGetCode} -> [ ",tuple(",Type(Id),",",JsGetCode,")" ];
+                _ -> [ ",tuple(",Type(S),",querySource('",nitro:to_list(S),"'))" ]
+            end end || S <- SourceList ], <<"]">>]).