PageRenderTime 53ms CodeModel.GetById 15ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

/modules/mod_base/actions/action_base_jquery_effect.erl

https://code.google.com/p/zotonic/
Erlang | 94 lines | 64 code | 12 blank | 18 comment | 4 complexity | 97da11d79d8f27bc6989a5279988fa63 MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2009 Marc Worrell
 3%%
 4%% Based on code copyright (c) 2008-2009 Rusty Klophaus
 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_jquery_effect).
21-include("zotonic.hrl").
22-export([render_action/4]).
23
24
25render_action(_TriggerId, TargetId, Args, Context) ->
26    Type    = proplists:get_value(type, Args),
27    Effect  = proplists:get_value(effect, Args),
28    Speed   = proplists:get_value(speed, Args),
29    Class   = proplists:get_value(class, Args),
30    Easing  = proplists:get_value(easing, Args),
31    Effect  = proplists:get_value(effect, Args),
32    Options = to_js(proplists:get_value(options, Args, [])),
33
34    Script = case z_convert:to_atom(Type) of
35        'show'          -> [<<"show(">>, map_speed(Speed), $), $;];
36        'hide'          -> [<<"hide(">>, map_speed(Speed), $), $;];
37        'remove'        -> 
38            case proplists:get_value(fadeout, Args) of
39                true -> [<<"fadeOut(">>, map_speed_default(Speed), <<",function() { $(this).remove(); });">>];
40                _ -> <<"remove();">>
41            end;
42        'slide_toggle'  -> [<<"slideToggle(">>, map_speed(Speed), $), $;];
43        'toggle'        -> <<"toggle();">>;
44        'set_class'     -> [<<"attr('class','">>, Class, $', $), $;];
45        'add_class'     -> [<<"addClass('">>, Class, $', $), $;];
46        'remove_class'  -> [<<"removeClass('">>, Class, $', $), $;];
47        'toggle_class'  -> [<<"toggleClass('">>, Class, $', $), $;];
48        'fade_in'       -> [<<"fadeIn(">>, map_speed(Speed), $), $;];
49        'fade_out'      -> [<<"fadeOut(">>, map_speed(Speed), $), $;];
50        'slide_down'    -> [<<"slideDown(">>, map_speed(Speed), $), $;];
51        'slide_up'      -> [<<"slideUp(">>, map_speed(Speed), $), $;];
52        'slide_fade_out'-> [<<"animate({opacity: 'hide', height: 'hide'}, ">>, map_speed_default(Speed), $), $;];
53        'slide_fade_in' -> [<<"animate({opacity: 'show', height: 'show'}, ">>, map_speed_default(Speed), $), $;];
54        'disable'       -> "attr('disabled', true).addClass('disabled');";
55        'enable'        -> "attr('disabled', false).removeClass('disabled');";
56
57        %% @todo check these, i think that with jQuery 1.3+ they should be 'animate' with a js_object/2 output
58        'effect'        -> io_lib:format("effect('~s', ~s, ~p);", [Effect, Options, map_speed_default(Speed)]);
59        'animate'       -> io_lib:format("animate(~s, ~p, '~s');", [Options, map_speed_default(Speed), Easing])
60    end,
61    case z_render:css_selector(TargetId, Args) of
62        Empty when Empty == undefined; Empty == []; Empty == <<>> -> {Script, Context};
63        Selector -> {[z_render:render_css_selector(Selector),$.,Script], Context}
64    end.
65
66
67map_speed_default(Speed) ->
68    case map_speed(Speed) of
69        [] -> "500";
70        S -> S
71    end.
72
73map_speed(N) when is_integer(N) -> integer_to_list(N);
74map_speed("normal") -> [];
75map_speed(undefined) -> [];
76map_speed(<<>>) -> [];
77map_speed([]) -> [];
78map_speed(Speed) -> 
79    case z_utils:only_digits(Speed) of
80        true -> Speed;
81        false -> [$', z_utils:js_escape(Speed), $']
82    end.
83
84    
85to_js(Options) ->
86    F = fun({Key, Value}) ->
87        case Value of 
88            true -> [Key, <<": true">>];
89            false -> [Key, <<": false">>];
90            V -> [Key, $', z_utils:js_escape(V), $']
91        end
92    end,
93    [${, string:join([F(X) || X <- Options], ","), $}].
94