Skip to content

Commit

Permalink
3 new elements
Browse files Browse the repository at this point in the history
  • Loading branch information
ofera committed May 3, 2009
1 parent bfb58e2 commit aa1984f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
36 changes: 36 additions & 0 deletions element_force_reload_on_back_button.erl
@@ -0,0 +1,36 @@
-module(element_force_reload_on_back_button).
-compile(export_all).

-include_lib("lib/nitrogen/include/wf.inc").
-record(force_reload_on_back_button, {?ELEMENT_BASE(element_force_reload_on_back_button)}).

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

% desc: put this element if you need the page to reload whenever the user
% used the back button in order to return to the page. mostly needed on
% dinamic created pages

% in plain html/java this will look like this:
% function body_onload() {
% if(document.getElementById("force_reload") != null) {
% if(force_reload.value != "initialvalue") {
% window.location = document.location.href;
% } else {
% force_reload.value = "newvalue"
% }
% }
% }
% <body onload=body_onload()>
% <input id="force_reload" type="hidden" value="initialvalue"/>

% i used jquery which better than using the body onload method
render(ControlID, _Record) ->
JS = "$(function() {
if(obj('" ++ ControlID ++ "').value != 'initialvalue')
window.location = document.location.href;
else
obj('" ++ ControlID ++ "').value = 'newvalue';
});",
Hidden = #hidden{id=ControlID, text="initialvalue"},
wf:wire(ControlID, JS),
element_hidden:render(ControlID, Hidden).
44 changes: 44 additions & 0 deletions element_image_x.erl
@@ -0,0 +1,44 @@
-module(element_image_x).
-compile(export_all).

-include_lib("lib/nitrogen/include/wf.inc").
-record(image_x, {?ELEMENT_BASE(element_image_x), image, alt, width, height, usemap}).

% desc: this enhance the basic image element, mostly for using 'usemap' tag
% it also allow adding inline width/height to the iamge


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

render(ControlID, Record) ->
Attributes = [
{id, ControlID},
{class, [image, Record#image_x.class]},
{style, Record#image_x.style},
{src, Record#image_x.image},
{usemap, Record#image_x.usemap}
],

OptionalArgs = [fun add_alt/2, fun add_width/2, fun add_height/2],
FinalAttributes = lists:foldl(fun(F, X) -> F(Record, X) end, Attributes, OptionalArgs),
wf_tags:emit_tag(img, FinalAttributes).



add_alt(Record, Attributes) ->
case Record#image_x.alt of
undefined -> Attributes;
Val -> [{alt, Val}|Attributes]
end.

add_width(Record, Attributes) ->
case Record#image_x.width of
undefined -> Attributes;
Val -> [{width, Val}|Attributes]
end.

add_height(Record, Attributes) ->
case Record#image_x.height of
undefined -> Attributes;
Val -> [{height, Val}|Attributes]
end.
20 changes: 20 additions & 0 deletions element_textarea_x.erl
@@ -0,0 +1,20 @@
-module(element_textarea_x).
-compile(export_all).

-include_lib("lib/nitrogen/include/wf.inc").
-record(textarea_x, {?ELEMENT_BASE(element_textarea_x), text="", html_encode=true, rows=2, columns=20}).

% desc: enhance the basic textarea with inline cols/rows values

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

render(ControlID, Record) ->
Content = wf:html_encode(Record#textarea_x.text, Record#textarea_x.html_encode),
wf_tags:emit_tag(textarea, Content, [
{id, ControlID},
{name, ControlID},
{class, [textarea, Record#textarea_x.class]},
{style, Record#textarea_x.style},
{rows, Record#textarea_x.rows},
{columns, Record#textarea_x.columns}
]).

0 comments on commit aa1984f

Please sign in to comment.