wf_event.erl 1.1 KB

123456789101112131415161718192021
  1. -module(wf_event).
  2. -author('Maxim Sokhatsky').
  3. -include_lib ("nitro/include/nitro.hrl").
  4. -compile(export_all).
  5. -record(cx, { handlers, actions, req, module, lang, path, session, params, form, state=[] }).
  6. -define(CTX, (get(context))).
  7. new(bin,Data) -> <<"ws.send(enc(tuple(atom('bin'),bin('",(wf:pickle(Data))/binary,"'))));">>.
  8. new(undefined, _, _, _, _, _) -> <<>>;
  9. new(Postback, Element, Delegate, Name, Data, Source) ->
  10. Module = nitro:coalesce([Delegate, ?CTX#cx.module]),
  11. Join=fun([]) -> []; ([E]) -> [$'|E]++[$'];
  12. ([H|T]) -> [[$'|H]++[$']] ++ [ [$,,$'|E]++[$'] || E <- T ] end,
  13. Event = #ev{name=Name, module=Module, msg=Postback, trigger=Element},
  14. erlang:list_to_binary([ <<"{ if (validateSources([">>,
  15. Join([ case is_atom(S) of true -> atom_to_list(S); false -> S end || S <- Source, S =/= []]),
  16. <<"])) ws.send(enc(tuple(atom('">>,nitro:to_binary(application:get_env(n2o,event,pickle)),
  17. <<"'),bin('">>,Element,<<"'),bin('">>,nitro:pickle(Event),<<"'),">>,Data,
  18. <<")));else console.log('Validation Error'); }">> ]).