/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. %% Copyright 2009 Marc Worrell
  6. %%
  7. %% Licensed under the Apache License, Version 2.0 (the "License");
  8. %% you may not use this file except in compliance with the License.
  9. %% You may obtain a copy of the License at
  10. %%
  11. %% http://www.apache.org/licenses/LICENSE-2.0
  12. %%
  13. %% Unless required by applicable law or agreed to in writing, software
  14. %% distributed under the License is distributed on an "AS IS" BASIS,
  15. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. %% See the License for the specific language governing permissions and
  17. %% limitations under the License.
  18. -module(scomp_base_button).
  19. -behaviour(gen_scomp).
  20. -export([vary/2, render/3]).
  21. -include("zotonic.hrl").
  22. vary(_Params, _Context) -> nocache.
  23. render(Params, _Vars, Context) ->
  24. Postback = proplists:get_value(postback, Params),
  25. Delegate = proplists:get_value(delegate, Params),
  26. Text = proplists:get_value(text, Params, <<"Submit">>),
  27. Id = z_ids:optid(proplists:get_value(id, Params)),
  28. Class = proplists:get_all_values(class, Params),
  29. Style = proplists:get_value(style, Params),
  30. Type = proplists:get_value(type, Params),
  31. Title = proplists:get_value(title, Params),
  32. Disabled = proplists:get_value(disabled, Params, false),
  33. Actions = proplists:get_all_values(action, Params),
  34. Options = [{action,X} || X <- Actions],
  35. Options1 = case Postback of
  36. undefined -> Options;
  37. Postback -> [{postback,Postback} | Options]
  38. end,
  39. Context1 = case Options1 of
  40. [] -> Context;
  41. _ ->
  42. Options2 = case Delegate of
  43. undefined -> Options1;
  44. _ -> [{delegate, Delegate} | Options1]
  45. end,
  46. z_render:wire(Id, {event,[{type,click}|Options2]}, Context)
  47. end,
  48. Attrs = [
  49. {<<"id">>, Id},
  50. {<<"name">>, case proplists:is_defined(id, Params) of true -> Id; false -> "" end},
  51. {<<"style">>, Style},
  52. {<<"title">>, Title}
  53. ],
  54. {Class1, Attrs1} = case z_convert:to_bool(Disabled) of
  55. false -> {Class, Attrs};
  56. true -> { ["disabled"|Class], [ {<<"disabled">>,"disabled"}|Attrs] }
  57. end,
  58. Attrs2 = case Type of
  59. undefined -> Attrs1;
  60. _ -> [ {<<"type">>, Type} | Attrs1 ]
  61. end,
  62. Context2 = z_tags:render_tag(
  63. <<"button">>,
  64. [{<<"class">>,Class1}|Attrs2],
  65. Text,
  66. Context1),
  67. {ok, Context2}.