PageRenderTime 32ms CodeModel.GetById 11ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_base/scomps/scomp_base_button.erl

https://code.google.com/p/zotonic/
Erlang | 79 lines | 50 code | 12 blank | 17 comment | 1 complexity | c06094b8d6d5c0cda0b59c185661a734 MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2009 Marc Worrell
 3%%
 4%% Based on code (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(scomp_base_button).
21-behaviour(gen_scomp).
22
23-export([vary/2, render/3]).
24
25-include("zotonic.hrl").
26
27vary(_Params, _Context) -> nocache.
28render(Params, _Vars, Context) ->
29    Postback  = proplists:get_value(postback, Params),
30	Delegate  = proplists:get_value(delegate, Params),
31    Text      = proplists:get_value(text, Params, <<"Submit">>),
32    Id        = z_ids:optid(proplists:get_value(id, Params)),
33    Class     = proplists:get_all_values(class, Params),
34    Style     = proplists:get_value(style, Params),
35    Type      = proplists:get_value(type, Params),
36    Title     = proplists:get_value(title, Params),
37    Disabled  = proplists:get_value(disabled, Params, false),
38    Actions   = proplists:get_all_values(action, Params),
39
40    Options   = [{action,X} || X <- Actions],
41    Options1  = case Postback of
42                	undefined -> Options;
43                	Postback  -> [{postback,Postback} | Options]
44                end,
45
46    Context1 = case Options1 of
47                    [] -> Context;
48                    _  -> 
49					    Options2  = case Delegate of
50										undefined -> Options1;
51										_ -> [{delegate, Delegate} | Options1]
52									end,
53						z_render:wire(Id, {event,[{type,click}|Options2]}, Context)
54               end,
55
56    Attrs = [
57        {<<"id">>,    Id},
58        {<<"name">>,  case proplists:is_defined(id, Params) of true -> Id; false -> "" end},
59        {<<"style">>, Style},
60        {<<"title">>, Title}
61    ],
62    
63    {Class1, Attrs1} = case z_convert:to_bool(Disabled) of
64        false -> {Class, Attrs};
65        true -> { ["disabled"|Class], [ {<<"disabled">>,"disabled"}|Attrs] }
66    end,
67    
68    Attrs2 = case Type of
69        undefined -> Attrs1;
70        _ -> [ {<<"type">>, Type} | Attrs1 ]
71    end,
72    
73    Context2 = z_tags:render_tag(
74                        <<"button">>,
75                        [{<<"class">>,Class1}|Attrs2],
76                    	Text,
77                    	Context1),
78    {ok, Context2}.
79