comparison cpack/dml/applications/csv_ui.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 /* Part of DML (Digital Music Laboratory)
2 Copyright 2014-2015 Samer Abdallah, University of London
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 :- module(csv_ui, []).
20
21 /** <module> UI for viewing CSV files
22 */
23 :- use_module(library(http/html_write)).
24 :- use_module(library(http/html_head)).
25 :- use_module(library(http/http_dispatch)).
26 :- use_module(library(http/http_parameters)).
27 :- use_module(library(decoration)).
28 :- use_module(library(htmlutils), [paginator//3]).
29 :- use_module(library(csvutils), [uri_to_csv/2]).
30 :- use_module(library(listutils), [drop/3,take/3]).
31 :- use_module(components(table), [table_from_goal//2]).
32 :- use_module(components(icons)).
33 :- use_module(cliopatria(hooks)).
34
35 :- set_prolog_flag(double_quotes,string).
36
37 :- http_handler(root(dml/csv/view), csv_view, []).
38
39 decoration:resource_view(URI,_) -->
40 { sub_string(URI,_,_,0,".csv") },
41 { http_link_to_id(archive_get,[uri(URI)],EntryURL) },
42 { http_link_to_id(csv_view,[uri(URI)],ViewURL) },
43 html_requires(font_awesome),
44 html([ a(href=EntryURL, [\icon(download)," Download"]), &('MediumSpace')
45 , a(href=ViewURL, [\icon(table)," View"])]).
46
47 decoration:resource_decoration(URI,Link) -->
48 { sub_string(URI,_,_,0,".csv") }, !,
49 { http_link_to_id(csv_view,[uri(URI)],ViewURL) },
50 html_requires(font_awesome),
51 html( span( [ a(href(ViewURL),\icon(table))
52 , &(nbsp), \Link
53 ])).
54
55 csv_view(Request) :-
56 http_parameters(Request,
57 [ uri(URI, [ optional(false), description("URI of CSV file")])
58 , page(Page, [ nonneg, default(1) ])
59 , limit(Limit, [ nonneg, default(50) ])
60 ]),
61 uri_to_csv(URI,Rows),
62 length(Rows,Total),
63 Offset is Limit*(Page-1),
64 Pages is ceil(Total/Limit),
65 insist(Page=<Pages),
66 drop(Offset,Rows,Rows1),
67 insist(Page=Pages -> Rows1=Rows2; take(Limit,Rows1,Rows2)),
68 format(string(FullTitle),"CSV view for ~w",[URI]),
69 reply_html_page(cliopatria(demo), [title(FullTitle)],
70 [ h1(FullTitle)
71 , \paginator(csv_view-[uri(URI),limit(Limit)],Page,Pages)
72 , \table_from_goal(csv_row(Rows2),[])
73 , \paginator(csv_view-[uri(URI),limit(Limit)],Page,Pages)
74 ]).
75
76 csv_row(Rows,Row) :- member(R,Rows), R=..[_|Row].
77