Browse Source

n2o flush and render_html

pal-alex 5 years ago
parent
commit
708e32b022
2 changed files with 24 additions and 15 deletions
  1. 6 0
      src/n2o_nitro.erl
  2. 18 15
      src/nitro.erl

+ 6 - 0
src/n2o_nitro.erl

@@ -1,6 +1,7 @@
 -module(n2o_nitro).
 -description('N2O Nitrogen Web Framework Protocol').
 -include_lib("nitro/include/n2o.hrl").
+-include_lib("nitro/include/nitro.hrl").
 -export([info/3,render_actions/1,io/1,io/2,event/1]).
 
 % Nitrogen pickle handler
@@ -26,10 +27,15 @@ info(#pickle{}=Event, Req, State) ->
     nitro:actions([]),
     {reply,{bert,html_events(Event,State)},Req,State};
 
+info(#flush{data=#wire{actions=Actions}}, Req, State) ->
+        io:format("flush ~p~n", [length(Actions)]),
+        {reply,{bert,{io, Actions, <<>>}},Req,State};
+
 info(#flush{data=Actions}, Req, State) ->
     nitro:actions(Actions),
     {reply,{bert,io(<<>>)},Req,State};
 
+
 info(#direct{data=Message}, Req, State) ->
     nitro:actions([]),
     {reply,{bert,case io(Message, State) of

+ 18 - 15
src/nitro.erl

@@ -126,10 +126,7 @@ update(Target, Elements) ->
     nitro:wire(#jq{target=Target,property=outerHTML,right=Elements,format="'~s'"}).
 
 insert_top(Tag,Target, Elements) ->
-    Pid = self(),
-    Ref = make_ref(),
-    spawn(fun() -> R = nitro:render(Elements), Pid ! {R,Ref,nitro:actions()} end),
-    {Render,Ref,Actions} = receive {_, Ref, _} = A -> A end,
+    {Render, _Ref, Actions} = render_html(Elements),
     nitro:wire(nitro:f(
         "qi('~s').insertBefore("
         "(function(){var div = qn('~s'); div.innerHTML = '~s'; return div.firstChild; })(),"
@@ -138,23 +135,30 @@ insert_top(Tag,Target, Elements) ->
     nitro:wire(nitro:render(Actions)).
 
 insert_bottom(Tag, Target, Elements) ->
-    Pid = self(),
-    Ref = make_ref(),
-    spawn(fun() -> R = nitro:render(Elements), Pid ! {R,Ref,nitro:actions()} end),
-    {Render,Ref,Actions} = receive {_, Ref, _} = A -> A end,
+    {Render, _Ref, Actions} = render_html(Elements),
     nitro:wire(nitro:f(
         "(function(){ var div = qn('~s'); div.innerHTML = '~s';"
                      "qi('~s').appendChild(div.firstChild); })();",
         [Tag,Render,Target])),
     nitro:wire(nitro:render(Actions)).
 
-insert_adjacent(Command,Target, Elements) ->
+insert_before(Target, Elements) -> insert_adjacent(beforebegin, Target, Elements).
+insert_after(Target, Elements) -> insert_adjacent(afterend, Target, Elements).
+
+insert_adjacent(Command, Target, Elements) -> insert_adjacent(Command, Target, Elements, "qi").
+insert_adjacent(Command, Target, Elements, Q) ->
+    {Render, _Ref, Actions} = render_html(Elements),
+    nitro:wire(nitro:f("~s('~s').insertAdjacentHTML('~s', '~s');",[Q,Target,Command,Render])),
+    nitro:wire(nitro:render(Actions)).
+
+
+render_html(Elements) ->
     Pid = self(),
     Ref = make_ref(),
-    spawn(fun() -> R = nitro:render(Elements), Pid ! {R,Ref,nitro:actions()} end),
-    {Render,Ref,Actions} = receive {_, Ref, _} = A -> A end,
-    nitro:wire(nitro:f("qi('~s').insertAdjacentHTML('~s', '~s');",[Target,Command,Render])),
-    nitro:wire(nitro:render(Actions)).
+    spawn(fun() -> R = nitro:render(Elements), Pid ! {R, Ref, nitro:actions()} end),
+    {Render, Ref, Actions} = receive {_, Ref, _} = A -> A end, 
+    {Render, Ref, Actions}.
+    
 
 actions() -> get(actions).
 actions(Ac) -> put(actions,Ac).
@@ -163,8 +167,7 @@ insert_top(Target, Elements) when element(1,Elements) == tr -> insert_top(tbody,
 insert_top(Target, Elements) -> insert_top('div',Target, Elements).
 insert_bottom(Target, Elements) when element(1,Elements) == tr -> insert_bottom(tbody, Target, Elements);
 insert_bottom(Target, Elements) -> insert_bottom('div', Target, Elements).
-insert_before(Target, Elements) -> insert_adjacent(beforebegin,Target, Elements).
-insert_after(Target, Elements) -> insert_adjacent(afterend,Target, Elements).
+
 
 clear(Target) ->
     nitro:wire("var x = qi('"++nitro:to_list(Target)++"'); while (x.firstChild) x.removeChild(x.firstChild);").