/modules/mod_signal/actions/action_signal_connect.erl

http://github.com/zotonic/zotonic · Erlang · 75 lines · 36 code · 13 blank · 26 comment · 0 complexity · 8755fb1710588123e86f5fd307f9ace4 MD5 · raw file

  1. %% @author Maas-Maarten Zeeman <mmzeeman@xs4all.nl>
  2. %% @copyright 2010 Maas-Maarten Zeeman
  3. %% Date: 2010-12-03
  4. %% @doc Connect a page to a signal
  5. %% Copyright 2010 Maas-Maarten Zeeman
  6. %%
  7. %% Licensed under the Apache License, Version 2.0 (the "License");
  8. %% you may not use this file except in compliance with the License.
  9. %% You may obtain a copy of the License at
  10. %%
  11. %% http://www.apache.org/licenses/LICENSE-2.0
  12. %%
  13. %% Unless required by applicable law or agreed to in writing, software
  14. %% distributed under the License is distributed on an "AS IS" BASIS,
  15. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. %% See the License for the specific language governing permissions and
  17. %% limitations under the License.
  18. -module(action_signal_connect).
  19. -author("Maas-Maarten Zeeman <mmzeeman@xs4all.nl>").
  20. -include("zotonic.hrl").
  21. -export([render_action/4, event/2, get_slot/2, store_slot/3, delete_slot/2]).
  22. render_action(TriggerId, TargetId, Args, Context) ->
  23. Signal = proplists:get_value(signal, Args),
  24. Actions = proplists:get_all_values(action, Args),
  25. Name = proplists:get_value(name, Args),
  26. Postback = {connect, [{signal, Signal}, {name, Name}, {actions, Actions}]},
  27. %% Genereer postback code voor het emitten van een signal.
  28. {Script, _Context} = z_render:make_postback(Postback, click, TriggerId, TargetId, ?MODULE, Context),
  29. {Script, Context}.
  30. %% @doc Connect a signal to a collection of actions.
  31. %
  32. event(#postback{message={connect, [{signal, Signal}, {name, Name}, {actions, Actions}]}}, Context) ->
  33. Slot = z_connect:page(Signal, Actions, Context),
  34. store_slot(Name, Slot, Context),
  35. Context.
  36. % @doc Store the slot under a name.
  37. %
  38. store_slot(undefined, _Slot, _Context) ->
  39. ok;
  40. store_slot(Name, Slot, Context) ->
  41. PageSlots = case z_context:get_page(slot_names, Context) of
  42. undefined -> [];
  43. S -> S
  44. end,
  45. z_context:set_page(slot_names, [{Name, Slot} | PageSlots], Context).
  46. % @doc Delete the slot with the given name.
  47. %
  48. delete_slot(undefined, _Context) -> ok;
  49. delete_slot(Name, Context) ->
  50. PageSlots = case z_context:get_page(slot_names, Context) of
  51. undefined -> [];
  52. S -> S
  53. end,
  54. UpdatedPageSlots = lists:keydelete(Name, 1, PageSlots),
  55. z_context:set_page(slot_names, [UpdatedPageSlots], Context).
  56. % @doc retrieve the slot with name
  57. %
  58. get_slot(undefined, _Context) ->
  59. undefined;
  60. get_slot(Name, Context) ->
  61. PageSlots = z_context:get_page(slot_names, Context),
  62. proplists:get_value(Name, PageSlots).