Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add 'multiselect' type, change wpart_bool so it renders to single che…

…ckbox
  • Loading branch information...
commit dff0cd073404caeb3340e8ee3be5cf5706511839 1 parent 50fb965
Piotr Dorobisz authored
View
3  lib/wparts-1.4.1/priv/html/multiselect.tpl
@@ -0,0 +1,3 @@
+<label>
+ <input type="checkbox" value="<% value %>" <% html %> /><% desc %>
+</label>
View
70 lib/wparts-1.4.1/src/wpart_bool.erl
@@ -23,79 +23,43 @@
-export([handle_call/1, build_html_tag/4, build_html_tag/3, load_tpl/0]).
--deprecated([build_html_tag/3]).
+-deprecated([build_html_tag/4]).
-include_lib("xmerl/include/xmerl.hrl").
--spec(handle_call/1 :: (tuple()) -> tuple()).
handle_call(#xmlElement{attributes = Attrs0}) ->
Attrs = wpart:xml2proplist(Attrs0),
-
- Selected0 = proplists:get_value("selected", Attrs, ""),
- Selected = lists:map(fun string:strip/1, string:tokens(Selected0, [$|])),
-
- #xmlText{value=get_html_tag([{"preselected", Selected} | proplists:delete("selected", Attrs)], ""),
+
+ #xmlText{value=get_html_tag(Attrs, ""),
type=cdata}.
--spec(build_html_tag/3 :: (string(), list(tuple()), list()) -> string()).
build_html_tag(Id, Params, Default) ->
- Options = proplists:get_value(options, Params, []),
- Selected = if
- Default == [] ->
- proplists:get_value(selected, Params, []);
- true ->
- []
- end,
- Attrs0 = wpart:normalize_html_attrs([{"options", Options},
- {"preselected", Selected} |
- proplists:get_value(html_attrs, Params, [])]),
+ Attrs0 = wpart:normalize_html_attrs(proplists:get_value(html_attrs, Params, [])),
Attrs = [{"name", Id}, {"id", Id} | proplists:delete("name", Attrs0)],
-
get_html_tag(Attrs, Default).
build_html_tag(Name, Prefix, Params, Default) ->
N = wpart_derived:generate_long_name(Prefix, Name),
Description = wpart_derived:get_description(Name, Params),
D = wpart_derived:find(N, Default),
+
Attrs0 = wpart:normalize_html_attrs(proplists:get_value(html_tags, Params, [])),
Attrs = [{"name", N} | proplists:delete("name", Attrs0)],
wpart_derived:surround_with_table(N, get_html_tag(Attrs, D), Description).
-get_html_tag(Attrs0, DefaultList) ->
- Bool = wpart_gen:tpl_get(bool),
-
- Preselected = proplists:get_value("preselected", Attrs0, []),
- OptionsString = proplists:get_value("options", Attrs0, []),
- Attrs = proplists:delete("preselected",
- proplists:delete("options", Attrs0)),
-
- Builder = fun(Option, Acc) ->
- [Value, Desc] = string:tokens(Option, [$:]),
-
- OptionAttrs = case checked(Value, DefaultList, Preselected) of
- true ->
- [{"checked", "checked"} | Attrs];
- false ->
- Attrs
- end,
-
- Acc ++ wpart_gen:build_html(Bool, [{"html", wpart:proplist2html(OptionAttrs)},
- {"value", Value},
- {"desc", Desc}])
- end,
-
- case string:tokens(OptionsString, [$|]) of
- [] ->
- "No options loaded.";
- Options ->
- lists:foldl(Builder, "", Options)
- end.
-
--spec(checked/3 :: (string(), list(string()), list(string())) -> boolean()).
-checked(Value, DefaultList, Preselected) ->
- lists:member(Value, DefaultList) orelse
- lists:member(Value, Preselected).
+get_html_tag(Attrs0, Default) ->
+ Attrs = if
+ Default == true ->
+ [{"checked", "checked"} | Attrs0];
+ true ->
+ Attrs0
+ end,
+
+ wpart_gen:build_html(wpart_gen:tpl_get(bool),
+ [{"html",
+ wpart:proplist2html([{"value", proplists:get_value("name", Attrs, "")} | Attrs])}
+ ]).
load_tpl() ->
wpart_gen:load_tpl(bool,
View
102 lib/wparts-1.4.1/src/wpart_multiselect.erl
@@ -0,0 +1,102 @@
+%% The contents of this file are subject to the Erlang Web Public License,
+%% Version 1.0, (the "License"); you may not use this file except in
+%% compliance with the License. You should have received a copy of the
+%% Erlang Web Public License along with this software. If not, it can be
+%% retrieved via the world wide web at http://www.erlang-consulting.com/.
+%%
+%% Software distributed under the License is distributed on an "AS IS"
+%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+%% the License for the specific language governing rights and limitations
+%% under the License.
+%%
+%% The Initial Developer of the Original Code is Erlang Training & Consulting
+%% Ltd. Portions created by Erlang Training & Consulting Ltd are Copyright 2008,
+%% Erlang Training & Consulting Ltd. All Rights Reserved.
+
+%%%-------------------------------------------------------------------
+%%% @author Michal Ptaszek <info@erlang-consulting.com>
+%%% @doc
+%%% @end
+%%%-------------------------------------------------------------------
+-module(wpart_multiselect).
+-behaviour(wpart).
+
+-export([handle_call/1, build_html_tag/4, build_html_tag/3, load_tpl/0]).
+
+-deprecated([build_html_tag/4]).
+
+-include_lib("xmerl/include/xmerl.hrl").
+
+-spec(handle_call/1 :: (tuple()) -> tuple()).
+handle_call(#xmlElement{attributes = Attrs0}) ->
+ Attrs = wpart:xml2proplist(Attrs0),
+
+ Selected0 = proplists:get_value("selected", Attrs, ""),
+ Selected = lists:map(fun string:strip/1, string:tokens(Selected0, [$|])),
+
+ #xmlText{value=get_html_tag([{"preselected", Selected} | proplists:delete("selected", Attrs)], ""),
+ type=cdata}.
+
+-spec(build_html_tag/3 :: (string(), list(tuple()), list()) -> string()).
+build_html_tag(Id, Params, Default) ->
+ Options = proplists:get_value(options, Params, []),
+ Selected = if
+ Default == [] ->
+ proplists:get_value(selected, Params, []);
+ true ->
+ []
+ end,
+ Attrs0 = wpart:normalize_html_attrs([{"options", Options},
+ {"preselected", Selected} |
+ proplists:get_value(html_attrs, Params, [])]),
+ Attrs = [{"name", Id}, {"id", Id} | proplists:delete("name", Attrs0)],
+
+ get_html_tag(Attrs, Default).
+
+build_html_tag(Name, Prefix, Params, Default) ->
+ N = wpart_derived:generate_long_name(Prefix, Name),
+ Description = wpart_derived:get_description(Name, Params),
+ D = wpart_derived:find(N, Default),
+ Attrs0 = wpart:normalize_html_attrs(proplists:get_value(html_tags, Params, [])),
+ Attrs = [{"name", N} | proplists:delete("name", Attrs0)],
+
+ wpart_derived:surround_with_table(N, get_html_tag(Attrs, D), Description).
+
+get_html_tag(Attrs0, DefaultList) ->
+ Multiselect = wpart_gen:tpl_get(multiselect),
+
+ Preselected = proplists:get_value("preselected", Attrs0, []),
+ OptionsString = proplists:get_value("options", Attrs0, []),
+ Attrs = proplists:delete("preselected",
+ proplists:delete("options", Attrs0)),
+
+ Builder = fun(Option, Acc) ->
+ [Value, Desc] = string:tokens(Option, [$:]),
+
+ OptionAttrs = case checked(Value, DefaultList, Preselected) of
+ true ->
+ [{"checked", "checked"} | Attrs];
+ false ->
+ Attrs
+ end,
+
+ Acc ++ wpart_gen:build_html(Multiselect, [{"html", wpart:proplist2html(OptionAttrs)},
+ {"value", Value},
+ {"desc", Desc}])
+ end,
+
+ case string:tokens(OptionsString, [$|]) of
+ [] ->
+ "No options loaded.";
+ Options ->
+ lists:foldl(Builder, "", Options)
+ end.
+
+-spec(checked/3 :: (string(), list(string()), list(string())) -> boolean()).
+checked(Value, DefaultList, Preselected) ->
+ lists:member(Value, DefaultList) orelse
+ lists:member(Value, Preselected).
+
+load_tpl() ->
+ wpart_gen:load_tpl(multiselect,
+ filename:join([code:priv_dir(wparts),"html","multiselect.tpl"])).
View
42 lib/wparts-1.4.1/src/wtype_bool.erl
@@ -35,29 +35,21 @@ handle_call(_Format, #xmlText{value=Val}) ->
validate({Types, Val}) ->
case wpart_valid:is_private(Types) of
true ->
- {ok, Val};
- false ->
- Val1 = format_val(Val),
- check_always(proplists:get_value(always, Types, []), Val1)
+ {ok, Val =/= undefined};
+ false ->
+ case lists:keysearch(always, 1, Types) of
+ {_, {_, Bool}} ->
+ if
+ Val == undefined, Bool == false ->
+ {ok, false};
+ Val == undefined, Bool == true ->
+ {error, {bad_bool_value, Val}};
+ Bool == false ->
+ {error, {bad_bool_value, Val}};
+ true ->
+ {ok, true}
+ end;
+ _ ->
+ {ok, Val =/= undefined}
+ end
end.
-
--spec(check_always/2 :: (list(string()), list(string())) -> {ok, list(string())} | {error, {not_all_mandatory_fields_checked, list(string())}}).
-check_always([], Vals) ->
- {ok, Vals};
-check_always(Always, Vals) ->
- case lists:all(fun(Val) ->
- lists:member(Val, Vals)
- end, Always) of
- true ->
- {ok, Vals};
- false ->
- {error, {not_all_mandatory_fields_checked, Vals}}
- end.
-
--spec(format_val/1 :: (undefined | string() | list(string())) -> list(string())).
-format_val(undefined) ->
- [];
-format_val(Val) when is_integer(hd(Val)) ->
- [Val];
-format_val(Val) ->
- Val.
View
63 lib/wparts-1.4.1/src/wtype_multiselect.erl
@@ -0,0 +1,63 @@
+%% The contents of this file are subject to the Erlang Web Public License,
+%% Version 1.0, (the "License"); you may not use this file except in
+%% compliance with the License. You should have received a copy of the
+%% Erlang Web Public License along with this software. If not, it can be
+%% retrieved via the world wide web at http://www.erlang-consulting.com/.
+%%
+%% Software distributed under the License is distributed on an "AS IS"
+%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
+%% the License for the specific language governing rights and limitations
+%% under the License.
+%%
+%% The Initial Developer of the Original Code is Erlang Training & Consulting
+%% Ltd. Portions created by Erlang Training & Consulting Ltd are Copyright 2008,
+%% Erlang Training & Consulting Ltd. All Rights Reserved.
+
+%%%-------------------------------------------------------------------
+%%% @author Michal Ptaszek <michal.ptaszek@erlang-consulting.com>
+%%% @doc
+%%% @end
+%%%-------------------------------------------------------------------
+-module(wtype_multiselect).
+-behaviour(wtype).
+
+-export([handle_call/2, validate/1]).
+
+-include_lib("xmerl/include/xmerl.hrl").
+
+handle_call(_Format, true) ->
+ "true";
+handle_call(_Format, false) ->
+ "false";
+handle_call(_Format, #xmlText{value=Val}) ->
+ #xmlText{value=handle_call(not_used, Val)}.
+
+validate({Types, Val}) ->
+ case wpart_valid:is_private(Types) of
+ true ->
+ {ok, Val};
+ false ->
+ Val1 = format_val(Val),
+ check_always(proplists:get_value(always, Types, []), Val1)
+ end.
+
+-spec(check_always/2 :: (list(string()), list(string())) -> {ok, list(string())} | {error, {not_all_mandatory_fields_checked, list(string())}}).
+check_always([], Vals) ->
+ {ok, Vals};
+check_always(Always, Vals) ->
+ case lists:all(fun(Val) ->
+ lists:member(Val, Vals)
+ end, Always) of
+ true ->
+ {ok, Vals};
+ false ->
+ {error, {not_all_mandatory_fields_checked, Vals}}
+ end.
+
+-spec(format_val/1 :: (undefined | string() | list(string())) -> list(string())).
+format_val(undefined) ->
+ [];
+format_val(Val) when is_integer(hd(Val)) ->
+ [Val];
+format_val(Val) ->
+ Val.
Please sign in to comment.
Something went wrong with that request. Please try again.