Skip to content

Commit

Permalink
Added html_name to following elements
Browse files Browse the repository at this point in the history
added records for restful_form restful_submit restful_reset
	modified:   wf.hrl

added restful elements
	new file:   ../src/elements/forms/element_restful_form.erl
	new file:   ../src/elements/forms/element_restful_submit.erl

added html_name attribute:
	modified:   ../src/elements/forms/element_button.erl
	modified:   ../src/elements/forms/element_checkbox.erl
	modified:   ../src/elements/forms/element_hidden.erl
	modified:   ../src/elements/forms/element_password.erl
	modified:   ../src/elements/forms/element_radio.erl
	modified:   ../src/elements/forms/element_textarea.erl
	modified:   ../src/elements/forms/element_textbox.erl

replaced tabs with space in element_textarea.erl

added html_name()
	modified:   ../src/lib/wf_tags.erl

moved some internal functions to wf_utils
	modified:   ../src/lib/wf_utils.erl
	modified:   ../src/elements/forms/element_inplace.erl
  • Loading branch information
StPanning committed May 13, 2012
1 parent 694a281 commit bb01ad6
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 40 deletions.
15 changes: 9 additions & 6 deletions include/wf.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,21 @@
-record(link, {?ELEMENT_BASE(element_link), title = "", text="", body="", new=false, html_encode=true, url="javascript:", postback, delegate}).
-record(error, {?ELEMENT_BASE(element_error), text="", html_encode=true}).
-record(span, {?ELEMENT_BASE(element_span), body="", text="", html_encode=true}).
-record(button, {?ELEMENT_BASE(element_button), text="Button", html_encode=true, postback, delegate}).
-record(button, {?ELEMENT_BASE(element_button), html_name, text="Button", html_encode=true, postback, delegate}).
-record(literal, {?ELEMENT_BASE(element_literal), text="", html_encode=true}).
-record(textbox, {?ELEMENT_BASE(element_textbox), text="", maxlength="", html_encode=true, next, postback, delegate}).
-record(hidden, {?ELEMENT_BASE(element_hidden), text="", html_encode=true}).
-record(textbox, {?ELEMENT_BASE(element_textbox), html_name, text="", maxlength="", html_encode=true, next, postback, delegate}).
-record(hidden, {?ELEMENT_BASE(element_hidden), html_name, text="", html_encode=true}).
-record(textarea, {?ELEMENT_BASE(element_textarea), text="", html_encode=true}).
-record(datepicker_textbox, {?ELEMENT_BASE(element_datepicker_textbox), text="", next, html_encode=true, validators=[], options = [{dateFormat, "yy-mm-dd"}] }).
-record(dropdown, {?ELEMENT_BASE(element_dropdown), options=[], html_encode=true, postback, delegate, value}).
-record(option, { text="", value=undefined, selected=false }).
-record(checkbox, {?ELEMENT_BASE(element_checkbox), text="", html_encode=true, checked=false, value="on", postback, delegate}).
-record(checkbox, {?ELEMENT_BASE(element_checkbox), html_name, text="", html_encode=true, checked=false, value="on", postback, delegate}).
-record(radiogroup, {?ELEMENT_BASE(element_radiogroup), body=[]}).
-record(radio, {?ELEMENT_BASE(element_radio), text="", html_encode=true, value, name, checked=false, postback, delegate}).
-record(password, {?ELEMENT_BASE(element_password), text="", html_encode=true, next, postback, delegate}).
-record(radio, {?ELEMENT_BASE(element_radio), html_name, text="", html_encode=true, value, name, checked=false, postback, delegate}).
-record(password, {?ELEMENT_BASE(element_password), html_name, text="", html_encode=true, next, postback, delegate}).
-record(restful_form, {?ELEMENT_BASE(element_restful_form), method="POST", action, html_name, body=[]}).
-record(restful_submit, {?ELEMENT_BASE(element_restful_submit), text="Submit", html_encode=true, html_name, postback, delegate}).
-record(restful_reset, {?ELEMENT_BASE(element_restful_reset), text="Submit", html_encode=true, html_name, postback, delegate}).
-record(panel, {?ELEMENT_BASE(element_panel), body="", text="", html_encode=true}).
-record(spinner, {?ELEMENT_BASE(element_spinner), image="/nitrogen/spinner.gif"}).
-record(image, {?ELEMENT_BASE(element_image), image="", alt}).
Expand Down
1 change: 1 addition & 0 deletions src/elements/forms/element_button.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ render_element(Record) ->
{type, button},
{class, [button, Record#button.class]},
{style, Record#button.style},
{name, Record#radio.name},
{value, Value}
]).
1 change: 1 addition & 0 deletions src/elements/forms/element_checkbox.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ render_element(Record) ->
{type, checkbox},
{class, [checkbox, Record#checkbox.class]},
{style, Record#checkbox.style},
{name, Record#checkbox.html_name},
{value, Record#checkbox.value},
{CheckedOrNot, true}
]),
Expand Down
1 change: 1 addition & 0 deletions src/elements/forms/element_hidden.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ render_element(Record) ->
wf_tags:emit_tag(input, [
{class, Record#hidden.class},
{type, hidden},
{name, Record#hidden.html_name},
{value, Value}
]).
28 changes: 7 additions & 21 deletions src/elements/forms/element_inplace.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ render_element(#inplace{text=Text, tag=Tag, delegate=Delegate, edit=Edit,
#buttonize { target=ViewPanelID }
],

View1 = replace_field(id, ViewID, ViewModule:reflect(), View),
View1 = wf_utils:replace_field(id, ViewID, ViewModule:reflect(), View),
View2 = append_field_actions(ViewAction, undefined, ViewModule:reflect(), View1),
View3 = replace_field_text(Text, View2, ViewModule:reflect()),

Expand All @@ -56,9 +56,9 @@ render_element(#inplace{text=Text, tag=Tag, delegate=Delegate, edit=Edit,
#event { type=keyup, keycode=27, actions=#script { script=["objs('", CancelButtonID, "').click();"] } }
],

Edit1 = replace_field(id, EditID, EditModule:reflect(), Edit),
Edit1 = wf_utils:replace_field(id, EditID, EditModule:reflect(), Edit),
Edit2 = append_field_actions(EditAction, OKButtonID, EditModule:reflect(), Edit1),
Edit3 = replace_field_text(Text, Edit2, EditModule:reflect()),
Edit3 = wf_utils:replace_field_text(Text, Edit2, EditModule:reflect()),

% No value in view mode cause the view panel unclickable thus
% we set edit mode in that case.
Expand Down Expand Up @@ -117,9 +117,9 @@ event({cancel, {ViewPanelID, _ViewID, EditPanelID, EditID}, _Tag}) ->

replace_field_text(Value, Element, Fields) ->
case element(1, Element) of
dropdown -> replace_field(value, Value, Fields, Element);
image -> replace_field(image, Value, Fields, Element);
_ -> replace_field(text, Value, Fields, Element)
dropdown -> wf_utils:replace_field(value, Value, Fields, Element);
image -> wf_utils:replace_field(image, Value, Fields, Element);
_ -> wf_utils:replace_field(text, Value, Fields, Element)
end.

modify_field_action(Action, Trigger) ->
Expand All @@ -131,7 +131,7 @@ modify_field_action(Action, Trigger) ->
%% Set required trigger to original validate actions and append
%% new actions specified by Actions.
append_field_actions(Actions, Trigger, Fields, Rec) ->
N = indexof(actions, Fields),
N = wf_utils:indexof(actions, Fields),
ModifiedOldActions = case element(N, Rec) of
undefined -> [];
OldActions when is_list(OldActions) ->
Expand All @@ -140,17 +140,3 @@ append_field_actions(Actions, Trigger, Fields, Rec) ->
end,
setelement(N, Rec, Actions ++ ModifiedOldActions).

indexof(Key, Fields) -> indexof(Key, Fields, 2).
indexof(_Key, [], _N) -> undefined;
indexof(Key, [Key|_T], N) -> N;
indexof(Key, [_|T], N) -> indexof(Key, T, N + 1).

replace_field(Key, Value, Fields, Rec) ->
N = indexof(Key, Fields),
setelement(N, Rec, Value).

get_field(Key, Fields, Rec) ->
case indexof(Key, Fields) of
undefined -> undefined;
N -> element(N, Rec)
end.
1 change: 1 addition & 0 deletions src/elements/forms/element_password.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ render_element(Record) ->
{type, password},
{class, [password, Record#password.class]},
{style, Record#password.style},
{name, Record#password.html_name},
{value, Value}
]).
2 changes: 1 addition & 1 deletion src/elements/forms/element_radio.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ render_element(Record) ->
[
%% Checkbox...
wf_tags:emit_tag(input, [
{name, Record#radio.name},
{id, Anchor},
{value, Record#radio.value},
{name, Record#radio.html_name},
{type, radio},
{class, [radio, Record#radio.class]},
{style, Record#radio.style},
Expand Down
55 changes: 55 additions & 0 deletions src/elements/forms/element_restful_form.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
% Nitrogen Web Framework for Erlang
% Copyright (c) 2012 Steffen Panning
% See MIT-LICENSE for licensing information.

-module (element_restful_form).
-include_lib ("wf.hrl").
-export([reflect/0, render_element/1]).

-define(IS_FORM(Tag), ( Tag == element_button orelse
Tag == element_checkbox orelse
Tag == element_hidden orelse
Tag == element_password orelse
Tag == element_radio orelse
Tag == element_textarea orelse
Tag == element_textbox orelse
% Tag == element_upload orelse
Tag == element_restful_submit)).

reflect() -> record_info(fields, restful_form).

render_element(Record) ->
?PRINT("START RENDER"),
WithName = inject_name(Record),
wf_tags:emit_tag(form, WithName#restful_form.body, [
wf_tags:html_name(WithName#restful_form.id,
WithName#restful_form.html_name),
{action, WithName#restful_form.action},
{method, WithName#restful_form.method}
]).

%%internal
inject_name(Element)
when is_tuple(Element) ->
?PRINT("START INJECT"),
Base = wf_utils:get_elementbase(Element),
Module = Base#elementbase.module,
Fields = Module:reflect(),
New = set_html_name(Module, Fields, Element),
case wf_utils:get_field(body, Fields, New) of
undefined -> New;
Body -> NewBody = [inject_name(N) || N <- Body],
wf_utils:replace_field(body, NewBody, Fields, New)
end;
inject_name(Element) ->
Element.

set_html_name(Tag, Fields, Rec)
when ?IS_FORM(Tag) ->
?PRINT("START SET_HTML_NAME"),
{ID, Name} = {wf_utils:get_field(id, Fields, Rec),
wf_utils:get_field(html_name, Fields, Rec)},
{_, NewName} = wf_tags:html_name(ID, Name),
wf_utils:replace_field(html_name, wf_utils:normalize_id(NewName), Fields, Rec);
set_html_name(_Tag, _Fields, Rec) ->
Rec.
26 changes: 26 additions & 0 deletions src/elements/forms/element_restful_submit.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
% Nitrogen Web Framework for Erlang
% Copyright (c) 2012 Steffen Panning
% See MIT-LICENSE for licensing information.

-module (element_restful_submit).
-include_lib ("wf.hrl").
-compile(export_all).

reflect() -> record_info(fields, restful_submit).

render_element(Record) ->
ID = Record#restful_submit.id,
Anchor = Record#restful_submit.anchor,
case Record#restful_submit.postback of
undefined -> ignore;
Postback -> wf:wire(Anchor, #event { type=click, validation_group=ID, postback=Postback, delegate=Record#restful_submit.delegate })
end,

Value = [" ", wf:html_encode(Record#restful_submit.text, Record#restful_submit.html_encode), " "],
wf_tags:emit_tag(input, [
{type, submit},
{name, Record#restful_submit.html_name},
{class, [restful_submit, Record#restful_submit.class]},
{style, Record#restful_submit.style},
{value, Value}
]).
19 changes: 10 additions & 9 deletions src/elements/forms/element_textarea.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ reflect() -> record_info(fields, textarea).
render_element(Record) ->
Text = html_encode(Record#textarea.text, Record#textarea.html_encode),
wf_tags:emit_tag(textarea, Text, [
{class, [textarea, Record#textarea.class]},
{style, Record#textarea.style}
{class, [textarea, Record#textarea.class]},
{style, Record#textarea.style},
{name, Record#radio.name}
]).

html_encode(L, false) -> wf:to_list(lists:flatten([L]));
html_encode(L, true) -> html_encode(wf:to_list(lists:flatten([L]))).
html_encode(L, true) -> html_encode(wf:to_list(lists:flatten([L]))).
html_encode([]) -> [];
html_encode([H|T]) ->
case H of
$< -> "&lt;" ++ html_encode(T);
$> -> "&gt;" ++ html_encode(T);
$" -> "&quot;" ++ html_encode(T);
$' -> "&#39;" ++ html_encode(T);
$& -> "&amp;" ++ html_encode(T);
$< -> "&lt;" ++ html_encode(T);
$> -> "&gt;" ++ html_encode(T);
$" -> "&quot;" ++ html_encode(T);
$' -> "&#39;" ++ html_encode(T);
$& -> "&amp;" ++ html_encode(T);
% $\n -> "<br>" ++ html_encode(T);
_ -> [H|html_encode(T)]
_ -> [H|html_encode(T)]
end.
3 changes: 2 additions & 1 deletion src/elements/forms/element_textbox.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ render_element(Record) ->
wf_tags:emit_tag(input, [
{type, text},
{class, [textbox, Record#textbox.class]},
{maxlength, Record#textbox.maxlength},
{maxlength, Record#textbox.maxlength},
{style, Record#textbox.style},
{name, Record#textbox.html_name},
{value, Value}
]).
9 changes: 8 additions & 1 deletion src/lib/wf_tags.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
TagName =/= 'td' andalso
TagName =/= 'iframe')).

-export ([emit_tag/2, emit_tag/3]).
-export ([emit_tag/2, emit_tag/3, html_name/2]).

html_name(Id, Name) ->
case Name of
undefined -> {name, Id};
"" -> {name, Id};
Name -> {name, Name}
end.

%%% Empty tags %%%

Expand Down
27 changes: 26 additions & 1 deletion src/lib/wf_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
coalesce/1,
is_process_alive/1,
debug/0, break/0,
get_elementbase/1, get_actionbase/1, get_validatorbase/1, replace_with_base/2
get_elementbase/1, get_actionbase/1, get_validatorbase/1, replace_with_base/2,
indexof/2,
replace_field/4,
get_field/3,
normalize_id/1
]).

-define(COPY_TO_BASERECORD(Name, Size, Record),
Expand Down Expand Up @@ -136,3 +140,24 @@ debug() ->
i:ib(?MODULE, break, 0).

break() -> ok.

indexof(Key, Fields) -> indexof(Key, Fields, 2).
indexof(_Key, [], _N) -> undefined;
indexof(Key, [Key|_T], N) -> N;
indexof(Key, [_|T], N) -> indexof(Key, T, N + 1).

replace_field(Key, Value, Fields, Rec) ->
N = indexof(Key, Fields),
setelement(N, Rec, Value).

get_field(Key, Fields, Rec) ->
case indexof(Key, Fields) of
undefined -> undefined;
N -> element(N, Rec)
end.
normalize_id(ID) ->
case wf:to_string_list(ID) of
[".wfid_" ++ _] = [NormalizedID] -> NormalizedID;
["page"] -> "page";
[NewID] -> ".wfid_" ++ NewID
end.

0 comments on commit bb01ad6

Please sign in to comment.