PageRenderTime 26ms CodeModel.GetById 1ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_base/actions/action_base_update.erl

http://github.com/zotonic/zotonic
Erlang | 104 lines | 79 code | 8 blank | 17 comment | 0 complexity | 8439f441b7b1785719e7e8bcf6d0d846 MD5 | raw file
  1%% @author Marc Worrell <marc@worrell.nl>
  2%% @copyright 2009-2015 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-2015 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 ->
 34            proplists:get_value(text, Args, "");
 35        Template -> 
 36            IsAll = proplists:get_value('include_all', Args),
 37            case z_convert:to_bool(proplists:get_value('catinclude', Args)) of
 38                true ->
 39                    #render{template={cat, Template}, vars=Args, is_all=IsAll};
 40                false ->
 41                    #render{template=Template, vars=Args, is_all=IsAll}
 42            end
 43    end,
 44    case {TargetId, Html} of
 45        {undefined,_} -> render_inline(Method, TargetId, Html, Args, Context);
 46        {_,#render{}} -> render_postback(Method, TriggerId, TargetId, Html, Args, Context);
 47        {_,_} -> render_static(Method, TargetId, Html, Args, Context)
 48    end.
 49
 50render_inline(Method, TargetId, Html, Args, Context) ->
 51    Sel = z_render:css_selector(TargetId, Args),
 52    case proplists:get_value(appear, Args) of
 53        true ->
 54            case Method of
 55                update        -> {[], z_render:appear_selector(Sel, Html, Context)};
 56                replace       -> {[], z_render:appear_replace_selector(Sel, Html, Context)};
 57                insert_top    -> {[], z_render:appear_top_selector(Sel, Html, Context)};
 58                insert_bottom -> {[], z_render:appear_bottom_selector(Sel, Html, Context)};
 59                insert_before -> {[], z_render:appear_before_selector(Sel, Html, Context)};
 60                insert_after  -> {[], z_render:appear_after_selector(Sel, Html, Context)};
 61                iframe        -> {[], z_render:update_iframe(TargetId, Html, Context)}
 62            end;
 63        _ -> 
 64            case Method of
 65                update        -> {[], z_render:update_selector(Sel, Html, Context)};
 66                replace       -> {[], z_render:replace_selector(Sel, Html, Context)};
 67                insert_top    -> {[], z_render:insert_top_selector(Sel, Html, Context)};
 68                insert_bottom -> {[], z_render:insert_bottom_selector(Sel, Html, Context)};
 69                insert_before -> {[], z_render:insert_before_selector(Sel, Html, Context)};
 70                insert_after  -> {[], z_render:insert_after_selector(Sel, Html, Context)};
 71                iframe        -> {[], z_render:update_iframe(TargetId, Html, Context)}
 72            end
 73    end.
 74
 75render_static(Method, TargetId, Html, Args, Context) ->
 76    Sel = z_render:css_selector(TargetId, Args),
 77    case proplists:get_value(appear, Args) of
 78        true -> 
 79            case Method of
 80                update        -> {z_render:appear_selector_js(Sel, Html), Context};
 81                replace       -> {z_render:appear_replace_selector_js(Sel, Html), Context};
 82                insert_top    -> {z_render:appear_top_selector_js(Sel, Html), Context};
 83                insert_bottom -> {z_render:appear_bottom_selector_js(Sel, Html), Context};
 84                insert_before -> {z_render:appear_before_selector_js(Sel, Html), Context};
 85                insert_after  -> {z_render:appear_after_selector_js(Sel, Html), Context}
 86            end;
 87        _ -> 
 88            case Method of 
 89                update        -> {z_render:update_selector_js(Sel, Html), Context};
 90                    replace   -> {z_render:replace_selector_js(Sel, Html), Context};
 91                insert_top    -> {z_render:insert_top_selector_js(Sel, Html), Context};
 92                insert_bottom -> {z_render:insert_bottom_selector_js(Sel, Html), Context};
 93                insert_before -> {z_render:insert_before_selector_js(Sel, Html), Context};
 94                insert_after  -> {z_render:insert_after_selector_js(Sel, Html), Context}
 95            end
 96    end.
 97
 98render_postback(Method, TriggerId, TargetId, Html, Args, Context) ->
 99    {PostbackMsgJS, _PickledPostback} = z_render:make_postback({render, Method, Html, Args}, undefined, TriggerId, TargetId, ?MODULE, Context),
100    {PostbackMsgJS, Context}.
101
102event(#postback{message={render, Method, Html, Args}, target=TargetId}, Context) ->
103    {[], Context1} = render_inline(Method, TargetId, Html, Args, Context),
104    Context1.