|
| 1 | +% vim: sw=4 ts=4 et ft=erlang |
| 2 | +% Nitrogen Web Framework for Erlang |
| 3 | +% Copyright (c) 2025 Jesse Gumm |
| 4 | +% See MIT-LICENSE for licensing information. |
| 5 | + |
| 6 | +-module(default_modal_handler). |
| 7 | +-include("wf.hrl"). |
| 8 | +-behaviour(modal_handler). |
| 9 | +-export ([ |
| 10 | + init/2, |
| 11 | + finish/2, |
| 12 | + process_open_action/3, |
| 13 | + process_close_action/3 |
| 14 | +]). |
| 15 | + |
| 16 | +%-record(state, {}). |
| 17 | + |
| 18 | +init(_Config, _State) -> |
| 19 | + {ok, []}. |
| 20 | + |
| 21 | +finish(_Config, _State) -> |
| 22 | + {ok, []}. |
| 23 | + |
| 24 | +process_open_action(Rec = #modal{}, _Config, _State) -> |
| 25 | + ID = wf:temp_id(), |
| 26 | + Body = build_body(ID, Rec), |
| 27 | + |
| 28 | + %% Rendering Body and capturing the Actions, otherwise the button actions will get wired to the page before the buttons exist |
| 29 | + {ok, RenderedBody, BodyActions} = wf:render_isolated(Body), |
| 30 | + |
| 31 | + Action = [ |
| 32 | + #insert_top{ |
| 33 | + trigger = Rec#modal.trigger, |
| 34 | + anchor = Rec#modal.anchor, |
| 35 | + target="body", |
| 36 | + elements=[ |
| 37 | + #lightbox{id=ID, body=[ |
| 38 | + #panel{class=lightbox_form, body=RenderedBody} |
| 39 | + ]} |
| 40 | + ] |
| 41 | + }, |
| 42 | + BodyActions, |
| 43 | + %% TODO: replace with scrollTo when it's added |
| 44 | + #js_fun{function="Nitrogen.$scroll_to_modal", args=[ID]}, |
| 45 | + #js_fun{function="Nitrogen.$add_modal", args=[ID]}, |
| 46 | + #js_fun{function="Nitrogen.$add_modal_zindex", args=[ID]} |
| 47 | + ], |
| 48 | + |
| 49 | + {ok, ID, Action}. |
| 50 | + |
| 51 | +build_body(ID, #modal{text=Text, body=Body, |
| 52 | + title_text=TitleT, title_body=TitleB, |
| 53 | + close_text=CloseT, close_body=CloseB, |
| 54 | + show_close_button=ShowCloseButton, |
| 55 | + buttons=Buttons0, options=_Opts}) -> |
| 56 | + Buttons = process_buttons(Buttons0), |
| 57 | + [ |
| 58 | + title(TitleT, TitleB), |
| 59 | + body(Text, Body), |
| 60 | + #panel{class=lightbox_buttons, body=[ |
| 61 | + Buttons, |
| 62 | + close_button(ShowCloseButton, ID, CloseT, CloseB) |
| 63 | + ]} |
| 64 | + ]. |
| 65 | + |
| 66 | +title(Text, Body) when ?WF_BLANK(Text) andalso ?WF_BLANK(Body) -> |
| 67 | + []; |
| 68 | +title(Text, Body) -> |
| 69 | + #h3{class=modal_title, text=Text, body=Body}. |
| 70 | + |
| 71 | +body(Text, Body) when ?WF_BLANK(Text) andalso ?WF_BLANK(Body) -> |
| 72 | + []; |
| 73 | +body(Text, Body) -> |
| 74 | + #panel{class=modal_body, text=Text, body=Body}. |
| 75 | + |
| 76 | +process_buttons([X | Rest]) when ?WF_BLANK(X) -> |
| 77 | + process_buttons(Rest); |
| 78 | +process_buttons([{Text, Postback} | Rest]) -> |
| 79 | + [#button{text=Text, postback=Postback} | process_buttons(Rest)]; |
| 80 | +process_buttons([{Text, Delegate, Postback} | Rest]) -> |
| 81 | + [#button{text=Text, postback=Postback, delegate=Delegate} | process_buttons(Rest)]; |
| 82 | +process_buttons([Button | Rest]) when ?IS_ELEMENT(Button) -> |
| 83 | + [Button | process_buttons(Rest)]; |
| 84 | +process_buttons([]) -> |
| 85 | + []. |
| 86 | + |
| 87 | +close_button(false, _, _, _) -> |
| 88 | + []; |
| 89 | +close_button(true, ID, Text, Body) when ?WF_BLANK(Text) andalso ?WF_BLANK(Body) -> |
| 90 | + #button{text="Close", click=#close_modal{id=ID}}; |
| 91 | +close_button(true, ID, Text, Body) -> |
| 92 | + #button{text=Text, body=Body, click=#close_modal{id=ID}}. |
| 93 | + |
| 94 | +process_close_action(#close_modal{id=ID}, _Config, _State) -> |
| 95 | + RemoveModalAndGetID = #js_fun{function="Nitrogen.$remove_modal", args=[ID]}, |
| 96 | + Script = #js_fun{function="Nitrogen.$remove", args=["body", RemoveModalAndGetID]}, |
| 97 | + {ok, ID, Script}. |
0 commit comments