PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_base/actions/action_base_update.erl

https://code.google.com/p/zotonic/
Erlang | 94 lines | 69 code | 8 blank | 17 comment | 0 complexity | 57766de58b20b5374889640338957250 MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2009 Marc Worrell
 3%% Date: 2009-07-16
 4%% @doc Replace the content of the target with the result of a render action.
 5
 6%% Copyright 2009 Marc Worrell
 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_base_update).
21-include("zotonic.hrl").
22-export([
23    render_action/4,
24    render_update/5,
25    event/2
26]).
27
28render_action(TriggerId, TargetId, Args, Context) ->
29    render_update(update, TriggerId, TargetId, Args, Context).
30    
31render_update(Method, TriggerId, TargetId, Args, Context) ->
32    Html = case proplists:get_value(template, Args) of
33        undefined -> proplists:get_value(text, Args, "");
34        Template -> #render{template=Template, vars=Args}
35    end,
36    case {TargetId, Html} of
37        {undefined,_} -> render_inline(Method, TargetId, Html, Args, Context);
38        {_,#render{}} -> render_postback(Method, TriggerId, TargetId, Html, Args, Context);
39        {_,_} -> render_static(Method, TargetId, Html, Args, Context)
40    end.
41
42render_inline(Method, TargetId, Html, Args, Context) ->
43    Sel = z_render:css_selector(TargetId, Args),
44    case proplists:get_value(appear, Args) of
45        true ->
46            case Method of
47                update        -> {[], z_render:appear_selector(Sel, Html, Context)};
48		replace	      -> {[], z_render:appear_replace_selector(Sel, Html, Context)};
49                insert_top    -> {[], z_render:appear_top_selector(Sel, Html, Context)};
50                insert_bottom -> {[], z_render:appear_bottom_selector(Sel, Html, Context)};
51		insert_before -> {[], z_render:appear_before_selector(Sel, Html, Context)};
52		insert_after  -> {[], z_render:appear_after_selector(Sel, Html, Context)}
53            end;
54        _ -> 
55            case Method of
56                update        -> {[], z_render:update_selector(Sel, Html, Context)};
57		replace       -> {[], z_render:replace_selector(Sel, Html, Context)};
58                insert_top    -> {[], z_render:insert_top_selector(Sel, Html, Context)};
59                insert_bottom -> {[], z_render:insert_bottom_selector(Sel, Html, Context)};
60		insert_before -> {[], z_render:insert_before_selector(Sel, Html, Context)};
61		insert_after  -> {[], z_render:insert_after_selector(Sel, Html, Context)}
62            end
63    end.
64
65render_static(Method, TargetId, Html, Args, Context) ->
66    Sel = z_render:css_selector(TargetId, Args),
67    case proplists:get_value(appear, Args) of
68        true -> 
69            case Method of
70                update        -> {z_render:appear_selector_js(Sel, Html), Context};
71		replace       -> {z_render:appear_replace_selector_js(Sel, Html), Context};
72                insert_top    -> {z_render:appear_top_selector_js(Sel, Html), Context};
73                insert_bottom -> {z_render:appear_bottom_selector_js(Sel, Html), Context};
74		insert_before -> {z_render:appear_before_selector_js(Sel, Html), Context};
75		insert_after  -> {z_render:appear_after_selector_js(Sel, Html), Context}
76            end;
77        _ -> 
78            case Method of 
79                update        -> {z_render:update_selector_js(Sel, Html), Context};
80		    replace   -> {z_render:replace_selector_js(Sel, Html), Context};
81                insert_top    -> {z_render:insert_top_selector_js(Sel, Html), Context};
82                insert_bottom -> {z_render:insert_bottom_selector_js(Sel, Html), Context};
83		insert_before -> {z_render:insert_before_selector_js(Sel, Html), Context};
84		insert_after  -> {z_render:insert_after_selector_js(Sel, Html), Context}
85            end
86    end.
87
88render_postback(Method, TriggerId, TargetId, Html, Args, Context) ->
89    {PostbackMsgJS, _PickledPostback} = z_render:make_postback({render, Method, Html, Args}, undefined, TriggerId, TargetId, ?MODULE, Context),
90    {PostbackMsgJS, Context}.
91
92event({postback, {render, Method, Html, Args}, _TriggerId, TargetId}, Context) ->
93    {[], Context1} = render_inline(Method, TargetId, Html, Args, Context),
94    Context1.