Skip to content

Commit

Permalink
complete pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
astro committed Aug 13, 2009
1 parent 224031e commit 6a855a8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
23 changes: 19 additions & 4 deletions priv/www/chaosbay.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ body {
background: white;
color: black;
}

a {
text-decoration: none;
color: #7f0000;
}
div#head {
background: black;
color: white;
Expand All @@ -24,7 +27,6 @@ div#head p a#add:before {
}
div#head p a {
color: white;
text-decoration: none;
}
div#head h1 {
font-size: x-large;
Expand All @@ -33,7 +35,6 @@ div#head h1 {
}
div#head h1 a {
color: white;
text-decoration: none;
}

div#content {
Expand Down Expand Up @@ -80,7 +81,6 @@ div#content table tr th {
}
div#content table tr th a {
color: white;
text-decoration: none;
}
div#content table tr th:first-child {
text-align: left;
Expand Down Expand Up @@ -117,6 +117,21 @@ div#content dl dt:after {
div#content dl dd {
font-size: small;
}
p#pages {
margin: 2em auto;
text-align: center;
font-size: 50%;
}
p#pages a {
margin: 0.5em;
padding: 0.5em;
border: 1px solid #774;
background: #ffc;
}
p#pages a#current {
border: 1px solid #447;
background: #ccf;
}
div#content #comments {
padding: 0em 1em;
}
Expand Down
53 changes: 37 additions & 16 deletions src/chaosbay_web.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,9 @@ count_request_(What, {_, _, _, _, _, _, _, _}) ->

-define(COL_LINK(Field), (case atom_to_list(Field) of
SortName ->
"/browse/" ++
SortName ++ "/" ++
OtherDirection ++ "/" ++
OffsetS ++ "/" ++
PatternEncoded;
browse_link(SortName, OtherDirection, 0, Pattern);
FieldS ->
"/browse/" ++
FieldS ++ "/a/" ++
OffsetS ++ "/" ++
PatternEncoded
browse_link(FieldS, asc, 0, Pattern)
end)).

request(Req, 'GET', "add") ->
Expand Down Expand Up @@ -160,14 +153,15 @@ request(Req, 'GET', "browse/" ++ Path) ->
?COUNT_REQUEST(browse),
[SortName, DirectionS, OffsetS, PatternEncoded] = util:split_string(Path, $/, 4),
{Direction, OtherDirection} = case DirectionS of
"a" -> {asc, "d"};
"d" -> {desc, "a"}
"a" -> {asc, desc};
"d" -> {desc, asc}
end,
Offset = list_to_integer(OffsetS),
io:format("Offset: ~p~n",[Offset]),
Pattern = mochiweb_util:unquote(PatternEncoded),
TorrentMetas = torrent_browse:search(Pattern,
?RESULTSET_LENGTH, Offset,
SortName, Direction),
{TorrentMetas, TorrentTotal} = torrent_browse:search(Pattern,
?RESULTSET_LENGTH, Offset,
SortName, Direction),
HTML =
[{table, [{"border", "1"}],
[{tr, [{th, [{a, [{"href", ?COL_LINK(name)}], ["Name"]}]},
Expand Down Expand Up @@ -214,7 +208,14 @@ request(Req, 'GET', "browse/" ++ Path) ->
{td, [integer_to_list(L)]},
{td, [util:human_bandwidth(Speed)]}
]}
end, TorrentMetas)]}],
end, TorrentMetas)]},
{p, [{"id", "pages"}],
[{a, if PageOffset == Offset -> [{"id", "current"}];
true -> [{"href", browse_link(SortName, Direction, PageOffset, Pattern)}]
end,
[lists:flatten(io_lib:format("~B", [PageOffset]))]}
|| PageOffset <- lists:seq(0, TorrentTotal, ?RESULTSET_LENGTH)]}
],
Body = lists:map(fun html:to_iolist/1, HTML),
html_ok(Req, Body);

Expand Down Expand Up @@ -430,7 +431,7 @@ request(Req, 'GET', {details, Name}) ->
{dd, [util:human_length(Length)]},
{dt, ["Info-Hash"]},
{dd, [{"class", "code"}],
[mochiweb_util:quote_plus(binary_to_list(Id))]},
[urlencode(binary_to_list(Id))]},
{dt, ["Seeders"]},
{dd, [integer_to_list(S)]},
{dt, ["Leechers"]},
Expand Down Expand Up @@ -530,6 +531,26 @@ link_to_torrent(Name) when is_binary(Name) ->
link_to_torrent(Name) ->
"/" ++ urlencode(Name) ++ ".torrent".

browse_link(SortName, Direction, Offset, Pattern) when is_atom(SortName) ->
browse_link(atom_to_list(SortName), Direction, Offset, Pattern);
browse_link(SortName, asc, Offset, Pattern) ->
browse_link(SortName, "a", Offset, Pattern);
browse_link(SortName, desc, Offset, Pattern) ->
browse_link(SortName, "a", Offset, Pattern);
browse_link(SortName, Direction, Offset, Pattern) when is_integer(Offset) ->
browse_link(SortName, Direction, integer_to_list(Offset), Pattern);
browse_link(SortName, Direction, Offset, Pattern) ->
io:format("browse_link(~p, ~p, ~p, ~p) -> ~p~n", [SortName, Direction, Offset, Pattern, "/browse/" ++
SortName ++ "/" ++
Direction ++ "/" ++
Offset ++ "/" ++
urlencode(Pattern)]),
"/browse/" ++
SortName ++ "/" ++
Direction ++ "/" ++
Offset ++ "/" ++
urlencode(Pattern).

urlencode(S) ->
lists:flatten(
lists:map(fun($+) -> "%20";
Expand Down
3 changes: 2 additions & 1 deletion src/torrent_browse.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ search(Pattern, Max, Offset, SortField, SortDir) ->
Sorted
end
end, sorted:new(SortN, SortDir, Max + Offset)),
sorted:to_list(Sorted).
Result = lists:nthtail(Offset, sorted:to_list(Sorted)),
{Result, sorted:get_total(Sorted)}.

build_filter("") ->
fun(_) -> true end;
Expand Down

0 comments on commit 6a855a8

Please sign in to comment.