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