action_event.erl 1.2 KB

123456789101112131415161718192021222324
  1. -module(action_event).
  2. -author('Maxim Sokhatsky').
  3. -author('Andrey Martemyanov').
  4. -include_lib("nitro/include/nitro.hrl").
  5. -include_lib("nitro/include/event.hrl").
  6. -compile(export_all).
  7. -define(B(E), nitro:to_binary(E)).
  8. render_action(#event{source=undefined}) -> [];
  9. render_action(#event{postback=Postback,actions=_A,source=Source,target=Control,type=Type,delegate=D,validation=V}) ->
  10. E = ?B(Control),
  11. ValidationSource = [ S || S <- Source, not is_tuple(S) ],
  12. PostbackBin = wf_event:new(Postback, E, D, event, data(E,Source), ValidationSource, V),
  13. ["{var x=qi('",E,"'); x && x.addEventListener('",?B(Type),
  14. "',function (event){ event.preventDefault(); ",PostbackBin,"});};"].
  15. data(E,SourceList) ->
  16. Type=fun(A) when is_atom(A) -> [ "atom('",atom_to_list(A),"')" ];
  17. (A) when is_binary(A) -> [ "bin('",binary_to_list(A),"')" ];
  18. (A) -> [ "string('",A,"')" ] end,
  19. list_to_binary(["[tuple(tuple(string('",E,"'),bin('detail')),[])",
  20. [ case S of {Id,Code} -> [ ",tuple(",Type(Id),",",Code,")" ];
  21. _ -> [ ",tuple(",Type(S),",querySource('",?B(S),"'))" ]
  22. end || S <- SourceList ],"]"]).