Kilobyte Wiki

Artifact Content
Login

Artifact 63239447e9e29f602e00d2b3012a550cc510747a:


-module(ideas_io).
-include("html_gen.hrl").
-export([
         get_ideas_html/0,
         get_ideas_html/1,
         inner_html/1,
         get_idea_html_for/1
        ]).

get_idea_html_for(Id) ->
    {ok, _} = ideas_db:table_ref(),
    Html = html_of_entry(ideas_db:entry_for_key(Id)),
    ideas_db:done(),
    Html.

get_ideas_html(#{tag:=Tag}) when Tag == <<"">> ->
    get_ideas_html();
get_ideas_html(#{tag:=Tag}) when is_binary(Tag) ->
    {ok, _} = ideas_db:table_ref(),
    Html = html_of_rows(ideas_db:entries_for_tag(Tag)),
    ideas_db:done(),
    Html.

get_ideas_html() ->
    {ok, _} = ideas_db:table_ref(),
    Html = html_of_rows(ideas_db:all_entries()),
    ideas_db:done(),
    Html.

show_link_if_has_notes(Row) ->
    case ideas_db:notes_of(Row) of
        <<"">> -> ideas_db:desc_of(Row);
        _ -> a(#{href => [<<"/view/idea/">>, integer_to_list(ideas_db:id_of(Row))]}, ideas_db:desc_of(Row))
    end.

inner_html(Rows) -> 
     [
      tr(#{'data-id' => integer_to_list(ideas_db:id_of(Row))}, 
         [
          td(a(#{href => [<<"/view/bytag/">>, ideas_db:tag_of(Row)]}, ideas_db:tag_of(Row))),
          td(show_link_if_has_notes(Row))
         ])
      || Row <- Rows].

html_of_rows(Rows) -> 
     html(body(table(inner_html(Rows)))).

html_of_entry(Entry) ->
    html(body([{ 'data-id', integer_to_list(ideas_db:id_of(Entry)) }],
           [
            h2(ideas_db:desc_of(Entry)),
            div_([{id,"tag"}],[<<"Tag: ">>, ideas_db:tag_of(Entry)]),
            a(#{href => ["/admin/view/", integer_to_list(ideas_db:id_of(Entry))]}, <<"Edit">>),
            div_([{id,"notes"}], ideas_db:notes_of(Entry))
           ])).