PageRenderTime 24ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

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